KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > commons > SyslogChannelLogFactory


1 package com.protomatter.syslog.commons;
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.File JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.text.MessageFormat JavaDoc;
57 import org.apache.commons.logging.*;
58 import com.protomatter.syslog.*;
59 import com.protomatter.syslog.xml.*;
60 import com.protomatter.util.Debug;
61
62 /**
63  * A subclass of the <tt>org.apache.commons.logging.LogFactory</tt> class.
64  */

65 public class SyslogChannelLogFactory
66 extends LogFactory
67 {
68     private static Object JavaDoc lock = new Object JavaDoc();
69     private static boolean configured = false;
70
71     private Map JavaDoc attributes = new HashMap JavaDoc();
72
73     private static String JavaDoc CONFIG_FILE_ATTRIBUTE = "Syslog.config.xml";
74
75     // We need local copies of these since we can't reference
76
// the Syslog.WARNING_PROPERTY constant without loading
77
// the Syslog class, which would show the warning ;-)
78
private static String JavaDoc CLASSLOADER_WARNING_PROPERTY = "Syslog.classloader.warning";
79     private static String JavaDoc WARNING_OFF = "off";
80
81     // By default, we show the classloader warning
82
private static boolean WARNING_DEFAULT = true;
83
84     /**
85      * Get a <tt>Log</tt> instance with the name of the
86      * given class's package (or the class name if
87      * it has no package).
88      *
89      * @see com.protomatter.syslog.commons.SyslogChannelLog
90      */

91     public Log getInstance(Class JavaDoc clazz)
92     throws LogConfigurationException
93     {
94         String JavaDoc name = clazz.getName();
95         int index = name.lastIndexOf(".");
96         if (index > 0)
97             return getInstance(name.substring(0, index));
98         return getInstance(name);
99     }
100
101     /**
102      * Get a <tt>Log</tt> instance with the given name.
103      *
104      * @see com.protomatter.syslog.commons.SyslogChannelLog
105      */

106     public Log getInstance(String JavaDoc name)
107     throws LogConfigurationException
108     {
109         if (!configured)
110         {
111             synchronized (lock)
112             {
113                 if (!configured)
114                 {
115                     try
116                     {
117                         String JavaDoc configFileName = (String JavaDoc)attributes.get(CONFIG_FILE_ATTRIBUTE);
118                         String JavaDoc xmlParserClass = (String JavaDoc)attributes.get(SyslogXML.XML_PARSER_PROPERTY);
119                         boolean showClassloaderWarning = WARNING_DEFAULT;
120                         if (attributes.get(CLASSLOADER_WARNING_PROPERTY) != null)
121                         {
122                             showClassloaderWarning = !(WARNING_OFF.equalsIgnoreCase((String JavaDoc)attributes.get(CLASSLOADER_WARNING_PROPERTY)));
123                         }
124
125                         if (configFileName == null)
126                         {
127                             throw new LogConfigurationException(
128                                 MessageFormat.format(Syslog.getResourceString(MessageConstants.MUST_SPECIFY_INIT_PARAM_MESSAGE),
129                                 new Object JavaDoc[] { CONFIG_FILE_ATTRIBUTE } ));
130                         }
131
132                         SyslogXML.configure(new File JavaDoc(configFileName), xmlParserClass, showClassloaderWarning);
133                     }
134                     catch (SyslogInitException x)
135                     {
136                         throw new LogConfigurationException(Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE), x);
137                     }
138                 }
139             }
140         }
141
142         return new SyslogChannelLog(name);
143     }
144
145     public void release()
146     {
147     }
148
149     public Object JavaDoc getAttribute(String JavaDoc name)
150     {
151         return attributes.get(name);
152     }
153
154     public String JavaDoc[] getAttributeNames()
155     {
156         return (String JavaDoc[])attributes.keySet().toArray(new String JavaDoc[0]);
157     }
158
159     public void removeAttribute(String JavaDoc name)
160     {
161         attributes.remove(name);
162     }
163
164     public void setAttribute(String JavaDoc name, Object JavaDoc value)
165     {
166         if (value == null)
167             removeAttribute(name);
168         else
169             attributes.put(name, value);
170     }
171 }
172
Popular Tags