View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.net;
15  
16  import java.util.Hashtable;
17  import java.util.Properties;
18  
19  import javax.naming.Context;
20  import javax.naming.InitialContext;
21  import javax.naming.NameNotFoundException;
22  import javax.naming.NamingException;
23  
24  import ch.qos.logback.core.AppenderBase;
25  
26  /**
27   * This class serves as a base class for 
28   * JMSTopicAppender and JMSQueueAppender
29   * 
30   * For more information about this appender, please refer to:
31   * http://logback.qos.ch/manual/appenders.html#JMSAppenderBase
32   *
33   * @author Ceki Gülcü
34   * @author Sébastien Pennec
35   */
36  public abstract class JMSAppenderBase<E> extends AppenderBase<E> {
37  
38    protected String securityPrincipalName;
39    protected String securityCredentials;
40    protected String initialContextFactoryName;
41    protected String urlPkgPrefixes;
42    protected String providerURL;
43    protected String userName;
44    protected String password;
45    
46    
47    protected Object lookup(Context ctx, String name) throws NamingException {
48      try {
49        return ctx.lookup(name);
50      } catch (NameNotFoundException e) {
51        addError("Could not find name [" + name + "].");
52        throw e;
53      }
54    }
55  
56    public Context buildJNDIContext() throws NamingException {
57      Context jndi = null;
58  
59      // addInfo("Getting initial context.");
60      if (initialContextFactoryName != null) {
61        Properties env = buildEnvProperties();
62        jndi = new InitialContext(env);
63      } else {
64        jndi = new InitialContext();
65      }
66      return jndi;
67    }
68    
69    public Properties buildEnvProperties() {
70      Properties env = new Properties();
71      env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
72      if (providerURL != null) {
73        env.put(Context.PROVIDER_URL, providerURL);
74      } else {
75        addWarn("You have set InitialContextFactoryName option but not the "
76            + "ProviderURL. This is likely to cause problems.");
77      }
78      if (urlPkgPrefixes != null) {
79        env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
80      }
81  
82      if (securityPrincipalName != null) {
83        env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
84        if (securityCredentials != null) {
85          env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
86        } else {
87          addWarn("You have set SecurityPrincipalName option but not the "
88              + "SecurityCredentials. This is likely to cause problems.");
89        }
90      }
91      return env;
92    }
93    
94    
95    
96    /**
97     * Returns the value of the <b>InitialContextFactoryName</b> option. See
98     * {@link #setInitialContextFactoryName} for more details on the meaning of
99     * this option.
100    */
101   public String getInitialContextFactoryName() {
102     return initialContextFactoryName;
103   }
104 
105   /**
106    * Setting the <b>InitialContextFactoryName</b> method will cause this
107    * <code>JMSAppender</code> instance to use the {@link
108    * InitialContext#InitialContext(Hashtable)} method instead of the no-argument
109    * constructor. If you set this option, you should also at least set the
110    * <b>ProviderURL</b> option.
111    * 
112    * <p>
113    * See also {@link #setProviderURL(String)}.
114    */
115   public void setInitialContextFactoryName(String initialContextFactoryName) {
116     this.initialContextFactoryName = initialContextFactoryName;
117   }
118 
119   public String getProviderURL() {
120     return providerURL;
121   }
122 
123   public void setProviderURL(String providerURL) {
124     this.providerURL = providerURL;
125   }
126 
127   public String getURLPkgPrefixes() {
128     return urlPkgPrefixes;
129   }
130 
131   public void setURLPkgPrefixes(String urlPkgPrefixes) {
132     this.urlPkgPrefixes = urlPkgPrefixes;
133   }
134 
135   public String getSecurityCredentials() {
136     return securityCredentials;
137   }
138 
139   public void setSecurityCredentials(String securityCredentials) {
140     this.securityCredentials = securityCredentials;
141   }
142 
143   public String getSecurityPrincipalName() {
144     return securityPrincipalName;
145   }
146 
147   public void setSecurityPrincipalName(String securityPrincipalName) {
148     this.securityPrincipalName = securityPrincipalName;
149   }
150 
151   public String getUserName() {
152     return userName;
153   }
154 
155   /**
156    * The user name to use when {@link
157    * javax.jms.TopicConnectionFactory#createTopicConnection(String, String)}
158    * creating a topic session}. If you set this option, you should also set the
159    * <b>Password</b> option. See {@link #setPassword(String)}.
160    */
161   public void setUserName(String userName) {
162     this.userName = userName;
163   }
164 
165   public String getPassword() {
166     return password;
167   }
168 
169   /**
170    * The paswword to use when creating a topic session.
171    */
172   public void setPassword(String password) {
173     this.password = password;
174   }
175 
176   
177 }