1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.net;
19
20 import org.apache.log4j.AppenderSkeleton;
21 import org.apache.log4j.helpers.LogLog;
22 import org.apache.log4j.spi.ErrorCode;
23 import org.apache.log4j.spi.LoggingEvent;
24
25 import javax.jms.JMSException;
26 import javax.jms.ObjectMessage;
27 import javax.jms.Session;
28 import javax.jms.Topic;
29 import javax.jms.TopicConnection;
30 import javax.jms.TopicConnectionFactory;
31 import javax.jms.TopicPublisher;
32 import javax.jms.TopicSession;
33 import javax.naming.Context;
34 import javax.naming.InitialContext;
35 import javax.naming.NameNotFoundException;
36 import javax.naming.NamingException;
37 import java.util.Properties;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public class JMSAppender extends AppenderSkeleton {
103
104 String securityPrincipalName;
105 String securityCredentials;
106 String initialContextFactoryName;
107 String urlPkgPrefixes;
108 String providerURL;
109 String topicBindingName;
110 String tcfBindingName;
111 String userName;
112 String password;
113 boolean locationInfo;
114
115 TopicConnection topicConnection;
116 TopicSession topicSession;
117 TopicPublisher topicPublisher;
118
119 public
120 JMSAppender() {
121 }
122
123
124
125
126
127
128 public
129 void setTopicConnectionFactoryBindingName(String tcfBindingName) {
130 this.tcfBindingName = tcfBindingName;
131 }
132
133
134
135
136 public
137 String getTopicConnectionFactoryBindingName() {
138 return tcfBindingName;
139 }
140
141
142
143
144
145
146 public
147 void setTopicBindingName(String topicBindingName) {
148 this.topicBindingName = topicBindingName;
149 }
150
151
152
153
154 public
155 String getTopicBindingName() {
156 return topicBindingName;
157 }
158
159
160
161
162
163
164 public
165 boolean getLocationInfo() {
166 return locationInfo;
167 }
168
169
170
171
172 public void activateOptions() {
173 TopicConnectionFactory topicConnectionFactory;
174
175 try {
176 Context jndi;
177
178 LogLog.debug("Getting initial context.");
179 if(initialContextFactoryName != null) {
180 Properties env = new Properties( );
181 env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
182 if(providerURL != null) {
183 env.put(Context.PROVIDER_URL, providerURL);
184 } else {
185 LogLog.warn("You have set InitialContextFactoryName option but not the "
186 +"ProviderURL. This is likely to cause problems.");
187 }
188 if(urlPkgPrefixes != null) {
189 env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
190 }
191
192 if(securityPrincipalName != null) {
193 env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
194 if(securityCredentials != null) {
195 env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
196 } else {
197 LogLog.warn("You have set SecurityPrincipalName option but not the "
198 +"SecurityCredentials. This is likely to cause problems.");
199 }
200 }
201 jndi = new InitialContext(env);
202 } else {
203 jndi = new InitialContext();
204 }
205
206 LogLog.debug("Looking up ["+tcfBindingName+"]");
207 topicConnectionFactory = (TopicConnectionFactory) lookup(jndi, tcfBindingName);
208 LogLog.debug("About to create TopicConnection.");
209 if(userName != null) {
210 topicConnection = topicConnectionFactory.createTopicConnection(userName,
211 password);
212 } else {
213 topicConnection = topicConnectionFactory.createTopicConnection();
214 }
215
216 LogLog.debug("Creating TopicSession, non-transactional, "
217 +"in AUTO_ACKNOWLEDGE mode.");
218 topicSession = topicConnection.createTopicSession(false,
219 Session.AUTO_ACKNOWLEDGE);
220
221 LogLog.debug("Looking up topic name ["+topicBindingName+"].");
222 Topic topic = (Topic) lookup(jndi, topicBindingName);
223
224 LogLog.debug("Creating TopicPublisher.");
225 topicPublisher = topicSession.createPublisher(topic);
226
227 LogLog.debug("Starting TopicConnection.");
228 topicConnection.start();
229
230 jndi.close();
231 } catch(JMSException e) {
232 errorHandler.error("Error while activating options for appender named ["+name+
233 "].", e, ErrorCode.GENERIC_FAILURE);
234 } catch(NamingException e) {
235 errorHandler.error("Error while activating options for appender named ["+name+
236 "].", e, ErrorCode.GENERIC_FAILURE);
237 } catch(RuntimeException e) {
238 errorHandler.error("Error while activating options for appender named ["+name+
239 "].", e, ErrorCode.GENERIC_FAILURE);
240 }
241 }
242
243 protected Object lookup(Context ctx, String name) throws NamingException {
244 try {
245 return ctx.lookup(name);
246 } catch(NameNotFoundException e) {
247 LogLog.error("Could not find name ["+name+"].");
248 throw e;
249 }
250 }
251
252 protected boolean checkEntryConditions() {
253 String fail = null;
254
255 if(this.topicConnection == null) {
256 fail = "No TopicConnection";
257 } else if(this.topicSession == null) {
258 fail = "No TopicSession";
259 } else if(this.topicPublisher == null) {
260 fail = "No TopicPublisher";
261 }
262
263 if(fail != null) {
264 errorHandler.error(fail +" for JMSAppender named ["+name+"].");
265 return false;
266 } else {
267 return true;
268 }
269 }
270
271
272
273
274 public synchronized void close() {
275
276
277 if(this.closed)
278 return;
279
280 LogLog.debug("Closing appender ["+name+"].");
281 this.closed = true;
282
283 try {
284 if(topicSession != null)
285 topicSession.close();
286 if(topicConnection != null)
287 topicConnection.close();
288 } catch(JMSException e) {
289 LogLog.error("Error while closing JMSAppender ["+name+"].", e);
290 } catch(RuntimeException e) {
291 LogLog.error("Error while closing JMSAppender ["+name+"].", e);
292 }
293
294 topicPublisher = null;
295 topicSession = null;
296 topicConnection = null;
297 }
298
299
300
301
302 public void append(LoggingEvent event) {
303 if(!checkEntryConditions()) {
304 return;
305 }
306
307 try {
308 ObjectMessage msg = topicSession.createObjectMessage();
309 if(locationInfo) {
310 event.getLocationInformation();
311 }
312 msg.setObject(event);
313 topicPublisher.publish(msg);
314 } catch(JMSException e) {
315 errorHandler.error("Could not publish message in JMSAppender ["+name+"].", e,
316 ErrorCode.GENERIC_FAILURE);
317 } catch(RuntimeException e) {
318 errorHandler.error("Could not publish message in JMSAppender ["+name+"].", e,
319 ErrorCode.GENERIC_FAILURE);
320 }
321 }
322
323
324
325
326
327
328 public String getInitialContextFactoryName() {
329 return initialContextFactoryName;
330 }
331
332
333
334
335
336
337
338
339
340
341 public void setInitialContextFactoryName(String initialContextFactoryName) {
342 this.initialContextFactoryName = initialContextFactoryName;
343 }
344
345 public String getProviderURL() {
346 return providerURL;
347 }
348
349 public void setProviderURL(String providerURL) {
350 this.providerURL = providerURL;
351 }
352
353 String getURLPkgPrefixes( ) {
354 return urlPkgPrefixes;
355 }
356
357 public void setURLPkgPrefixes(String urlPkgPrefixes ) {
358 this.urlPkgPrefixes = urlPkgPrefixes;
359 }
360
361 public String getSecurityCredentials() {
362 return securityCredentials;
363 }
364
365 public void setSecurityCredentials(String securityCredentials) {
366 this.securityCredentials = securityCredentials;
367 }
368
369
370 public String getSecurityPrincipalName() {
371 return securityPrincipalName;
372 }
373
374 public void setSecurityPrincipalName(String securityPrincipalName) {
375 this.securityPrincipalName = securityPrincipalName;
376 }
377
378 public String getUserName() {
379 return userName;
380 }
381
382
383
384
385
386
387
388
389 public void setUserName(String userName) {
390 this.userName = userName;
391 }
392
393 public String getPassword() {
394 return password;
395 }
396
397
398
399
400 public void setPassword(String password) {
401 this.password = password;
402 }
403
404
405
406
407
408
409 public void setLocationInfo(boolean locationInfo) {
410 this.locationInfo = locationInfo;
411 }
412
413
414
415
416
417 protected TopicConnection getTopicConnection() {
418 return topicConnection;
419 }
420
421
422
423
424
425 protected TopicSession getTopicSession() {
426 return topicSession;
427 }
428
429
430
431
432
433 protected TopicPublisher getTopicPublisher() {
434 return topicPublisher;
435 }
436
437
438
439
440
441 public boolean requiresLayout() {
442 return false;
443 }
444 }