KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > internal > stats > ClassloaderStats


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

11 package org.eclipse.core.runtime.internal.stats;
12
13 import java.io.*;
14 import java.util.*;
15
16 /**
17  * Contains information about the classes and the bundles loaded by a given classloader. Typically there is one classloader per plugin so at levels above boot, this equates to information about
18  * classes and bundles in a plugin.
19  */

20 public class ClassloaderStats {
21     private String JavaDoc id;
22     private long loadingTime; // time spent loading classes
23
private int failureCount = 0; // number of classes requested but that we fail to provide
24
/**
25      * classes loaded by the plugin (key: class name, value: ClassStats)
26      */

27     private Map classes = Collections.synchronizedMap(new HashMap(20));
28     private ArrayList bundles = new ArrayList(2); // bundles loaded
29

30     private boolean keepTraces = false; // indicate whether or not the traces of classes loaded are kept
31

32     // filters to indicate which classes we want to keep the traces
33
private static ArrayList packageFilters = new ArrayList(4); // filters on a package basis
34
private static Set pluginFilters = new HashSet(5); // filters on a plugin basis
35

36     private static Hashtable classStacks = new Hashtable(); // represents the classes that are currently being loaded
37
/**
38      * a dictionary of the classloaderStats (key: pluginId, value: ClassloaderStats)
39      */

40     private static Map loaders = Collections.synchronizedMap(new HashMap(20));
41     public static File traceFile;
42
43     static {
44         if (StatsManager.TRACE_CLASSES || StatsManager.TRACE_BUNDLES)
45             initializeTraceOptions();
46     }
47
48     private static void initializeTraceOptions() {
49         // create the trace file
50
String JavaDoc filename = StatsManager.TRACE_FILENAME;
51         traceFile = new File(filename);
52         traceFile.delete();
53
54         //load the filters
55
if (!StatsManager.TRACE_CLASSES)
56             return;
57         filename = StatsManager.TRACE_FILTERS;
58         if (filename == null || filename.length() == 0)
59             return;
60         try {
61             File filterFile = new File(filename);
62             System.out.print("Runtime tracing elements defined in: " + filterFile.getAbsolutePath() + "..."); //$NON-NLS-1$ //$NON-NLS-2$
63
InputStream input = new FileInputStream(filterFile);
64             System.out.println(" Loaded."); //$NON-NLS-1$
65
Properties filters = new Properties() {
66                 private static final long serialVersionUID = 3546359543853365296L;
67
68                 public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
69                     addFilters((String JavaDoc) key, (String JavaDoc) value);
70                     return null;
71                 }
72             };
73             try {
74                 filters.load(input);
75             } finally {
76                 input.close();
77             }
78         } catch (IOException e) {
79             System.out.println(" No trace filters loaded."); //$NON-NLS-1$
80
}
81     }
82
83     protected static void addFilters(String JavaDoc key, String JavaDoc value) {
84         String JavaDoc[] filters = StatsManager.getArrayFromList(value);
85         if ("plugins".equals(key)) //$NON-NLS-1$
86
pluginFilters.addAll(Arrays.asList(filters));
87         if ("packages".equals(key)) //$NON-NLS-1$
88
packageFilters.addAll(Arrays.asList(filters));
89     }
90
91     public static void startLoadingClass(String JavaDoc id, String JavaDoc className) {
92         findLoader(id).startLoadClass(className);
93     }
94
95     // get and create if does not exist
96
private static ClassloaderStats findLoader(String JavaDoc id) {
97         synchronized (loaders) {
98             ClassloaderStats result = (ClassloaderStats) loaders.get(id);
99             if (result == null) {
100                 result = new ClassloaderStats(id);
101                 loaders.put(id, result);
102             }
103             return result;
104         }
105     }
106
107     public static synchronized Stack getClassStack() {
108         Stack result = (Stack) classStacks.get(Thread.currentThread());
109         if (result == null) {
110             result = new Stack();
111             classStacks.put(Thread.currentThread(), result);
112         }
113         return result;
114     }
115
116     public static ClassloaderStats[] getLoaders() {
117         //the parameter to toArray is of size zero for thread safety, otherwise this
118
//could return an array with null entries if the map shrinks concurrently
119
return (ClassloaderStats[]) loaders.values().toArray(new ClassloaderStats[0]);
120     }
121
122     public static void endLoadingClass(String JavaDoc id, String JavaDoc className, boolean success) {
123         findLoader(id).endLoadClass(className, success);
124     }
125
126     public static void loadedBundle(String JavaDoc id, ResourceBundleStats info) {
127         findLoader(id).loadedBundle(info);
128     }
129
130     public static ClassloaderStats getLoader(String JavaDoc id) {
131         return (ClassloaderStats) loaders.get(id);
132     }
133
134     public ClassloaderStats(String JavaDoc id) {
135         this.id = id;
136         keepTraces = pluginFilters.contains(id);
137     }
138
139     public void addBaseClasses(String JavaDoc[] baseClasses) {
140         for (int i = 0; i < baseClasses.length; i++) {
141             String JavaDoc name = baseClasses[i];
142             if (classes.get(name) == null) {
143                 ClassStats value = new ClassStats(name, this);
144                 value.toBaseClass();
145                 classes.put(name, value);
146             }
147         }
148     }
149
150     private void loadedBundle(ResourceBundleStats bundle) {
151         bundles.add(bundle);
152     }
153
154     public ArrayList getBundles() {
155         return bundles;
156     }
157
158     private synchronized void startLoadClass(String JavaDoc name) {
159         getClassStack().push(findClass(name));
160     }
161
162     // internal method that return the existing classStats or creates one
163
private ClassStats findClass(String JavaDoc name) {
164         ClassStats result = (ClassStats) classes.get(name);
165         return result == null ? new ClassStats(name, this) : result;
166     }
167
168     private synchronized void endLoadClass(String JavaDoc name, boolean success) {
169         ClassStats current = (ClassStats) getClassStack().pop();
170         if (!success) {
171             failureCount++;
172             return;
173         }
174         if (current.getLoadOrder() >= 0)
175             return;
176
177         classes.put(name, current);
178         current.setLoadOrder(classes.size());
179         current.loadingDone();
180         traceLoad(name, current);
181
182         // is there something on the load stack. if so, link them together...
183
Stack classStack = getClassStack();
184         if (classStack.size() != 0) {
185             // get the time spent loading cli and subtract its load time from the class that requires loading
186
ClassStats previous = ((ClassStats) classStack.peek());
187             previous.addTimeLoadingOthers(current.getTimeLoading());
188             current.setLoadedBy(previous);
189             previous.loaded(current);
190         } else {
191             loadingTime = loadingTime + current.getTimeLoading();
192         }
193     }
194
195     private void traceLoad(String JavaDoc name, ClassStats target) {
196         // Stack trace code
197
if (!keepTraces) {
198             boolean found = false;
199             for (int i = 0; !found && i < packageFilters.size(); i++)
200                 if (name.startsWith((String JavaDoc) packageFilters.get(i)))
201                     found = true;
202             if (!found)
203                 return;
204         }
205
206         // Write the stack trace. The position in the file are set to the corresponding classStat object
207
try {
208             target.setTraceStart(traceFile.length());
209             PrintWriter output = new PrintWriter(new FileOutputStream(traceFile.getAbsolutePath(), true));
210             try {
211                 output.println("Loading class: " + name); //$NON-NLS-1$
212
output.println("Class loading stack:"); //$NON-NLS-1$
213
output.println("\t" + name); //$NON-NLS-1$
214
Stack classStack = getClassStack();
215                 for (int i = classStack.size() - 1; i >= 0; i--)
216                     output.println("\t" + ((ClassStats) classStack.get(i)).getClassName()); //$NON-NLS-1$
217
output.println("Stack trace:"); //$NON-NLS-1$
218
new Throwable JavaDoc().printStackTrace(output);
219             } finally {
220                 output.close();
221             }
222             target.setTraceEnd(traceFile.length());
223         } catch (FileNotFoundException e) {
224             e.printStackTrace();
225         }
226     }
227
228     public int getClassLoadCount() {
229         return classes.size();
230     }
231
232     public long getClassLoadTime() {
233         return loadingTime;
234     }
235
236     public ClassStats[] getClasses() {
237         //the parameter to toArray is of size zero for thread safety, otherwise this
238
//could return an array with null entries if the map shrinks concurrently
239
return (ClassStats[]) classes.values().toArray(new ClassStats[0]);
240     }
241
242     public String JavaDoc getId() {
243         return id;
244     }
245 }
246
Popular Tags