KickJava   Java API By Example, From Geeks To Geeks.

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

65 public class PerClassPolicy_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.PerClassPolicy" &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;classPattern&gt;<i>ClassPattern-1</i>&lt;/classPattern&gt;
94    * &lt;classPattern&gt;<i>ClassPattern-2</i>&lt;/classPattern&gt;
95    * ...
96    * &lt;classPattern&gt;<i>ClassPattern-N</i>&lt;/classPattern&gt;
97    *
98    * &lt;className&gt;<i>ClassName-1</i>&lt;/className&gt;
99    * &lt;className&gt;<i>ClassName-2</i>&lt;/className&gt;
100    * ...
101    * &lt;className&gt;<i>ClassName-N</i>&lt;/className&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.PerClassPolicy.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.PerClassPolicy.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.PerClassPolicy" &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> class.
158    * --&gt;</font>
159    * &lt;PolicyGroup&gt;
160    * &lt;logMask&gt;DEBUG&lt;/logMask&gt;
161    * &lt;channels&gt;ALL_CHANNEL&lt;/channels&gt;
162    *
163    * &lt;classPattern&gt;com.protomatter.xml.*&lt;/classPattern&gt;
164    * &lt;classPattern&gt;com.protomatter.jdbc.*&lt;/classPattern&gt;
165    *
166    * &lt;className&gt;com.protomatter.util.Mutex&lt;/className&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.JdbcConnectionPool</TT> class.
173    * --&gt;</font>
174    * &lt;PolicyGroup&gt;
175    * &lt;logMask&gt;INFO&lt;/logMask&gt;
176    * &lt;channels&gt;ALL_CHANNEL&lt;/channels&gt;
177    *
178    * &lt;classPattern&gt;com.protomatter.pas.*&lt;/classPattern&gt;
179    * &lt;classPattern&gt;com.protomatter.util.*&lt;/classPattern&gt;
180    *
181    * &lt;className&gt;com.protomatter.jdbc.pool.JdbcConnectionPool&lt;/className&gt;
182    * &lt;/PolicyGroup&gt;
183    *
184    * <font color="#888888">&lt;!--
185    * Getting fancy now. If the channel is OPS_CHANNEL or DEV_CHANNEL
186    * and the class name contains the word "Debug" then allow
187    * messages at or above the ERROR level.
188    * --&gt;</font>
189    * &lt;PolicyGroup&gt;
190    * &lt;logMask&gt;ERROR&lt;/logMask&gt;
191    * &lt;channels&gt;OPS_CHANNEL, DEV_CHANNEL&lt;/channels&gt;
192    *
193    * &lt;classPattern&gt;*Debug*&lt;/classPattern&gt;
194    * &lt;/PolicyGroup&gt;
195    *
196    * &lt;/Policy&gt;
197    * </B></PRE>
198    * </TD></TR></TABLE><P>
199    *
200    */

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