1 11 package org.eclipse.osgi.framework.stats; 12 13 import java.io.*; 14 import java.util.*; 15 import org.eclipse.osgi.framework.adaptor.BundleWatcher; 16 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor; 17 import org.eclipse.osgi.framework.debug.DebugOptions; 18 import org.osgi.framework.Bundle; 19 20 public class StatsManager implements BundleWatcher { 21 private Hashtable plugins = new Hashtable(20); 24 private Map activationStacks = new HashMap(5); 25 private static boolean booting = true; 28 private static StatsManager defaultInstance; 29 30 public static boolean MONITOR_ACTIVATION = false; 31 public static boolean MONITOR_CLASSES = false; 32 public static boolean MONITOR_RESOURCES = false; 33 public static String TRACE_FILENAME = "runtime.traces"; public static String TRACE_FILTERS = "trace.properties"; public static boolean TRACE_CLASSES = false; 36 public static boolean TRACE_BUNDLES = false; 37 public static final String FRAMEWORK_SYMBOLICNAME = "org.eclipse.osgi"; 39 private static final String OPTION_MONITOR_ACTIVATION = FRAMEWORK_SYMBOLICNAME + "/monitor/activation"; private static final String OPTION_MONITOR_CLASSES = FRAMEWORK_SYMBOLICNAME + "/monitor/classes"; private static final String OPTION_MONITOR_RESOURCES = FRAMEWORK_SYMBOLICNAME + "/monitor/resources"; private static final String OPTION_TRACE_BUNDLES = FRAMEWORK_SYMBOLICNAME + "/trace/activation"; private static final String OPTION_TRACE_CLASSES = FRAMEWORK_SYMBOLICNAME + "/trace/classLoading"; private static final String OPTION_TRACE_FILENAME = FRAMEWORK_SYMBOLICNAME + "/trace/filename"; private static final String OPTION_TRACE_FILTERS = FRAMEWORK_SYMBOLICNAME + "/trace/filters"; 48 static { 49 setDebugOptions(); 50 } 51 52 public static StatsManager getDefault() { 53 if (defaultInstance == null) { 54 defaultInstance = new StatsManager(); 55 defaultInstance.initialize(); 56 } 57 return defaultInstance; 58 } 59 60 public static void setDebugOptions() { 61 DebugOptions options = DebugOptions.getDefault(); 62 if (options == null) 64 return; 65 MONITOR_ACTIVATION = options.getBooleanOption(OPTION_MONITOR_ACTIVATION, false); 66 MONITOR_CLASSES = options.getBooleanOption(OPTION_MONITOR_CLASSES, false); 67 MONITOR_RESOURCES = options.getBooleanOption(OPTION_MONITOR_RESOURCES, false); 68 TRACE_CLASSES = options.getBooleanOption(OPTION_TRACE_CLASSES, false); 69 TRACE_BUNDLES = options.getBooleanOption(OPTION_TRACE_BUNDLES, false); 70 TRACE_FILENAME = options.getOption(OPTION_TRACE_FILENAME, TRACE_FILENAME); 71 TRACE_FILTERS = options.getOption(OPTION_TRACE_FILTERS, TRACE_FILTERS); 72 } 73 74 public static void doneBooting() { 75 booting = false; 76 } 77 78 public static boolean isBooting() { 79 return booting; 80 } 81 82 88 public static String [] getArrayFromList(String prop) { 89 if (prop == null || prop.trim().equals("")) return new String [0]; 91 Vector list = new Vector(); 92 StringTokenizer tokens = new StringTokenizer(prop, ","); while (tokens.hasMoreTokens()) { 94 String token = tokens.nextToken().trim(); 95 if (!token.equals("")) list.addElement(token); 97 } 98 return list.isEmpty() ? new String [0] : (String []) list.toArray(new String [list.size()]); 99 } 100 101 private void initialize() { 102 BundleStats plugin = findPlugin(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME); 104 plugin.setTimestamp(System.currentTimeMillis()); 105 plugin.setActivationOrder(plugins.size()); 106 plugin.setDuringStartup(booting); 107 } 108 109 public void startActivation(Bundle bundle) { 110 BundleStats plugin = findPlugin(bundle.getSymbolicName()); 112 plugin.setTimestamp(System.currentTimeMillis()); 113 plugin.setActivationOrder(plugins.size()); 114 plugin.setDuringStartup(booting); 115 116 Stack activationStack = (Stack) activationStacks.get(Thread.currentThread()); 117 if (activationStack==null) { 118 activationStack = new Stack(); 119 activationStacks.put(Thread.currentThread(), activationStack); 120 } 121 122 if (activationStack.size() != 0) { 124 BundleStats activatedBy = (BundleStats) activationStack.peek(); 125 activatedBy.activated(plugin); 126 plugin.setActivatedBy(activatedBy); 127 } 128 activationStack.push(plugin); 129 130 if (TRACE_BUNDLES == true) { 131 traceActivate(bundle.getSymbolicName(), plugin); 132 } 133 } 134 135 public void endActivation(Bundle pluginId) { 136 Stack activationStack = (Stack) activationStacks.get(Thread.currentThread()); 137 BundleStats plugin = (BundleStats) activationStack.pop(); 139 plugin.endActivation(); 140 } 141 142 private void traceActivate(String id, BundleStats plugin) { 143 try { 144 PrintWriter output = new PrintWriter(new FileOutputStream(ClassloaderStats.traceFile.getAbsolutePath(), true)); 145 try { 146 long startPosition = ClassloaderStats.traceFile.length(); 147 output.println("Activating plugin: " + id); output.println("Plugin activation stack:"); Stack activationStack = (Stack) activationStacks.get(Thread.currentThread()); 150 for (int i = activationStack.size() - 1; i >= 0; i--) 151 output.println("\t" + ((BundleStats) activationStack.get(i)).getPluginId()); output.println("Class loading stack:"); Stack classStack = ClassloaderStats.getClassStack(); 154 for (int i = classStack.size() - 1; i >= 0; i--) 155 output.println("\t" + ((ClassStats) classStack.get(i)).getClassName()); output.println("Stack trace:"); new Throwable ().printStackTrace(output); 158 plugin.setTraceStart(startPosition); 159 } finally { 160 output.close(); 161 plugin.setTraceEnd(ClassloaderStats.traceFile.length()); 162 } 163 } catch (IOException e) { 164 e.printStackTrace(); 165 } 166 } 167 168 public BundleStats findPlugin(String id) { 169 BundleStats result = (BundleStats) plugins.get(id); 170 try { 171 if (result == null) { 172 result = new BundleStats(id); 173 plugins.put(id, result); 174 } 175 } catch (IllegalAccessError e) { 176 e.printStackTrace(); 177 } 178 return result; 179 } 180 181 public BundleStats[] getPlugins() { 182 return (BundleStats[]) plugins.values().toArray(new BundleStats[plugins.size()]); 183 } 184 185 public BundleStats getPlugin(String id) { 186 return (BundleStats) plugins.get(id); 187 } 188 189 } | Popular Tags |