KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DirectoryChannelLogger.java
3  *
4  * Copyright (C) 2002-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.IOException JavaDoc;
32
33 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogger;
34
35
36 /**
37  * Logs coverage reports to a directory of logs. The directories are split
38  * by the channel index, and the directory contains one log per class file
39  * analyzed.
40  * <p>
41  * As of 2004-Jun-3, the output format has changed to:
42  * <PRE>
43  * <i>method index</i> <i>mark index</i> EOL
44  * </PRE>
45  * where both indices are output in hexidecimal format. This is for performance
46  * reasons.
47  *
48  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
49  * @version $Date: 2004/07/07 09:39:10 $
50  * @since December 17, 2002
51  */

52 public class DirectoryChannelLogger implements IChannelLogger
53 {
54     public static final String JavaDoc CLASS_LOG_EXTENTION = ".class.log";
55     
56     protected File JavaDoc baseDir;
57     
58     
59     public DirectoryChannelLogger( File JavaDoc baseDir )
60     {
61         // basedir can be null!
62
this.baseDir = baseDir;
63         if (baseDir == null)
64         {
65             System.err.println("DirectoryLogger base directory is null.");
66         }
67         else
68         {
69             if (!baseDir.exists())
70             {
71                 baseDir.mkdir();
72             }
73         }
74     }
75     
76     
77     /**
78      * Records a coverage of a marked bytecode instruction. This method should
79      * never throw an exception.
80      *
81      * @param classSignature a signature of the class file being covered.
82      * this signature includes the fully-qualified name of the class,
83      * along with a checksum to uniquely identify it.
84      * @param methodIndex index for a method within the class. The meta-data
85      * store will know how to translate the index to a method signature.
86      * @param markIndex the index of the bytecode instruction mark for this
87      * particular channel.
88      */

89     public void cover( String JavaDoc classSignature, short methodIndex,
90             short markIndex )
91     {
92         if (this.baseDir != null)
93         {
94 //System.err.println("DirectoryChannelLogger: "+classSignature+":"+methodIndex+"."+markIndex);
95
File JavaDoc f = getClassFile( this.baseDir, classSignature );
96             FileWriter JavaDoc fw = null;
97             try
98             {
99                 char[] out = createCoverString( methodIndex, markIndex );
100                 synchronized (this)
101                 {
102                     fw = new FileWriter JavaDoc( f.toString(), true );
103                     fw.write( out );
104                     fw.flush();
105 //System.out.println("** wrote ["+out+"] to "+f.getAbsolutePath());
106
}
107             }
108             catch (IOException JavaDoc ioe)
109             {
110                 // gasp!!!!
111
ioe.printStackTrace();
112                 
113                 // prevent these errors from occuring again
114
this.baseDir = null;
115             }
116             finally
117             {
118                 if (fw != null)
119                 {
120                     try
121                     {
122                         fw.close();
123                     }
124                     catch (IOException JavaDoc ioe)
125                     {
126                         // gasp again!
127
ioe.printStackTrace();
128                         
129                         // this this happened on a close, I'll ignore it
130
}
131                 }
132             }
133         }
134     }
135     
136     
137     private static final char[] HEX = {
138         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
139     };
140     /**
141      * Make static final so that the invocation time is minimized.
142      * <p>
143      * This now returns a character array, for performance reasons. The
144      * array's format is in hexidecimal.
145      */

146     protected static final char[] createCoverString(
147             short methodIndex, short markIndex )
148     {
149         char c[] = new char[10];
150         c[9] = '\n';
151         
152         // avoid the integer promotion later on,
153
// and do the masking with the HEX index lookup
154
int imeth = (int)methodIndex;
155         int imark = (int)markIndex;
156         
157         // unroll the loop
158

159         // make sure we do a bitwise shift, not an arithmetic shift.
160
c[8] = HEX[ imark & 0xf ]; imark >>>= 4;
161         c[7] = HEX[ imark & 0xf ]; imark >>>= 4;
162         c[6] = HEX[ imark & 0xf ]; imark >>>= 4;
163         c[5] = HEX[ imark & 0xf ];
164         
165         c[4] = ' ';
166         
167         c[3] = HEX[ imeth & 0xf ]; imeth >>>= 4;
168         c[2] = HEX[ imeth & 0xf ]; imeth >>>= 4;
169         c[1] = HEX[ imeth & 0xf ]; imeth >>>= 4;
170         c[0] = HEX[ imeth & 0xf ];
171         
172         return c;
173     }
174     
175     
176     /**
177      * Make static final so that the invocation time is minimized.
178      */

179     protected static final File JavaDoc getClassFile( File JavaDoc basedir,
180             String JavaDoc classSignature )
181     {
182         return new File JavaDoc( basedir, classSignature + CLASS_LOG_EXTENTION );
183     }
184 }
185
186
Popular Tags