1 18 package org.objectweb.util.monolog.wrapper.common; 19 20 import org.objectweb.util.monolog.api.Logger; 21 import org.objectweb.util.monolog.api.Handler; 22 import org.objectweb.util.monolog.api.Level; 23 import org.objectweb.util.monolog.api.BasicLevel; 24 import org.objectweb.util.monolog.api.MonologFactory; 25 import org.objectweb.util.monolog.api.MonologFactoryListener; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.HashSet ; 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Properties ; 34 import java.util.StringTokenizer ; 35 36 40 public abstract class AbstractFactory 41 implements MonologFactory, Configurable { 42 43 public final static String CLASSLOADER_ISOLATION = "monolog.isolateclassloader"; 44 45 public static String [] handlerTypes = { 46 "console", "file", "rollingfile", "ntevent","jmx" 47 }; 48 49 public static String [][] handlerType2className = null; 50 51 public static boolean classLoaderIsoltion = true; 52 53 56 protected static String rootLoggerPrefix = null; 57 58 62 protected static String rootLoggerName = null; 63 64 67 public static String getRootLoggerPrefix() { 68 return rootLoggerPrefix; 69 } 70 71 public static String getTopicWithoutPrefix(String topic) { 72 if (classLoaderIsoltion && rootLoggerPrefix != null 73 && topic.startsWith(rootLoggerPrefix)) { 74 return topic.substring(rootLoggerPrefix.length()); 75 } else { 76 return topic; 77 } 78 } 79 80 87 protected static String monoLoggerName(String name) { 88 if (!classLoaderIsoltion || name.startsWith(rootLoggerPrefix)) { 89 if (debug) { 90 debug("name already prefixed: " + name); 91 } 92 return name; 93 } 94 return rootLoggerPrefix + name; 95 } 96 97 100 public static boolean debug = false; 101 102 static { 103 debug = new Boolean (System.getProperty("monolog.debug")).booleanValue(); 104 classLoaderIsoltion = Boolean.valueOf( 105 System.getProperty(CLASSLOADER_ISOLATION, "true")).booleanValue(); 106 } 107 108 109 116 public static void debug(String m) { 117 if (debug) { 118 System.out.println(m); 119 } 120 } 121 122 public static void warn(String m) { 123 System.err.println("WARN: " + m); 124 } 125 126 129 protected String resourceBundleName = null; 130 131 136 protected Map nameToLevel = null; 137 138 145 protected Map intToNames = null; 146 147 152 protected Map handlers = null; 153 154 159 protected Collection monologFactoryListeners = null; 160 161 165 public AbstractFactory() { 166 intToNames = new HashMap (); 167 nameToLevel = new HashMap (); 168 handlers = new HashMap (); 169 monologFactoryListeners = new HashSet (); 170 defineLevel(BasicLevel.LEVEL_INHERIT); 171 defineLevel(BasicLevel.LEVEL_DEBUG); 172 defineLevel(BasicLevel.LEVEL_INFO); 173 defineLevel(BasicLevel.LEVEL_WARN); 174 defineLevel(BasicLevel.LEVEL_ERROR); 175 defineLevel(BasicLevel.LEVEL_FATAL); 176 if (handlerType2className == null) { 177 synchronized(getClass()) { 178 if (handlerType2className == null) { 179 initHandlerType2className(); 180 } 181 } 182 } 183 } 184 185 public abstract String getWrapperName(); 186 187 protected void initHandlerType2className() { 188 handlerType2className = getDefaultHandlerType2className(); 189 for(int i=0; i<handlerType2className.length; i++) { 190 handlerTypes[i] = handlerType2className[i][0]; 191 } 192 193 String hts = System.getProperty("monolog." + getWrapperName() + ".handlerTypes"); 194 if (hts != null && hts.length() > 0) { 195 StringTokenizer st = new StringTokenizer (hts,",;:|.", false); 196 Map m = null; 197 if (st.hasMoreTokens()) { 198 m = new HashMap (); 199 } 200 while(st.hasMoreTokens()) { 201 String ht = st.nextToken(); 203 String hcn = System.getProperty("monolog.handlerType." + ht); 205 if (hcn != null && hcn.length() > 0) { 206 m.put(ht, hcn); 207 } else { 208 debug("Handler type '" + ht + "' not well defined: " + hcn); 209 } 210 } 211 if (m != null && m.size() > 0) { 212 String [] newHT = new String [handlerTypes.length + m.size()]; 214 System.arraycopy(handlerTypes, 0, newHT, 0, handlerTypes.length); 215 String [][] newHT2CN = new String [handlerTypes.length + m.size()][]; 216 System.arraycopy(handlerType2className, 0, newHT2CN, 0, handlerType2className.length); 217 218 int i = handlerTypes.length; 220 for(Iterator it = m.entrySet().iterator(); it.hasNext();) { 221 Map.Entry me = (Map.Entry ) it.next(); 222 handlerTypes[i] = (String ) me.getKey(); 223 handlerType2className[i][0] = handlerTypes[i]; 224 handlerType2className[i][1] = (String ) me.getValue(); 225 } 226 handlerTypes = newHT; 227 handlerType2className = newHT2CN; 228 } 229 } 230 } 231 232 abstract protected String [][] getDefaultHandlerType2className(); 233 234 244 private Level defineLevel(Level l) { 245 String name = l.getName(); 247 int value = l.getIntValue(); 248 Level res = (Level) nameToLevel.get(name); 249 if (res != null) { 250 return (res.getIntValue() == value ? res : null); 252 } else { 253 res = l; 254 nameToLevel.put(name, res); 255 Integer i = new Integer (value); 256 Object temp = intToNames.get(i); 257 if (temp != null) { 258 if (temp instanceof String ) { 259 if (!((String ) temp).equalsIgnoreCase(name)) { 260 ArrayList al = new ArrayList (5); 263 al.add(temp); 264 al.add(name); 265 intToNames.put(i, al); 266 } 267 } 268 else if (temp instanceof ArrayList ) { 269 ArrayList al = (ArrayList ) temp; 271 if (!al.contains(name)) { 272 al.add(name); 274 } 275 } 276 } 277 else { 278 intToNames.put(i, name); 280 } 281 Iterator iterator = monologFactoryListeners.iterator (); 283 while (iterator.hasNext ()) { 284 MonologFactoryListener mfl = ((MonologFactoryListener)iterator.next ()); 285 mfl.levelCreated (res); 286 } 287 } 288 return res; 290 } 291 292 295 public abstract void configure(Properties prop) throws Exception ; 296 297 300 public abstract Logger getLogger(String key); 301 302 public abstract Logger[] getLoggers(); 303 304 public String getTopicPrefix() { 305 return rootLoggerPrefix; 306 } 307 308 public String getResourceBundleName() { 309 return resourceBundleName; 310 } 311 312 public void setResourceBundleName(String rbn) { 313 resourceBundleName = rbn; 314 } 315 316 317 320 public Handler createHandler(String hn, String handlertype) { 321 Handler res = (Handler) handlers.get(hn); 322 if (res != null) { 323 return res; 324 } 325 if (handlertype == null) { 326 return null; 327 } 328 int i =0; 329 for(;i<handlerType2className.length 330 && !handlerType2className[i][0].equalsIgnoreCase(handlertype); i++); 331 String handlerClassName; 332 if (i<handlerType2className.length) { 333 handlerClassName = handlerType2className[i][1]; 334 } else { 335 handlerClassName = handlertype; 336 } 337 debug("Instanciating the handler '" + hn + "', class name=" + handlerClassName); 338 try { 339 res = (Handler) Class.forName(handlerClassName).newInstance(); 340 } catch (Throwable e) { 341 warn("Impossible to instanciate the handler: name=" + hn 342 + ", class name=" + handlerClassName +": " + e.getMessage()); 343 e.printStackTrace(System.err); 344 return null; 345 } 346 res.setAttribute("handlertype", handlertype); 347 res.setName(hn); 348 handlers.put(hn, res); 349 Iterator iterator = monologFactoryListeners.iterator (); 351 while (iterator.hasNext ()) { 352 MonologFactoryListener mfl = ((MonologFactoryListener)iterator.next ()); 353 mfl.handlerCreated(res); 354 } 355 return res; 356 } 357 358 public Handler[] getHandlers() { 359 return (Handler[]) handlers.values().toArray(new Handler[0]); 360 } 361 362 public Handler getHandler(String hn) { 363 return (Handler) handlers.get(hn); 364 } 365 366 public Handler removeHandler(String hn) { 367 Handler res = (Handler) handlers.remove(hn); 368 if (res != null) { 369 Iterator iterator = monologFactoryListeners.iterator (); 371 while (iterator.hasNext ()) { 372 MonologFactoryListener mfl = ((MonologFactoryListener)iterator.next ()); 373 mfl.handlerRemoved(getHandler(hn)); 374 } 375 } 376 return res; 377 } 378 379 382 public Level defineLevel(String name, int value) { 383 return defineLevel(new LevelImpl(name, value)); 384 } 385 386 public Level defineLevel(String name, String value) { 387 return defineLevel(new LevelImpl(name, value, this)); 388 } 389 390 public Level getLevel(String name) { 391 return (Level) nameToLevel.get(name); 392 } 393 394 public Level getLevel(int value) { 395 Object temp = intToNames.get(new Integer (value)); 396 if (temp == null) { 397 return null; 398 } 399 else if (temp instanceof String ) { 400 return getLevel((String ) temp); 401 } 402 else if (temp instanceof ArrayList ) { 403 return getLevel((String ) ((ArrayList ) temp).get(0)); 404 } 405 return null; 406 } 407 408 public Level[] getLevels() { 409 return (Level[]) nameToLevel.values().toArray(new Level[0]); 410 } 411 412 public void removeLevel(String name) { 413 Level removed = (Level) nameToLevel.remove(name); 414 if (removed != null) { 415 Integer i = new Integer (removed.getIntValue()); 416 Object temp = intToNames.get(i); 417 if (temp instanceof String ) { 418 intToNames.remove(i); 419 } else if (temp instanceof ArrayList ) { 420 ((ArrayList ) temp).remove(name); 421 } 422 Iterator iterator = monologFactoryListeners.iterator (); 424 while (iterator.hasNext ()) { 425 MonologFactoryListener mfl = ((MonologFactoryListener)iterator.next ()); 426 mfl.levelRemoved(removed); 427 } 428 } 429 } 430 433 public void addMonologFactoryListener (MonologFactoryListener mfl) { 434 monologFactoryListeners.add(mfl); 435 } 436 437 public void removeMonologFactoryListener (MonologFactoryListener mfl) { 438 monologFactoryListeners.remove(mfl); 439 } 440 } 441 | Popular Tags |