1
2
3
4
5
6
7
8
9
10
11
12
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
28
29
30
31
32
33
34
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
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
98
99
100
101 public String getInitialContextFactoryName() {
102 return initialContextFactoryName;
103 }
104
105
106
107
108
109
110
111
112
113
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
157
158
159
160
161 public void setUserName(String userName) {
162 this.userName = userName;
163 }
164
165 public String getPassword() {
166 return password;
167 }
168
169
170
171
172 public void setPassword(String password) {
173 this.password = password;
174 }
175
176
177 }