KickJava   Java API By Example, From Geeks To Geeks.

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


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-channel 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 channel names that their
63  * mask and channel list applies to. If a message
64  * coming from a channel 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 channels <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 channels <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 "channel 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.JDK14PerChannelPolicy_Helper XML configuration class
85  */

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

94   public JDK14PerChannelPolicy()
95   {
96     super();
97     removeAllChannels();
98     setLogMask(0);
99   }
100
101   /**
102    * Get the list of policy groups.
103    */

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

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

129   public boolean shouldLog(SyslogMessage message)
130   {
131     // if any of the policy groups say yes, let it through
132
if (message.channel != null)
133     {
134       int size = groupList.size();
135       PolicyGroup group = null;
136       for (int i=0; i<size; i++)
137       {
138         group = (PolicyGroup)groupList.get(i);
139         if (group.shouldLog(message))
140           return true;
141       }
142     }
143
144     return false;
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 channel issuing the log message is
175    * in some set.
176    *
177    * @see JDK14PerChannelPolicy
178    */

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

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

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

207     public void clearChannelSet()
208     {
209       this.channelNameSet = new HashSet();
210     }
211
212     /**
213      * Add a channelname to the set of channel names
214      * we're listening to.
215      */

216     public void addChannelName(String JavaDoc channelname)
217     {
218       this.channelNameSet.add(channelname);
219     }
220
221     /**
222      * Remove a channel name from the set of
223      * channel names we're listening to.
224      */

225     public void removeChannelName(String JavaDoc channelname)
226     {
227       this.channelNameSet.remove(channelname);
228     }
229
230     /**
231      * Get the set of channel patterns we're listing to.
232      */

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

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

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

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

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

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