KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)CacheDirChannelLogger.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 net.sourceforge.groboutils.util.datastruct.v1.HashCache;
30
31 import java.io.BufferedWriter JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.Writer JavaDoc;
36
37
38 /**
39  * This is the same as the DirectoryChannelLogger, except that the open
40  * files are cached for access.
41  * <P>
42  * Besides some other issues with this (see open bugs), it can cause
43  * issues if multiple loggers are trying to access the same file. This
44  * could happen if the CoverageLogger class is loaded by different class
45  * loaders; as a result, multiple CoverageLogger instances will be created,
46  * allowing for multiple channel loggers to write to the same area.
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 May 9, 2003
51  */

52 public class CacheDirChannelLogger extends DirectoryChannelLogger
53         implements Runnable JavaDoc, HashCache.ObjectManager
54 {
55     private final HashCache openedFiles;
56     
57     
58     public CacheDirChannelLogger( File JavaDoc baseDir, int cacheSize )
59     {
60         super( baseDir );
61         if (baseDir != null)
62         {
63             // even though the logic in the factory shouldn't allow this,
64
// it apparently can happen.
65
// See bug 923349
66
if (cacheSize <= 0)
67             {
68                 cacheSize = CacheDirChannelLoggerFactory.DEFAULT_CACHESIZE;
69             }
70             this.openedFiles = new HashCache( this, cacheSize );
71             addShutdownHook();
72         }
73         else
74         {
75             this.openedFiles = null;
76         }
77     }
78     
79     
80     /**
81      * Records a coverage of a marked bytecode instruction. This method should
82      * never throw an exception.
83      *
84      * @param classSignature a signature of the class file being covered.
85      * this signature includes the fully-qualified name of the class,
86      * along with a checksum to uniquely identify it.
87      * @param methodIndex index for a method within the class. The meta-data
88      * store will know how to translate the index to a method signature.
89      * @param markIndex the index of the bytecode instruction mark for this
90      * particular channel.
91      */

92     public void cover( String JavaDoc classSignature, short methodIndex,
93             short markIndex )
94     {
95         if (this.baseDir != null)
96         {
97             File JavaDoc f = getClassFile( this.baseDir, classSignature );
98             Writer JavaDoc w = null;
99             try
100             {
101                 char[] out = createCoverString( methodIndex, markIndex );
102                 synchronized (this)
103                 {
104                     w = (Writer JavaDoc)this.openedFiles.get( f );
105                     if (w != null)
106                     {
107                         w.write( out );
108                     }
109                 }
110             }
111             catch (IOException JavaDoc ioe)
112             {
113                 // gasp!!!!
114
ioe.printStackTrace();
115                 
116                 // prevent these errors from occuring again
117
this.baseDir = null;
118             }
119         }
120     }
121     
122     
123     /**
124      * Called when the shutdown hook is called.
125      */

126     public void run()
127     {
128         synchronized( this )
129         {
130             this.openedFiles.clear();
131         }
132     }
133     
134
135     /**
136      * Called by the HashCache
137      */

138     public Object JavaDoc createObject( Object JavaDoc key )
139     {
140         File JavaDoc f = (File JavaDoc)key;
141         try
142         {
143             // bug 907800
144
BufferedWriter JavaDoc bw = new BufferedWriter JavaDoc(
145                 new FileWriter JavaDoc( f.toString(), true ) );
146             return bw;
147         }
148         catch (IOException JavaDoc ioe)
149         {
150             // gasp!!!!
151
ioe.printStackTrace();
152             
153             // prevent these errors from occuring again
154
this.baseDir = null;
155             
156             // we have to return something...
157
return null;
158         }
159     }
160     
161     
162     /**
163      * Called by HashCache when the given object is being removed from the
164      * cache.
165      *
166      * @param key the key associated with the object.
167      * @param obj the object being cleaned up.
168      */

169     public void cleanUpObject( Object JavaDoc key, Object JavaDoc obj )
170     {
171         if (obj != null)
172         {
173             Writer JavaDoc w = (Writer JavaDoc)obj;
174             try
175             {
176                 w.flush();
177                 w.close();
178             }
179             catch (IOException JavaDoc ioe)
180             {
181                 ioe.printStackTrace();
182             }
183         }
184     }
185
186
187     
188     protected void finalize() throws Throwable JavaDoc
189     {
190         // our cleanup method is the "run" method. Of course!
191
run();
192         
193         super.finalize();
194     }
195     
196     
197     protected void addShutdownHook()
198     {
199         Class JavaDoc c = Runtime JavaDoc.class;
200         try
201         {
202             java.lang.reflect.Method JavaDoc m = c.getMethod(
203                 "addShutdownHook", new Class JavaDoc[] { Thread JavaDoc.class } );
204             Thread JavaDoc t = new Thread JavaDoc( this,
205                 this.getClass().getName()+" shutdown hook" );
206             m.invoke( Runtime.getRuntime(), new Object JavaDoc[] { t } );
207         }
208         catch (Exception JavaDoc ex)
209         {
210             // prolly JDK 1.3 not supported.
211
System.err.println(this.getClass().getName()+
212                 " should only be run in a JDK 1.3 compatible JVM.");
213             ex.printStackTrace();
214         }
215     }
216 }
217
218
Popular Tags