KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > logmanipulators > BaseManipulator


1 package net.sourceforge.cruisecontrol.logmanipulators;
2
3 import java.io.File JavaDoc;
4 import java.io.FilenameFilter JavaDoc;
5 import java.util.Calendar JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import net.sourceforge.cruisecontrol.CruiseControlException;
12 import net.sourceforge.cruisecontrol.Manipulator;
13 import net.sourceforge.cruisecontrol.Log;
14 import net.sourceforge.cruisecontrol.util.ValidationHelper;
15
16 public abstract class BaseManipulator implements Manipulator {
17
18     private static final Map JavaDoc UNITS;
19     
20     private transient Integer JavaDoc unit = null;
21     private transient int every = -1;
22
23     static {
24         Map JavaDoc units = new HashMap JavaDoc(4, 1.0f);
25         units.put("DAY", new Integer JavaDoc(Calendar.DAY_OF_MONTH));
26         units.put("WEEK", new Integer JavaDoc(Calendar.WEEK_OF_YEAR));
27         units.put("MONTH", new Integer JavaDoc(Calendar.MONTH));
28         units.put("YEAR", new Integer JavaDoc(Calendar.YEAR));
29         UNITS = Collections.unmodifiableMap(units);
30     }
31
32     public BaseManipulator() {
33         super();
34     }
35
36     /**
37      * Identifies the relevant Logfiles from the given Logdir
38      * @param logDir the logDir as String
39      * @return File-Array of the the relevant files.
40      */

41     protected File JavaDoc[] getRelevantFiles(String JavaDoc logDir, boolean ignoreSuffix) {
42         File JavaDoc[] backupFiles = null;
43         if (this.every != -1 && this.unit != null) {
44             File JavaDoc dir = new File JavaDoc(logDir);
45             Calendar JavaDoc cal = Calendar.getInstance();
46             
47             cal.add(unit.intValue(), -every);
48
49             backupFiles = dir.listFiles(new LogfileNameFilter(cal.getTime(), ignoreSuffix));
50         }
51         return backupFiles;
52     }
53
54     public void validate() throws CruiseControlException {
55         ValidationHelper.assertFalse(every == -1 || unit == null,
56                 "BackupEvery and backupUnit must be set");
57     }
58     
59     /**
60      * sets the backup keep amount
61      *
62      * @param every
63      * @throws CruiseControlException
64      */

65     public void setEvery(int every) throws CruiseControlException {
66         this.every = every;
67     }
68
69     /**
70      * sets the unit on which the backup should run. valid are YEAR, MONTH, WEEK, DAY
71      *
72      * @param unit String that is used as Key for the Calendar-Constants
73      * @throws CruiseControlException
74      */

75     public void setUnit(String JavaDoc unit) throws CruiseControlException {
76         this.unit = (Integer JavaDoc) UNITS.get(unit.toUpperCase());
77     }
78
79     Integer JavaDoc getUnit() {
80         return this.unit;
81     }
82
83     private class LogfileNameFilter implements FilenameFilter JavaDoc {
84
85         private Date JavaDoc logdate = null;
86         
87         private boolean ignoreSuffix = false;
88
89         public LogfileNameFilter(Date JavaDoc logdate, boolean ignoreSuffix) {
90             this.logdate = logdate;
91             this.ignoreSuffix = ignoreSuffix;
92         }
93
94         public boolean accept(File JavaDoc dir, String JavaDoc name) {
95             boolean result = name.startsWith("log");
96             if (!ignoreSuffix) {
97                 result &= name.endsWith(".xml");
98             }
99             if (result) {
100                 try {
101                     Date JavaDoc logfileDate = Log.parseDateFromLogFileName(name);
102                     result = logfileDate.before(logdate);
103                 } catch (Exception JavaDoc e) {
104                     result = false;
105                 }
106             }
107             return result;
108         }
109
110     }
111
112 }
113
Popular Tags