KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > logger > CacheDirChannelLoggerFactory


1 /*
2  * @(#)CacheDirChannelLoggerFactory.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.logger;
28
29 import java.io.File JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogger;
33 import net.sourceforge.groboutils.codecoverage.v2.IChannelLoggerFactory;
34
35
36 /**
37  * This is the same as the <tt>DirectoryChannelLoggerFactory</tt>, except
38  * that it caches the files as open.
39  *
40  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
41  * @version $Date: 2004/04/21 02:08:44 $
42  * @since May 9, 2003
43  */

44 public class CacheDirChannelLoggerFactory implements IChannelLoggerFactory
45 {
46     public static final String JavaDoc DIRECTORY_PROPERTY = "dir";
47     public static final String JavaDoc DEFAULT_DIRECTORY = "./.cache-cover-logs";
48     
49     public static final String JavaDoc CACHESIZE_PROPERTY = "cache-size";
50     public static final int DEFAULT_CACHESIZE = 25;
51     
52     /**
53      * Creates a specific logger type. Initializes the logger based on the
54      * given collection of properties.
55      *
56      * @param propertyPrefix the prefix that all logger properties will begin
57      * with. Expect all logger-specific properties to be appended directly
58      * to this string.
59      * @param props the property collection to pull the logger properties from.
60      * @param channelIndex channel number to log to.
61      * @return the initialized logger.
62      */

63     public IChannelLogger createChannelLogger(
64             String JavaDoc propertyPrefix, Properties JavaDoc props, short channelIndex )
65     {
66         String JavaDoc directory = getDirectory( propertyPrefix, props );
67         int cacheSize = getCacheSize( propertyPrefix, props );
68         
69         File JavaDoc dir = new File JavaDoc( 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 JavaDoc getDirectory( String JavaDoc propertyPrefix, Properties JavaDoc props )
88     {
89         String JavaDoc 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 JavaDoc propertyPrefix, Properties JavaDoc props )
100     {
101         String JavaDoc 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 JavaDoc 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