KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > ActivityMonitor


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import com.tc.logging.LogLevel;
7 import com.tc.logging.TCLogger;
8 import com.tc.logging.TCLogging;
9
10 import java.lang.ref.SoftReference JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.LinkedList JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 /**
20  * This class can be used while debugging to record some activity based on a key and later can be used to inpect the
21  * activity when something goes wrong. Do note that this class can bloat up the memory and take up a lot of resources.
22  * SoftREferences are used to avoid "OutOfMemory" Errors.
23  */

24 public class ActivityMonitor {
25
26   private static final TCLogger logger = TCLogging.getLogger(ActivityMonitor.class);
27   private static Map JavaDoc activyMonitors = new HashMap JavaDoc();
28
29   private final String JavaDoc name;
30   private final Map JavaDoc activityMap = Collections.synchronizedMap(new HashMap JavaDoc());
31   private final boolean softref;
32
33   static {
34     // Since this is only used when debugging...
35
logger.setLevel(LogLevel.DEBUG);
36   }
37
38   public ActivityMonitor() {
39     this("Activity Monitor");
40   }
41
42   public ActivityMonitor(String JavaDoc name) {
43     this(name, true);
44   }
45
46   public ActivityMonitor(String JavaDoc name, boolean softref) {
47     this.name = name;
48     this.softref = softref;
49   }
50
51   public void addActivity(Object JavaDoc id, String JavaDoc what) {
52     if (softref) {
53       addActivitySoftRef(id, what);
54     } else {
55       addActivityNormal(id, what);
56     }
57   }
58
59   private void addActivitySoftRef(Object JavaDoc id, String JavaDoc what) {
60     synchronized (activityMap) {
61       SoftReference JavaDoc ref = (SoftReference JavaDoc) activityMap.get(id);
62       if (ref == null) {
63         ref = new SoftReference JavaDoc(new LinkedList JavaDoc());
64         activityMap.put(id, ref);
65       }
66       List JavaDoc list = (List JavaDoc) ref.get();
67       if (list == null) {
68         logger.debug(name + " :: GC cleared activity for " + id + "!");
69         list = new LinkedList JavaDoc();
70         list.add("GC cleared activity ! - (" + new Date JavaDoc() + ")");
71         ref = new SoftReference JavaDoc(list);
72         activityMap.put(id, ref);
73       }
74       addActivityList(list, what);
75     }
76   }
77
78   private void addActivityList(List JavaDoc list, String JavaDoc what) {
79     synchronized (list) {
80       list.add(what + " - " + Thread.currentThread().getName() + " @ " + new Date JavaDoc());
81       try {
82         throw new Exception JavaDoc(" happened at -");
83       } catch (Exception JavaDoc ex) {
84         list.add(ex.getStackTrace());
85       }
86     }
87   }
88
89   private void addActivityNormal(Object JavaDoc id, String JavaDoc what) {
90     synchronized (activityMap) {
91       List JavaDoc list = (List JavaDoc) activityMap.get(id);
92       if (list == null) {
93         list = new LinkedList JavaDoc();
94         activityMap.put(id, list);
95       }
96       addActivityList(list, what);
97     }
98   }
99
100   public void printActivityFor(Object JavaDoc id) {
101     if (softref) {
102       printActivityForSoftRef(id);
103     } else {
104       printActivityForNormal(id);
105     }
106   }
107
108   private void printActivityForSoftRef(Object JavaDoc id) {
109     SoftReference JavaDoc ref = (SoftReference JavaDoc) activityMap.get(id);
110     if (ref == null) {
111       logger.debug(name + " :: No Activity for " + id);
112       return;
113     }
114     List JavaDoc list = (List JavaDoc) ref.get();
115     if (list == null) {
116       logger.debug(name + " :: GC cleared Activity for " + id + " !!!");
117       return;
118
119     }
120     printActivityForList(list, id);
121   }
122
123   private void printActivityForNormal(Object JavaDoc id) {
124     List JavaDoc list = (List JavaDoc) activityMap.get(id);
125     if (list == null) {
126       logger.debug(name + " :: No Activity for " + id);
127       return;
128     }
129     printActivityForList(list, id);
130   }
131
132   private void printActivityForList(List JavaDoc list, Object JavaDoc id) {
133     logger.debug(name + " :: Activity for " + id + " ---- START ----");
134     synchronized (list) {
135       for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
136         Object JavaDoc element = iter.next();
137         if (element instanceof StackTraceElement JavaDoc[]) {
138           StackTraceElement JavaDoc ste[] = (StackTraceElement JavaDoc[]) element;
139           for (int i = 0; i < ste.length; i++) {
140             final String JavaDoc steStr = ste[i].toString();
141             if (steStr.indexOf(this.getClass().getName()) == -1) {
142               logger.debug("\tat " + steStr);
143             }
144           }
145         } else {
146           logger.debug(element + " happened at ");
147         }
148       }
149       logger.debug(name + " :: Activity for " + id + " ---- END ----");
150     }
151   }
152
153   public void clear() {
154     activityMap.clear();
155   }
156
157   public static ActivityMonitor getActivityMonitor(String JavaDoc name) {
158     return getActivityMonitor(name, true);
159   }
160
161   public static ActivityMonitor getActivityMonitor(String JavaDoc name, boolean softref) {
162     synchronized (activyMonitors) {
163       ActivityMonitor monitor = (ActivityMonitor) activyMonitors.get(name);
164       if (monitor == null) {
165         monitor = new ActivityMonitor(name, softref);
166         activyMonitors.put(name, monitor);
167       }
168       return monitor;
169     }
170   }
171 }
172
Popular Tags