KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > log > LogFactory


1 /*_############################################################################
2   _##
3   _## SNMP4J - LogFactory.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.log;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.Collections JavaDoc;
25
26 /**
27  * The <code>LogFactory</code> singleton is used by SNMP4J to determine
28  * the logging framework used to process SNMP4J log messages. By default
29  * {@link NoLogger} instances are used.
30  *
31  * @author Frank Fock
32  * @version 1.8
33  * @since 1.2.1
34  */

35 public class LogFactory {
36
37   public static final String JavaDoc SNMP4J_LOG_FACTORY_SYSTEM_PROPERTY =
38       "snmp4j.LogFactory";
39
40   private static LogFactory snmp4jLogFactory = null;
41   private static boolean configChecked = false;
42
43   /**
44    * Gets the logger for the supplied class.
45    *
46    * @param c
47    * the class for which a logger needs to be created.
48    * @return
49    * the <code>LogAdapter</code> instance.
50    */

51   public static LogAdapter getLogger(Class JavaDoc c) {
52     checkConfig();
53     if (snmp4jLogFactory == null) {
54       return NoLogger.instance;
55     }
56     else {
57       return snmp4jLogFactory.createLogger(c.getName());
58     }
59   }
60
61   private static void checkConfig() {
62     if (!configChecked) {
63       configChecked = true;
64       getFactoryFromSystemProperty();
65     }
66   }
67
68   private synchronized static void getFactoryFromSystemProperty() {
69     try {
70       String JavaDoc factory =
71           System.getProperty(SNMP4J_LOG_FACTORY_SYSTEM_PROPERTY, null);
72       if (factory != null) {
73         try {
74           Class JavaDoc c = Class.forName(factory);
75           snmp4jLogFactory = (LogFactory) c.newInstance();
76         }
77         catch (ClassNotFoundException JavaDoc ex) {
78         }
79         catch (IllegalAccessException JavaDoc ex) {
80         }
81         catch (InstantiationException JavaDoc ex) {
82         }
83       }
84     }
85     catch (SecurityException JavaDoc sec) {
86     }
87   }
88
89   /**
90    * Returns the top level logger.
91    * @return
92    * a LogAdapter instance.
93    * @since 1.7
94    */

95   public LogAdapter getRootLogger() {
96     return NoLogger.instance;
97   }
98
99   /**
100    * Gets the logger for the supplied class name.
101    *
102    * @param className
103    * the class name for which a logger needs to be created.
104    * @return
105    * the <code>LogAdapter</code> instance.
106    * @since 1.7
107    */

108   public static LogAdapter getLogger(String JavaDoc className) {
109     checkConfig();
110     if (snmp4jLogFactory == null) {
111       return NoLogger.instance;
112     }
113     else {
114       return snmp4jLogFactory.createLogger(className);
115     }
116   }
117
118   /**
119    * Creates a Logger for the specified class. This method returns the
120    * {@link NoLogger} logger instance which disables logging.
121    * Overwrite this method the return a custom logger to enable logging for
122    * SNMP4J.
123    *
124    * @param c
125    * the class for which a logger needs to be created.
126    * @return
127    * the <code>LogAdapter</code> instance.
128    */

129   protected LogAdapter createLogger(Class JavaDoc c) {
130     return NoLogger.instance;
131   }
132
133   /**
134    * Creates a Logger for the specified class. This method returns the
135    * {@link NoLogger} logger instance which disables logging.
136    * Overwrite this method the return a custom logger to enable logging for
137    * SNMP4J.
138    *
139    * @param className
140    * the class name for which a logger needs to be created.
141    * @return
142    * the <code>LogAdapter</code> instance.
143    * @since 1.7
144    */

145   protected LogAdapter createLogger(String JavaDoc className) {
146     return NoLogger.instance;
147   }
148
149   /**
150    * Sets the log factory to be used by SNMP4J. Call this method before
151    * any other SNMP4J class is referenced or created to set and use a custom
152    * log factory.
153    *
154    * @param factory
155    * a <code>LogFactory</code> instance.
156    */

157   public static void setLogFactory(LogFactory factory) {
158     configChecked = true;
159     snmp4jLogFactory = factory;
160   }
161
162   /**
163    * Gets the log factory to be used by SNMP4J.
164    * @return
165    * a <code>LogFactory</code> instance.
166    * @since 1.7
167    */

168   public static LogFactory getLogFactory() {
169     return snmp4jLogFactory;
170   }
171
172   /**
173    * Returns all available LogAdapters in depth first order.
174    * @return
175    * a read-only Iterator.
176    * @since 1.7
177    */

178   public Iterator JavaDoc loggers() {
179     return Collections.singletonList(NoLogger.instance).iterator();
180   }
181
182 }
183
Popular Tags