KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)FileSingleSourceLoggerFactory.java
3  *
4  * Copyright (C) 2004 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.io.FileWriter JavaDoc;
31 import java.io.Writer JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import java.util.Properties JavaDoc;
35
36
37 /**
38  * Output the shared marks to a file.
39  * <P>
40  * Since this outputs to a single file, this has a potential issue
41  * when multiple loggers are trying to access the same file. This
42  * could happen if the CoverageLogger class is loaded by different class
43  * loaders; as a result, multiple CoverageLogger instances will be created,
44  * allowing for multiple channel loggers to write to the same area.
45  * <P>
46  * We resolve this issue by adding a pseudo-random value to the end of
47  * the file name.
48  *
49  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
50  * @version $Date: 2004/07/07 09:39:10 $
51  * @since April 16, 2004
52  */

53 public class FileSingleSourceLoggerFactory extends AbstractSingleSourceLoggerFactory
54 {
55     public static final String JavaDoc DIRECTORY_PROPERTY = "dir";
56     public static final String JavaDoc FILENAME_PROPERTY = "file";
57     private static final String JavaDoc DEFAULT_FILENAME_PREFIX = "single.";
58     private static final String JavaDoc FILENAME_SUFFIX = ".log";
59     
60     private File JavaDoc outfile;
61     
62     /**
63      * Setup the source writer. This can return <tt>null</tt>. Its
64      * actions will be synchronized for you.
65      */

66     protected Writer JavaDoc setupSource()
67     {
68         setReloadSourceAfterError( false );
69         
70         try
71         {
72 //System.out.println(":: Creating FileWriter for "+outfile.getAbsolutePath());
73
return new FileWriter JavaDoc( this.outfile );
74         }
75         catch (IOException JavaDoc e)
76         {
77             e.printStackTrace();
78             return null;
79         }
80     }
81     
82     
83     /**
84      * Extend this method to setup your own properties. Be sure to
85      * call the super's setupProps, though. Be sure to keep the
86      * properties around for how to connect to the source.
87      */

88     protected void setupProps( String JavaDoc prefix, Properties JavaDoc props )
89     {
90         super.setupProps( prefix, props );
91         
92         // fix the filename issue by adding a random value to the
93
// name
94
String JavaDoc pseudoRandom = Long.toString(
95             System.currentTimeMillis() ) + Long.toString(
96             Math.round( Math.random() * 1000.0 ) ) +
97             FILENAME_SUFFIX;
98         
99         // the filename property should only really be used for debugging
100
// purposes, as it invalidates the "single." prefix that the
101
// report filter looks for.
102
String JavaDoc filename = props.getProperty( prefix + FILENAME_PROPERTY );
103         if (filename != null)
104         {
105             this.outfile = new File JavaDoc( filename + pseudoRandom );
106         }
107         else
108         {
109             String JavaDoc dir = props.getProperty( prefix + DIRECTORY_PROPERTY );
110             if (dir != null)
111             {
112                 this.outfile = new File JavaDoc( dir,
113                     DEFAULT_FILENAME_PREFIX + pseudoRandom );
114             }
115         }
116         
117         if (this.outfile == null)
118         {
119             this.outfile = new File JavaDoc( DEFAULT_FILENAME_PREFIX + pseudoRandom );
120         }
121         
122         File JavaDoc parent = this.outfile.getParentFile();
123         if (!parent.exists())
124         {
125             parent.mkdirs();
126         }
127     }
128 }
129
130
Popular Tags