001: /*
002: * @(#)FileSingleSourceLoggerFactory.java
003: *
004: * Copyright (C) 2004 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.logger;
028:
029: import java.io.File;
030: import java.io.FileWriter;
031: import java.io.Writer;
032: import java.io.IOException;
033:
034: import java.util.Properties;
035:
036: /**
037: * Output the shared marks to a file.
038: * <P>
039: * Since this outputs to a single file, this has a potential issue
040: * when multiple loggers are trying to access the same file. This
041: * could happen if the CoverageLogger class is loaded by different class
042: * loaders; as a result, multiple CoverageLogger instances will be created,
043: * allowing for multiple channel loggers to write to the same area.
044: * <P>
045: * We resolve this issue by adding a pseudo-random value to the end of
046: * the file name.
047: *
048: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
049: * @version $Date: 2004/07/07 09:39:10 $
050: * @since April 16, 2004
051: */
052: public class FileSingleSourceLoggerFactory extends
053: AbstractSingleSourceLoggerFactory {
054: public static final String DIRECTORY_PROPERTY = "dir";
055: public static final String FILENAME_PROPERTY = "file";
056: private static final String DEFAULT_FILENAME_PREFIX = "single.";
057: private static final String FILENAME_SUFFIX = ".log";
058:
059: private File outfile;
060:
061: /**
062: * Setup the source writer. This can return <tt>null</tt>. Its
063: * actions will be synchronized for you.
064: */
065: protected Writer setupSource() {
066: setReloadSourceAfterError(false);
067:
068: try {
069: //System.out.println(":: Creating FileWriter for "+outfile.getAbsolutePath());
070: return new FileWriter(this .outfile);
071: } catch (IOException e) {
072: e.printStackTrace();
073: return null;
074: }
075: }
076:
077: /**
078: * Extend this method to setup your own properties. Be sure to
079: * call the super's setupProps, though. Be sure to keep the
080: * properties around for how to connect to the source.
081: */
082: protected void setupProps(String prefix, Properties props) {
083: super .setupProps(prefix, props);
084:
085: // fix the filename issue by adding a random value to the
086: // name
087: String pseudoRandom = Long.toString(System.currentTimeMillis())
088: + Long.toString(Math.round(Math.random() * 1000.0))
089: + FILENAME_SUFFIX;
090:
091: // the filename property should only really be used for debugging
092: // purposes, as it invalidates the "single." prefix that the
093: // report filter looks for.
094: String filename = props.getProperty(prefix + FILENAME_PROPERTY);
095: if (filename != null) {
096: this .outfile = new File(filename + pseudoRandom);
097: } else {
098: String dir = props.getProperty(prefix + DIRECTORY_PROPERTY);
099: if (dir != null) {
100: this .outfile = new File(dir, DEFAULT_FILENAME_PREFIX
101: + pseudoRandom);
102: }
103: }
104:
105: if (this .outfile == null) {
106: this .outfile = new File(DEFAULT_FILENAME_PREFIX
107: + pseudoRandom);
108: }
109:
110: File parent = this.outfile.getParentFile();
111: if (!parent.exists()) {
112: parent.mkdirs();
113: }
114: }
115: }
|