View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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  
19  import ch.qos.logback.core.status.StatusManager;
20  
21  public class ContextBase implements Context {
22  
23    private long birthTime = System.currentTimeMillis();
24    
25    private String name;
26    private StatusManager sm = new BasicStatusManager();
27    // TODO propertyMap should be observable so that we can be notified
28    // when it changes so that a new instance of propertyMap can be
29    // serialized. For the time being, we ignore this shortcoming.
30    Map<String, String> propertyMap = new HashMap<String, String>();
31    Map<String, Object> objectMap = new HashMap<String, Object>();
32  
33    Object configurationLock = new Object();
34    
35    public StatusManager getStatusManager() {
36      return sm;
37    }
38  
39    /**
40     * Set the {@link StatusManager} for this context. Note that by default this
41     * context is initialized with a {@link BasicStatusManager}. A null value for
42     * the 'statusManager' argument is not allowed.
43     * 
44     * <p> A malicious attacker can set the status manager to a dummy instance,
45     * disabling internal error reporting.
46     * 
47     * @param statusManager
48     *                the new status manager
49     */
50    public void setStatusManager(StatusManager statusManager) {
51      // this method was added in response to http://jira.qos.ch/browse/LBCORE-35
52      if (sm == null) {
53        throw new IllegalArgumentException("null StatusManager not allowed");
54      }
55      this.sm = statusManager;
56    }
57  
58    public Map<String, String> getCopyOfPropertyMap() {
59      return new HashMap<String, String>(propertyMap);
60    }
61  
62    public void putProperty(String key, String val) {
63      this.propertyMap.put(key, val);
64    }
65  
66    public String getProperty(String key) {
67      return (String) this.propertyMap.get(key);
68    }
69  
70    public Object getObject(String key) {
71      return objectMap.get(key);
72    }
73  
74    public void putObject(String key, Object value) {
75      objectMap.put(key, value);
76    }
77  
78    public String getName() {
79      return name;
80    }
81  
82    /**
83     * Clear the internal objectMap and all properties.
84     */
85    public void reset() {
86      propertyMap.clear();
87      objectMap.clear();
88    }
89  
90    /**
91     * The context name can be set only if it is not already set, or if the
92     * current name is the default context name, namely "default", or if the
93     * current name and the old name are the same.
94     * 
95     * @throws IllegalStateException
96     *                 if the context already has a name, other than "default".
97     */
98    public void setName(String name) throws IllegalStateException {
99      if (name != null && name.equals(this.name)) {
100       return; // idempotent naming
101     }
102     if (this.name == null
103         || CoreConstants.DEFAULT_CONTEXT_NAME.equals(this.name)) {
104       this.name = name;
105     } else {
106       throw new IllegalStateException("Context has been already given a name");
107     }
108   }
109 
110   public long getBithTime() {
111     return birthTime;
112   }
113 
114   public Object getConfigurationLock() {
115     return configurationLock;
116   }
117 }