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.log4j;
19  
20  import org.slf4j.MDC;
21  
22  import java.util.Stack;
23  
24  /**
25   * A log4j's NDC implemented in terms of SLF4J MDC primitives.
26   *
27   * @since SLF4J 1.6.0
28   */
29  
30  public class NDC {
31  
32    public final static String PREFIX = "NDC";
33  
34    public static void clear() {
35      int depth = getDepth();
36      for (int i = 0; i < depth; i++) {
37        String key = PREFIX + i;
38        MDC.remove(key);
39      }
40    }
41  
42    public static Stack cloneStack() {
43      return null;
44    }
45  
46    public static void inherit(Stack stack) {
47    }
48  
49    static public String get() {
50      return null;
51    }
52  
53    public static int getDepth() {
54      int i = 0;
55      while (true) {
56        String val = MDC.get(PREFIX + i);
57        if (val != null) {
58          i++;
59        } else {
60          break;
61        }
62      }
63      return i;
64    }
65  
66    public static String pop() {
67      int next = getDepth();
68      if (next == 0) {
69        return "";
70      }
71      int last = next - 1;
72      String key = PREFIX + last;
73      String val = MDC.get(key);
74      MDC.remove(key);
75      return val;
76    }
77  
78    public static String peek() {
79      int next = getDepth();
80      if (next == 0) {
81        return "";
82      }
83      int last = next - 1;
84      String key = PREFIX + last;
85      String val = MDC.get(key);
86      return val;
87    }
88  
89    public static void push(String message) {
90      int next = getDepth();
91      MDC.put(PREFIX + next, message);
92    }
93  
94    static public void remove() {
95      clear();
96    }
97  
98    static public void setMaxDepth(int maxDepth) {
99    }
100 
101 }