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    public static Stack cloneStack() {
42      return null;
43    }
44  
45    public static void inherit(Stack stack) {
46    }
47  
48    static public String get() {
49      return null;
50    }
51  
52    public static int getDepth() {
53      int i = 0;
54      while (true) {
55        String val = MDC.get(PREFIX + i);
56        if (val != null) {
57          i++;
58        } else {
59          break;
60        }
61      }
62      return i;
63    }
64  
65    public static String pop() {
66      int next = getDepth();
67      if (next == 0) {
68        return "";
69      }
70      int last = next - 1;
71      String key = PREFIX + last;
72      String val = MDC.get(key);
73      MDC.remove(key);
74      return val;
75    }
76  
77    public static String peek() {
78      int next = getDepth();
79      if (next == 0) {
80        return "";
81      }
82      int last = next - 1;
83      String key = PREFIX + last;
84      String val = MDC.get(key);
85      return val;
86    }
87  
88    public static void push(String message) {
89      int next = getDepth();
90      MDC.put(PREFIX + next, message);
91    }
92  
93    static public void remove() {
94      clear();
95    }
96  
97    static public void setMaxDepth(int maxDepth) {
98    }
99  
100 }