KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > JDK14PerClassPolicy


1 package com.protomatter.syslog;
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.util.*;
54 import java.util.regex.*;
55 import java.text.MessageFormat JavaDoc;
56
57 /**
58  * A policy that can make decision on a per-class basis.
59  * It maintains a default log mask and channel list
60  * itself, but also has a list of "policy groups" that
61  * each have a log mask and channel list of their own
62  * in addition to a list of class names that their
63  * mask and channel list applies to. If a message
64  * coming from a class in one of those lists matches
65  * the mask and channel list, the message is logged. If
66  * not, but the message severity and channel match the
67  * default mask and channel list, it is also logged.
68  * Otherwise, the message is ignored.<P>
69  *
70  * This policy can be used to effectively say that
71  * messages from classes <TT>A</TT> and <TT>B</TT> should be logged
72  * if their severity is <TT>WARNING</TT> or greater, and that
73  * messages from classes <TT>C</TT> and <TT>D</TT> should be logged
74  * if their severity is <TT>INFO</TT> or greater and on a certain
75  * set of channels and that if all else fails, messages at
76  * or above the <TT>INFO</TT> level will be logged. It's
77  * very configurable.<P>
78  *
79  * Each "class name" is actually a regular expression,
80  * so you can match things like "<tt>com.protomatter.syslog.*</tt>"
81  * and other stuff. This functionality uses the
82  * <TT>java.util.regex</TT> package from JDK 1.4.
83  *
84  * @see com.protomatter.syslog.xml.JDK14PerClassPolicy_Helper XML configuration class
85  */

86 public class JDK14PerClassPolicy
87 extends SimpleLogPolicy
88 {
89   private List groupList = new ArrayList();
90
91   /**
92    * Default constructor.
93    */

94   public JDK14PerClassPolicy()
95   {
96     super();
97   }
98
99   /**
100    * Get the list of policy groups.
101    */

102   public List getGroupList()
103   {
104     return this.groupList;
105   }
106   /**
107    * Set the list of policy groups.
108    */

109   public void setGroupList(List list)
110   {
111     this.groupList = list;
112   }
113
114   /**
115    * Decide if the message should be logged. Each
116    * policy group is asked if it should be logged,
117    * and if none of them say yes, then we defer
118    * to our superclass. Each policy group maintains
119    * a channel list, log mask and a set of class names
120    * -- to decide if it should log the message,
121    * the policy group first checks the message's
122    * severity and channel against its log mask
123    * and channel list. If it passes this test,
124    * the policy group checks to see if the
125    * class name of the caller is in it's list.
126    */

127   public boolean shouldLog(SyslogMessage message)
128   {
129     // if any of the policy groups say yes, let it through
130
if (message.loggerClassname != null)
131     {
132       int size = groupList.size();
133       PolicyGroup group = null;
134       for (int i=0; i<size; i++)
135       {
136         group = (PolicyGroup)groupList.get(i);
137         if (group.shouldLog(message))
138           return true;
139       }
140     }
141
142     // otherwise, defer to the superclass, which
143
// knows about channels and log masks.
144
return super.shouldLog(message);
145   }
146
147   /**
148    * Get the list of policy groups.
149    */

150   public Iterator getPolicyGroups()
151   {
152     return this.groupList.iterator();
153   }
154
155   /**
156    * Add a policy group to our list.
157    */

158   public void addPolicyGroup(PolicyGroup group)
159   {
160     this.groupList.add(group);
161   }
162
163   /**
164    * Remove a policy group from our list.
165    */

166   public void removePolicyGroup(PolicyGroup group)
167   {
168     this.groupList.remove(group);
169   }
170
171   /**
172    * A policy within a policy -- this is exactly like
173    * the SimpleLogPolicy except that it also checks to
174    * see if the class issuing the log message is
175    * in some set.
176    *
177    * @see JDK14PerClassPolicy
178    */

179   public static class PolicyGroup
180   extends SimpleLogPolicy
181   {
182     private Set patternSet = new HashSet();
183     private Set classNameSet = new HashSet();
184     private Pattern pattern = null;
185
186     /**
187      * Default constructor.
188      */

189     public PolicyGroup()
190     {
191       super();
192     }
193
194     /**
195      * Get the set of class names (exact matches) we're listing to.
196      */

197     public Iterator getClassSet()
198     {
199       return this.classNameSet.iterator();
200     }
201
202     /**
203      * Clear out the set of class names
204      * we're listing to.
205      */

206     public void clearClassSet()
207     {
208       this.classNameSet = new HashSet();
209     }
210
211     /**
212      * Add a classname to the set of class names
213      * we're listening to.
214      */

215     public void addClass(String JavaDoc classname)
216     {
217       this.classNameSet.add(classname);
218     }
219
220     /**
221      * Remove a class name from the set of
222      * class names we're listening to.
223      */

224     public void removeClass(String JavaDoc classname)
225     {
226       this.classNameSet.remove(classname);
227     }
228
229     /**
230      * Get the set of class patterns we're listing to.
231      */

232     public Iterator getPatternSet()
233     {
234       return this.patternSet.iterator();
235     }
236
237     /**
238      * Clear out the set of class name patterns
239      * we're listing to.
240      */

241     public void clearPatternSet()
242     {
243       this.patternSet = new HashSet();
244       this.pattern = null;
245     }
246
247     /**
248      * Add a classname to the set of class name
249      * patterns we're listening to.
250      */

251     public void addClassPattern(String JavaDoc classname)
252     {
253       this.patternSet.add(classname);
254       generatePattern();
255     }
256
257     /**
258      * Remove a class name pattern from the set of
259      * class name patterns we're listening to.
260      */

261     public void removeClassPattern(String JavaDoc classname)
262     {
263       this.patternSet.remove(classname);
264       generatePattern();
265     }
266
267     /**
268      * Recompile the pattern each time something
269      * about the set of patterns changes.
270      */

271     private void generatePattern()
272     throws IllegalArgumentException JavaDoc
273     {
274       StringBuffer JavaDoc thePattern = new StringBuffer JavaDoc();
275       try
276       {
277         Iterator patterns = getPatternSet();
278         while (patterns.hasNext())
279         {
280           thePattern.append(patterns.next());
281           if (patterns.hasNext())
282             thePattern.append("|");
283         }
284         pattern = Pattern.compile(thePattern.toString());
285       }
286       catch (PatternSyntaxException x)
287       {
288         throw new IllegalArgumentException JavaDoc(
289           MessageFormat.format(Syslog.getResources().getString(MessageConstants.INVALID_PATTERN_MESSAGE),
290           new Object JavaDoc[] { thePattern }));
291       }
292     }
293
294     /**
295      * Determine if the given message should be
296      * logged. The message severity and channel
297      * are first checked by our superclass, then
298      * we see if the caller class name is in our
299      * set.
300      */

301     public boolean shouldLog(SyslogMessage m)
302     {
303       // passes the channel/level test.
304
if (super.shouldLog(m))
305       {
306         // exact matches...
307
if (classNameSet.contains(m.loggerClassname))
308         {
309           return true;
310         }
311         else if (pattern != null) // pattern matches
312
{
313           return pattern.matcher(m.loggerClassname).matches();
314         }
315       }
316
317       // if it's not in our list, we don't care about it.
318
return false;
319     }
320   }
321 }
322
Popular Tags