KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > logBridge > LogBridgeManager


1 package org.grlea.logBridge;
2
3 // $Id: LogBridgeManager.java,v 1.1 2005/03/02 10:46:05 grlea Exp $
4
// Copyright (c) 2005 Graham Lea. All rights reserved.
5

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

18 import org.grlea.logBridge.impl.NullLogBridgeFactory;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 /**
26  * <p>Manages a global {@link LogBridgeFactory} and the creation of {@link LogBridge}s from that
27  * factory.</p>
28  *
29  * @author grlea
30  * @version $Revision: 1.1 $
31  */

32 public class
33 LogBridgeManager
34 {
35    /**
36     * The key of the property (in a properties file or as a system property) that can be used to
37     * specify the class of Log Bridge Factory to use. The preferred method is to set the factory
38     * programatically.
39     */

40    public static final String JavaDoc FACTORY_KEY = LogBridgeFactory.class.getName();
41
42    /**
43     * The name of the properties file that can be used (on the classpath) to specify which class of
44     * Log Bridge Factory to use. The preferred method is to set the factory
45     * programatically.
46     */

47    public static final String JavaDoc PROPERTIES_FILE = "logBridge.properties";
48
49    /**
50     * The global Log Bridge Factory.
51     */

52    private static LogBridgeFactory globalFactory = null;
53
54    /**
55     * An object to synchronize on when accessing or changing the global factory.
56     */

57    private static final Object JavaDoc factoryLock = new Object JavaDoc();
58
59    /**
60     * Private to prevent instantiation.
61     */

62    private
63    LogBridgeManager()
64    {}
65
66    /**
67     * Returns the global Log Bridge Factory currently in use by <code>LogBridgeManager</code>,
68     * initialising one if necessary. If the factory needs to be initialised and the calling thread's
69     * {@link Thread#getContextClassLoader context class loader} is set, that class loader will be
70     * used to load any dynamically specified class, otherwise this class' class loader will be used.
71     */

72    public static LogBridgeFactory
73    getFactory()
74    {
75       synchronized (factoryLock)
76       {
77          if (globalFactory == null)
78             createFactory();
79       }
80
81       return globalFactory;
82    }
83
84    /**
85     * Creates a global log bridge factory. This method creates the default global log bridge,
86     * determined by the following method:
87     * <ol>
88     * <li>If the system property org.grlea.logBridge.LogBridgeFactory exists and its value is the
89     * name of a class that can be loaded and is an implementation of {@link LogBridgeFactory}, an
90     * instance of that class is created and used;</li>
91     * <li>If a properties file called 'logBridge.properties' can be loaded from this class' class
92     * loader and contains a property org.grlea.logBridge.LogBridgeFactory and its value is the name
93     * of a class that can be loaded and is an implementation of {@link LogBridgeFactory}, an
94     * instance of that class is created and used;</li>
95     * <li>Otherwise, a {@link NullLogBridgeFactory} is used.</li>
96     * </ol>
97     */

98    private static void
99    createFactory()
100    {
101       // Check system property
102
{
103          String JavaDoc className = System.getProperty(FACTORY_KEY);
104          if (className != null)
105          {
106             LogBridgeFactory factory = createFactory(className);
107             if (factory != null)
108             {
109                globalFactory = factory;
110                return;
111             }
112          }
113       }
114
115       // Check for properties file
116
URL JavaDoc propertiesFile = LogBridgeManager.class.getClassLoader().getResource(PROPERTIES_FILE);
117       if (propertiesFile != null)
118       {
119          try
120          {
121             InputStream JavaDoc in = propertiesFile.openStream();
122             Properties JavaDoc properties = new Properties JavaDoc();
123             properties.load(in);
124             String JavaDoc className = properties.getProperty(FACTORY_KEY);
125             if (className != null)
126             {
127                LogBridgeFactory factory = createFactory(className);
128                if (factory != null)
129                {
130                   globalFactory = factory;
131                   return;
132                }
133             }
134          }
135          catch (IOException JavaDoc e)
136          {
137             System.err.println("ERROR (LogBridgeManager): Error reading properties file '" +
138                                propertiesFile + "': " + e);
139          }
140       }
141
142       // Use a null logger
143
globalFactory = new NullLogBridgeFactory();
144    }
145
146    /**
147     * Attempts to create a {@link LogBridgeFactory} instance from the given class name.
148     *
149     * @param className the fully-qualified name of the class from which an instane of
150     * LogBridgeFactory is to be created.
151     *
152     * @return a newly created instance of LogBridgeFactory, or <code>null</code> if one could not
153     * be created using the given class name.
154     */

155    private static LogBridgeFactory
156    createFactory(String JavaDoc className)
157    {
158       try
159       {
160          ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
161          if (classLoader == null)
162             classLoader = LogBridgeManager.class.getClassLoader();
163
164          Class JavaDoc factoryClass = classLoader.loadClass(className);
165          Object JavaDoc newObject = factoryClass.newInstance();
166          if (!(newObject instanceof LogBridgeFactory))
167          {
168             System.err.println("ERROR (LogBridgeManager): Class '" + className +
169                                "' is not an implementation of " + LogBridgeFactory.class.getName());
170             return null;
171          }
172          return (LogBridgeFactory) newObject;
173       }
174       catch (ClassNotFoundException JavaDoc e)
175       {
176          System.err.println(
177             "ERROR (LogBridgeManager): Class '" + className + "' not found: " + e);
178       }
179       catch (InstantiationException JavaDoc e)
180       {
181          System.err.println(
182             "ERROR (LogBridgeManager): Error creating instance of " + className + ": " + e);
183       }
184       catch (IllegalAccessException JavaDoc e)
185       {
186          System.err.println(
187             "ERROR (LogBridgeManager): Error creating instance of " + className + ": " + e);
188       }
189       return null;
190    }
191
192    /**
193     * Sets the global Log Bridge Factory to be used by <code>LogBridgeManager</code>.
194     *
195     * @param newFactory the new factory to be used.
196     *
197     * @throws IllegalArgumentException if <code>newFactory</code> is <code>null</code>.
198     */

199    public static void
200    setFactory(LogBridgeFactory newFactory)
201    {
202       if (newFactory == null)
203          throw new IllegalArgumentException JavaDoc("factory cannot be set to null.");
204       globalFactory = newFactory;
205    }
206
207    /**
208     * Creates and returns a Log Bridge using <code>LogBridgeManager</code>'s global factory.
209     *
210     * @param sourceClass the class that the Log Bridge object is for, the class which will be the
211     * source of the messages sent to the bridge.
212     *
213     * @return the log bridge.
214     */

215    public static LogBridge
216    createLogBridge(Class JavaDoc sourceClass)
217    {
218       return getFactory().getLogBridge(sourceClass);
219    }
220 }
221
Popular Tags