001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.util;
018    
019    import java.util.Hashtable;
020    import javax.jms.Connection;
021    import javax.jms.ConnectionFactory;
022    import javax.jms.JMSException;
023    import javax.naming.Context;
024    import javax.naming.InitialContext;
025    import javax.naming.NamingException;
026    
027    import org.apache.log4j.helpers.LogLog;
028    
029    /**
030     * A JMS 1.1 log4j appender which uses JNDI to locate a JMS ConnectionFactory to
031     * use for logging events.
032     * 
033     * 
034     */
035    public class JndiJmsLogAppender extends JmsLogAppenderSupport {
036    
037        private String jndiName;
038        private String userName;
039        private String password;
040    
041        private String initialContextFactoryName;
042        private String providerURL;
043        private String urlPkgPrefixes;
044        private String securityPrincipalName;
045        private String securityCredentials;
046    
047        public JndiJmsLogAppender() {
048        }
049    
050        public String getJndiName() {
051            return jndiName;
052        }
053    
054        public void setJndiName(String jndiName) {
055            this.jndiName = jndiName;
056        }
057    
058        public String getUserName() {
059            return userName;
060        }
061    
062        public void setUserName(String userName) {
063            this.userName = userName;
064        }
065    
066        public String getPassword() {
067            return password;
068        }
069    
070        public void setPassword(String password) {
071            this.password = password;
072        }
073    
074        // to customize the JNDI context
075        // -------------------------------------------------------------------------
076        public String getInitialContextFactoryName() {
077            return initialContextFactoryName;
078        }
079    
080        public void setInitialContextFactoryName(String initialContextFactoryName) {
081            this.initialContextFactoryName = initialContextFactoryName;
082        }
083    
084        public String getProviderURL() {
085            return providerURL;
086        }
087    
088        public void setProviderURL(String providerURL) {
089            this.providerURL = providerURL;
090        }
091    
092        public String getUrlPkgPrefixes() {
093            return urlPkgPrefixes;
094        }
095    
096        public void setUrlPkgPrefixes(String urlPkgPrefixes) {
097            this.urlPkgPrefixes = urlPkgPrefixes;
098        }
099    
100        public String getSecurityPrincipalName() {
101            return securityPrincipalName;
102        }
103    
104        public void setSecurityPrincipalName(String securityPrincipalName) {
105            this.securityPrincipalName = securityPrincipalName;
106        }
107    
108        public String getSecurityCredentials() {
109            return securityCredentials;
110        }
111    
112        public void setSecurityCredentials(String securityCredentials) {
113            this.securityCredentials = securityCredentials;
114        }
115    
116        // Implementation methods
117        // -------------------------------------------------------------------------
118        protected Connection createConnection() throws JMSException, NamingException {
119            InitialContext context = createInitialContext();
120            LogLog.debug("Looking up ConnectionFactory with jndiName: " + jndiName);
121            ConnectionFactory factory = (ConnectionFactory)context.lookup(jndiName);
122            if (factory == null) {
123                throw new JMSException("No such ConnectionFactory for name: " + jndiName);
124            }
125            if (userName != null) {
126                return factory.createConnection(userName, password);
127            } else {
128                return factory.createConnection();
129            }
130        }
131    
132        protected InitialContext createInitialContext() throws NamingException {
133            if (initialContextFactoryName == null) {
134                return new InitialContext();
135            } else {
136                Hashtable<String, String> env = new Hashtable<String, String>();
137                env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
138                if (providerURL != null) {
139                    env.put(Context.PROVIDER_URL, providerURL);
140                } else {
141                    LogLog.warn("You have set InitialContextFactoryName option but not the " + "ProviderURL. This is likely to cause problems.");
142                }
143                if (urlPkgPrefixes != null) {
144                    env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
145                }
146    
147                if (securityPrincipalName != null) {
148                    env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
149                    if (securityCredentials != null) {
150                        env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
151                    } else {
152                        LogLog.warn("You have set SecurityPrincipalName option but not the " + "SecurityCredentials. This is likely to cause problems.");
153                    }
154                }
155                LogLog.debug("Looking up JNDI context with environment: " + env);
156                return new InitialContext(env);
157            }
158        }
159    }