View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.log4j;
18  
19  import org.slf4j.MDC;
20  
21  import java.util.Stack;
22  
23  /**
24   * A log4j's NDC implemented in terms of SLF4J MDC primitives.
25   *
26   * @since SLF4J 1.6.0
27   */
28  
29  public class NDC {
30  
31    public final static String PREFIX = "NDC";
32  
33    public static void clear() {
34      int depth = getDepth();
35      for (int i = 0; i < depth; i++) {
36        String key = PREFIX + i;
37        MDC.remove(key);
38      }
39    }
40  
41    @SuppressWarnings("rawtypes")
42    public static Stack cloneStack() {
43      return null;
44    }
45  
46    @SuppressWarnings("rawtypes")
47    public static void inherit(Stack stack) {
48    }
49  
50    static public String get() {
51      return null;
52    }
53  
54    public static int getDepth() {
55      int i = 0;
56      while (true) {
57        String val = MDC.get(PREFIX + i);
58        if (val != null) {
59          i++;
60        } else {
61          break;
62        }
63      }
64      return i;
65    }
66  
67    public static String pop() {
68      int next = getDepth();
69      if (next == 0) {
70        return "";
71      }
72      int last = next - 1;
73      String key = PREFIX + last;
74      String val = MDC.get(key);
75      MDC.remove(key);
76      return val;
77    }
78  
79    public static String peek() {
80      int next = getDepth();
81      if (next == 0) {
82        return "";
83      }
84      int last = next - 1;
85      String key = PREFIX + last;
86      String val = MDC.get(key);
87      return val;
88    }
89  
90    public static void push(String message) {
91      int next = getDepth();
92      MDC.put(PREFIX + next, message);
93    }
94  
95    static public void remove() {
96      clear();
97    }
98  
99    static public void setMaxDepth(int maxDepth) {
100   }
101 
102 }