View Javadoc

1   package com.atlassian.core.logging;
2   
3   import org.apache.log4j.spi.LoggingEvent;
4   
5   import java.util.LinkedList;
6   import java.util.List;
7   
8   /**
9    * A simple ThreadLocal stack to prevent circular content includes.
10   */
11  public class ThreadLocalErrorCollection
12  {
13      public static final int DEFAULT_LIMIT =100;
14  
15      private static final ThreadLocal threadLocalCollection = new ThreadLocal(){
16          protected Object initialValue(){
17              return new LinkedList();
18          }
19      };
20  
21      private static final ThreadLocal threadLocalEnabled = new ThreadLocal();
22  
23      private static int limit = DEFAULT_LIMIT;
24  
25      public static void add(long timeInMillis, LoggingEvent e)
26      {
27          if (!isEnabled())
28              return;
29  
30          List loggingEvents = getList();
31  
32          loggingEvents.add(new DatedLoggingEvent(timeInMillis, e));
33  
34          while (loggingEvents.size() > limit)
35              loggingEvents.remove(0);
36  
37      }
38  
39      public static void clear()
40      {
41          threadLocalCollection.remove();
42      }
43  
44      public static List getList()
45      {
46          List list = (List) threadLocalCollection.get();
47          return list;
48      }
49  
50      public static boolean isEmpty()
51      {
52          return getList().isEmpty();
53      }
54  
55      public static int getLimit()
56      {
57          return limit;
58      }
59  
60      public static void setLimit(int limit)
61      {
62          ThreadLocalErrorCollection.limit = limit;
63      }
64  
65      public static boolean isEnabled()
66      {
67          Boolean enabledState = (Boolean) threadLocalEnabled.get();
68          return Boolean.TRUE == enabledState;
69      }
70  
71      public static void enable()
72      {
73          threadLocalEnabled.set(Boolean.TRUE);
74      }
75  
76      public static void disable()
77      {
78          threadLocalEnabled.remove();
79      }
80  }