KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)MinDirChannelLogger.java
3  *
4  * Copyright (C) 2003-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 import java.util.HashSet JavaDoc;
33 import java.util.Set JavaDoc;
34
35
36 /**
37  * Same as the DirectoryChannelLogger, but includes a JDK 1.2 optimization
38  * to minimize the size of the log files.
39  *
40  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
41  * @author Graf Leonardo <a HREF="mailto:leolegenie@hotmail.com">leolegenie@hotmail.com</a>
42  * @version $Date: 2004/07/07 09:39:10 $
43  * @since December 3, 2003
44  */

45 public class MinDirChannelLogger extends DirectoryChannelLogger
46 {
47     private final Set JavaDoc covered = new HashSet JavaDoc( 10000, 0.75f );
48     
49     
50     public MinDirChannelLogger( File JavaDoc baseDir )
51     {
52         super( baseDir );
53     }
54     
55     
56     /**
57      * Records a coverage of a marked bytecode instruction. This method should
58      * never throw an exception.
59      *
60      * @param classSignature a signature of the class file being covered.
61      * this signature includes the fully-qualified name of the class,
62      * along with a checksum to uniquely identify it.
63      * @param methodIndex index for a method within the class. The meta-data
64      * store will know how to translate the index to a method signature.
65      * @param markIndex the index of the bytecode instruction mark for this
66      * particular channel.
67      */

68     public void cover( String JavaDoc classSignature, short methodIndex,
69             short markIndex )
70     {
71         if (this.baseDir != null)
72         {
73             char[] out = createCoverString( methodIndex, markIndex );
74             
75             // optimize
76
// String key = classSignature + '.' + out;
77
int csl = classSignature.length();
78             char cs[] = new char[ csl + 3 ];
79             classSignature.getChars( 0, csl, cs, 0 );
80             cs[csl] = '-';
81             cs[csl+1] = (char)methodIndex;
82             cs[csl+2] = (char)markIndex;
83             String JavaDoc key = new String JavaDoc( cs );
84             
85             synchronized (this)
86             {
87                 // access to covered needs to be inside the synchronized block
88
if (!covered.contains( key ))
89                 {
90                     covered.add( key );
91                     
92                     File JavaDoc f = getClassFile( this.baseDir, classSignature );
93                     FileWriter JavaDoc fw = null;
94                     try
95                     {
96                         fw = new FileWriter JavaDoc( f.toString(), true );
97                         try
98                         {
99                             fw.write( out );
100                             fw.flush();
101                         }
102                         finally
103                         {
104                             fw.close();
105                         }
106                     }
107                     catch (IOException JavaDoc ioe)
108                     {
109                         // don't throw this exception outside this block.
110
ioe.printStackTrace();
111                         
112                         // prevent these errors from occuring again
113
this.baseDir = null;
114                     }
115                 }
116             }
117         }
118     }
119 }
120
121
Popular Tags