1 26 27 package net.sourceforge.groboutils.codecoverage.v2.logger; 28 29 import java.io.File ; 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 44 public class CacheDirChannelLoggerFactory implements IChannelLoggerFactory 45 { 46 public static final String DIRECTORY_PROPERTY = "dir"; 47 public static final String DEFAULT_DIRECTORY = "./.cache-cover-logs"; 48 49 public static final String CACHESIZE_PROPERTY = "cache-size"; 50 public static final int DEFAULT_CACHESIZE = 25; 51 52 63 public IChannelLogger createChannelLogger( 64 String propertyPrefix, Properties props, short channelIndex ) 65 { 66 String directory = getDirectory( propertyPrefix, props ); 67 int cacheSize = getCacheSize( propertyPrefix, props ); 68 69 File dir = new File ( directory, Short.toString( channelIndex ) ); 70 if (dir.exists()) 71 { 72 if (!dir.isDirectory()) 73 { 74 System.err.println( 75 "DirectoryLogger base directory is a file."); 76 dir = null; 77 } 78 } 79 else 80 { 81 dir.mkdirs(); 82 } 83 return new CacheDirChannelLogger( dir, cacheSize ); 84 } 85 86 87 protected String getDirectory( String propertyPrefix, Properties props ) 88 { 89 String directory = props.getProperty( 90 propertyPrefix + DIRECTORY_PROPERTY ); 91 if (directory == null) 92 { 93 directory = DEFAULT_DIRECTORY; 94 } 95 return directory; 96 } 97 98 99 protected int getCacheSize( String propertyPrefix, Properties props ) 100 { 101 String csS = props.getProperty( 102 propertyPrefix + CACHESIZE_PROPERTY ); 103 int cs = DEFAULT_CACHESIZE; 104 if (csS != null) 105 { 106 try 107 { 108 int i = Integer.parseInt( csS ); 109 if (i > 0) 110 { 111 cs = i; 112 } 113 } 114 catch (NumberFormatException e) 115 { 116 System.out.println("Bad cache size property value: "+ 117 csS ); 118 } 119 } 120 121 return cs; 122 } 123 } 124 125 | Popular Tags |