1 26 27 package net.sourceforge.groboutils.codecoverage.v2.logger; 28 29 import java.io.InputStream ; 30 import java.util.Properties ; 31 32 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogger; 33 import net.sourceforge.groboutils.codecoverage.v2.IChannelLoggerFactory; 34 35 36 54 public final class CoverageLogger implements ICoverageLoggerConst 55 { 56 private static final String PROP_FILE_NAME = "grobocoverage.properties"; 57 private static final String PROP_FILE_RES = '/' + PROP_FILE_NAME; 58 59 private static final boolean DEBUG = false; 60 61 private static final String FACTORY_PROP = "factory"; 62 private static final String CHANNEL_COUNT_PROP = "channel-count"; 63 private static final String LOGGER_INIT_BASE_PROP = "logger."; 64 private static final int DEFAULT_CHANNEL_COUNT = 0; 65 private static final Class DEFAULT_CHANNEL_FACTORY = 66 NoOpChannelLoggerFactory.class; 67 68 private static IChannelLogger LOGGERS[]; 73 private static short LOGGERS_COUNT = (short)0; 74 75 78 static { 79 initBase(); 80 } 81 82 83 84 85 89 public static final void cover( String classSig, short methodIndex, 90 short channel, short markIndex ) 91 { 92 if (channel >= 0 && channel < LOGGERS_COUNT) 93 { 94 LOGGERS[ (int)channel ].cover( classSig, methodIndex, markIndex ); 99 } 100 115 } 116 117 118 125 public static final void initBase() 126 { 127 LOGGERS_COUNT = (short)0; 130 131 Properties props = null; 132 try 133 { 134 InputStream is = CoverageLogger.class.getResourceAsStream( 135 PROP_FILE_RES ); 136 if (is == null) 137 { 138 is = ClassLoader.getSystemResourceAsStream( PROP_FILE_NAME ); 139 } 140 if (is != null) 141 { 142 debug( "Loading "+PROP_FILE_NAME ); 143 props = new Properties (); 144 props.load( is ); 145 } 146 else 147 { 148 error( "No resource named "+PROP_FILE_NAME+"." ); 149 } 150 } 151 catch (ThreadDeath td) 152 { 153 throw td; 155 } 156 catch (Throwable t) 157 { 158 error( t.toString() ); 159 } 161 162 if (props == null) 163 { 164 props = System.getProperties(); 165 error( "Please create and/or add the file "+PROP_FILE_NAME+ 166 " to the classpath." ); 167 if (DEBUG) 168 { 169 System.exit(1); 170 } 171 } 172 init( props ); 173 } 174 175 176 183 public static final void init( Properties props ) 184 { 185 LOGGERS_COUNT = (short)0; 188 189 if (props == null) 190 { 191 error( "Encountered null properties instance." ); 192 return; 193 } 194 195 short channelCount = getChannelCount( props ); 196 IChannelLoggerFactory factory = getChannelLoggerFactory( props ); 197 if (factory != null) 198 { 199 202 IChannelLogger ls[] = new IChannelLogger[ channelCount ]; 203 for (short i = 0; i < channelCount; ++i) 204 { 205 ls[ (int)i ] = factory. 206 createChannelLogger( LOGGER_INIT_BASE_PROP, props, i ); 207 debug( "Logger "+i+" = "+ls[(int)i] ); 208 } 209 210 LOGGERS = ls; 211 212 } 213 215 if (LOGGERS == null) 216 { 217 debug( "Logger list is null." ); 218 LOGGERS = new IChannelLogger[ 0 ]; 219 } 220 221 LOGGERS_COUNT = (short)LOGGERS.length; 222 } 223 224 225 private static final short getChannelCount( Properties props ) 226 { 227 short channelCount = DEFAULT_CHANNEL_COUNT; 228 String countStr = props.getProperty( CHANNEL_COUNT_PROP ); 229 if (countStr != null && countStr.length() > 0) 230 { 231 try 232 { 233 int i = Integer.parseInt( countStr ); 236 237 if (i < 0 || i > Short.MAX_VALUE) 238 { 239 error( "Channel count is outside range [0.."+ 240 Short.MAX_VALUE+"]. Using default." ); 241 } 242 else 243 { 244 channelCount = (short)i; 246 } 247 } 248 catch (NumberFormatException ex) 249 { 250 error( "Trouble translating channel count ('"+countStr+ 251 "') to a number. Using default." ); 252 } 253 } 254 debug( "Channel Count = "+channelCount ); 255 return channelCount; 256 } 257 258 259 private static final IChannelLoggerFactory getChannelLoggerFactory( 260 Properties props ) 261 { 262 String factoryClassName = props.getProperty( FACTORY_PROP ); 263 Class factoryClass = DEFAULT_CHANNEL_FACTORY; 264 IChannelLoggerFactory factory = null; 265 266 if (factoryClassName != null) 267 { 268 try 269 { 270 Class c = Class.forName( factoryClassName ); 273 274 factoryClass = c; 276 } 277 catch (ThreadDeath td) 278 { 279 throw td; 281 } 282 catch (Throwable t) 283 { 284 error( "Could not load factory class '"+factoryClassName+ 285 "'. Using default." ); 286 } 287 } 288 289 try 290 { 291 factory = (IChannelLoggerFactory)factoryClass.newInstance(); 292 } 293 catch (ThreadDeath td) 294 { 295 throw td; 297 } 298 catch (Throwable t) 299 { 300 error( "Couldn't create a new factory or cast it to a "+ 301 (IChannelLoggerFactory.class.getName())+ 302 ". Not using a logger." ); 303 } 304 305 debug( "Factory = "+factory ); 306 return factory; 307 } 308 309 310 311 private static final void error( String message ) 312 { 313 System.err.println( CoverageLogger.class.getName()+": "+message ); 314 } 315 316 317 private static final void debug( String message ) 318 { 319 if (DEBUG) 320 { 321 System.out.println( "DEBUG ["+ 322 CoverageLogger.class.getName()+"]: "+message ); 323 } 324 } 325 } 326 327 | Popular Tags |