View Javadoc

1   /***
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.util;
19  
20  import org.apache.log4j.helpers.LogLog;
21  
22  import javax.jms.Connection;
23  import javax.jms.ConnectionFactory;
24  import javax.jms.JMSException;
25  import javax.naming.Context;
26  import javax.naming.InitialContext;
27  import javax.naming.NamingException;
28  import java.util.Hashtable;
29  
30  /***
31   * A JMS 1.1 log4j appender which uses JNDI to locate a JMS ConnectionFactory
32   * to use for logging events.
33   *
34   * @version $Revision: 1.1 $
35   */
36  public class JndiJmsLogAppender extends JmsLogAppenderSupport {
37  
38      private String jndiName;
39      private String userName;
40      private String password;
41  
42      private String initialContextFactoryName;
43      private String providerURL;
44      private String urlPkgPrefixes;
45      private String securityPrincipalName;
46      private String securityCredentials;
47  
48      public JndiJmsLogAppender() {
49      }
50  
51      public String getJndiName() {
52          return jndiName;
53      }
54  
55      public void setJndiName(String jndiName) {
56          this.jndiName = jndiName;
57      }
58  
59      public String getUserName() {
60          return userName;
61      }
62  
63      public void setUserName(String userName) {
64          this.userName = userName;
65      }
66  
67      public String getPassword() {
68          return password;
69      }
70  
71      public void setPassword(String password) {
72          this.password = password;
73      }
74  
75  
76      // to customize the JNDI context
77      //-------------------------------------------------------------------------
78      public String getInitialContextFactoryName() {
79          return initialContextFactoryName;
80      }
81  
82      public void setInitialContextFactoryName(String initialContextFactoryName) {
83          this.initialContextFactoryName = initialContextFactoryName;
84      }
85  
86      public String getProviderURL() {
87          return providerURL;
88      }
89  
90      public void setProviderURL(String providerURL) {
91          this.providerURL = providerURL;
92      }
93  
94      public String getUrlPkgPrefixes() {
95          return urlPkgPrefixes;
96      }
97  
98      public void setUrlPkgPrefixes(String urlPkgPrefixes) {
99          this.urlPkgPrefixes = urlPkgPrefixes;
100     }
101 
102     public String getSecurityPrincipalName() {
103         return securityPrincipalName;
104     }
105 
106     public void setSecurityPrincipalName(String securityPrincipalName) {
107         this.securityPrincipalName = securityPrincipalName;
108     }
109 
110     public String getSecurityCredentials() {
111         return securityCredentials;
112     }
113 
114     public void setSecurityCredentials(String securityCredentials) {
115         this.securityCredentials = securityCredentials;
116     }
117 
118     // Implementation methods
119     //-------------------------------------------------------------------------
120     protected Connection createConnection() throws JMSException, NamingException {
121         InitialContext context = createInitialContext();
122         LogLog.debug("Looking up ConnectionFactory with jndiName: " + jndiName);
123         ConnectionFactory factory = (ConnectionFactory) context.lookup(jndiName);
124         if (factory == null) {
125             throw new JMSException("No such ConnectionFactory for name: " + jndiName);
126         }
127         if (userName != null) {
128             return factory.createConnection(userName, password);
129         }
130         else {
131             return factory.createConnection();
132         }
133     }
134 
135     protected InitialContext createInitialContext() throws NamingException {
136         if (initialContextFactoryName == null) {
137             return new InitialContext();
138         }
139         else {
140             Hashtable env = new Hashtable();
141             env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
142             if (providerURL != null) {
143                 env.put(Context.PROVIDER_URL, providerURL);
144             }
145             else {
146                 LogLog.warn("You have set InitialContextFactoryName option but not the "
147                         + "ProviderURL. This is likely to cause problems.");
148             }
149             if (urlPkgPrefixes != null) {
150                 env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
151             }
152 
153             if (securityPrincipalName != null) {
154                 env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
155                 if (securityCredentials != null) {
156                     env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
157                 }
158                 else {
159                     LogLog.warn("You have set SecurityPrincipalName option but not the "
160                             + "SecurityCredentials. This is likely to cause problems.");
161                 }
162             }
163             LogLog.debug("Looking up JNDI context with environment: " + env);
164             return new InitialContext(env);
165         }
166     }
167 }