001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2006, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.logmanipulators;
037:
038: import java.io.File;
039: import java.io.FilenameFilter;
040: import java.util.Calendar;
041: import java.util.Collections;
042: import java.util.Date;
043: import java.util.HashMap;
044: import java.util.Map;
045:
046: import net.sourceforge.cruisecontrol.CruiseControlException;
047: import net.sourceforge.cruisecontrol.Manipulator;
048: import net.sourceforge.cruisecontrol.Log;
049: import net.sourceforge.cruisecontrol.util.ValidationHelper;
050:
051: public abstract class BaseManipulator implements Manipulator {
052:
053: private static final Map UNITS;
054:
055: private transient Integer unit = null;
056: private transient int every = -1;
057:
058: static {
059: Map units = new HashMap(4, 1.0f);
060: units.put("DAY", new Integer(Calendar.DAY_OF_MONTH));
061: units.put("WEEK", new Integer(Calendar.WEEK_OF_YEAR));
062: units.put("MONTH", new Integer(Calendar.MONTH));
063: units.put("YEAR", new Integer(Calendar.YEAR));
064: UNITS = Collections.unmodifiableMap(units);
065: }
066:
067: public BaseManipulator() {
068: super ();
069: }
070:
071: /**
072: * Identifies the relevant Logfiles from the given Logdir
073: * @param logDir the logDir as String
074: * @return File-Array of the the relevant files.
075: */
076: protected File[] getRelevantFiles(String logDir,
077: boolean ignoreSuffix) {
078: File[] backupFiles = null;
079: if (this .every != -1 && this .unit != null) {
080: File dir = new File(logDir);
081: Calendar cal = Calendar.getInstance();
082:
083: cal.add(unit.intValue(), -every);
084:
085: backupFiles = dir.listFiles(new LogfileNameFilter(cal
086: .getTime(), ignoreSuffix));
087: }
088: return backupFiles;
089: }
090:
091: public void validate() throws CruiseControlException {
092: ValidationHelper.assertFalse(every == -1 || unit == null,
093: "BackupEvery and backupUnit must be set");
094: }
095:
096: /**
097: * sets the backup keep amount
098: *
099: * @param every
100: * @throws CruiseControlException
101: */
102: public void setEvery(int every) throws CruiseControlException {
103: this .every = every;
104: }
105:
106: /**
107: * sets the unit on which the backup should run. valid are YEAR, MONTH, WEEK, DAY
108: *
109: * @param unit String that is used as Key for the Calendar-Constants
110: * @throws CruiseControlException
111: */
112: public void setUnit(String unit) throws CruiseControlException {
113: this .unit = (Integer) UNITS.get(unit.toUpperCase());
114: }
115:
116: Integer getUnit() {
117: return this .unit;
118: }
119:
120: private class LogfileNameFilter implements FilenameFilter {
121:
122: private Date logdate = null;
123:
124: private boolean ignoreSuffix = false;
125:
126: public LogfileNameFilter(Date logdate, boolean ignoreSuffix) {
127: this .logdate = logdate;
128: this .ignoreSuffix = ignoreSuffix;
129: }
130:
131: public boolean accept(File dir, String name) {
132: boolean result = name.startsWith("log");
133: if (!ignoreSuffix) {
134: result &= name.endsWith(".xml");
135: }
136: if (result) {
137: try {
138: Date logfileDate = Log
139: .parseDateFromLogFileName(name);
140: result = logfileDate.before(logdate);
141: } catch (Exception e) {
142: result = false;
143: }
144: }
145: return result;
146: }
147:
148: }
149:
150: }
|