1 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 ; 11 import java.util.Collections ; 12 import java.util.Date ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.LinkedList ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 24 public class ActivityMonitor { 25 26 private static final TCLogger logger = TCLogging.getLogger(ActivityMonitor.class); 27 private static Map activyMonitors = new HashMap (); 28 29 private final String name; 30 private final Map activityMap = Collections.synchronizedMap(new HashMap ()); 31 private final boolean softref; 32 33 static { 34 logger.setLevel(LogLevel.DEBUG); 36 } 37 38 public ActivityMonitor() { 39 this("Activity Monitor"); 40 } 41 42 public ActivityMonitor(String name) { 43 this(name, true); 44 } 45 46 public ActivityMonitor(String name, boolean softref) { 47 this.name = name; 48 this.softref = softref; 49 } 50 51 public void addActivity(Object id, String what) { 52 if (softref) { 53 addActivitySoftRef(id, what); 54 } else { 55 addActivityNormal(id, what); 56 } 57 } 58 59 private void addActivitySoftRef(Object id, String what) { 60 synchronized (activityMap) { 61 SoftReference ref = (SoftReference ) activityMap.get(id); 62 if (ref == null) { 63 ref = new SoftReference (new LinkedList ()); 64 activityMap.put(id, ref); 65 } 66 List list = (List ) ref.get(); 67 if (list == null) { 68 logger.debug(name + " :: GC cleared activity for " + id + "!"); 69 list = new LinkedList (); 70 list.add("GC cleared activity ! - (" + new Date () + ")"); 71 ref = new SoftReference (list); 72 activityMap.put(id, ref); 73 } 74 addActivityList(list, what); 75 } 76 } 77 78 private void addActivityList(List list, String what) { 79 synchronized (list) { 80 list.add(what + " - " + Thread.currentThread().getName() + " @ " + new Date ()); 81 try { 82 throw new Exception (" happened at -"); 83 } catch (Exception ex) { 84 list.add(ex.getStackTrace()); 85 } 86 } 87 } 88 89 private void addActivityNormal(Object id, String what) { 90 synchronized (activityMap) { 91 List list = (List ) activityMap.get(id); 92 if (list == null) { 93 list = new LinkedList (); 94 activityMap.put(id, list); 95 } 96 addActivityList(list, what); 97 } 98 } 99 100 public void printActivityFor(Object id) { 101 if (softref) { 102 printActivityForSoftRef(id); 103 } else { 104 printActivityForNormal(id); 105 } 106 } 107 108 private void printActivityForSoftRef(Object id) { 109 SoftReference ref = (SoftReference ) activityMap.get(id); 110 if (ref == null) { 111 logger.debug(name + " :: No Activity for " + id); 112 return; 113 } 114 List list = (List ) 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 id) { 124 List list = (List ) 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 list, Object id) { 133 logger.debug(name + " :: Activity for " + id + " ---- START ----"); 134 synchronized (list) { 135 for (Iterator iter = list.iterator(); iter.hasNext();) { 136 Object element = iter.next(); 137 if (element instanceof StackTraceElement []) { 138 StackTraceElement ste[] = (StackTraceElement []) element; 139 for (int i = 0; i < ste.length; i++) { 140 final String 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 name) { 158 return getActivityMonitor(name, true); 159 } 160 161 public static ActivityMonitor getActivityMonitor(String 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 |