1 11 package org.eclipse.osgi.framework.stats; 12 13 import java.io.*; 14 import java.util.*; 15 16 20 public class ClassloaderStats { 21 private String id; 22 private long loadingTime; private int failureCount = 0; 27 private Map classes = Collections.synchronizedMap(new HashMap(20)); 28 private ArrayList bundles = new ArrayList(2); 30 private boolean keepTraces = false; 32 private static ArrayList packageFilters = new ArrayList(4); private static Set pluginFilters = new HashSet(5); 36 private static Stack classStack = new Stack(); 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 String filename = StatsManager.TRACE_FILENAME; 51 traceFile = new File(filename); 52 traceFile.delete(); 53 54 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() + "..."); InputStream input = new FileInputStream(filterFile); 64 System.out.println(" Loaded."); Properties filters = new Properties() { 66 public Object put(Object key, Object value) { 67 addFilters((String ) key, (String ) value); 68 return null; 69 } 70 }; 71 try { 72 filters.load(input); 73 } finally { 74 input.close(); 75 } 76 } catch (IOException e) { 77 System.out.println(" No trace filters loaded."); } 79 } 80 81 protected static void addFilters(String key, String value) { 82 String [] filters = StatsManager.getArrayFromList(value); 83 if ("plugins".equals(key)) pluginFilters.addAll(Arrays.asList(filters)); 85 if ("packages".equals(key)) packageFilters.addAll(Arrays.asList(filters)); 87 } 88 89 public static void startLoadingClass(String id, String className) { 90 findLoader(id).startLoadClass(className); 91 } 92 93 private static ClassloaderStats findLoader(String id) { 95 synchronized (loaders) { 96 ClassloaderStats result = (ClassloaderStats) loaders.get(id); 97 if (result == null) { 98 result = new ClassloaderStats(id); 99 loaders.put(id, result); 100 } 101 return result; 102 } 103 } 104 105 public static Stack getClassStack() { 106 return classStack; 107 } 108 109 public static ClassloaderStats[] getLoaders() { 110 return (ClassloaderStats[]) loaders.values().toArray(new ClassloaderStats[0]); 113 } 114 115 public static void endLoadingClass(String id, String className, boolean success) { 116 findLoader(id).endLoadClass(className, success); 117 } 118 119 public static void loadedBundle(String id, ResourceBundleStats info) { 120 findLoader(id).loadedBundle(info); 121 } 122 123 public static ClassloaderStats getLoader(String id) { 124 return (ClassloaderStats) loaders.get(id); 125 } 126 127 public ClassloaderStats(String id) { 128 this.id = id; 129 keepTraces = pluginFilters.contains(id); 130 } 131 132 public void addBaseClasses(String [] baseClasses) { 133 for (int i = 0; i < baseClasses.length; i++) { 134 String name = baseClasses[i]; 135 if (classes.get(name) == null) { 136 ClassStats value = new ClassStats(name, this); 137 value.toBaseClass(); 138 classes.put(name, value); 139 } 140 } 141 } 142 143 private void loadedBundle(ResourceBundleStats bundle) { 144 bundles.add(bundle); 145 } 146 147 public ArrayList getBundles() { 148 return bundles; 149 } 150 151 private synchronized void startLoadClass(String name) { 152 classStack.push(findClass(name)); 153 } 154 155 private ClassStats findClass(String name) { 157 ClassStats result = (ClassStats) classes.get(name); 158 return result == null ? new ClassStats(name, this) : result; 159 } 160 161 private synchronized void endLoadClass(String name, boolean success) { 162 ClassStats current = (ClassStats) classStack.pop(); 163 if (!success) { 164 failureCount++; 165 return; 166 } 167 if (current.getLoadOrder() >= 0) 168 return; 169 170 classes.put(name, current); 171 current.setLoadOrder(classes.size()); 172 current.loadingDone(); 173 traceLoad(name, current); 174 175 if (classStack.size() != 0) { 177 ClassStats previous = ((ClassStats) classStack.peek()); 179 previous.addTimeLoadingOthers(current.getTimeLoading()); 180 current.setLoadedBy(previous); 181 previous.loaded(current); 182 } else { 183 loadingTime = loadingTime + current.getTimeLoading(); 184 } 185 } 186 187 private void traceLoad(String name, ClassStats target) { 188 if (!keepTraces) { 190 boolean found = false; 191 for (int i = 0; !found && i < packageFilters.size(); i++) 192 if (name.startsWith((String ) packageFilters.get(i))) 193 found = true; 194 if (!found) 195 return; 196 } 197 198 try { 200 target.setTraceStart(traceFile.length()); 201 PrintWriter output = new PrintWriter(new FileOutputStream(traceFile.getAbsolutePath(), true)); 202 try { 203 output.println("Loading class: " + name); output.println("Class loading stack:"); output.println("\t" + name); for (int i = classStack.size() - 1; i >= 0; i--) 207 output.println("\t" + ((ClassStats) classStack.get(i)).getClassName()); output.println("Stack trace:"); new Throwable ().printStackTrace(output); 210 } finally { 211 output.close(); 212 } 213 target.setTraceEnd(traceFile.length()); 214 } catch (FileNotFoundException e) { 215 e.printStackTrace(); 216 } 217 } 218 219 public int getClassLoadCount() { 220 return classes.size(); 221 } 222 223 public long getClassLoadTime() { 224 return loadingTime; 225 } 226 227 public ClassStats[] getClasses() { 228 return (ClassStats[]) classes.values().toArray(new ClassStats[0]); 231 } 232 233 public String getId() { 234 return id; 235 } 236 } | Popular Tags |