KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > logging > log4j > NamedNDC


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.geronimo.system.logging.log4j;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * Provides named nested diagnotic contexts.
26  *
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
28  */

29 public final class NamedNDC {
30     /**
31      * Mapping from names to NamedNDCs.
32      * Currently there is no way to remove a NamedNCD once created, so be sure you really
33      * want a new NDC before creating one.
34      * @todo make this a weak-valued map
35      */

36     private static final Map JavaDoc contexts = new HashMap JavaDoc();
37
38     /**
39      * Gets the NamedNDC by name, or creates a new one of one does not already exist.
40      * @param name the name of the desired NamedNDC
41      * @return the existing NamedNDC or a new one
42      */

43     public static NamedNDC getNamedNDC(String JavaDoc 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 JavaDoc value) {
60         listThreadLocal.getList().addLast(value);
61     }
62
63     public Object JavaDoc get() {
64         LinkedList JavaDoc list = listThreadLocal.getList();
65         if (list.isEmpty()) {
66             return null;
67         }
68         return list.getLast();
69     }
70
71     public Object JavaDoc pop() {
72         LinkedList JavaDoc 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 JavaDoc {
84         public LinkedList JavaDoc getList() {
85             return (LinkedList JavaDoc) get();
86         }
87
88         protected Object JavaDoc initialValue() {
89             return new LinkedList JavaDoc();
90         }
91     }
92 }
93
Popular Tags