KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > ContextMap


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log;
9
10 import java.io.ObjectStreamException JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * The ContextMap contains non-hierarchical context information
18  * relevent to a particular LogEvent. It may include information
19  * such as;
20  * <p/>
21  * <ul>
22  * <li>user -&gt;fred</li>
23  * <li>hostname -&gt;helm.realityforge.org</li>
24  * <li>ipaddress -&gt;1.2.3.4</li>
25  * <li>interface -&gt;127.0.0.1</li>
26  * <li>caller -&gt;com.biz.MyCaller.method(MyCaller.java:18)</li>
27  * <li>source -&gt;1.6.3.2:33</li>
28  * </ul>
29  * The context is bound to a thread (and inherited by sub-threads) but
30  * it can also be added to by LogTargets.
31  *
32  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
33  */

34 public final class ContextMap implements Serializable JavaDoc {
35     ///Thread local for holding instance of map associated with current thread
36
private static final ThreadLocal JavaDoc c_context = new InheritableThreadLocal JavaDoc();
37
38     private final ContextMap m_parent;
39
40     ///Container to hold map of elements
41
private Map JavaDoc m_map = Collections.synchronizedMap(new HashMap JavaDoc());
42
43     ///Flag indicating whether this map should be readonly
44
private transient boolean m_readOnly;
45
46     /**
47      * Get the Current ContextMap.
48      * This method returns a ContextMap associated with current thread. If the
49      * thread doesn't have a ContextMap associated with it then a new
50      * ContextMap is created.
51      *
52      * @return the current ContextMap
53      */

54     public final static ContextMap getCurrentContext() {
55         return getCurrentContext(true);
56     }
57
58     /**
59      * Get the Current ContextMap.
60      * This method returns a ContextMap associated with current thread.
61      * If the thread doesn't have a ContextMap associated with it and
62      * autocreate is true then a new ContextMap is created.
63      *
64      * @param autocreate true if a ContextMap is to be created if it doesn't exist
65      * @return the current ContextMap
66      */

67     public final static ContextMap getCurrentContext(final boolean autocreate) {
68         //Check security permission here???
69
ContextMap context = (ContextMap)c_context.get();
70
71         if (null == context && autocreate) {
72             context = new ContextMap();
73             c_context.set(context);
74         }
75
76         return context;
77     }
78
79     /**
80      * Bind a particular ContextMap to current thread.
81      *
82      * @param context the context map (may be null)
83      */

84     public final static void bind(final ContextMap context) {
85         //Check security permission here??
86
c_context.set(context);
87     }
88
89     /**
90      * Default constructor.
91      */

92     public ContextMap() {
93         this(null);
94     }
95
96     /**
97      * Constructor that sets parent contextMap.
98      *
99      * @param parent the parent ContextMap
100      */

101     public ContextMap(final ContextMap parent) {
102         m_parent = parent;
103     }
104
105     /**
106      * Make the context read-only.
107      * This makes it safe to allow untrusted code reference
108      * to ContextMap.
109      */

110     public void makeReadOnly() {
111         m_readOnly = true;
112     }
113
114     /**
115      * Determine if context is read-only.
116      *
117      * @return true if Context is read only, false otherwise
118      */

119     public boolean isReadOnly() {
120         return m_readOnly;
121     }
122
123     /**
124      * Empty the context map.
125      */

126     public void clear() {
127         checkReadable();
128
129         m_map.clear();
130     }
131
132     /**
133      * Get an entry from the context.
134      *
135      * @param key the key to map
136      * @param defaultObject a default object to return if key does not exist
137      * @return the object in context
138      */

139     public Object JavaDoc get(final String JavaDoc key, final Object JavaDoc defaultObject) {
140         final Object JavaDoc object = get(key);
141
142         if (null != object)
143             return object;
144         else
145             return defaultObject;
146     }
147
148     /**
149      * Get an entry from the context.
150      *
151      * @param key the key to map
152      * @return the object in context or null if none with specified key
153      */

154     public Object JavaDoc get(final String JavaDoc key) {
155         final Object JavaDoc result = m_map.get(key);
156
157         if (null == result && null != m_parent) {
158             return m_parent.get(key);
159         }
160
161         return result;
162     }
163
164     /**
165      * Set a value in context
166      *
167      * @param key the key
168      * @param value the value (may be null)
169      */

170     public void set(final String JavaDoc key, final Object JavaDoc value) {
171         checkReadable();
172
173         if (value == null) {
174             m_map.remove(key);
175         }
176         else {
177             m_map.put(key, value);
178         }
179     }
180
181
182     /**
183      * Get the number of contexts in map.
184      *
185      * @return the number of contexts in map
186      */

187     public int getSize() {
188         return m_map.size();
189     }
190
191     /**
192      * Helper method that sets context to read-only after de-serialization.
193      *
194      * @return the corrected object version
195      * @throws ObjectStreamException if an error occurs
196      */

197     private Object JavaDoc readResolve() throws ObjectStreamException JavaDoc {
198         makeReadOnly();
199         return this;
200     }
201
202     /**
203      * Utility method to verify that Context is read-only.
204      */

205     private void checkReadable() {
206         if (isReadOnly()) {
207             throw new IllegalStateException JavaDoc("ContextMap is read only and can not be modified");
208         }
209     }
210 }
211
Popular Tags