1 34 35 package org.dspace.checker; 36 37 import java.io.FileInputStream ; 38 import java.io.FileNotFoundException ; 39 import java.io.IOException ; 40 import java.text.ParseException ; 41 import java.util.Enumeration ; 42 import java.util.HashMap ; 43 import java.util.Iterator ; 44 import java.util.List ; 45 import java.util.Map ; 46 import java.util.Properties ; 47 import java.util.regex.Matcher ; 48 import java.util.regex.Pattern ; 49 50 import org.apache.log4j.Logger; 51 import org.dspace.core.ConfigurationManager; 52 import org.dspace.core.Utils; 53 54 65 public final class ResultsPruner 66 { 67 68 71 private static final Logger LOG = Logger.getLogger(ResultsPruner.class); 72 73 79 public static ResultsPruner getDefaultPruner() 80 { 81 try 82 { 83 return getPruner(ConfigurationManager.getConfigurationFile() 84 .getAbsolutePath()); 85 } 86 catch (FileNotFoundException e) 87 { 88 throw new RuntimeException ( 89 "VeryExceptionalException - config file not there! ", e); 90 } 91 92 } 93 94 103 public static ResultsPruner getPruner(String propsFile) 104 throws FileNotFoundException 105 { 106 Properties props = new Properties (); 107 FileInputStream fin = null; 108 try 109 { 110 fin = new FileInputStream (propsFile); 111 props.load(fin); 112 ResultsPruner rp = new ResultsPruner(); 113 Pattern retentionPattern = Pattern 114 .compile("checker\\.retention\\.(.*)"); 115 for (Enumeration en = props.propertyNames(); en.hasMoreElements();) 116 { 117 String name = (String ) en.nextElement(); 118 Matcher matcher = retentionPattern.matcher(name); 119 if (!matcher.matches()) 120 continue; 121 String resultCode = matcher.group(1); 122 long duration; 123 try 124 { 125 duration = Utils.parseDuration(props.getProperty(name)); 126 } 127 catch (ParseException e) 128 { 129 throw new RuntimeException ("Problem parsing duration: " 130 + e.getMessage(), e); 131 } 132 if ("default".equals(resultCode)) 133 { 134 rp.setDefaultDuration(duration); 135 } 136 else 137 { 138 rp.addInterested(resultCode, duration); 139 } 140 } 141 return rp; 142 } 143 catch (IOException e) 144 { 145 throw new RuntimeException ("Problem loading properties file: " 146 + e.getMessage(), e); 147 } 148 finally 149 { 150 if (fin != null) 151 try 152 { 153 fin.close(); 154 } 155 catch (IOException e) 156 { 157 LOG.warn(e); 158 } 159 } 160 } 161 162 163 private long defaultDuration = 31536000000L; 164 165 168 Map interests = new HashMap (); 169 170 173 private ChecksumResultDAO checksumResultDAO = null; 174 175 178 private ChecksumHistoryDAO checksumHistoryDAO = null; 179 180 183 public ResultsPruner() 184 { 185 checksumResultDAO = new ChecksumResultDAO(); 186 checksumHistoryDAO = new ChecksumHistoryDAO(); 187 } 188 189 200 public void addInterested(String result, long duration) 201 { 202 interests.put(result, new Long (duration)); 203 } 204 205 219 public void addInterested(String result, String duration) 220 throws ParseException 221 { 222 addInterested(result, Utils.parseDuration(duration)); 223 } 224 225 230 public long getDefaultDuration() 231 { 232 return defaultDuration; 233 } 234 235 241 public int prune() 242 { 243 List codes = checksumResultDAO.listAllCodes(); 244 for (Iterator iter = codes.iterator(); iter.hasNext();) 245 { 246 String code = (String ) iter.next(); 247 if (!interests.containsKey(code)) 248 { 249 interests.put(code, new Long (defaultDuration)); 250 } 251 252 } 253 return checksumHistoryDAO.prune(interests); 254 } 255 256 263 public void setDefaultDuration(long defaultDuration) 264 { 265 this.defaultDuration = defaultDuration; 266 } 267 } 268 | Popular Tags |