KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > xml > PerChannelPolicy_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>PerChannelPolicy</tt>.
64  */

65 public class PerChannelPolicy_Helper
66 extends SimpleLogPolicy_Helper
67 {
68   /**
69    * Configure this policy given the XML element.
70    * This class inherits the policy element parameters
71    * that {@link SimpleLogPolicy_Helper#configure(Object,Element) SimpleLogPolicy_Helper}
72    * understands. The additional <tt>Policy</tt> elements should
73    * look like this:<P>
74    *
75    * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
76    * <TR><TD>
77    * <PRE><B>
78    *
79    * &lt;Policy class="com.protomatter.syslog.PerChannelPolicy" &gt;
80    *
81    * <font color="#888888">&lt;!--
82    * Config params from {@link SimpleLogPolicy_Helper#configure(Object,Element) SimpleLogPolicy_Helper} can
83    * get inserted here.
84    * --&gt;</font>
85    *
86    * &lt;PolicyGroup&gt;
87    *
88    * <font color="#888888">&lt;!--
89    * Config params from {@link SimpleLogPolicy_Helper#configure(Object,Element) SimpleLogPolicy_Helper} can
90    * get inserted here.
91    * --&gt;</font>
92    *
93    * &lt;channelPattern&gt;<i>ChannelPattern-1</i>&lt;/channelPattern&gt;
94    * &lt;channelPattern&gt;<i>ChannelPattern-2</i>&lt;/channelPattern&gt;
95    * ...
96    * &lt;channelPattern&gt;<i>ChannelPattern-N</i>&lt;/channelPattern&gt;
97    *
98    * &lt;channelName&gt;<i>ChannelName-1</i>&lt;/channelName&gt;
99    * &lt;channelName&gt;<i>ChannelName-2</i>&lt;/channelName&gt;
100    * ...
101    * &lt;channelName&gt;<i>ChannelName-N</i>&lt;/channelName&gt;
102    * &lt;/PolicyGroup&gt;
103    *
104    * <font color="#888888">&lt;!-- Insert more PolicyGroups here --&gt;</font>
105    *
106    * &lt;/Policy&gt;
107    * </B></PRE>
108    * </TD></TR></TABLE><P>
109    *
110    * This class basically reads each <TT>&lt;PolicyGroup&gt;</tt>
111    * element and creates a new {@link com.protomatter.syslog.PerChannelPolicy.PolicyGroup PolicyGroup} instance and
112    * configures that instance with the <tt>&lt;PolicyGroup&gt;</tt>
113    * element.<P>
114    *
115    * If none of the pattern groups has anything to say about
116    * logging the message, the decision is deferred to the
117    * {@link com.protomatter.syslog.SimpleLogPolicy superclass} and it makes the
118    * decision based on the log mask and channel information
119    * that was defined in the enclosing <tt>Policy</tt> element.<P>
120    *
121    * <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH="90%">
122    * <TR CLASS="TableHeadingColor">
123    * <TD COLSPAN=3><B>Element</B></TD>
124    * </TR>
125    * <TR CLASS="TableHeadingColor">
126    * <TD><B>name</B></TD>
127    * <TD><B>value</B></TD>
128    * <TD><B>required</B></TD>
129    * </TR>
130    *
131    * <TR CLASS="TableRowColor">
132    * <TD VALIGN=TOP><tt>PolicyGroup</tt></TD>
133    * <TD VALIGN=TOP>See {@link com.protomatter.syslog.PerChannelPolicy.PolicyGroup PolicyGroup} docs.</TD>
134    * <TD VALIGN=TOP><i>no</i></TD>
135    * </TR>
136    *
137    * </TABLE><P>
138    *
139    * The following example should explain things better:<P>
140    *
141    * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
142    * <TR><TD>
143    * <PRE><B>
144    *
145    * &lt;Policy class="com.protomatter.syslog.PerChannelPolicy" &gt;
146    *
147    * <font color="#888888">&lt;!--
148    * Defaults -- if none of the groups below match,
149    * then only allow messages at or above the WARNING level.
150    * --&gt;</font>
151    * &lt;logMask&gt;WARNING&lt;/logMask&gt;
152    * &lt;channels&gt;ALL_CHANNEL&lt;/channels&gt;
153    *
154    * <font color="#888888">&lt;!--
155    * Log DEBUG and above messages for messages coming from
156    * the <tt>com.protomatter.xml.*</tt> and <tt>com.protomatter.jdbc.*</tt>
157    * packages, and from the <tt>com.protomatter.util.Mutex</tt> channel.
158    * --&gt;</font>
159    * &lt;PolicyGroup&gt;
160    * &lt;logMask&gt;DEBUG&lt;/logMask&gt;
161    * &lt;channels&gt;&lt;/channels&gt;
162    *
163    * &lt;channelPattern&gt;com.protomatter.xml.*&lt;/channelPattern&gt;
164    * &lt;channelPattern&gt;com.protomatter.jdbc.*&lt;/channelPattern&gt;
165    *
166    * &lt;channelName&gt;com.protomatter.util.Mutex&lt;/channelName&gt;
167    * &lt;/PolicyGroup&gt;
168    *
169    * <font color="#888888">&lt;!--
170    * Log INFO and above messages for messages coming from
171    * the <tt>com.protomatter.pas.*</tt> and <tt>com.protomatter.util.*</tt>
172    * packages, and from the <CC>com.protomatter.jdbc.pool</TT> channel.
173    * --&gt;</font>
174    * &lt;PolicyGroup&gt;
175    * &lt;logMask&gt;INFO&lt;/logMask&gt;
176    * &lt;channels&gt;&lt;/channels&gt;
177    *
178    * &lt;channelPattern&gt;com.protomatter.pas.*&lt;/channelPattern&gt;
179    * &lt;channelPattern&gt;com.protomatter.util.*&lt;/channelPattern&gt;
180    *
181    * &lt;channelName&gt;com.protomatter.jdbc.pool&lt;/channelName&gt;
182    * &lt;/PolicyGroup&gt;
183    *
184    * <font color="#888888">&lt;!--
185    * If the channel name contains the word "Debug" then allow
186    * messages at or above the ERROR level.
187    * --&gt;</font>
188    * &lt;PolicyGroup&gt;
189    * &lt;logMask&gt;ERROR&lt;/logMask&gt;
190    * &lt;channels&gt;&lt;/channels&gt;
191    *
192    * &lt;channelPattern&gt;*Debug*&lt;/channelPattern&gt;
193    * &lt;/PolicyGroup&gt;
194    *
195    * &lt;/Policy&gt;
196    * </B></PRE>
197    * </TD></TR></TABLE><P>
198    *
199    */

200   public void configure(Object JavaDoc o, Element e)
201   throws SyslogInitException
202   {
203     super.configure(o, e);
204     PerChannelPolicy policy = (PerChannelPolicy)o;
205
206     List myList = new ArrayList();
207
208     Iterator params = e.getChildren("PolicyGroup", e.getNamespace()).iterator();
209     while (params.hasNext())
210     {
211       Element param = (Element)params.next();
212       PerChannelPolicy.PolicyGroup g = new PerChannelPolicy.PolicyGroup();
213       configureGroup(g, param);
214       myList.add(g);
215     }
216
217     policy.setGroupList(myList);
218   }
219
220   public void configureGroup(PerChannelPolicy.PolicyGroup g, Element e)
221   throws SyslogInitException
222   {
223     super.configure(g, e);
224
225     Iterator kids = e.getChildren("channelPattern", e.getNamespace()).iterator();
226     while (kids.hasNext())
227     {
228       Element param = (Element)kids.next();
229       String JavaDoc value = param.getTextTrim();
230       if (value != null)
231         g.addChannelPattern(value);
232     }
233
234     kids = e.getChildren("channelName", e.getNamespace()).iterator();
235     while (kids.hasNext())
236     {
237       Element param = (Element)kids.next();
238       String JavaDoc value = param.getTextTrim();
239       if (value != null)
240         g.addChannelName(value);
241     }
242   }
243
244   public Element getConfiguration(Object JavaDoc o, Element element)
245   {
246     element = super.getConfiguration(o, element);
247
248     PerChannelPolicy policy = (PerChannelPolicy)o;
249
250     Iterator i = policy.getPolicyGroups();
251     while (i.hasNext())
252     {
253       PerChannelPolicy.PolicyGroup g = (PerChannelPolicy.PolicyGroup)i.next();
254       Element e = new Element("PolicyGroup");
255       e = getConfigurationGroup(g, e);
256       element.getChildren().add(e);
257     }
258     return element;
259   }
260
261   public Element getConfigurationGroup(PerChannelPolicy.PolicyGroup g, Element element)
262   {
263     Element e = super.getConfiguration(g, element);
264
265     // fill in the list of channeles.
266
Iterator i = g.getPatternSet();
267     while (i.hasNext())
268     {
269       e.getChildren().add(
270         (new Element("channelPattern")).setText((String JavaDoc)i.next()));
271     }
272
273     // fill in the list of channeles.
274
i = g.getChannelSet();
275     while (i.hasNext())
276     {
277       e.getChildren().add(
278         (new Element("channelName")).setText((String JavaDoc)i.next()));
279     }
280
281     return e;
282   }
283 }
284
Popular Tags