1   /** 
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    *
4    * Copyright (C) 1999-2005, QOS.ch, LOGBack.com
5    *
6    * This library is free software, you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation.
9    */
10  package ch.qos.logback.classic.control;
11  
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  import ch.qos.logback.classic.ClassicGlobal;
16  import ch.qos.logback.classic.Level;
17  
18  /**
19   * This logger context quite optimized for logger retrieval.
20   * 
21   * <p>It uses a single loggerMap where the key is the logger name and the value
22   * is the logger.
23   * 
24   * <p>This approach acts a lower limit for what is achievable for low memory
25   * usage as well as low creation/retrieval times. However, this simplicity also
26   * results in slow effective level evaluation, the most frequently exercised
27   * part of the API.
28   * 
29   * <p>This class is expected to contain correct results, and serve to verify
30   * the correctness of a more sophisticated implementation.
31   * 
32   * @author ceki
33   */
34  public class ControlLoggerContext {
35  
36    private ControlLogger root;
37    //
38    // Hashtable loggerMap = new Hashtable();
39    Map<String, ControlLogger> loggerMap = new HashMap<String, ControlLogger>();
40  
41    public ControlLoggerContext() {
42      this.root = new ControlLogger("root", null);
43      this.root.setLevel(Level.DEBUG);
44    }
45  
46    /**
47     * Return this contexts root logger
48     * 
49     * @return
50     */
51    public ControlLogger getRootLogger() {
52      return root;
53    }
54  
55    public ControlLogger exists(String name) {
56      if (name == null) {
57        throw new IllegalArgumentException("name parameter cannot be null");
58      }
59  
60      synchronized (loggerMap) {
61        return (ControlLogger) loggerMap.get(name);
62      }
63    }
64  
65    public final ControlLogger getLogger(String name) {
66      if (name == null) {
67        throw new IllegalArgumentException("name parameter cannot be null");
68      }
69  
70      synchronized (loggerMap) {
71        ControlLogger cl = (ControlLogger) loggerMap.get(name);
72        if (cl != null) {
73          return cl;
74        }
75        ControlLogger parent = this.root;
76  
77        int i = 0;
78        while (true) {
79          i = name.indexOf(ClassicGlobal.LOGGER_SEPARATOR, i);
80          if (i == -1) {
81            // System.out.println("FINAL-Creating logger named [" + name + "] with
82            // parent " + parent.getName());
83            cl = new ControlLogger(name, parent);
84            loggerMap.put(name, cl);
85            return cl;
86          } else {
87            String parentName = name.substring(0, i);
88            ControlLogger p = (ControlLogger) loggerMap.get(parentName);
89            if (p == null) {
90              // System.out.println("INTERMEDIARY-Creating logger [" + parentName
91              // + "] with parent " + parent.getName());
92              p = new ControlLogger(parentName, parent);
93              loggerMap.put(parentName, p);
94            }
95            parent = p;
96          }
97          // make i move past the last found dot.
98          i++;
99        }
100     }
101   }
102 
103   public Map<String, ControlLogger> getLoggerMap() {
104     return loggerMap;
105   }
106 }