KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.PrintWriter JavaDoc;
54 import java.util.*;
55 import java.text.*;
56 import java.io.*;
57 import java.net.*;
58
59 import com.protomatter.util.*;
60
61 /**
62  * The default LogPolicy that knows about log levels and channels.
63  *
64  * @see LogPolicy
65  * @see com.protomatter.syslog.xml.SimpleLogPolicy_Helper XML configuration class
66  */

67 public class SimpleLogPolicy
68 implements LogPolicy
69 {
70   private String JavaDoc name = null;
71   private int logMask = Syslog.INHERIT_MASK;
72   private HashMap channels = null;
73   private boolean allChannels = false;
74   private boolean initialized = false;
75   private Object JavaDoc VALUE = new Object JavaDoc();
76
77   /**
78    * All channels are listened to by default, and the
79    * default log mask is inherited from Syslog itself.
80    */

81   public SimpleLogPolicy()
82   {
83     super();
84     Set channels = new HashSet();
85     channels.add(Syslog.ALL_CHANNEL);
86     setChannels(channels);
87   }
88
89   /**
90    * Get the name of this policy.
91    */

92   public String JavaDoc getName()
93   {
94     return this.name;
95   }
96
97   /**
98    * Set the name of this policy.
99    */

100   public void setName(String JavaDoc name)
101   {
102     this.name = name;
103   }
104
105   /**
106    * Set the list of channels to use. Channel names
107    * are values in the vector.
108    */

109   public synchronized void setChannels(List channels)
110   {
111     Set set = new HashSet();
112     Iterator i = channels.iterator();
113     while (i.hasNext())
114     {
115       set.add(i.next());
116     }
117     setChannels(set);
118   }
119
120   /**
121    * Set the list of channels to use. Channel names are
122    * looked for as keys in the hashtable.
123    */

124   public synchronized void setChannels(Set channelSet)
125   {
126     this.channels = new HashMap();
127     Iterator i = channelSet.iterator();
128     while (i.hasNext())
129     {
130       channels.put(i.next(), VALUE);
131     }
132     this.allChannels = channelSet.contains(Syslog.ALL_CHANNEL);
133   }
134
135   /**
136    * Add the given channel to the list of channels we
137    * are listening to.
138    */

139   public synchronized void addChannel(String JavaDoc channel)
140   {
141     channels.put(channel, VALUE);
142     this.allChannels = channels.containsKey(Syslog.ALL_CHANNEL);
143   }
144
145   /**
146    * Remove the given channel from the list of channels
147    * we are listening to.
148    */

149   public synchronized void removeChannel(String JavaDoc channel)
150   {
151     channels.remove(channel);
152     this.allChannels = channels.containsKey(Syslog.ALL_CHANNEL);
153   }
154
155   /**
156    * Remove all channels from the list of channels
157    * we are listening to.
158    */

159   public synchronized void removeAllChannels()
160   {
161     channels = new HashMap();
162     this.allChannels = false;
163   }
164
165   /**
166    * Get the list of channels this policy listens to.
167    */

168   public Iterator getChannels()
169   {
170     return channels.keySet().iterator();
171   }
172
173   /**
174    * Determine if a log message should be logged given the information.
175    * Only checks to see if the log level is in the mask.
176    */

177   public boolean shouldLog(SyslogMessage message)
178   {
179     boolean inMask = false;
180     if (this.logMask == Syslog.INHERIT_MASK)
181       inMask = ((message.level & Syslog.currentLogMask) > 0);
182     else
183       inMask = ((message.level & this.logMask) > 0);
184
185     // not in our mask... not gonna log it.
186
if (!inMask)
187       return false;
188
189     // we check channels.
190
if (!allChannels)
191     {
192       // it's on the ALL_CHANNEL -- we pay attention to these
193
// no matter what.
194
if (Syslog.ALL_CHANNEL.equals(message.channel))
195         return true;
196
197       // is the channel in our list?
198
return channels.containsKey(message.channel);
199     }
200
201     // we're listening to the ALL_CHANNEL channel,
202
// meaning we listen to every channel.
203
return true;
204   }
205
206   /**
207    * Check if the given level is covered by the given mask.
208    */

209   protected final boolean inMask(int level, int mask)
210   {
211     return ((level & mask) > 0);
212   }
213
214   /**
215    * Set the log mask.
216    *
217    * @see Syslog#parseLogMask
218    */

219   public final void setLogMask(String JavaDoc mask)
220   {
221     setLogMask(Syslog.parseLogMask(mask));
222   }
223
224   /**
225     * Set the mask for logging of messages.
226     * For example, to log all messages of type ERROR or greater,
227     * you would call:
228     *
229     * setLogMask(Syslog.atOrAbove(Syslog.ERROR));
230     */

231   public final void setLogMask(int mask)
232   {
233     this.logMask = mask;
234   }
235
236   /**
237    * Get the mask for logging of messages.
238    */

239   public final int getLogMask()
240   {
241     return this.logMask;
242   }
243 }
244
Popular Tags