KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > datastore > DirMetaDataWriter


1 /*
2  * @(#)DirMetaDataWriter.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.datastore;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
33
34
35 /**
36  * Stores meta-data in a directory structure.
37  *
38  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
39  * @version $Date: 2004/04/15 05:48:26 $
40  * @since December 15, 2002
41  * @see ZipMetaDataReader
42  */

43 public class DirMetaDataWriter implements IMetaDataWriter
44 {
45     private DirMetaDataIO store;
46     
47     
48     /**
49      * Creates a meta-data writer for a specific directory. This directory
50      * should be dedicated to just this writer.
51      */

52     public DirMetaDataWriter( File JavaDoc basedir )
53             throws IOException JavaDoc
54     {
55         if (basedir == null)
56         {
57             throw new IllegalArgumentException JavaDoc( "No null args." );
58         }
59         this.store = new DirMetaDataIO( basedir );
60     }
61     
62     
63     /**
64      * Returns a mark meta-data writer for a specific class. If the
65      * module has not already been added to the store, it will be added.
66      */

67     public IClassMetaDataWriter getClassWriter( IAnalysisModule module )
68             throws IOException JavaDoc
69     {
70         if (module == null)
71         {
72             throw new IllegalArgumentException JavaDoc("no null args");
73         }
74         checkClose();
75         addModule( module );
76         return new DirClassMetaDataWriter( module, this.store );
77     }
78     
79     
80     /**
81      * Closes this writer to prevent further access.
82      */

83     public void close()
84             throws IOException JavaDoc
85     {
86         checkClose();
87         this.store.close();
88         this.store = null;
89     }
90     
91     
92     /**
93      * Conditionally add the module if it isn't already known.
94      */

95     private void addModule( IAnalysisModule module )
96             throws IOException JavaDoc
97     {
98         /*
99         System.out.println(
100 "********************************************\n"+
101 "Oi! There seems to be a bug in this method!\n"+
102 "********************************************\n"
103         );
104         */

105         
106         
107         AnalysisModuleSet ams = this.store.getAnalysisModuleSet();
108         if (ams.getAnalysisModuleIndex( module ) < 0)
109         {
110             // add the module
111
ams.addAnalysisModule( module );
112             this.store.putAnalysisModuleSet( ams );
113         }
114     }
115     
116     
117     private void checkClose()
118             throws IOException JavaDoc
119     {
120         if (this.store == null)
121         {
122             throw new IOException JavaDoc( "Writer has already been closed." );
123         }
124     }
125     
126     
127     // this shouldn't close the DirMetaDataIO instance, but rather the
128
// DirMetaDataIO instance should close itself.
129
protected void finalize() throws Throwable JavaDoc
130     {
131         Exception JavaDoc ex = null;
132         if (this.store != null)
133         {
134             ex = new IllegalStateException JavaDoc("Did not close writer.");
135         }
136         
137         super.finalize();
138         
139         // class-based post condition
140
if (ex != null)
141         {
142             throw ex;
143         }
144     }
145 }
146
Popular Tags