KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > JMSLog


1 package com.protomatter.syslog;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.net.*;
55 import java.util.*;
56 import java.text.*;
57
58 import javax.jms.*;
59 import javax.naming.*;
60
61 import com.protomatter.util.*;
62
63 /**
64  * A logger that writes messages to JMS.
65  * The JMS session used has no transaction attribute
66  * itself, so it will obey any JTS transaction context
67  * which is currently active.<P>
68  *
69  * The {@link SyslogServer SyslogServer} class can be used as
70  * a standalone JMS message receiver. Please see the JavaDoc
71  * for that class for more information.
72  *
73  * @see com.protomatter.syslog.xml.JMSLog_Helper XML configuration class
74  */

75 public class JMSLog
76 extends BasicLogger
77 implements JMSConstants
78 {
79   private String JavaDoc topicName = null;
80   private String JavaDoc factoryName = null;
81   private String JavaDoc user = null;
82   private String JavaDoc pass = null;
83
84   private boolean persistent = false;
85   private int priority = 5;
86   private int ttl = 30 * 60 * 1000; // 30 minutes
87

88   private TopicSession session = null;
89   private TopicPublisher publisher = null;
90   private boolean running = false;
91
92   /**
93    * You will need to call the configure() method if
94    * you use this constructor.
95    */

96   public JMSLog()
97   {
98     super();
99   }
100
101   /**
102    * Get the message priority.
103    */

104   public int getPriority()
105   {
106     return this.priority;
107   }
108   /**
109    * Set the message priority. Default is 5.
110    */

111   public void setPriority(int priority)
112   {
113     this.ttl = priority;
114   }
115
116   /**
117    * Get the message TTL.
118    */

119   public int getTTL()
120   {
121     return this.ttl;
122   }
123   /**
124    * Get the message TTL. Default is 30 minutes.
125    */

126   public void setTTL(int ttl)
127   {
128     this.ttl = ttl;
129   }
130
131   /**
132    * Get the message persistence flag.
133    */

134   public boolean getPersistent()
135   {
136     return this.persistent;
137   }
138   /**
139    * Set the message persistence flag. Default is false.
140    */

141   public void setPersistent(boolean persistent)
142   {
143     this.persistent = persistent;
144   }
145
146   /**
147    * Get the JNDI name of the JMS topic we'll publish to.
148    */

149   public String JavaDoc getTopicName()
150   {
151     return this.topicName;
152   }
153   /**
154    * Set the JNDI name of the JMS topic we'll publish to.
155    */

156   public void setTopicName(String JavaDoc topicName)
157   {
158     this.topicName = topicName;
159   }
160
161   /**
162    * Get the JNDI name of the JMS connection factory we'll use.
163    */

164   public String JavaDoc getFactoryName()
165   {
166     return this.factoryName;
167   }
168   /**
169    * Set the JNDI name of the JMS connection factory we'll use.
170    */

171   public void setFactoryName(String JavaDoc factoryName)
172   {
173     this.factoryName = factoryName;
174   }
175
176   /**
177    * Get the username for authenticating to the JMS connection.
178    * THIS IS NOT THE JNDI CREDENTIALS.
179    */

180   public String JavaDoc getUser()
181   {
182     return this.user;
183   }
184   /**
185    * Set the username for authenticating to the JMS connection.
186    * THIS IS NOT THE JNDI CREDENTIALS.
187    */

188   public void setUser(String JavaDoc user)
189   {
190     this.user = user;
191   }
192
193   /**
194    * Get the password for authenticating to the JMS connection.
195    * THIS IS NOT THE JNDI CREDENTIALS.
196    */

197   public String JavaDoc getPass()
198   {
199     return this.pass;
200   }
201   /**
202    * Set the password for authenticating to the JMS connection.
203    * THIS IS NOT THE JNDI CREDENTIALS.
204    */

205   public void setPass(String JavaDoc pass)
206   {
207     this.pass = pass;
208   }
209
210   /**
211    * Write a log message. A <tt>javax.jms.TextMessage</tt>
212    * is created. The text of the message is set to the
213    * detail formatting output from text formatter
214    * configured for this logger.<P>
215    *
216    * Message properties are set as follows:<P>
217    *
218    * <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH="90%">
219    *
220    * <TR CLASS="TableHeadingColor">
221    * <TD><B>name</B></TD>
222    * <TD><B>type</B></TD>
223    * <TD><B>value</B></TD>
224    * </TR>
225    *
226    * <TR CLASS="TableRowColor">
227    * <TD VALIGN=TOP>JMS_PROP_HOST ("<TT>host</TT>")</TD>
228    * <TD VALIGN=TOP>String</TD>
229    * <TD>Originating host IP address.
230    * </TD>
231    * </TR>
232    *
233    * <TR CLASS="TableRowColor">
234    * <TD VALIGN=TOP>JMS_PROP_TIME ("<TT>time</TT>")</TD>
235    * <TD VALIGN=TOP>Long</TD>
236    * <TD>Time the message was sent.
237    * </TD>
238    * </TR>
239    *
240    * <TR CLASS="TableRowColor">
241    * <TD VALIGN=TOP>JMS_PROP_CHANNEL ("<TT>channel</TT>")</TD>
242    * <TD VALIGN=TOP>String</TD>
243    * <TD>Channel name.
244    * </TD>
245    * </TR>
246    *
247    * <TR CLASS="TableRowColor">
248    * <TD VALIGN=TOP>JMS_PROP_LOGGER ("<TT>logger</TT>")</TD>
249    * <TD VALIGN=TOP>String</TD>
250    * <TD>Full class name of the logger.
251    * </TD>
252    * </TR>
253    *
254    * <TR CLASS="TableRowColor">
255    * <TD VALIGN=TOP>JMS_PROP_MESSAGE ("<TT>message</TT>")</TD>
256    * <TD VALIGN=TOP>String</TD>
257    * <TD>Short message.
258    * </TD>
259    * </TR>
260    *
261    * <TR CLASS="TableRowColor">
262    * <TD VALIGN=TOP>JMS_PROP_LEVEL ("<TT>level</TT>")</TD>
263    * <TD VALIGN=TOP>Integer</TD>
264    * <TD>Severity level.
265    * </TD>
266    * </TR>
267    *
268    * <TR CLASS="TableRowColor">
269    * <TD VALIGN=TOP>JMS_PROP_THREAD ("<TT>thread</TT>")</TD>
270    * <TD VALIGN=TOP>String</TD>
271    * <TD>Originating thread's <tt>getName()</tt> value.
272    * </TD>
273    * </TR>
274    *
275    * </TABLE>
276    */

277   public final void log(SyslogMessage sm)
278   {
279     if (!running)
280       return;
281
282     try
283     {
284       String JavaDoc text = null;
285       if (sm.detail == null)
286       {
287         text = "";
288       }
289       else
290       {
291         StringBuffer JavaDoc b = new StringBuffer JavaDoc(256);
292         formatter.formatMessageDetail(b, sm);
293         text = b.toString();
294       }
295       String JavaDoc ip = sm.host.getHostAddress();
296       String JavaDoc msg = (sm.msg == null) ? "" : sm.msg.toString();
297       String JavaDoc threadName = (sm.thread != null) ? sm.thread.getName() : "";
298
299       // session and sender access is single threaded
300
// according to the JMS spec.
301
synchronized(session)
302       {
303         TextMessage m = session.createTextMessage();
304         if (text != null)
305           m.setText(text);
306
307         m.setStringProperty(JMS_PROP_MSG_TYPE, JMS_PROP_MSG_TYPE_VALUE);
308
309         m.setStringProperty(JMS_PROP_HOST, ip);
310         m.setLongProperty(JMS_PROP_TIME, sm.time);
311         m.setStringProperty(JMS_PROP_CHANNEL, sm.channel);
312         m.setStringProperty(JMS_PROP_LOGGER, sm.loggerClassname);
313         m.setStringProperty(JMS_PROP_MESSAGE, msg);
314         m.setIntProperty(JMS_PROP_LEVEL, sm.level);
315         m.setStringProperty(JMS_PROP_THREAD, threadName);
316
317         publisher.publish(m);
318       }
319     }
320     catch (JMSException x)
321     {
322       System.err.println("############################################################");
323       System.err.println(MessageFormat.format(
324         Syslog.getResourceString(MessageConstants.JMS_ATTEMPT_MESSAGE_1),
325         new Object JavaDoc[] { "JMSLog" }));
326       System.err.println(MessageFormat.format(
327         Syslog.getResourceString(MessageConstants.JMS_ATTEMPT_MESSAGE_2),
328         new Object JavaDoc[] { getName() } ));
329       System.err.println("");
330       System.err.println(Syslog.getResourceString(MessageConstants.JMS_ATTEMPT_CAUSE_MESSAGE));
331       x.printStackTrace();
332       System.err.println("");
333       System.err.println(Syslog.getResourceString(MessageConstants.JMS_ATTEMPT_ORIGINAL_MESSAGE));
334       StringBuffer JavaDoc b = new StringBuffer JavaDoc(256);
335       (new SimpleSyslogTextFormatter()).formatLogEntry(b, sm);
336       System.err.println(b);
337       System.err.println("############################################################");
338     }
339   }
340
341   /**
342    * Prepare for shutdown.
343    */

344   public void shutdown()
345   {
346     this.running = false;
347   }
348
349   public void start()
350   {
351     try
352     {
353       // hopefully we're in an appserver here ;-)
354
Context ctx = new InitialContext();
355
356       Topic topic = (Topic)ctx.lookup(topicName);
357
358       TopicConnectionFactory tcf =
359         (TopicConnectionFactory)ctx.lookup(factoryName);
360
361       TopicConnection connection = null;
362       if (user != null)
363         connection = tcf.createTopicConnection(user, pass);
364       else
365         connection = tcf.createTopicConnection();
366
367       session =
368         connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
369
370       connection.start();
371
372       publisher = session.createPublisher(topic);
373       publisher.setPriority(this.priority);
374       publisher.setTimeToLive(this.ttl);
375       publisher.setDeliveryMode(this.persistent
376         ? DeliveryMode.PERSISTENT
377         : DeliveryMode.NON_PERSISTENT);
378
379       running = true;
380     }
381     catch (NamingException nx)
382     {
383       throw new ChainedRuntimeException(MessageFormat.format(
384         Syslog.getResourceString(MessageConstants.JMS_NAMING_EXCEPITON_MESSAGE),
385         new Object JavaDoc[] { "NamingException", "JMSLog" }), nx);
386     }
387     catch (JMSException jx)
388     {
389       throw new ChainedRuntimeException(MessageFormat.format(
390         Syslog.getResourceString(MessageConstants.JMS_JMS_EXCEPITON_MESSAGE),
391         new Object JavaDoc[] { "JMSException", "JMSLog" }), jx);
392     }
393   }
394
395   public void flush()
396   {
397     // do nothing.
398
}
399 }
400
Popular Tags