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 */
017package org.apache.activemq.util;
018
019import java.util.Hashtable;
020import javax.jms.Connection;
021import javax.jms.ConnectionFactory;
022import javax.jms.JMSException;
023import javax.naming.Context;
024import javax.naming.InitialContext;
025import javax.naming.NamingException;
026
027/**
028 * A JMS 1.1 log4j appender which uses JNDI to locate a JMS ConnectionFactory to
029 * use for logging events.
030 * 
031 * 
032 */
033public class JndiJmsLogAppender extends JmsLogAppenderSupport {
034
035    private String jndiName;
036    private String userName;
037    private String password;
038
039    private String initialContextFactoryName;
040    private String providerURL;
041    private String urlPkgPrefixes;
042    private String securityPrincipalName;
043    private String securityCredentials;
044
045    public JndiJmsLogAppender() {
046    }
047
048    public String getJndiName() {
049        return jndiName;
050    }
051
052    public void setJndiName(String jndiName) {
053        this.jndiName = jndiName;
054    }
055
056    public String getUserName() {
057        return userName;
058    }
059
060    public void setUserName(String userName) {
061        this.userName = userName;
062    }
063
064    public String getPassword() {
065        return password;
066    }
067
068    public void setPassword(String password) {
069        this.password = password;
070    }
071
072    // to customize the JNDI context
073    // -------------------------------------------------------------------------
074    public String getInitialContextFactoryName() {
075        return initialContextFactoryName;
076    }
077
078    public void setInitialContextFactoryName(String initialContextFactoryName) {
079        this.initialContextFactoryName = initialContextFactoryName;
080    }
081
082    public String getProviderURL() {
083        return providerURL;
084    }
085
086    public void setProviderURL(String providerURL) {
087        this.providerURL = providerURL;
088    }
089
090    public String getUrlPkgPrefixes() {
091        return urlPkgPrefixes;
092    }
093
094    public void setUrlPkgPrefixes(String urlPkgPrefixes) {
095        this.urlPkgPrefixes = urlPkgPrefixes;
096    }
097
098    public String getSecurityPrincipalName() {
099        return securityPrincipalName;
100    }
101
102    public void setSecurityPrincipalName(String securityPrincipalName) {
103        this.securityPrincipalName = securityPrincipalName;
104    }
105
106    public String getSecurityCredentials() {
107        return securityCredentials;
108    }
109
110    public void setSecurityCredentials(String securityCredentials) {
111        this.securityCredentials = securityCredentials;
112    }
113
114    // Implementation methods
115    // -------------------------------------------------------------------------
116    protected Connection createConnection() throws JMSException, NamingException {
117        InitialContext context = createInitialContext();
118        // LogLog.debug("Looking up ConnectionFactory with jndiName: " + jndiName);
119        ConnectionFactory factory = (ConnectionFactory)context.lookup(jndiName);
120        if (factory == null) {
121            throw new JMSException("No such ConnectionFactory for name: " + jndiName);
122        }
123        if (userName != null) {
124            return factory.createConnection(userName, password);
125        } else {
126            return factory.createConnection();
127        }
128    }
129
130    protected InitialContext createInitialContext() throws NamingException {
131        if (initialContextFactoryName == null) {
132            return new InitialContext();
133        } else {
134            Hashtable<String, String> env = new Hashtable<String, String>();
135            env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
136            if (providerURL != null) {
137                env.put(Context.PROVIDER_URL, providerURL);
138            } else {
139                // LogLog.warn("You have set InitialContextFactoryName option but not the " + "ProviderURL. This is likely to cause problems.");
140            }
141            if (urlPkgPrefixes != null) {
142                env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
143            }
144
145            if (securityPrincipalName != null) {
146                env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
147                if (securityCredentials != null) {
148                    env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
149                } else {
150                    // LogLog.warn("You have set SecurityPrincipalName option but not the " + "SecurityCredentials. This is likely to cause problems.");
151                }
152            }
153            // LogLog.debug("Looking up JNDI context with environment: " + env);
154            return new InitialContext(env);
155        }
156    }
157}