1 18 19 package org.objectweb.util.monolog.wrapper.config; 20 21 import org.objectweb.util.monolog.api.Handler; 22 import org.objectweb.util.monolog.api.Level; 23 import org.objectweb.util.monolog.api.Logger; 24 import org.objectweb.util.monolog.api.MonologFactory; 25 import org.objectweb.util.monolog.api.MonologFactoryListener; 26 import org.objectweb.util.monolog.api.TopicalLogger; 27 import org.objectweb.util.monolog.wrapper.common.LevelImpl; 28 29 import java.io.Serializable ; 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.Properties ; 33 34 41 public class BasicFactory 42 implements MonologFactory, Serializable { 43 44 49 protected HashMap nameToLevel = null; 50 51 58 protected HashMap intToNames = null; 59 60 65 protected HashMap handlers = null; 66 67 72 protected HashMap loggers = null; 73 74 77 protected String resourceBundleName = null; 78 79 83 public BasicFactory() { 84 intToNames = new HashMap (); 85 nameToLevel = new HashMap (); 86 handlers = new HashMap (); 87 loggers = new HashMap (); 88 defineDefaultLevels(); 89 defineRootLogger(); 90 } 91 92 102 protected void defineDefaultLevels() { 103 defineLevel("INHERIT", -1); 104 defineLevel("DEBUG", 10000); 105 defineLevel("INFO", 20000); 106 defineLevel("WARN", 30000); 107 defineLevel("ERROR", 40000); 108 defineLevel("FATAL", 50000); 109 } 110 111 114 protected void defineRootLogger() { 115 getLogger("").setLevel(getLevel("WARN")); 116 } 117 118 public void addMonologFactoryListener(MonologFactoryListener mfl) { 119 } 120 121 public void removeMonologFactoryListener(MonologFactoryListener mfl) { 122 } 123 124 127 public Level defineLevel(String name, int value) { 128 return defineLevel(new LevelImpl(name, value)); 129 } 130 131 public Level defineLevel(String name, String value) { 132 return defineLevel(new LevelImpl(name, value, this)); 133 } 134 135 public Level getLevel(String name) { 136 return (Level) nameToLevel.get(name); 137 } 138 139 public Level getLevel(int value) { 140 Object temp = intToNames.get(new Integer (value)); 141 if (temp == null) { 142 return null; 143 } 144 else if (temp instanceof String ) { 145 return getLevel((String ) temp); 146 } 147 else if (temp instanceof ArrayList ) { 148 return getLevel((String ) ((ArrayList ) temp).get(0)); 149 } 150 return null; 151 } 152 153 public Level[] getLevels() { 154 return (Level[]) nameToLevel.values().toArray(new Level[0]); 155 } 156 157 public void removeLevel(String name) { 158 Level removed = (Level) nameToLevel.remove(name); 159 if (removed != null) { 160 Integer i = new Integer (removed.getIntValue()); 161 Object temp = intToNames.get(i); 162 if (temp instanceof String ) { 163 intToNames.remove(i); 164 } 165 else if (temp instanceof ArrayList ) { 166 ((ArrayList ) temp).remove(name); 167 } 168 } 169 } 170 171 174 184 private Level defineLevel(Level l) { 185 String name = l.getName(); 186 int value = l.getIntValue(); 187 Level res = (Level) nameToLevel.get(name); 188 if (res != null) { 189 return (res.getIntValue() == value ? res : null); 191 } 192 else { 193 res = l; 194 nameToLevel.put(name, res); 195 Integer i = new Integer (value); 196 Object temp = intToNames.get(i); 197 if (temp != null) { 198 if (temp instanceof String ) { 199 if (!((Level) temp).getName().equalsIgnoreCase(name)) { 200 ArrayList al = new ArrayList (5); 203 al.add(temp); 204 al.add(name); 205 intToNames.put(i, al); 206 } 207 } 208 else if (temp instanceof ArrayList ) { 209 ArrayList al = (ArrayList ) temp; 211 if (!al.contains(name)) { 212 al.add(name); 214 } 215 } 216 } 217 else { 218 intToNames.put(i, name); 220 } 221 } 222 return res; 223 } 224 225 public void configure(Properties prop) throws Exception { 226 } 227 228 231 public Logger getLogger(String _key) { 232 String key = _key; 233 if ("".equals(_key)) { 234 key = "root"; 235 } 236 synchronized (loggers) { 237 Logger res = (Logger) loggers.get(key); 238 if (res == null) { 239 res = new BasicLogger(key, this); 240 loggers.put(key, res); 241 } 242 return res; 243 } 244 } 245 public void removeLogger(Logger logger) { 246 synchronized (loggers) { 247 loggers.remove(logger.getName()); 248 } 249 } 250 251 public Logger getLogger(String key, String resourceBundleName) { 252 return getLogger(key); 253 } 254 255 public String getResourceBundleName() { 256 return resourceBundleName; 257 } 258 259 public void setResourceBundleName(String rbn) { 260 resourceBundleName = rbn; 261 } 262 263 public Logger[] getLoggers() { 264 return (TopicalLogger[]) loggers.values().toArray( 265 new TopicalLogger[loggers.size()]); 266 } 267 public String getTopicPrefix() { 268 return null; 269 } 270 271 272 275 public Handler createHandler(String hn, String handlertype) { 276 Handler res = (Handler) handlers.get(hn); 277 if (res != null) { 278 return null; 279 } 280 res = new BasicHandler(hn, handlertype); 281 handlers.put(hn, res); 282 return res; 283 } 284 285 public Handler[] getHandlers() { 286 return (Handler[]) handlers.values().toArray(new Handler[0]); 287 } 288 289 public Handler getHandler(String hn) { 290 return (Handler) handlers.get(hn); 291 } 292 293 public Handler removeHandler(String hn) { 294 return (Handler) handlers.remove(hn); 295 } 296 } 297 | Popular Tags |