KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > xml > UNIXSyslogLog_Helper


1 package com.protomatter.syslog.xml;
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 com.protomatter.xml.*;
59 import com.protomatter.syslog.*;
60 import org.jdom.*;
61
62 /**
63  * XML configuration helper for <tt>UNIXSyslogLog</tt>.
64  */

65 public class UNIXSyslogLog_Helper
66             extends BasicLogger_Helper
67 {
68     /**
69      * Configure this logger given the XML element.
70      * The <tt>&lt;Logger&gt;</tt> element should look like this:<P>
71      *
72      * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
73      * <TR><TD>
74      * <PRE><B>
75      *
76      * &lt;Logger class="com.protomatter.syslog.UNIXSyslogLog" &gt;
77      *
78      * <font color="#888888">&lt;!--
79      *
80      * Note that the <tt>hostname</tt> attribute of the
81      * <tt>&lt;Syslog&gt;</tt> tag must be specified.
82      *
83      * Config params from {@link BasicLogger_Helper#configure(Object,Element) BasicLogger_Helper} can
84      * get inserted here.
85      *
86      * <B>Note:</B> The <TT>&lt;Format/&gt;</TT> tag is completely ignored
87      * because the format is dictated by the UNIX
88      * syslog system.
89      * --&gt;</font>
90      *
91      * &lt;logServer&gt;<i>hostname or IP address</i>&lt;/logServer&gt;
92      * &lt;port&gt;<i>port</i>&lt;/port&gt;
93      * &lt;facility&gt;<i>facility-id</i>&lt;/facility&gt;
94      * &lt;tag&gt;<i>tag-value</i>&lt;/tag&gt;
95      * &lt;showHostname&gt;<i>true|false</i>&lt;/showHostname&gt;
96      *
97      * &lt;messageTemplate&gt;<i>message-body-template</i>&lt;/messageTemplate&gt;
98      *
99      * &lt;SeverityMap&gt;
100      * &lt;MapEntry&gt;
101      * &lt;syslog&gt;DEBUG&lt;/syslog&gt;
102      * &lt;unix&gt;DEBUG&lt;/unix&gt;
103      * &lt;/MapEntry&gt;
104      * &lt;MapEntry&gt;
105      * &lt;syslog&gt;INFO&lt;/syslog&gt;
106      * &lt;unix&gt;INFO&lt;/unix&gt;
107      * &lt;/MapEntry&gt;
108      * &lt;MapEntry&gt;
109      * &lt;syslog&gt;WARNING&lt;/syslog&gt;
110      * &lt;unix&gt;WARNING&lt;/unix&gt;
111      * &lt;/MapEntry&gt;
112      * &lt;MapEntry&gt;
113      * &lt;syslog&gt;ERROR&lt;/syslog&gt;
114      * &lt;unix&gt;ERROR&lt;/unix&gt;
115      * &lt;/MapEntry&gt;
116      * &lt;MapEntry&gt;
117      * &lt;syslog&gt;FATAL&lt;/syslog&gt;
118      * &lt;unix&gt;EMERGENCY&lt;/unix&gt;
119      * &lt;/MapEntry&gt;
120      * &lt;/SeverityMap&gt;
121      *
122      * &lt;/Logger&gt;
123      * </B></PRE>
124      * </TD></TR></TABLE><P>
125      *
126      * <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH="90%">
127      * <TR CLASS="TableHeadingColor">
128      * <TD COLSPAN=3><B>Element</B></TD>
129      * </TR>
130      * <TR CLASS="TableHeadingColor">
131      * <TD><B>name</B></TD>
132      * <TD><B>value</B></TD>
133      * <TD><B>required</B></TD>
134      * </TR>
135      *
136      * <TR CLASS="TableRowColor">
137      * <TD VALIGN=TOP><TT>logServer</TT></TD>
138      * <TD>The hostname or IP address of the log server that
139      * packets should be sent to.
140      * </TD>
141      * <TD VALIGN=TOP>yes</TD>
142      * </TR>
143      *
144      * <TR CLASS="TableRowColor">
145      * <TD VALIGN=TOP><TT>port</TT></TD>
146      * <TD>The port number to send packets to on the log server.
147      * </TD>
148      * <TD VALIGN=TOP>no (default is <TT>514</TT>)</TD>
149      * </TR>
150      *
151      * <TR CLASS="TableRowColor">
152      * <TD VALIGN=TOP><TT>facility</TT></TD>
153      * <TD>The UNIX syslog facility ID that messages appear from.
154      * A list can be found <a HREF="../UNIXSyslogLog.html#setFacility(int)">here</a>.
155      * </TD>
156      * <TD VALIGN=TOP>no (default is <TT>16</TT>)</TD>
157      * </TR>
158      *
159      * <TR CLASS="TableRowColor">
160      * <TD VALIGN=TOP><TT>tag</TT></TD>
161      * <TD>Text value of the packet "tag" -- the default
162      * is "<TT>ProtomatterSyslog</TT>" and is generally meant
163      * to be the application or "process" name.
164      * </TD>
165      * <TD VALIGN=TOP>no (default is "<tt>ProtomatterSyslog</tt>")</TD>
166      * </TR>
167      *
168      * <TR CLASS="TableRowColor">
169      * <TD VALIGN=TOP><TT>showHostname</TT></TD>
170      * <TD><tt>true</tt> or <tt>false</tt> -- decide if we should
171      * show the hostname before the "tag". The spec says that you
172      * should do this, but when I tested under Red Hat Linux 7.2,
173      * it just ended up repeating the data. Your mileage may
174      * vary.
175      * </TD>
176      * <TD VALIGN=TOP>no (default is <tt>false</tt>)</TD>
177      * </TR>
178      *
179      * <TR CLASS="TableRowColor">
180      * <TD VALIGN=TOP><TT>messageTemplate</TT></TD>
181      * <TD colspan="2">
182      * The template for formatting the body of the message. The
183      * following tokens are replaced in this string:<P>
184      * <ul>
185      * <table border=0 cellpadding=3 cellspacing=0>
186      * <tr><td><tt>{CHANNEL}</tt></td><td>Channel name</td></tr>
187      * <tr><td><tt>{CALLER-FULLNAME}</tt></td><td>Full caller class name (with package)</td></tr>
188      * <tr><td><tt>{CALLER}</tt></td><td>Caller class name</td></tr>
189      * <tr><td><tt>{MESSAGE}</tt></td><td>Short message</td></tr>
190      * <tr><td><tt>{THREAD}</tt></td><td>Thread name</td></tr>
191      * <tr><td><tt>{SEVERITY}</tt></td><td>Severity ("<tt>DEBUG</tt>" ... "<tt>FATAL</tt>")</td></tr>
192      * </table>
193      * </ul><P>
194      *
195      * default is <nobr>"<tt>[{SEVERITY}] {CALLER}: {MESSAGE}</tt>"</nobr></TD>
196      *
197      * </TR>
198      *
199      * <TR CLASS="TableRowColor">
200      * <TD VALIGN=TOP><tt>SeverityMap</tt></TD>
201      * <TD VALIGN=TOP>
202      * Contains a set of <TT>&lt;MapEntry&gt;</TT> elements,
203      * each containing a <TT>&lt;syslog&gt;</TT> and
204      * <TT>&lt;unix&gt;</TT> elements. Valid values for
205      * the <TT>&lt;syslog&gt;</tt> element are <TT>DEBUG</TT>,
206      * <TT>INFO</TT>, <TT>WARNING</TT>, <TT>ERROR</TT> and
207      * <TT>FATAL</TT>. You <i>must</i> specify map entries for
208      * <i>all</i> of the syslog severities. Each must map to
209      * a UNIX syslog severity, whose legal values are:
210      * <TT>DEBUG</TT>, <TT>INFO</TT>, <TT>NOTICE</TT>,
211      * <TT>WARNING</TT>, <TT>ERROR</TT>, <TT>CRITICAL</TT>
212      * and <TT>EMERGENCY</TT>.
213      * </TD>
214      * <TD VALIGN=TOP>no (default is shown above)</TD>
215      * </TR>
216      *
217      * </TABLE><P>
218      *
219      */

220     public void configure(Object JavaDoc o, Element e)
221     throws SyslogInitException
222     {
223         super.configure(o, e, false, true);
224
225         UNIXSyslogLog log = (UNIXSyslogLog)o;
226
227         String JavaDoc tmp = e.getChildTextTrim("logServer", e.getNamespace());
228         if (tmp != null)
229         {
230             try
231             {
232                 log.setLogServer(InetAddress.getByName(tmp));
233             }
234             catch (UnknownHostException x)
235             {
236                 throw new IllegalArgumentException JavaDoc(
237                     MessageFormat.format(Syslog.getResourceString(MessageConstants.UNIX_UNKNOWN_HOST),
238                                          new Object JavaDoc[] { tmp } ));
239             }
240         }
241         else
242         {
243             throw new IllegalArgumentException JavaDoc(
244                 MessageFormat.format(Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_PARAM_MESSAGE),
245                                      new Object JavaDoc[] { "logServer" } ));
246         }
247
248         tmp = e.getChildTextTrim("port", e.getNamespace());
249         if (tmp != null)
250         {
251             try
252             {
253                 log.setPort(Integer.parseInt(tmp));
254             }
255             catch (NumberFormatException JavaDoc x)
256             {
257                 throw new IllegalArgumentException JavaDoc(
258                     MessageFormat.format(Syslog.getResourceString(MessageConstants.UNIX_BAD_PORT),
259                                          new Object JavaDoc[] { tmp } ));
260             }
261         }
262
263         tmp = e.getChildTextTrim("tag", e.getNamespace());
264         if (tmp != null)
265         {
266             log.setTag(tmp);
267         }
268
269         tmp = e.getChildTextTrim("facility", e.getNamespace());
270         if (tmp != null)
271         {
272             try
273             {
274                 log.setFacility(Integer.parseInt(tmp));
275             }
276             catch (NumberFormatException JavaDoc x)
277             {
278                 throw new IllegalArgumentException JavaDoc(
279                     MessageFormat.format(Syslog.getResourceString(MessageConstants.UNIX_BAD_FACILITY),
280                                          new Object JavaDoc[] { tmp } ));
281             }
282         }
283
284         tmp = e.getChildTextTrim("showHostname", e.getNamespace());
285         if (tmp != null)
286             log.setShowHostname("true".equalsIgnoreCase(tmp.trim()));
287
288         tmp = e.getChildTextTrim("messageTemplate", e.getNamespace());
289         if (tmp != null)
290             log.setMessageTemplate(tmp.trim());
291
292         Element element = e.getChild("SeverityMap", e.getNamespace());
293         if (element != null)
294         {
295             Iterator params = element.getChildren("MapEntry", e.getNamespace()).iterator();
296             Map severityMap = new HashMap();
297             while (params.hasNext())
298             {
299                 Element param = (Element)params.next();
300                 String JavaDoc syslog = param.getChildTextTrim("syslog", e.getNamespace());
301                 String JavaDoc unix = param.getChildTextTrim("unix", e.getNamespace());
302                 int syslogSeverity = 0;
303                 int unixSeverity = 0;
304                 if (syslog != null && unix != null)
305                 {
306                     if ("DEBUG".equalsIgnoreCase(syslog))
307                         syslogSeverity = Syslog.DEBUG;
308                     else if ("INFO".equalsIgnoreCase(syslog))
309                         syslogSeverity = Syslog.INFO;
310                     else if ("WARNING".equalsIgnoreCase(syslog))
311                         syslogSeverity = Syslog.WARNING;
312                     else if ("ERROR".equalsIgnoreCase(syslog))
313                         syslogSeverity = Syslog.ERROR;
314                     else if ("FATAL".equalsIgnoreCase(syslog))
315                         syslogSeverity = Syslog.FATAL;
316                     else
317                         throw new SyslogInitException(MessageFormat.format(
318                           Syslog.getResourceString(MessageConstants.UNIX_BAD_SYSLOG_SEVERITY),
319                           new Object JavaDoc[] { syslog } ));
320
321                     if ("DEBUG".equalsIgnoreCase(unix))
322                         unixSeverity = UNIXSyslogLog.UNIX_DEBUG;
323                     else if ("INFO".equalsIgnoreCase(unix))
324                         unixSeverity = UNIXSyslogLog.UNIX_INFO;
325                     else if ("NOTICE".equalsIgnoreCase(unix))
326                         unixSeverity = UNIXSyslogLog.UNIX_NOTICE;
327                     else if ("WARNING".equalsIgnoreCase(unix))
328                         unixSeverity = UNIXSyslogLog.UNIX_WARNING;
329                     else if ("ERROR".equalsIgnoreCase(unix))
330                         unixSeverity = UNIXSyslogLog.UNIX_ERROR;
331                     else if ("CRITICAL".equalsIgnoreCase(unix))
332                         unixSeverity = UNIXSyslogLog.UNIX_CRITICAL;
333                     else if ("ALERT".equalsIgnoreCase(unix))
334                         unixSeverity = UNIXSyslogLog.UNIX_ALERT;
335                     else if ("EMERGENCY".equalsIgnoreCase(unix))
336                         unixSeverity = UNIXSyslogLog.UNIX_EMERGENCY;
337                     else
338                         throw new SyslogInitException(MessageFormat.format(
339                           Syslog.getResourceString(MessageConstants.UNIX_BAD_UNIX_SEVERITY),
340                           new Object JavaDoc[] { unix } ));
341
342                     severityMap.put(new Integer JavaDoc(syslogSeverity), new Integer JavaDoc(unixSeverity));
343                 }
344             }
345             log.setSeverityMap(severityMap);
346         }
347         else
348         {
349             log.setSeverityMap(UNIXSyslogLog.DEFAULT_SEVERITY_MAP);
350         }
351
352     }
353
354     public Element getConfiguration(Object JavaDoc o, Element element)
355     {
356         Element e = super.getConfiguration(o, element, false, true);
357
358         UNIXSyslogLog log = (UNIXSyslogLog)o;
359
360         Element logServer = new Element("logServer");
361         logServer.setText(log.getLogServer().getHostName());
362         e.getChildren().add(logServer);
363
364         Element port = new Element("port");
365         port.setText(String.valueOf(log.getPort()));
366         e.getChildren().add(port);
367
368         Element facility = new Element("facility");
369         facility.setText(String.valueOf(log.getFacility()));
370         e.getChildren().add(facility);
371
372         Element tag = new Element("tag");
373         tag.setText(log.getTag());
374         e.getChildren().add(tag);
375
376         Element showHostname = new Element("showHostname");
377         showHostname.setText(String.valueOf(log.getShowHostname()));
378         e.getChildren().add(showHostname);
379
380         Element messageTemplate = new Element("messageTemplate");
381         messageTemplate.setText(log.getMessageTemplate());
382         e.getChildren().add(messageTemplate);
383
384         Element param = new Element("SeverityMap");
385
386         Map severityMap = log.getSeverityMap();
387
388         Element mapEntry = new Element("MapEntry");
389         mapEntry.getChildren().add(new Element("syslog").setText("DEBUG"));
390         mapEntry.getChildren().add(new Element("unix").setText(getUNIXSeverityName(severityMap, Syslog.DEBUG)));
391         param.getChildren().add(mapEntry);
392
393         mapEntry = new Element("MapEntry");
394         mapEntry.getChildren().add(new Element("syslog").setText("INFO"));
395         mapEntry.getChildren().add(new Element("unix").setText(getUNIXSeverityName(severityMap, Syslog.INFO)));
396         param.getChildren().add(mapEntry);
397
398         mapEntry = new Element("MapEntry");
399         mapEntry.getChildren().add(new Element("syslog").setText("WARNING"));
400         mapEntry.getChildren().add(new Element("unix").setText(getUNIXSeverityName(severityMap, Syslog.WARNING)));
401         param.getChildren().add(mapEntry);
402
403         mapEntry = new Element("MapEntry");
404         mapEntry.getChildren().add(new Element("syslog").setText("ERROR"));
405         mapEntry.getChildren().add(new Element("unix").setText(getUNIXSeverityName(severityMap, Syslog.ERROR)));
406         param.getChildren().add(mapEntry);
407
408         mapEntry = new Element("MapEntry");
409         mapEntry.getChildren().add(new Element("syslog").setText("FATAL"));
410         mapEntry.getChildren().add(new Element("unix").setText(getUNIXSeverityName(severityMap, Syslog.FATAL)));
411         param.getChildren().add(mapEntry);
412
413
414         e.getChildren().add(param);
415
416         return e;
417     }
418
419     private static String JavaDoc getUNIXSeverityName(Map severityMap, int syslogSeverity)
420     {
421         int severity = ((Integer JavaDoc)severityMap.get(new Integer JavaDoc(syslogSeverity))).intValue();
422         switch (severity)
423         {
424             case UNIXSyslogLog.UNIX_DEBUG:
425                 return "DEBUG";
426             case UNIXSyslogLog.UNIX_INFO:
427                 return "INFO";
428             case UNIXSyslogLog.UNIX_NOTICE:
429                 return "NOTICE";
430             case UNIXSyslogLog.UNIX_WARNING:
431                 return "WARNING";
432             case UNIXSyslogLog.UNIX_ERROR:
433                 return "ERROR";
434             case UNIXSyslogLog.UNIX_CRITICAL:
435                 return "CRITICAL";
436             case UNIXSyslogLog.UNIX_ALERT:
437                 return "ALERT";
438             case UNIXSyslogLog.UNIX_EMERGENCY:
439                 return "EMERGENCY";
440         }
441         return "GARBAGE";
442     }
443 }
444
Popular Tags