001: /*
002: * @(#)DirMetaDataWriter.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.datastore;
028:
029: import java.io.File;
030: import java.io.IOException;
031:
032: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
033:
034: /**
035: * Stores meta-data in a directory structure.
036: *
037: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
038: * @version $Date: 2004/04/15 05:48:26 $
039: * @since December 15, 2002
040: * @see ZipMetaDataReader
041: */
042: public class DirMetaDataWriter implements IMetaDataWriter {
043: private DirMetaDataIO store;
044:
045: /**
046: * Creates a meta-data writer for a specific directory. This directory
047: * should be dedicated to just this writer.
048: */
049: public DirMetaDataWriter(File basedir) throws IOException {
050: if (basedir == null) {
051: throw new IllegalArgumentException("No null args.");
052: }
053: this .store = new DirMetaDataIO(basedir);
054: }
055:
056: /**
057: * Returns a mark meta-data writer for a specific class. If the
058: * module has not already been added to the store, it will be added.
059: */
060: public IClassMetaDataWriter getClassWriter(IAnalysisModule module)
061: throws IOException {
062: if (module == null) {
063: throw new IllegalArgumentException("no null args");
064: }
065: checkClose();
066: addModule(module);
067: return new DirClassMetaDataWriter(module, this .store);
068: }
069:
070: /**
071: * Closes this writer to prevent further access.
072: */
073: public void close() throws IOException {
074: checkClose();
075: this .store.close();
076: this .store = null;
077: }
078:
079: /**
080: * Conditionally add the module if it isn't already known.
081: */
082: private void addModule(IAnalysisModule module) throws IOException {
083: /*
084: System.out.println(
085: "********************************************\n"+
086: "Oi! There seems to be a bug in this method!\n"+
087: "********************************************\n"
088: );
089: */
090:
091: AnalysisModuleSet ams = this .store.getAnalysisModuleSet();
092: if (ams.getAnalysisModuleIndex(module) < 0) {
093: // add the module
094: ams.addAnalysisModule(module);
095: this .store.putAnalysisModuleSet(ams);
096: }
097: }
098:
099: private void checkClose() throws IOException {
100: if (this .store == null) {
101: throw new IOException("Writer has already been closed.");
102: }
103: }
104:
105: // this shouldn't close the DirMetaDataIO instance, but rather the
106: // DirMetaDataIO instance should close itself.
107: protected void finalize() throws Throwable {
108: Exception ex = null;
109: if (this .store != null) {
110: ex = new IllegalStateException("Did not close writer.");
111: }
112:
113: super .finalize();
114:
115: // class-based post condition
116: if (ex != null) {
117: throw ex;
118: }
119: }
120: }
|