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.plugins.Plugin;
21 import org.apache.log4j.plugins.Receiver;
22 import org.apache.log4j.spi.LoggingEvent;
23
24 import javax.jms.*;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NameNotFoundException;
28 import javax.naming.NamingException;
29 import java.io.FileInputStream;
30 import java.util.Properties;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class JMSReceiver extends Receiver implements MessageListener {
49
50 private boolean active = false;
51
52 protected String topicFactoryName;
53 protected String topicName;
54 protected String userId;
55 protected String password;
56 protected TopicConnection topicConnection;
57 protected String jndiPath;
58
59 private String remoteInfo;
60 private String providerUrl;
61
62 public JMSReceiver() {
63 }
64
65 public JMSReceiver(String _topicFactoryName, String _topicName,
66 String _userId, String _password, String _jndiPath) {
67 topicFactoryName = _topicFactoryName;
68 topicName = _topicName;
69 userId = _userId;
70 password = _password;
71 jndiPath = _jndiPath;
72 }
73
74
75
76
77
78 public void setJndiPath(String _jndiPath) {
79 jndiPath = _jndiPath;
80 }
81
82
83
84
85
86 public String getJndiPath() {
87 return jndiPath;
88 }
89
90
91
92
93
94 public void setTopicFactoryName(String _topicFactoryName) {
95 topicFactoryName = _topicFactoryName;
96 }
97
98
99
100
101 public String getTopicFactoryName() {
102 return topicFactoryName;
103 }
104
105
106
107
108
109 public void setTopicName(String _topicName) {
110 topicName = _topicName;
111 }
112
113
114
115
116 public String getTopicName() {
117 return topicName;
118 }
119
120
121
122
123
124 public void setUserId(String _userId) {
125 userId = _userId;
126 }
127
128
129
130
131 public String getUserId() {
132 return userId;
133 }
134
135
136
137
138
139 public void setPassword(String _password) {
140 password = _password;
141 }
142
143
144
145
146 public String getPassword() {
147 return password;
148 }
149
150
151
152
153
154
155
156
157
158
159 public boolean isEquivalent(Plugin testPlugin) {
160
161 if (testPlugin instanceof JMSReceiver) {
162
163 JMSReceiver receiver = (JMSReceiver) testPlugin;
164
165
166 return (
167 topicFactoryName.equals(receiver.getTopicFactoryName()) &&
168 (jndiPath == null || jndiPath.equals(receiver.getJndiPath())) &&
169 super.isEquivalent(testPlugin)
170 );
171 }
172
173 return false;
174 }
175
176
177
178
179 public synchronized boolean isActive() {
180 return active;
181 }
182
183
184
185
186 protected synchronized void setActive(boolean _active) {
187 active = _active;
188 }
189
190
191
192
193 public void activateOptions() {
194 if (!isActive()) {
195 try {
196 remoteInfo = topicFactoryName + ":" + topicName;
197
198 Context ctx;
199 if (jndiPath == null || jndiPath.equals("")) {
200 ctx = new InitialContext();
201 } else {
202 FileInputStream is = new FileInputStream(jndiPath);
203 Properties p = new Properties();
204 p.load(is);
205 is.close();
206 ctx = new InitialContext(p);
207 }
208
209
210 providerUrl = (String) ctx.getEnvironment().get(Context.PROVIDER_URL);
211 TopicConnectionFactory topicConnectionFactory;
212 topicConnectionFactory =
213 (TopicConnectionFactory) lookup(ctx, topicFactoryName);
214
215 if (userId != null && password != null) {
216 topicConnection =
217 topicConnectionFactory.createTopicConnection(userId, password);
218 } else {
219 topicConnection =
220 topicConnectionFactory.createTopicConnection();
221 }
222
223 TopicSession topicSession =
224 topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
225
226 Topic topic = (Topic) ctx.lookup(topicName);
227
228 TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
229
230 topicSubscriber.setMessageListener(this);
231
232 topicConnection.start();
233
234 setActive(true);
235 } catch (Exception e) {
236 setActive(false);
237 if (topicConnection != null) {
238 try {
239 topicConnection.close();
240 } catch (Exception e2) {
241
242 }
243 topicConnection = null;
244 }
245 getLogger().error("Could not start JMSReceiver.", e);
246 }
247 }
248 }
249
250
251
252
253 public synchronized void shutdown() {
254 if (isActive()) {
255
256 setActive(false);
257
258 if (topicConnection != null) {
259 try {
260 topicConnection.close();
261 } catch (Exception e) {
262
263 }
264 topicConnection = null;
265 }
266 }
267 }
268
269 public void onMessage(Message message) {
270 try {
271 if (message instanceof ObjectMessage) {
272
273 ObjectMessage objectMessage = (ObjectMessage) message;
274 LoggingEvent event = (LoggingEvent) objectMessage.getObject();
275
276
277 event.setProperty("log4j.remoteSourceInfo", remoteInfo);
278 event.setProperty("log4j.jmsProviderUrl", providerUrl);
279
280 doPost(event);
281 } else {
282 getLogger().warn("Received message is of type " + message.getJMSType()
283 + ", was expecting ObjectMessage.");
284 }
285 } catch (Exception e) {
286 getLogger().error("Exception thrown while processing incoming message.", e);
287 }
288 }
289
290 protected Object lookup(Context ctx, String name) throws NamingException {
291 try {
292 return ctx.lookup(name);
293 } catch (NameNotFoundException e) {
294 getLogger().error("Could not find name [" + name + "].");
295 throw e;
296 }
297 }
298
299 }