1 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 ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import java.io.InputStream ; 26 import java.io.PrintWriter ; 27 import java.io.Writer ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 36 public class LoaderRepositorySelector implements org.apache.log4j.spi.RepositorySelector 37 { 38 39 private static boolean initialized = false; 40 41 private static Object guard = LogManager.getRootLogger(); 44 private static Map repositories = new HashMap (); 45 private static LoggerRepository defaultRepository; 46 private static Logger log = Logger.getLogger(LoaderRepositorySelector.class); 47 48 51 public static synchronized void register(ClassLoader classloader, Loader loader, String pathToLog4jFile) 52 { 53 if (classloader == null) 54 { 55 throw new IllegalArgumentException ("No classloader provided"); 56 } 57 if (loader == null) 58 { 59 throw new IllegalArgumentException ("No loader provided"); 60 } 61 if (pathToLog4jFile == null) 62 { 63 throw new IllegalArgumentException ("No pathToLog4jFile provided"); 64 } 65 66 if (!initialized) 68 { 69 RepositorySelector theSelector = new LoaderRepositorySelector(); 71 LogManager.setRepositorySelector(theSelector, guard); 72 initialized = true; 73 } 74 75 InputStream log4JConfig = null; 76 try 77 { 78 log4JConfig = loader.load(pathToLog4jFile); 79 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 80 DocumentBuilder builder = factory.newDocumentBuilder(); 81 Document 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 e) 89 { 90 log.error(e); 91 throw new IllegalArgumentException ("Cannot load log4j configuration"); 92 } 93 finally 94 { 95 Tools.safeClose(log4JConfig); 96 } 97 } 98 99 public static synchronized void unregister(ClassLoader classloader) 100 { 101 if (classloader == null) 102 { 103 throw new IllegalArgumentException ("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 writer = new PrintWriter (System.out); 114 Tools.dumpClassLoaderHeirarchyInfo(writer, classloader); 115 } 116 } 117 118 private LoaderRepositorySelector() 119 { 120 } 121 122 public LoggerRepository getLoggerRepository() 123 { 124 ClassLoader 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 |