001: /*
002: * @(#)CacheDirChannelLoggerFactory.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.logger;
028:
029: import java.io.File;
030: import java.util.Properties;
031:
032: import net.sourceforge.groboutils.codecoverage.v2.IChannelLogger;
033: import net.sourceforge.groboutils.codecoverage.v2.IChannelLoggerFactory;
034:
035: /**
036: * This is the same as the <tt>DirectoryChannelLoggerFactory</tt>, except
037: * that it caches the files as open.
038: *
039: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
040: * @version $Date: 2004/04/21 02:08:44 $
041: * @since May 9, 2003
042: */
043: public class CacheDirChannelLoggerFactory implements
044: IChannelLoggerFactory {
045: public static final String DIRECTORY_PROPERTY = "dir";
046: public static final String DEFAULT_DIRECTORY = "./.cache-cover-logs";
047:
048: public static final String CACHESIZE_PROPERTY = "cache-size";
049: public static final int DEFAULT_CACHESIZE = 25;
050:
051: /**
052: * Creates a specific logger type. Initializes the logger based on the
053: * given collection of properties.
054: *
055: * @param propertyPrefix the prefix that all logger properties will begin
056: * with. Expect all logger-specific properties to be appended directly
057: * to this string.
058: * @param props the property collection to pull the logger properties from.
059: * @param channelIndex channel number to log to.
060: * @return the initialized logger.
061: */
062: public IChannelLogger createChannelLogger(String propertyPrefix,
063: Properties props, short channelIndex) {
064: String directory = getDirectory(propertyPrefix, props);
065: int cacheSize = getCacheSize(propertyPrefix, props);
066:
067: File dir = new File(directory, Short.toString(channelIndex));
068: if (dir.exists()) {
069: if (!dir.isDirectory()) {
070: System.err
071: .println("DirectoryLogger base directory is a file.");
072: dir = null;
073: }
074: } else {
075: dir.mkdirs();
076: }
077: return new CacheDirChannelLogger(dir, cacheSize);
078: }
079:
080: protected String getDirectory(String propertyPrefix,
081: Properties props) {
082: String directory = props.getProperty(propertyPrefix
083: + DIRECTORY_PROPERTY);
084: if (directory == null) {
085: directory = DEFAULT_DIRECTORY;
086: }
087: return directory;
088: }
089:
090: protected int getCacheSize(String propertyPrefix, Properties props) {
091: String csS = props.getProperty(propertyPrefix
092: + CACHESIZE_PROPERTY);
093: int cs = DEFAULT_CACHESIZE;
094: if (csS != null) {
095: try {
096: int i = Integer.parseInt(csS);
097: if (i > 0) {
098: cs = i;
099: }
100: } catch (NumberFormatException e) {
101: System.out.println("Bad cache size property value: "
102: + csS);
103: }
104: }
105:
106: return cs;
107: }
108: }
|