001: package com.protomatter.syslog.xml;
002:
003: /**
004: * {{{ The Protomatter Software License, Version 1.0
005: * derived from The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution,
022: * if any, must include the following acknowledgment:
023: * "This product includes software developed for the
024: * Protomatter Software Project
025: * (http://protomatter.sourceforge.net/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Protomatter" and "Protomatter Software Project" must
030: * not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact support@protomatter.com.
033: *
034: * 5. Products derived from this software may not be called "Protomatter",
035: * nor may "Protomatter" appear in their name, without prior written
036: * permission of the Protomatter Software Project
037: * (support@protomatter.com).
038: *
039: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042: * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
043: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050: * SUCH DAMAGE. }}}
051: */
052:
053: import java.io.*;
054: import java.net.*;
055: import java.util.*;
056: import java.text.*;
057:
058: import com.protomatter.xml.*;
059: import com.protomatter.syslog.*;
060: import org.jdom.*;
061:
062: /**
063: * XML configuration helper for <tt>UNIXSyslogLog</tt>.
064: */
065: public class UNIXSyslogLog_Helper extends BasicLogger_Helper {
066: /**
067: * Configure this logger given the XML element.
068: * The <tt><Logger></tt> element should look like this:<P>
069: *
070: * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
071: * <TR><TD>
072: * <PRE><B>
073: *
074: * <Logger class="com.protomatter.syslog.UNIXSyslogLog" >
075: *
076: * <font color="#888888"><!--
077: *
078: * Note that the <tt>hostname</tt> attribute of the
079: * <tt><Syslog></tt> tag must be specified.
080: *
081: * Config params from {@link BasicLogger_Helper#configure(Object,Element) BasicLogger_Helper} can
082: * get inserted here.
083: *
084: * <B>Note:</B> The <TT><Format/></TT> tag is completely ignored
085: * because the format is dictated by the UNIX
086: * syslog system.
087: * --></font>
088: *
089: * <logServer><i>hostname or IP address</i></logServer>
090: * <port><i>port</i></port>
091: * <facility><i>facility-id</i></facility>
092: * <tag><i>tag-value</i></tag>
093: * <showHostname><i>true|false</i></showHostname>
094: *
095: * <messageTemplate><i>message-body-template</i></messageTemplate>
096: *
097: * <SeverityMap>
098: * <MapEntry>
099: * <syslog>DEBUG</syslog>
100: * <unix>DEBUG</unix>
101: * </MapEntry>
102: * <MapEntry>
103: * <syslog>INFO</syslog>
104: * <unix>INFO</unix>
105: * </MapEntry>
106: * <MapEntry>
107: * <syslog>WARNING</syslog>
108: * <unix>WARNING</unix>
109: * </MapEntry>
110: * <MapEntry>
111: * <syslog>ERROR</syslog>
112: * <unix>ERROR</unix>
113: * </MapEntry>
114: * <MapEntry>
115: * <syslog>FATAL</syslog>
116: * <unix>EMERGENCY</unix>
117: * </MapEntry>
118: * </SeverityMap>
119: *
120: * </Logger>
121: * </B></PRE>
122: * </TD></TR></TABLE><P>
123: *
124: * <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH="90%">
125: * <TR CLASS="TableHeadingColor">
126: * <TD COLSPAN=3><B>Element</B></TD>
127: * </TR>
128: * <TR CLASS="TableHeadingColor">
129: * <TD><B>name</B></TD>
130: * <TD><B>value</B></TD>
131: * <TD><B>required</B></TD>
132: * </TR>
133: *
134: * <TR CLASS="TableRowColor">
135: * <TD VALIGN=TOP><TT>logServer</TT></TD>
136: * <TD>The hostname or IP address of the log server that
137: * packets should be sent to.
138: * </TD>
139: * <TD VALIGN=TOP>yes</TD>
140: * </TR>
141: *
142: * <TR CLASS="TableRowColor">
143: * <TD VALIGN=TOP><TT>port</TT></TD>
144: * <TD>The port number to send packets to on the log server.
145: * </TD>
146: * <TD VALIGN=TOP>no (default is <TT>514</TT>)</TD>
147: * </TR>
148: *
149: * <TR CLASS="TableRowColor">
150: * <TD VALIGN=TOP><TT>facility</TT></TD>
151: * <TD>The UNIX syslog facility ID that messages appear from.
152: * A list can be found <a href="../UNIXSyslogLog.html#setFacility(int)">here</a>.
153: * </TD>
154: * <TD VALIGN=TOP>no (default is <TT>16</TT>)</TD>
155: * </TR>
156: *
157: * <TR CLASS="TableRowColor">
158: * <TD VALIGN=TOP><TT>tag</TT></TD>
159: * <TD>Text value of the packet "tag" -- the default
160: * is "<TT>ProtomatterSyslog</TT>" and is generally meant
161: * to be the application or "process" name.
162: * </TD>
163: * <TD VALIGN=TOP>no (default is "<tt>ProtomatterSyslog</tt>")</TD>
164: * </TR>
165: *
166: * <TR CLASS="TableRowColor">
167: * <TD VALIGN=TOP><TT>showHostname</TT></TD>
168: * <TD><tt>true</tt> or <tt>false</tt> -- decide if we should
169: * show the hostname before the "tag". The spec says that you
170: * should do this, but when I tested under Red Hat Linux 7.2,
171: * it just ended up repeating the data. Your mileage may
172: * vary.
173: * </TD>
174: * <TD VALIGN=TOP>no (default is <tt>false</tt>)</TD>
175: * </TR>
176: *
177: * <TR CLASS="TableRowColor">
178: * <TD VALIGN=TOP><TT>messageTemplate</TT></TD>
179: * <TD colspan="2">
180: * The template for formatting the body of the message. The
181: * following tokens are replaced in this string:<P>
182: * <ul>
183: * <table border=0 cellpadding=3 cellspacing=0>
184: * <tr><td><tt>{CHANNEL}</tt></td><td>Channel name</td></tr>
185: * <tr><td><tt>{CALLER-FULLNAME}</tt></td><td>Full caller class name (with package)</td></tr>
186: * <tr><td><tt>{CALLER}</tt></td><td>Caller class name</td></tr>
187: * <tr><td><tt>{MESSAGE}</tt></td><td>Short message</td></tr>
188: * <tr><td><tt>{THREAD}</tt></td><td>Thread name</td></tr>
189: * <tr><td><tt>{SEVERITY}</tt></td><td>Severity ("<tt>DEBUG</tt>" ... "<tt>FATAL</tt>")</td></tr>
190: * </table>
191: * </ul><P>
192: *
193: * default is <nobr>"<tt>[{SEVERITY}] {CALLER}: {MESSAGE}</tt>"</nobr></TD>
194: *
195: * </TR>
196: *
197: * <TR CLASS="TableRowColor">
198: * <TD VALIGN=TOP><tt>SeverityMap</tt></TD>
199: * <TD VALIGN=TOP>
200: * Contains a set of <TT><MapEntry></TT> elements,
201: * each containing a <TT><syslog></TT> and
202: * <TT><unix></TT> elements. Valid values for
203: * the <TT><syslog></tt> element are <TT>DEBUG</TT>,
204: * <TT>INFO</TT>, <TT>WARNING</TT>, <TT>ERROR</TT> and
205: * <TT>FATAL</TT>. You <i>must</i> specify map entries for
206: * <i>all</i> of the syslog severities. Each must map to
207: * a UNIX syslog severity, whose legal values are:
208: * <TT>DEBUG</TT>, <TT>INFO</TT>, <TT>NOTICE</TT>,
209: * <TT>WARNING</TT>, <TT>ERROR</TT>, <TT>CRITICAL</TT>
210: * and <TT>EMERGENCY</TT>.
211: * </TD>
212: * <TD VALIGN=TOP>no (default is shown above)</TD>
213: * </TR>
214: *
215: * </TABLE><P>
216: *
217: */
218: public void configure(Object o, Element e)
219: throws SyslogInitException {
220: super .configure(o, e, false, true);
221:
222: UNIXSyslogLog log = (UNIXSyslogLog) o;
223:
224: String tmp = e.getChildTextTrim("logServer", e.getNamespace());
225: if (tmp != null) {
226: try {
227: log.setLogServer(InetAddress.getByName(tmp));
228: } catch (UnknownHostException x) {
229: throw new IllegalArgumentException(
230: MessageFormat
231: .format(
232: Syslog
233: .getResourceString(MessageConstants.UNIX_UNKNOWN_HOST),
234: new Object[] { tmp }));
235: }
236: } else {
237: throw new IllegalArgumentException(
238: MessageFormat
239: .format(
240: Syslog
241: .getResourceString(MessageConstants.XML_MUST_SPECIFY_PARAM_MESSAGE),
242: new Object[] { "logServer" }));
243: }
244:
245: tmp = e.getChildTextTrim("port", e.getNamespace());
246: if (tmp != null) {
247: try {
248: log.setPort(Integer.parseInt(tmp));
249: } catch (NumberFormatException x) {
250: throw new IllegalArgumentException(
251: MessageFormat
252: .format(
253: Syslog
254: .getResourceString(MessageConstants.UNIX_BAD_PORT),
255: new Object[] { tmp }));
256: }
257: }
258:
259: tmp = e.getChildTextTrim("tag", e.getNamespace());
260: if (tmp != null) {
261: log.setTag(tmp);
262: }
263:
264: tmp = e.getChildTextTrim("facility", e.getNamespace());
265: if (tmp != null) {
266: try {
267: log.setFacility(Integer.parseInt(tmp));
268: } catch (NumberFormatException x) {
269: throw new IllegalArgumentException(
270: MessageFormat
271: .format(
272: Syslog
273: .getResourceString(MessageConstants.UNIX_BAD_FACILITY),
274: new Object[] { tmp }));
275: }
276: }
277:
278: tmp = e.getChildTextTrim("showHostname", e.getNamespace());
279: if (tmp != null)
280: log.setShowHostname("true".equalsIgnoreCase(tmp.trim()));
281:
282: tmp = e.getChildTextTrim("messageTemplate", e.getNamespace());
283: if (tmp != null)
284: log.setMessageTemplate(tmp.trim());
285:
286: Element element = e.getChild("SeverityMap", e.getNamespace());
287: if (element != null) {
288: Iterator params = element.getChildren("MapEntry",
289: e.getNamespace()).iterator();
290: Map severityMap = new HashMap();
291: while (params.hasNext()) {
292: Element param = (Element) params.next();
293: String syslog = param.getChildTextTrim("syslog", e
294: .getNamespace());
295: String unix = param.getChildTextTrim("unix", e
296: .getNamespace());
297: int syslogSeverity = 0;
298: int unixSeverity = 0;
299: if (syslog != null && unix != null) {
300: if ("DEBUG".equalsIgnoreCase(syslog))
301: syslogSeverity = Syslog.DEBUG;
302: else if ("INFO".equalsIgnoreCase(syslog))
303: syslogSeverity = Syslog.INFO;
304: else if ("WARNING".equalsIgnoreCase(syslog))
305: syslogSeverity = Syslog.WARNING;
306: else if ("ERROR".equalsIgnoreCase(syslog))
307: syslogSeverity = Syslog.ERROR;
308: else if ("FATAL".equalsIgnoreCase(syslog))
309: syslogSeverity = Syslog.FATAL;
310: else
311: throw new SyslogInitException(
312: MessageFormat
313: .format(
314: Syslog
315: .getResourceString(MessageConstants.UNIX_BAD_SYSLOG_SEVERITY),
316: new Object[] { syslog }));
317:
318: if ("DEBUG".equalsIgnoreCase(unix))
319: unixSeverity = UNIXSyslogLog.UNIX_DEBUG;
320: else if ("INFO".equalsIgnoreCase(unix))
321: unixSeverity = UNIXSyslogLog.UNIX_INFO;
322: else if ("NOTICE".equalsIgnoreCase(unix))
323: unixSeverity = UNIXSyslogLog.UNIX_NOTICE;
324: else if ("WARNING".equalsIgnoreCase(unix))
325: unixSeverity = UNIXSyslogLog.UNIX_WARNING;
326: else if ("ERROR".equalsIgnoreCase(unix))
327: unixSeverity = UNIXSyslogLog.UNIX_ERROR;
328: else if ("CRITICAL".equalsIgnoreCase(unix))
329: unixSeverity = UNIXSyslogLog.UNIX_CRITICAL;
330: else if ("ALERT".equalsIgnoreCase(unix))
331: unixSeverity = UNIXSyslogLog.UNIX_ALERT;
332: else if ("EMERGENCY".equalsIgnoreCase(unix))
333: unixSeverity = UNIXSyslogLog.UNIX_EMERGENCY;
334: else
335: throw new SyslogInitException(
336: MessageFormat
337: .format(
338: Syslog
339: .getResourceString(MessageConstants.UNIX_BAD_UNIX_SEVERITY),
340: new Object[] { unix }));
341:
342: severityMap.put(new Integer(syslogSeverity),
343: new Integer(unixSeverity));
344: }
345: }
346: log.setSeverityMap(severityMap);
347: } else {
348: log.setSeverityMap(UNIXSyslogLog.DEFAULT_SEVERITY_MAP);
349: }
350:
351: }
352:
353: public Element getConfiguration(Object o, Element element) {
354: Element e = super .getConfiguration(o, element, false, true);
355:
356: UNIXSyslogLog log = (UNIXSyslogLog) o;
357:
358: Element logServer = new Element("logServer");
359: logServer.setText(log.getLogServer().getHostName());
360: e.getChildren().add(logServer);
361:
362: Element port = new Element("port");
363: port.setText(String.valueOf(log.getPort()));
364: e.getChildren().add(port);
365:
366: Element facility = new Element("facility");
367: facility.setText(String.valueOf(log.getFacility()));
368: e.getChildren().add(facility);
369:
370: Element tag = new Element("tag");
371: tag.setText(log.getTag());
372: e.getChildren().add(tag);
373:
374: Element showHostname = new Element("showHostname");
375: showHostname.setText(String.valueOf(log.getShowHostname()));
376: e.getChildren().add(showHostname);
377:
378: Element messageTemplate = new Element("messageTemplate");
379: messageTemplate.setText(log.getMessageTemplate());
380: e.getChildren().add(messageTemplate);
381:
382: Element param = new Element("SeverityMap");
383:
384: Map severityMap = log.getSeverityMap();
385:
386: Element mapEntry = new Element("MapEntry");
387: mapEntry.getChildren().add(
388: new Element("syslog").setText("DEBUG"));
389: mapEntry.getChildren().add(
390: new Element("unix").setText(getUNIXSeverityName(
391: severityMap, Syslog.DEBUG)));
392: param.getChildren().add(mapEntry);
393:
394: mapEntry = new Element("MapEntry");
395: mapEntry.getChildren().add(
396: new Element("syslog").setText("INFO"));
397: mapEntry.getChildren().add(
398: new Element("unix").setText(getUNIXSeverityName(
399: severityMap, Syslog.INFO)));
400: param.getChildren().add(mapEntry);
401:
402: mapEntry = new Element("MapEntry");
403: mapEntry.getChildren().add(
404: new Element("syslog").setText("WARNING"));
405: mapEntry.getChildren().add(
406: new Element("unix").setText(getUNIXSeverityName(
407: severityMap, Syslog.WARNING)));
408: param.getChildren().add(mapEntry);
409:
410: mapEntry = new Element("MapEntry");
411: mapEntry.getChildren().add(
412: new Element("syslog").setText("ERROR"));
413: mapEntry.getChildren().add(
414: new Element("unix").setText(getUNIXSeverityName(
415: severityMap, Syslog.ERROR)));
416: param.getChildren().add(mapEntry);
417:
418: mapEntry = new Element("MapEntry");
419: mapEntry.getChildren().add(
420: new Element("syslog").setText("FATAL"));
421: mapEntry.getChildren().add(
422: new Element("unix").setText(getUNIXSeverityName(
423: severityMap, Syslog.FATAL)));
424: param.getChildren().add(mapEntry);
425:
426: e.getChildren().add(param);
427:
428: return e;
429: }
430:
431: private static String getUNIXSeverityName(Map severityMap,
432: int syslogSeverity) {
433: int severity = ((Integer) severityMap.get(new Integer(
434: syslogSeverity))).intValue();
435: switch (severity) {
436: case UNIXSyslogLog.UNIX_DEBUG:
437: return "DEBUG";
438: case UNIXSyslogLog.UNIX_INFO:
439: return "INFO";
440: case UNIXSyslogLog.UNIX_NOTICE:
441: return "NOTICE";
442: case UNIXSyslogLog.UNIX_WARNING:
443: return "WARNING";
444: case UNIXSyslogLog.UNIX_ERROR:
445: return "ERROR";
446: case UNIXSyslogLog.UNIX_CRITICAL:
447: return "CRITICAL";
448: case UNIXSyslogLog.UNIX_ALERT:
449: return "ALERT";
450: case UNIXSyslogLog.UNIX_EMERGENCY:
451: return "EMERGENCY";
452: }
453: return "GARBAGE";
454: }
455: }
|