KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > checker > ResultsPruner


1 /*
2  * Copyright (c) 2004-2005, Hewlett-Packard Company and Massachusetts
3  * Institute of Technology. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Hewlett-Packard Company nor the name of the
17  * Massachusetts Institute of Technology nor the names of their
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32  * DAMAGE.
33  */

34
35 package org.dspace.checker;
36
37 import java.io.FileInputStream JavaDoc;
38 import java.io.FileNotFoundException JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.text.ParseException JavaDoc;
41 import java.util.Enumeration JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Map JavaDoc;
46 import java.util.Properties JavaDoc;
47 import java.util.regex.Matcher JavaDoc;
48 import java.util.regex.Pattern JavaDoc;
49
50 import org.apache.log4j.Logger;
51 import org.dspace.core.ConfigurationManager;
52 import org.dspace.core.Utils;
53
54 /**
55  * Manages the deletion of results from the checksum history. It uses the
56  * dspace.cfg file as the default configuration file for the deletion settings
57  * and can use a different configuration file if it is passed in.
58  *
59  * @author Jim Downing
60  * @author Grace Carpenter
61  * @author Nathan Sarr
62  *
63  *
64  */

65 public final class ResultsPruner
66 {
67
68     /**
69      * Default logger.
70      */

71     private static final Logger LOG = Logger.getLogger(ResultsPruner.class);
72
73     /**
74      * Factory method for the default results pruner configuration using
75      * dspace.cfg
76      *
77      * @return a ResultsPruner that represent the default retention policy
78      */

79     public static ResultsPruner getDefaultPruner()
80     {
81         try
82         {
83             return getPruner(ConfigurationManager.getConfigurationFile()
84                     .getAbsolutePath());
85         }
86         catch (FileNotFoundException JavaDoc e)
87         {
88             throw new RuntimeException JavaDoc(
89                     "VeryExceptionalException - config file not there! ", e);
90         }
91
92     }
93
94     /**
95      * Factory method for ResultsPruners
96      *
97      * @param propsFile
98      * to configure the results pruner.
99      * @return the configured results pruner.
100      * @throws FileNotFoundException
101      * it the configuration file cannot be found.
102      */

103     public static ResultsPruner getPruner(String JavaDoc propsFile)
104             throws FileNotFoundException JavaDoc
105     {
106         Properties JavaDoc props = new Properties JavaDoc();
107         FileInputStream JavaDoc fin = null;
108         try
109         {
110             fin = new FileInputStream JavaDoc(propsFile);
111             props.load(fin);
112             ResultsPruner rp = new ResultsPruner();
113             Pattern JavaDoc retentionPattern = Pattern
114                     .compile("checker\\.retention\\.(.*)");
115             for (Enumeration JavaDoc en = props.propertyNames(); en.hasMoreElements();)
116             {
117                 String JavaDoc name = (String JavaDoc) en.nextElement();
118                 Matcher JavaDoc matcher = retentionPattern.matcher(name);
119                 if (!matcher.matches())
120                     continue;
121                 String JavaDoc resultCode = matcher.group(1);
122                 long duration;
123                 try
124                 {
125                     duration = Utils.parseDuration(props.getProperty(name));
126                 }
127                 catch (ParseException JavaDoc e)
128                 {
129                     throw new RuntimeException JavaDoc("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 JavaDoc e)
144         {
145             throw new RuntimeException JavaDoc("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 JavaDoc e)
156                 {
157                     LOG.warn(e);
158                 }
159         }
160     }
161
162     /** Ten years */
163     private long defaultDuration = 31536000000L;
164
165     /**
166      * Map of retention durations, keyed by result code name
167      */

168     Map JavaDoc interests = new HashMap JavaDoc();
169
170     /**
171      * Checksum results database Data access
172      */

173     private ChecksumResultDAO checksumResultDAO = null;
174
175     /**
176      * Checksum history database data access.
177      */

178     private ChecksumHistoryDAO checksumHistoryDAO = null;
179
180     /**
181      * Default Constructor
182      */

183     public ResultsPruner()
184     {
185         checksumResultDAO = new ChecksumResultDAO();
186         checksumHistoryDAO = new ChecksumHistoryDAO();
187     }
188
189     /**
190      * Add a result and the length of time before the history with this type of
191      * result is removed from the database.
192      *
193      * @param result
194      * code in the database.
195      *
196      * @param duration
197      * before bitstreams with the specified result type in the
198      * checksum history is removed.
199      */

200     public void addInterested(String JavaDoc result, long duration)
201     {
202         interests.put(result, new Long JavaDoc(duration));
203     }
204
205     /**
206      * Add a result and the length of time before it is removed from the
207      * checksum history table.
208      *
209      * @param result
210      * code in the database.
211      *
212      * @param duration
213      * before bitstreams with the specified result type in the
214      * checksum history is removed.
215      *
216      * @throws ParseException
217      * if the duration cannot be parsed into a long value.
218      */

219     public void addInterested(String JavaDoc result, String JavaDoc duration)
220             throws ParseException JavaDoc
221     {
222         addInterested(result, Utils.parseDuration(duration));
223     }
224
225     /**
226      * The default amount of time before a result is removed.
227      *
228      * @return the default duration.
229      */

230     public long getDefaultDuration()
231     {
232         return defaultDuration;
233     }
234
235     /**
236      * Prunes the results retaining results as configured by the interests
237      * registered with this object.
238      *
239      * @return number of results removed.
240      */

241     public int prune()
242     {
243         List JavaDoc codes = checksumResultDAO.listAllCodes();
244         for (Iterator JavaDoc iter = codes.iterator(); iter.hasNext();)
245         {
246             String JavaDoc code = (String JavaDoc) iter.next();
247             if (!interests.containsKey(code))
248             {
249                 interests.put(code, new Long JavaDoc(defaultDuration));
250             }
251
252         }
253         return checksumHistoryDAO.prune(interests);
254     }
255
256     /**
257      * The default duration before records are removed from the checksum history
258      * table.
259      *
260      * @param defaultDuration
261      * used before records are removed from the checksum history.
262      */

263     public void setDefaultDuration(long defaultDuration)
264     {
265         this.defaultDuration = defaultDuration;
266     }
267 }
268
Popular Tags