1 17 18 package org.apache.geronimo.system.logging.log4j; 19 20 import java.util.HashMap ; 21 import java.util.LinkedList ; 22 import java.util.Map ; 23 24 29 public final class NamedNDC { 30 36 private static final Map contexts = new HashMap (); 37 38 43 public static NamedNDC getNamedNDC(String name) { 44 synchronized (contexts) { 45 NamedNDC context = (NamedNDC) contexts.get(name); 46 if (context == null) { 47 context = new NamedNDC(); 48 contexts.put(name, context); 49 } 50 return context; 51 } 52 } 53 54 private final ListThreadLocal listThreadLocal = new ListThreadLocal(); 55 56 private NamedNDC() { 57 } 58 59 public void push(Object value) { 60 listThreadLocal.getList().addLast(value); 61 } 62 63 public Object get() { 64 LinkedList list = listThreadLocal.getList(); 65 if (list.isEmpty()) { 66 return null; 67 } 68 return list.getLast(); 69 } 70 71 public Object pop() { 72 LinkedList list = listThreadLocal.getList(); 73 if (list.isEmpty()) { 74 return null; 75 } 76 return list.removeLast(); 77 } 78 79 public void clear() { 80 listThreadLocal.getList().clear(); 81 } 82 83 private final static class ListThreadLocal extends ThreadLocal { 84 public LinkedList getList() { 85 return (LinkedList ) get(); 86 } 87 88 protected Object initialValue() { 89 return new LinkedList (); 90 } 91 } 92 } 93 | Popular Tags |