View Javadoc

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