1 package net.sourceforge.cruisecontrol.logmanipulators; 2 3 import java.io.File ; 4 import java.io.FilenameFilter ; 5 import java.util.Calendar ; 6 import java.util.Collections ; 7 import java.util.Date ; 8 import java.util.HashMap ; 9 import java.util.Map ; 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 UNITS; 19 20 private transient Integer unit = null; 21 private transient int every = -1; 22 23 static { 24 Map units = new HashMap (4, 1.0f); 25 units.put("DAY", new Integer (Calendar.DAY_OF_MONTH)); 26 units.put("WEEK", new Integer (Calendar.WEEK_OF_YEAR)); 27 units.put("MONTH", new Integer (Calendar.MONTH)); 28 units.put("YEAR", new Integer (Calendar.YEAR)); 29 UNITS = Collections.unmodifiableMap(units); 30 } 31 32 public BaseManipulator() { 33 super(); 34 } 35 36 41 protected File [] getRelevantFiles(String logDir, boolean ignoreSuffix) { 42 File [] backupFiles = null; 43 if (this.every != -1 && this.unit != null) { 44 File dir = new File (logDir); 45 Calendar 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 65 public void setEvery(int every) throws CruiseControlException { 66 this.every = every; 67 } 68 69 75 public void setUnit(String unit) throws CruiseControlException { 76 this.unit = (Integer ) UNITS.get(unit.toUpperCase()); 77 } 78 79 Integer getUnit() { 80 return this.unit; 81 } 82 83 private class LogfileNameFilter implements FilenameFilter { 84 85 private Date logdate = null; 86 87 private boolean ignoreSuffix = false; 88 89 public LogfileNameFilter(Date logdate, boolean ignoreSuffix) { 90 this.logdate = logdate; 91 this.ignoreSuffix = ignoreSuffix; 92 } 93 94 public boolean accept(File dir, String name) { 95 boolean result = name.startsWith("log"); 96 if (!ignoreSuffix) { 97 result &= name.endsWith(".xml"); 98 } 99 if (result) { 100 try { 101 Date logfileDate = Log.parseDateFromLogFileName(name); 102 result = logfileDate.before(logdate); 103 } catch (Exception e) { 104 result = false; 105 } 106 } 107 return result; 108 } 109 110 } 111 112 } 113 | Popular Tags |