1 17 18 package org.apache.geronimo.kernel.log; 19 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogConfigurationException; 28 import org.apache.commons.logging.LogFactory; 29 30 33 public class GeronimoLogFactory extends LogFactory { 34 private final static Object factoryLock = new Object (); 35 private static final HashMap instancesByClassLoader = new HashMap (); 37 38 private static LogFactory logFactory = new BootstrapLogFactory(); 39 40 public GeronimoLogFactory() { 41 } 42 43 public LogFactory getLogFactory() { 44 synchronized (factoryLock) { 45 return logFactory; 46 } 47 } 48 49 public void setLogFactory(LogFactory logFactory) { 50 GeronimoLogFactory.logFactory = logFactory; 52 53 Set logs = getInstances(); 55 for (Iterator iterator = logs.iterator(); iterator.hasNext();) { 56 GeronimoLog log = (GeronimoLog) iterator.next(); 57 log.setLog(logFactory.getInstance(log.getName())); 58 } 59 } 60 61 public Set getInstances() { 62 synchronized (factoryLock) { 63 Set logs = new HashSet (); 64 for (Iterator iterator = instancesByClassLoader.values().iterator(); iterator.hasNext();) { 65 Map instanceMap = ((Map ) iterator.next()); 66 logs.addAll(instanceMap.values()); 67 68 } 69 return logs; 70 } 71 } 72 73 public Log getInstance(Class clazz) throws LogConfigurationException { 74 synchronized (factoryLock) { 75 return getInstance(clazz.getName()); 76 } 77 } 78 79 public Log getInstance(String name) throws LogConfigurationException { 80 synchronized (factoryLock) { 81 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 83 Map instances = (Map ) instancesByClassLoader.get(contextClassLoader); 84 if (instances == null) { 85 instances = new HashMap (); 86 instancesByClassLoader.put(contextClassLoader, instances); 87 } 88 89 Log log = (Log) instances.get(name); 91 if (log == null) { 92 log = new GeronimoLog(name, logFactory.getInstance(name)); 93 instances.put(name, log); 94 } 95 return log; 96 } 97 } 98 99 public void release() { 100 synchronized (factoryLock) { 101 instancesByClassLoader.clear(); 111 } 112 } 113 114 public Object getAttribute(String name) { 115 synchronized (factoryLock) { 116 return logFactory.getAttribute(name); 117 } 118 } 119 120 public String [] getAttributeNames() { 121 synchronized (factoryLock) { 122 return logFactory.getAttributeNames(); 123 } 124 } 125 126 public void removeAttribute(String name) { 127 synchronized (factoryLock) { 128 logFactory.removeAttribute(name); 129 } 130 } 131 132 public void setAttribute(String name, Object value) { 133 synchronized (factoryLock) { 134 logFactory.setAttribute(name, value); 135 } 136 } 137 } 138 139 | Popular Tags |