KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > JndiJmsLogAppender


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 import org.apache.log4j.helpers.LogLog;
21
22 import javax.jms.Connection JavaDoc;
23 import javax.jms.ConnectionFactory JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import java.util.Hashtable JavaDoc;
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: 449919 $
35  */

36 public class JndiJmsLogAppender extends JmsLogAppenderSupport {
37
38     private String JavaDoc jndiName;
39     private String JavaDoc userName;
40     private String JavaDoc password;
41
42     private String JavaDoc initialContextFactoryName;
43     private String JavaDoc providerURL;
44     private String JavaDoc urlPkgPrefixes;
45     private String JavaDoc securityPrincipalName;
46     private String JavaDoc securityCredentials;
47
48     public JndiJmsLogAppender() {
49     }
50
51     public String JavaDoc getJndiName() {
52         return jndiName;
53     }
54
55     public void setJndiName(String JavaDoc jndiName) {
56         this.jndiName = jndiName;
57     }
58
59     public String JavaDoc getUserName() {
60         return userName;
61     }
62
63     public void setUserName(String JavaDoc userName) {
64         this.userName = userName;
65     }
66
67     public String JavaDoc getPassword() {
68         return password;
69     }
70
71     public void setPassword(String JavaDoc password) {
72         this.password = password;
73     }
74
75
76     // to customize the JNDI context
77
//-------------------------------------------------------------------------
78
public String JavaDoc getInitialContextFactoryName() {
79         return initialContextFactoryName;
80     }
81
82     public void setInitialContextFactoryName(String JavaDoc initialContextFactoryName) {
83         this.initialContextFactoryName = initialContextFactoryName;
84     }
85
86     public String JavaDoc getProviderURL() {
87         return providerURL;
88     }
89
90     public void setProviderURL(String JavaDoc providerURL) {
91         this.providerURL = providerURL;
92     }
93
94     public String JavaDoc getUrlPkgPrefixes() {
95         return urlPkgPrefixes;
96     }
97
98     public void setUrlPkgPrefixes(String JavaDoc urlPkgPrefixes) {
99         this.urlPkgPrefixes = urlPkgPrefixes;
100     }
101
102     public String JavaDoc getSecurityPrincipalName() {
103         return securityPrincipalName;
104     }
105
106     public void setSecurityPrincipalName(String JavaDoc securityPrincipalName) {
107         this.securityPrincipalName = securityPrincipalName;
108     }
109
110     public String JavaDoc getSecurityCredentials() {
111         return securityCredentials;
112     }
113
114     public void setSecurityCredentials(String JavaDoc securityCredentials) {
115         this.securityCredentials = securityCredentials;
116     }
117
118     // Implementation methods
119
//-------------------------------------------------------------------------
120
protected Connection JavaDoc createConnection() throws JMSException JavaDoc, NamingException JavaDoc {
121         InitialContext JavaDoc context = createInitialContext();
122         LogLog.debug("Looking up ConnectionFactory with jndiName: " + jndiName);
123         ConnectionFactory JavaDoc factory = (ConnectionFactory JavaDoc) context.lookup(jndiName);
124         if (factory == null) {
125             throw new JMSException JavaDoc("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 JavaDoc createInitialContext() throws NamingException JavaDoc {
136         if (initialContextFactoryName == null) {
137             return new InitialContext JavaDoc();
138         }
139         else {
140             Hashtable JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc(env);
165         }
166     }
167 }
168
Popular Tags