View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.spi;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /**
23   *
24   */
25  public class DefaultThreadContextMap implements ThreadContextMap {
26  
27      private final boolean useMap;
28  
29      private final ThreadLocal<Map<String, String>> localMap =
30          new InheritableThreadLocal<Map<String, String>>() {
31              @Override
32              protected Map<String, String> childValue(final Map<String, String> parentValue) {
33                  return parentValue == null || !useMap ? null : new HashMap<String, String>(parentValue);
34              }
35          };
36  
37      public DefaultThreadContextMap(final boolean useMap) {
38          this.useMap = useMap;
39      }
40  
41      /**
42       * Put a context value (the <code>o</code> parameter) as identified
43       * with the <code>key</code> parameter into the current thread's
44       * context map.
45       * <p/>
46       * <p>If the current thread does not have a context map it is
47       * created as a side effect.
48       * @param key The key name.
49       * @param value The key value.
50       */
51      public void put(final String key, final String value) {
52          if (!useMap) {
53              return;
54          }
55          Map<String, String> map = localMap.get();
56          if (map == null) {
57              map = new HashMap<String, String>();
58              localMap.set(map);
59          }
60          map.put(key, value);
61      }
62  
63      /**
64       * Get the context identified by the <code>key</code> parameter.
65       * <p/>
66       * <p>This method has no side effects.
67       * @param key The key to locate.
68       * @return The value associated with the key or null.
69       */
70      public String get(final String key) {
71          final Map<String, String> map = localMap.get();
72          return map == null ? null : map.get(key);
73      }
74  
75      /**
76       * Remove the the context identified by the <code>key</code>
77       * parameter.
78       * @param key The key to remove.
79       */
80      public void remove(final String key) {
81          final Map<String, String> map = localMap.get();
82          if (map != null) {
83              map.remove(key);
84          }
85      }
86  
87      /**
88       * Clear the context.
89       */
90      public void clear() {
91          localMap.remove();
92      }
93  
94      /**
95       * Determine if the key is in the context.
96       * @param key The key to locate.
97       * @return True if the key is in the context, false otherwise.
98       */
99      public boolean containsKey(final String key) {
100         final Map<String, String> map = localMap.get();
101         return map == null ? false : map.containsKey(key);
102     }
103 
104     /**
105      * Get a copy of current thread's context Map.
106      * @return a copy of the context.
107      */
108     public Map<String, String> getContext() {
109         final Map<String, String> map = localMap.get();
110         return map == null ? new HashMap<String, String>() : new HashMap<String, String>(map);
111     }
112 
113     /**
114      * Return the context Map.
115      * @return the Context Map.
116      */
117     public Map<String, String> get() {
118         return localMap.get();
119     }
120 
121     /**
122      * Returns true if the Map is empty.
123      * @return true if the Map is empty, false otherwise.
124      */
125     public boolean isEmpty() {
126         final Map<String, String> map = localMap.get();
127         return map == null || map.size() == 0;
128     }
129 }