KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > stats > StatsManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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     // This connect plugins and their info, and so allows to access the info without running through
22
// the plugin registry. This map only contains activated plugins. The key is the plugin Id
23
private Hashtable plugins = new Hashtable(20);
24     private Map activationStacks = new HashMap(5);
25 // private Stack activationStack = new Stack(); // a stack of the plugins being activated
26
private static boolean booting = true; // the state of the platform. This value is changed by the InternalPlatform itself.
27

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 JavaDoc TRACE_FILENAME = "runtime.traces"; //$NON-NLS-1$
34
public static String JavaDoc TRACE_FILTERS = "trace.properties"; //$NON-NLS-1$
35
public static boolean TRACE_CLASSES = false;
36     public static boolean TRACE_BUNDLES = false;
37     public static final String JavaDoc FRAMEWORK_SYMBOLICNAME = "org.eclipse.osgi"; //$NON-NLS-1$
38

39     //Option names for spies
40
private static final String JavaDoc OPTION_MONITOR_ACTIVATION = FRAMEWORK_SYMBOLICNAME + "/monitor/activation"; //$NON-NLS-1$
41
private static final String JavaDoc OPTION_MONITOR_CLASSES = FRAMEWORK_SYMBOLICNAME + "/monitor/classes"; //$NON-NLS-1$
42
private static final String JavaDoc OPTION_MONITOR_RESOURCES = FRAMEWORK_SYMBOLICNAME + "/monitor/resources"; //$NON-NLS-1$
43
private static final String JavaDoc OPTION_TRACE_BUNDLES = FRAMEWORK_SYMBOLICNAME + "/trace/activation"; //$NON-NLS-1$
44
private static final String JavaDoc OPTION_TRACE_CLASSES = FRAMEWORK_SYMBOLICNAME + "/trace/classLoading"; //$NON-NLS-1$
45
private static final String JavaDoc OPTION_TRACE_FILENAME = FRAMEWORK_SYMBOLICNAME + "/trace/filename"; //$NON-NLS-1$
46
private static final String JavaDoc OPTION_TRACE_FILTERS = FRAMEWORK_SYMBOLICNAME + "/trace/filters"; //$NON-NLS-1$
47

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         // may be null if debugging is not enabled
63
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     /**
83      * Returns the result of converting a list of comma-separated tokens into an array
84      *
85      * @return the array of string tokens
86      * @param prop the initial comma-separated string
87      */

88     public static String JavaDoc[] getArrayFromList(String JavaDoc prop) {
89         if (prop == null || prop.trim().equals("")) //$NON-NLS-1$
90
return new String JavaDoc[0];
91         Vector list = new Vector();
92         StringTokenizer tokens = new StringTokenizer(prop, ","); //$NON-NLS-1$
93
while (tokens.hasMoreTokens()) {
94             String JavaDoc token = tokens.nextToken().trim();
95             if (!token.equals("")) //$NON-NLS-1$
96
list.addElement(token);
97         }
98         return list.isEmpty() ? new String JavaDoc[0] : (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
99     }
100
101     private void initialize() {
102         // add the system bundle
103
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         // should be called from a synchronized location to protect against concurrent updates
111
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         // set the parentage of activation
123
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         // should be called from a synchronized location to protect against concurrent updates
138
BundleStats plugin = (BundleStats) activationStack.pop();
139         plugin.endActivation();
140     }
141
142     private void traceActivate(String JavaDoc 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); //$NON-NLS-1$
148
output.println("Plugin activation stack:"); //$NON-NLS-1$
149
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()); //$NON-NLS-1$
152
output.println("Class loading stack:"); //$NON-NLS-1$
153
Stack classStack = ClassloaderStats.getClassStack();
154                 for (int i = classStack.size() - 1; i >= 0; i--)
155                     output.println("\t" + ((ClassStats) classStack.get(i)).getClassName()); //$NON-NLS-1$
156
output.println("Stack trace:"); //$NON-NLS-1$
157
new Throwable JavaDoc().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 JavaDoc 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 JavaDoc 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 JavaDoc id) {
186         return (BundleStats) plugins.get(id);
187     }
188
189 }
Popular Tags