KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > xml > SimpleLogPolicy_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 com.protomatter.syslog.*;
57 import com.protomatter.xml.*;
58 import org.jdom.*;
59 import org.jdom.output.*;
60 import org.jdom.input.*;
61
62 /**
63  * XML configuration helper for <tt>SimpleLogPolicy</tt>.
64  */

65 public class SimpleLogPolicy_Helper
66 implements XMLConfigHelper
67 {
68   /**
69    * Configure this policy given the XML element.
70    * The <tt>&lt;Policy&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;Policy class="<i>PolicyClassName</i>" &gt;
77    *
78    * &lt;logMask&gt;<i>LogMask</i>&lt;/logMask&gt;
79    * &lt;channels&gt;<i>ChannelList</i>&lt;/channels&gt;
80    *
81    * &lt;/Policy&gt;
82    * </B></PRE>
83    * </TD></TR></TABLE><P>
84    *
85    * This class reads the "<tt>logMask</tt>" and "<tt>channels</tt>"
86    * elements.<P>
87    *
88    * <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH="90%">
89    * <TR CLASS="TableHeadingColor">
90    * <TD COLSPAN=3><B>Element</B></TD>
91    * </TR>
92    * <TR CLASS="TableHeadingColor">
93    * <TD><B>name</B></TD>
94    * <TD><B>value</B></TD>
95    * <TD><B>required</B></TD>
96    * </TR>
97    *
98    * <TR CLASS="TableRowColor">
99    * <TD VALIGN=TOP><TT>logMask</TT></TD>
100    * <TD>A log mask string. Integers are treated as raw masks
101    * and log level names (<TT>DEBUG</TT>, <TT>INFO</TT>,
102    * <tt>WARNING</TT>, <TT>ERROR</TT> and <TT>FATAL</TT>)
103    * are interpreted as at-or-above the given level.
104    * </TD>
105    * <TD VALIGN=TOP>no (default is <tt>INHERIT_MASK</TT>)</TD>
106    * </TR>
107    *
108    * <TR CLASS="TableRowColor">
109    * <TD VALIGN=TOP><TT>channels</TT></TD>
110    * <TD>A comma and/or space separated list of channel names.
111    * The constants <TT>DEFAULT_CHANNEL</TT> and <TT>ALL_CHANNEL</TT>
112    * are interpreted as their symbolic values.
113    * </TD>
114    * <TD VALIGN=TOP>no (default is <tt>ALL_CHANNEL</TT>)</TD>
115    * </TR>
116    *
117    * </TABLE><P>
118    */

119   public void configure(Object JavaDoc what, Element e)
120   throws SyslogInitException
121   {
122     SimpleLogPolicy policy = (SimpleLogPolicy)what;
123
124     String JavaDoc tmp = e.getChildTextTrim("logMask", e.getNamespace());
125     if (tmp != null)
126     {
127       policy.setLogMask(tmp);
128     }
129
130     tmp = e.getChildTextTrim("channels", e.getNamespace());
131     if (tmp == null)
132       tmp = "";
133
134     // if they are specifying any channels at all,
135
// then make them explicity list the ones they
136
// want with no defaults.
137
policy.removeAllChannels();
138     StringTokenizer st = new StringTokenizer(tmp, ", ");
139     while (st.hasMoreTokens())
140     {
141       String JavaDoc chan = st.nextToken();
142       if (chan.equals("ALL_CHANNEL"))
143         policy.addChannel(Syslog.ALL_CHANNEL);
144       else if (chan.equals("DEFAULT_CHANNEL"))
145         policy.addChannel(Syslog.DEFAULT_CHANNEL);
146       else
147         policy.addChannel(chan);
148     }
149   }
150
151   public Element getConfiguration(Object JavaDoc o, Element element)
152   {
153     SimpleLogPolicy policy = (SimpleLogPolicy)o;
154     if (element == null)
155     {
156       element = new Element("Policy");
157       element.setAttribute("name", policy.getName());
158       element.setAttribute("class", policy.getClass().getName());
159     }
160
161     Element param = new Element("channels");
162     boolean allChannels = false;
163     Iterator i = policy.getChannels();
164     while (i.hasNext())
165     {
166       String JavaDoc channel = (String JavaDoc)i.next();
167       if (Syslog.ALL_CHANNEL.equals(channel))
168         allChannels = true;
169     }
170
171     if (allChannels)
172     {
173       param.setText("ALL_CHANNEL");
174     }
175     else
176     {
177       StringBuffer JavaDoc channelList = new StringBuffer JavaDoc();
178       i = policy.getChannels();
179       while (i.hasNext())
180       {
181         channelList.append(i.next());
182         if (i.hasNext())
183           channelList.append(", ");
184       }
185       param.setText(channelList.toString());
186     }
187     element.getChildren().add(param);
188
189     Element mask = new Element("logMask");
190     mask.setText(Syslog.getLogMaskAsString(policy.getLogMask()));
191     element.getChildren().add(mask);
192
193     return element;
194   }
195 }
196
Popular Tags