View Javadoc

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