KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > logging > LoaderRepositorySelector


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.logging;
10
11 import org.apache.log4j.Hierarchy;
12 import org.apache.log4j.Level;
13 import org.apache.log4j.LogManager;
14 import org.apache.log4j.Logger;
15 import org.apache.log4j.spi.LoggerRepository;
16 import org.apache.log4j.spi.RepositorySelector;
17 import org.apache.log4j.spi.RootCategory;
18 import org.apache.log4j.xml.DOMConfigurator;
19 import org.jboss.portal.common.util.Loader;
20 import org.jboss.portal.common.util.Tools;
21 import org.w3c.dom.Document JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * @author Stan Silvert
33  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
34  * @version $Revision: 1.3 $
35  */

36 public class LoaderRepositorySelector implements org.apache.log4j.spi.RepositorySelector
37 {
38
39    private static boolean initialized = false;
40
41    // This object is used for the guard because it doesn't get
42
// recycled when the application is redeployed.
43
private static Object JavaDoc guard = LogManager.getRootLogger();
44    private static Map JavaDoc repositories = new HashMap JavaDoc();
45    private static LoggerRepository defaultRepository;
46    private static Logger log = Logger.getLogger(LoaderRepositorySelector.class);
47
48    /**
49     * Register with this repository selector.
50     */

51    public static synchronized void register(ClassLoader JavaDoc classloader, Loader loader, String JavaDoc pathToLog4jFile)
52    {
53       if (classloader == null)
54       {
55          throw new IllegalArgumentException JavaDoc("No classloader provided");
56       }
57       if (loader == null)
58       {
59          throw new IllegalArgumentException JavaDoc("No loader provided");
60       }
61       if (pathToLog4jFile == null)
62       {
63          throw new IllegalArgumentException JavaDoc("No pathToLog4jFile provided");
64       }
65
66       // Set the global RepositorySelector
67
if (!initialized)
68       {
69          // defaultRepository = LogManager.getLoggerRepository();
70
RepositorySelector theSelector = new LoaderRepositorySelector();
71          LogManager.setRepositorySelector(theSelector, guard);
72          initialized = true;
73       }
74
75       InputStream JavaDoc log4JConfig = null;
76       try
77       {
78          log4JConfig = loader.load(pathToLog4jFile);
79          DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
80          DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
81          Document JavaDoc doc = builder.parse(log4JConfig);
82          DOMConfigurator conf = new DOMConfigurator();
83
84          Hierarchy hierarchy = new Hierarchy(new RootCategory(Level.DEBUG));
85          conf.doConfigure(doc.getDocumentElement(), hierarchy);
86          repositories.put(classloader, hierarchy);
87       }
88       catch (Exception JavaDoc e)
89       {
90          log.error(e);
91          throw new IllegalArgumentException JavaDoc("Cannot load log4j configuration");
92       }
93       finally
94       {
95          Tools.safeClose(log4JConfig);
96       }
97    }
98
99    public static synchronized void unregister(ClassLoader JavaDoc classloader)
100    {
101       if (classloader == null)
102       {
103          throw new IllegalArgumentException JavaDoc("No classloader provided");
104       }
105       Hierarchy hierarchy = (Hierarchy)repositories.remove(classloader);
106       if (hierarchy != null)
107       {
108          hierarchy.shutdown();
109       }
110       else
111       {
112          System.out.print("No hierarchy found for classloader : ");
113          Writer JavaDoc writer = new PrintWriter JavaDoc(System.out);
114          Tools.dumpClassLoaderHeirarchyInfo(writer, classloader);
115       }
116    }
117
118    private LoaderRepositorySelector()
119    {
120    }
121
122    public LoggerRepository getLoggerRepository()
123    {
124       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
125       LoggerRepository repository = (LoggerRepository)repositories.get(loader);
126       if (repository == null)
127       {
128          return defaultRepository;
129       }
130       else
131       {
132          return repository;
133       }
134    }
135 }
136
Popular Tags