1 29 30 package com.caucho.log; 31 32 import com.caucho.loader.EnvironmentLocal; 33 34 import java.lang.ref.SoftReference ; 35 import java.util.Collections ; 36 import java.util.Enumeration ; 37 import java.util.HashMap ; 38 import java.util.logging.LogManager ; 39 import java.util.logging.Logger ; 40 41 44 public class LogManagerImpl extends LogManager { 45 private static final String LOG_LOCAL = "caucho.log.manager"; 46 47 private static final HashMap <String ,SoftReference <EnvironmentLogger>> _envLoggers 48 = new HashMap <String ,SoftReference <EnvironmentLogger>>(); 49 50 private static final EnvironmentLocal<HashMap <String ,Logger >> _localLoggers 52 = new EnvironmentLocal<HashMap <String ,Logger >>(); 53 54 55 public LogManagerImpl() 56 { 57 } 58 59 62 public synchronized boolean addLogger(Logger logger) 63 { 64 String name = logger.getName(); 65 66 EnvironmentLogger envLogger = null; 67 68 SoftReference <EnvironmentLogger> loggerRef = _envLoggers.get(name); 69 if (loggerRef != null) 70 envLogger = loggerRef.get(); 71 72 if (envLogger == null) { 73 envLogger = new EnvironmentLogger(name, logger.getResourceBundleName()); 74 75 _envLoggers.put(name, new SoftReference <EnvironmentLogger>(envLogger)); 76 77 EnvironmentLogger parent = buildParentTree(name); 78 79 if (parent != null) 80 envLogger.setParent(parent); 81 } 82 83 if (! logger.getClass().equals(Logger .class)) { 85 return envLogger.addLogger(logger); 86 } 87 88 return false; 89 } 90 91 94 private EnvironmentLogger buildParentTree(String childName) 95 { 96 if (childName.equals("")) 97 return null; 98 99 int p = childName.lastIndexOf('.'); 100 101 String parentName; 102 103 if (p > 0) 104 parentName = childName.substring(0, p); 105 else 106 parentName = ""; 107 108 EnvironmentLogger parent = null; 109 110 SoftReference <EnvironmentLogger> parentRef = _envLoggers.get(parentName); 111 if (parentRef != null) 112 parent = parentRef.get(); 113 114 if (parent != null) 115 return parent; 116 else { 117 parent = new EnvironmentLogger(parentName, null); 118 _envLoggers.put(parentName, new SoftReference <EnvironmentLogger>(parent)); 119 120 EnvironmentLogger grandparent = buildParentTree(parentName); 121 122 if (grandparent != null) 123 parent.setParent(grandparent); 124 125 return parent; 126 } 127 } 128 129 132 public synchronized Logger getLogger(String name) 133 { 134 SoftReference <EnvironmentLogger> envLoggerRef = _envLoggers.get(name); 135 136 EnvironmentLogger envLogger = null; 137 if (envLoggerRef != null) 138 envLogger = envLoggerRef.get(); 139 140 if (envLogger == null) 141 return null; 142 143 Logger customLogger = envLogger.getLogger(); 144 145 if (customLogger != null) 146 return customLogger; 147 else 148 return envLogger; 149 } 150 151 154 public Enumeration <String > getLoggerNames() 155 { 156 return Collections.enumeration(_envLoggers.keySet()); 157 } 158 159 162 public void reset() 163 { 164 } 165 } 166 | Popular Tags |