JAL-3048 test updated for AlignExportSettings changes
[jalview.git] / srcjar2 / org / apache / log4j / MDC.java
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
18 package org.apache.log4j;
19
20 import org.apache.log4j.helpers.Loader;
21 import org.apache.log4j.helpers.ThreadLocalMap;
22
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.util.Hashtable;
26
27 /**
28  * The MDC class is similar to the {@link NDC} class except that it is
29  * based on a map instead of a stack. It provides <em>mapped
30  * diagnostic contexts</em>. A <em>Mapped Diagnostic Context</em>, or
31  * MDC in short, is an instrument for distinguishing interleaved log
32  * output from different sources. Log output is typically interleaved
33  * when a server handles multiple clients near-simultaneously.
34  * <p/>
35  * <p><b><em>The MDC is managed on a per thread basis</em></b>. A
36  * child thread automatically inherits a <em>copy</em> of the mapped
37  * diagnostic context of its parent.
38  * <p/>
39  * <p>The MDC class requires JDK 1.2 or above. Under JDK 1.1 the MDC
40  * will always return empty values but otherwise will not affect or
41  * harm your application.</p>
42  *
43  * <p>Attention: the application is required to clean up. In web applications
44  * this can happen with creating a Servlet Filter and overriding the
45  * onFilter method like:</p>
46  *
47  * <pre>
48  * try {
49  *    MDC.put(myKey);
50  *    chain.doFilter(request, response);
51  * } finally {
52  *    MDC.remove(myKey);
53  * }
54  * </pre>
55  *
56  * <p>Please also see: {@link http://logging.apache.org/log4j/1.2/faq.html#mdcmemoryleak}</p>
57  *
58  * @author Ceki G&uuml;lc&uuml;
59  * @since 1.2
60  */
61 public class MDC {
62
63     final static MDC mdc = new MDC();
64
65     static final int HT_SIZE = 7;
66
67     boolean java1;
68
69     Object tlm;
70
71     private Method removeMethod;
72
73     private MDC() {
74         java1 = Loader.isJava1();
75         if (!java1) {
76             tlm = new ThreadLocalMap();
77         }
78
79         try {
80             removeMethod = ThreadLocal.class.getMethod("remove", null);
81         } catch (NoSuchMethodException e) {
82             // don't do anything - java prior 1.5
83         }
84     }
85
86     /**
87      * Put a context value (the <code>o</code> parameter) as identified
88      * with the <code>key</code> parameter into the current thread's
89      * context map.
90      * <p/>
91      * <p>If the current thread does not have a context map it is
92      * created as a side effect.
93      */
94     public static void put(String key, Object o) {
95         if (mdc != null) {
96             mdc.put0(key, o);
97         }
98     }
99
100     /**
101      * Get the context identified by the <code>key</code> parameter.
102      * <p/>
103      * <p>This method has no side effects.
104      */
105     public static Object get(String key) {
106         if (mdc != null) {
107             return mdc.get0(key);
108         }
109         return null;
110     }
111
112     /**
113      * Remove the the context identified by the <code>key</code>
114      * parameter.
115      */
116     public static void remove(String key) {
117         if (mdc != null) {
118             mdc.remove0(key);
119         }
120     }
121
122
123     /**
124      * Get the current thread's MDC as a hashtable. This method is
125      * intended to be used internally.
126      */
127     public static Hashtable getContext() {
128         if (mdc != null) {
129             return mdc.getContext0();
130         } else {
131             return null;
132         }
133     }
134
135     /**
136      * Remove all values from the MDC.
137      *
138      * @since 1.2.16
139      */
140     public static void clear() {
141         if (mdc != null) {
142             mdc.clear0();
143         }
144     }
145
146
147     private void put0(String key, Object o) {
148         if (java1 || tlm == null) {
149             return;
150         } else {
151             Hashtable ht = (Hashtable) ((ThreadLocalMap) tlm).get();
152             if (ht == null) {
153                 ht = new Hashtable(HT_SIZE);
154                 ((ThreadLocalMap) tlm).set(ht);
155             }
156             ht.put(key, o);
157         }
158     }
159
160     private Object get0(String key) {
161         if (java1 || tlm == null) {
162             return null;
163         } else {
164             Hashtable ht = (Hashtable) ((ThreadLocalMap) tlm).get();
165             if (ht != null && key != null) {
166                 return ht.get(key);
167             } else {
168                 return null;
169             }
170         }
171     }
172
173     private void remove0(String key) {
174         if (!java1 && tlm != null) {
175             Hashtable ht = (Hashtable) ((ThreadLocalMap) tlm).get();
176             if (ht != null) {
177                 ht.remove(key);
178                 // clean up if this was the last key
179                 if (ht.isEmpty()) {
180                         clear0();
181                 }
182             }
183         }
184     }
185
186     private Hashtable getContext0() {
187         if (java1 || tlm == null) {
188             return null;
189         } else {
190             return (Hashtable) ((ThreadLocalMap) tlm).get();
191         }
192     }
193
194     private void clear0() {
195         if (!java1 && tlm != null) {
196             Hashtable ht = (Hashtable) ((ThreadLocalMap) tlm).get();
197             if (ht != null) {
198                 ht.clear();
199             }
200             if (removeMethod != null) {
201                 // java 1.3/1.4 does not have remove - will suffer from a memory leak
202                 try {
203                     removeMethod.invoke(tlm, null);
204                 } catch (IllegalAccessException e) {
205                     // should not happen
206                 } catch (InvocationTargetException e) {
207                     // should not happen
208                 }
209             }
210         }
211     }
212 }