01: package org.jzonic.jlo;
02:
03: import java.util.Map;
04: import java.util.HashMap;
05: import java.util.Iterator;
06:
07: /**
08: * User: Mecky
09: * Date: 19.07.2005
10: * Time: 16:15:14
11: */
12: public class NDC {
13:
14: private static final ThreadLocal resources = new ThreadLocal();
15:
16: private NDC() {
17: }
18:
19: public static void put(String key, Object obj) {
20: Map map = (Map) resources.get();
21: // if no map then create one
22: if (map == null) {
23: map = new HashMap();
24: resources.set(map);
25: }
26: map.put(key, obj);
27: }
28:
29: public static Object get(String key) {
30: Map map = (Map) resources.get();
31: if (map == null) {
32: return null;
33: }
34: return map.get(key);
35: }
36:
37: public void remove(String key) {
38: Map map = (Map) resources.get();
39: if (map != null) {
40: if (map.containsKey(key)) {
41: map.remove(key);
42: }
43: }
44: }
45:
46: public static String getAsString() {
47: Map map = (Map) resources.get();
48: if (map == null) {
49: return null;
50: }
51: StringBuffer buffer = new StringBuffer();
52: buffer.append("[");
53: Iterator it = map.keySet().iterator();
54: while (it.hasNext()) {
55: String key = (String) it.next();
56: if (buffer.length() > 1) {
57: buffer.append(",");
58: }
59: buffer.append(key);
60: buffer.append("=");
61: buffer.append(map.get(key).toString());
62: }
63: buffer.append("]");
64: return buffer.toString();
65: }
66:
67: }
|