1 9 package com.vladium.emma.rt; 10 11 import java.io.File ; 12 13 import com.vladium.logging.Logger; 14 import com.vladium.util.IProperties; 15 import com.vladium.util.Property; 16 import com.vladium.util.exit.ExitHookManager; 17 import com.vladium.emma.IAppConstants; 18 import com.vladium.emma.EMMAProperties; 19 import com.vladium.emma.data.ICoverageData; 20 import com.vladium.emma.data.DataFactory; 21 22 26 public 27 abstract class RT implements IAppConstants 28 { 29 31 32 public static synchronized ICoverageData reset (final boolean createCoverageData, final boolean createExitHook) 33 { 34 36 39 ClassLoader loader = RT.class.getClassLoader (); 40 if (loader == null) loader = ClassLoader.getSystemClassLoader (); 41 42 IProperties appProperties = null; 43 try 44 { 45 appProperties = EMMAProperties.getAppProperties (loader); 46 } 47 catch (Throwable t) 48 { 49 t.printStackTrace (System.out); 51 } 52 s_appProperties = appProperties; 53 54 55 if (EXIT_HOOK_MANAGER != null) 56 { 57 59 if (s_exitHook != null) 60 { 61 64 EXIT_HOOK_MANAGER.removeExitHook (s_exitHook); 65 s_exitHook = null; 66 } 67 } 68 69 ICoverageData cdata = s_cdata; if (createCoverageData) 71 { 72 cdata = DataFactory.newCoverageData (); 73 s_cdata = cdata; 74 } 75 else 76 { 77 s_cdata = null; 78 } 79 80 if (EXIT_HOOK_MANAGER != null) 81 { 82 if (createExitHook && (cdata != null)) 83 { 84 final Runnable exitHook = new RTExitHook (RT.class, cdata, getCoverageOutFile (), getCoverageOutMerge ()); 85 86 92 RTExitHook.createClassLoaderClosure (); 93 94 if (EXIT_HOOK_MANAGER.addExitHook (exitHook)) 95 { 96 s_exitHook = exitHook; 97 } 98 } 100 } 101 102 return cdata; 103 } 104 105 public static void r (final boolean [][] coverage, final String classVMName, final long stamp) 106 { 107 113 final ICoverageData cdata = getCoverageData (); 115 117 if (cdata != null) 118 { 119 synchronized (cdata.lock ()) 120 { 121 126 cdata.addClass (coverage, classVMName, stamp); 127 } 128 } 129 } 130 131 public static synchronized ICoverageData getCoverageData () 132 { 133 return s_cdata; 134 } 135 136 public static synchronized IProperties getAppProperties () 137 { 138 return s_appProperties; 139 } 140 141 148 public static synchronized void dumpCoverageData (File outFile, final boolean merge, final boolean stopDataCollection) 149 { 150 if (DEBUG) System.out.println ("RT::dumpCoverageData() DUMPING " + RT.class.getClassLoader ()); 151 outFile = outFile != null ? outFile : getCoverageOutFile (); 152 153 ICoverageData cdata = s_cdata; if (stopDataCollection) s_cdata = null; 156 RTCoverageDataPersister.dumpCoverageData (cdata, ! stopDataCollection, outFile, merge); 157 } 158 159 public static synchronized void dumpCoverageData (File outFile, final boolean stopDataCollection) 160 { 161 outFile = outFile != null ? outFile : getCoverageOutFile (); 162 163 ICoverageData cdata = s_cdata; if (stopDataCollection) s_cdata = null; 166 RTCoverageDataPersister.dumpCoverageData (cdata, ! stopDataCollection, outFile, getCoverageOutMerge ()); 167 } 168 169 171 173 175 176 private RT () {} 178 179 private static File getCoverageOutFile () 180 { 181 final IProperties appProperties = getAppProperties (); if (appProperties != null) 183 { 184 final String property = appProperties.getProperty (EMMAProperties.PROPERTY_COVERAGE_DATA_OUT_FILE, 185 EMMAProperties.DEFAULT_COVERAGE_DATA_OUT_FILE); 186 return new File (property); 187 } 188 189 return new File (EMMAProperties.DEFAULT_COVERAGE_DATA_OUT_FILE); 190 } 191 192 private static boolean getCoverageOutMerge () 193 { 194 final IProperties appProperties = getAppProperties (); if (appProperties != null) 196 { 197 199 final String property = appProperties.getProperty (EMMAProperties.PROPERTY_COVERAGE_DATA_OUT_MERGE, 200 EMMAProperties.DEFAULT_COVERAGE_DATA_OUT_MERGE.toString ()); 201 return Property.toBoolean (property); 202 } 203 204 return EMMAProperties.DEFAULT_COVERAGE_DATA_OUT_MERGE.booleanValue (); 205 } 206 207 208 private static ICoverageData s_cdata; 209 private static Runnable s_exitHook; 210 private static IProperties s_appProperties; 212 private static final ExitHookManager EXIT_HOOK_MANAGER; 214 private static final boolean DEBUG = false; 215 216 static 217 { 218 if (DEBUG) System.out.println ("RT[" + System.identityHashCode (RT.class) + "]::<clinit>: loaded by " + RT.class.getClassLoader ()); 219 220 ExitHookManager temp = null; 221 try 222 { 223 temp = ExitHookManager.getSingleton (); 224 } 225 catch (Throwable t) 226 { 227 t.printStackTrace (System.out); 229 } 230 EXIT_HOOK_MANAGER = temp; 231 232 233 if (RTSettings.isStandaloneMode ()) 234 { 235 if (DEBUG) System.out.println ("RT::<clinit>: STANDALONE MODE"); 236 237 reset (true, true); 239 240 final Logger log = Logger.getLogger (); 242 if (log.atINFO ()) 243 { 244 log.info ("collecting runtime coverage data ..."); 245 } 246 } 247 else 248 { 249 reset (false, false); 251 } 252 } 253 254 } | Popular Tags |