View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  import java.util.concurrent.*;
19  
20  import ch.qos.logback.core.status.StatusManager;
21  
22  import static ch.qos.logback.core.CoreConstants.CONTEXT_NAME_KEY;
23  
24  public class ContextBase implements Context {
25  
26    private long birthTime = System.currentTimeMillis();
27  
28    private String name;
29    private StatusManager sm = new BasicStatusManager();
30    // TODO propertyMap should be observable so that we can be notified
31    // when it changes so that a new instance of propertyMap can be
32    // serialized. For the time being, we ignore this shortcoming.
33    Map<String, String> propertyMap = new HashMap<String, String>();
34    Map<String, Object> objectMap = new HashMap<String, Object>();
35  
36    Object configurationLock = new Object();
37  
38    // 0 idle threads, 2 maximum threads, no idle waiting
39    ExecutorService executorService = new ThreadPoolExecutor(0, 2,
40            0L, TimeUnit.MILLISECONDS,
41            new LinkedBlockingQueue<Runnable>());
42  
43    public StatusManager getStatusManager() {
44      return sm;
45    }
46  
47    /**
48     * Set the {@link StatusManager} for this context. Note that by default this
49     * context is initialized with a {@link BasicStatusManager}. A null value for
50     * the 'statusManager' argument is not allowed.
51     * <p/>
52     * <p> A malicious attacker can set the status manager to a dummy instance,
53     * disabling internal error reporting.
54     *
55     * @param statusManager the new status manager
56     */
57    public void setStatusManager(StatusManager statusManager) {
58      // this method was added in response to http://jira.qos.ch/browse/LBCORE-35
59      if (sm == null) {
60        throw new IllegalArgumentException("null StatusManager not allowed");
61      }
62      this.sm = statusManager;
63    }
64  
65    public Map<String, String> getCopyOfPropertyMap() {
66      return new HashMap<String, String>(propertyMap);
67    }
68  
69    public void putProperty(String key, String val) {
70      this.propertyMap.put(key, val);
71    }
72  
73    /**
74     * Given a key, return the corresponding property value. If invoked with
75     * the special key "CONTEXT_NAME", the name of the context is returned.
76     *
77     * @param key
78     * @return
79     */
80    public String getProperty(String key) {
81      if (CONTEXT_NAME_KEY.equals(key))
82        return getName();
83  
84      return (String) this.propertyMap.get(key);
85    }
86  
87    public Object getObject(String key) {
88      return objectMap.get(key);
89    }
90  
91    public void putObject(String key, Object value) {
92      objectMap.put(key, value);
93    }
94  
95    public String getName() {
96      return name;
97    }
98  
99    /**
100    * Clear the internal objectMap and all properties.
101    */
102   public void reset() {
103     propertyMap.clear();
104     objectMap.clear();
105   }
106 
107   /**
108    * The context name can be set only if it is not already set, or if the
109    * current name is the default context name, namely "default", or if the
110    * current name and the old name are the same.
111    *
112    * @throws IllegalStateException if the context already has a name, other than "default".
113    */
114   public void setName(String name) throws IllegalStateException {
115     if (name != null && name.equals(this.name)) {
116       return; // idempotent naming
117     }
118     if (this.name == null
119             || CoreConstants.DEFAULT_CONTEXT_NAME.equals(this.name)) {
120       this.name = name;
121     } else {
122       throw new IllegalStateException("Context has been already given a name");
123     }
124   }
125 
126   public long getBirthTime() {
127     return birthTime;
128   }
129 
130   public Object getConfigurationLock() {
131     return configurationLock;
132   }
133 
134   public ExecutorService getExecutorService() {
135     return  executorService;
136   }
137 }