KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > mybatchfwk > history > FileExecutionHistory


1 /*
2  * MyBatchFramework - Open-source batch framework.
3  * Copyright (C) 2006 Jérôme Bertèche cyberteche@users.sourceforge.net
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * Jérôme Bertèche
16  * Email: cyberteche@users.sourceforge.net
17  */

18 package net.sf.mybatchfwk.history;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import net.sf.mybatchfwk.BatchConfiguration;
28 import net.sf.mybatchfwk.BatchException;
29
30 /**
31  * An history storage that store the id of the executed tasks into two files,
32  * one for the completed tasks, one another for the failed tasks.
33  *
34  * @author Jérôme Bertèche (cyberteche@users.sourceforge.net)
35  */

36 public class FileExecutionHistory implements IExecutionHistory {
37     
38     public static final String JavaDoc PROPERTY_STORAGE_FOLDER = "mbf.executionHistory.storageFolder";
39     
40     public static final String JavaDoc COMPLETED_TASKS_FILENAME = "mbf-completedTasks";
41     
42     public static final String JavaDoc FAILED_TASKS_FILENAME = "mbf-failedTasks";
43     
44     private File JavaDoc completedTasksFile;
45     private File JavaDoc failedTasksFile;
46     
47     private KeysFileIndexer completedTasksIndexer;
48     private KeysFileIndexer failedTasksIndexer;
49     
50     private KeysFileIndexer oldCompletedTasksIndexer;
51     private KeysFileIndexer oldFailedTasksIndexer;
52     
53     /* (non-Javadoc)
54      * @see net.sf.mybatchfwk.IHistoryStorage#initStorage()
55      */

56     public void initStorage(BatchConfiguration configuration) throws BatchException {
57         
58         String JavaDoc storageFolder = configuration.getPropertiesLoader().getProperty(PROPERTY_STORAGE_FOLDER);
59         if ((storageFolder == null) || ("".equals(storageFolder.trim()))) {
60             throw new BatchException("the parameter '" + PROPERTY_STORAGE_FOLDER + "' is mandatory");
61         }
62         
63         File JavaDoc file = new File JavaDoc(storageFolder);
64         if (!file.exists()) {
65             if (file.mkdirs() != true) {
66                 throw new BatchException("unable to create the storage folder '" + storageFolder + "'");
67             }
68         }
69         
70         this.completedTasksFile = new File JavaDoc(file, COMPLETED_TASKS_FILENAME + ".txt");
71         File JavaDoc oldCompletedTasksFile = new File JavaDoc(file, COMPLETED_TASKS_FILENAME + "-old.txt");
72         rotateFile(completedTasksFile, oldCompletedTasksFile);
73         
74         this.failedTasksFile = new File JavaDoc(file, FAILED_TASKS_FILENAME + ".txt");
75         File JavaDoc oldFailedTasksFile = new File JavaDoc(file, FAILED_TASKS_FILENAME + "-old.txt");
76         rotateFile(failedTasksFile, oldFailedTasksFile);
77         
78         try {
79             completedTasksIndexer = new KeysFileIndexer(completedTasksFile, "rw");
80             failedTasksIndexer = new KeysFileIndexer(failedTasksFile, "rw");
81             
82             if (oldCompletedTasksFile.exists()) {
83                 oldCompletedTasksIndexer = new KeysFileIndexer(oldCompletedTasksFile, "r");
84             }
85             if (oldFailedTasksFile.exists()) {
86                 oldFailedTasksIndexer = new KeysFileIndexer(oldFailedTasksFile, "r");
87             }
88         } catch (IOException JavaDoc e) {
89             throw new BatchException(e);
90         }
91     }
92     
93     /**
94      *
95      * @param file
96      * @param oldFile
97      * @throws BatchException
98      */

99     protected void rotateFile(File JavaDoc file, File JavaDoc oldFile) throws BatchException {
100         if (oldFile.exists()) {
101             if (!oldFile.delete()) {
102                 throw new BatchException("unable to delete the file '" + oldFile.getPath() + "'");
103             }
104         }
105         if (file.exists()) {
106             if (!file.renameTo(oldFile)) {
107                 throw new BatchException("unable to rename the file '" + file.getPath() + "' to '" + oldFile.getPath() + "'");
108             }
109         }
110     }
111     
112     /**
113      * Close an indexer if it exists.
114      * If an exception occured, then it is ignored.
115      * @param indexer
116      */

117     protected void closeIndexer(KeysFileIndexer indexer) {
118         if (indexer != null) {
119             try {
120                 indexer.closeReader();
121             } catch (Exception JavaDoc e) {}
122         }
123     }
124     
125     /* (non-Javadoc)
126      * @see net.sf.mybatchfwk.IHistoryStorage#closeStorage()
127      */

128     public void closeStorage() throws BatchException {
129         closeIndexer(oldCompletedTasksIndexer);
130         closeIndexer(oldFailedTasksIndexer);
131         closeIndexer(completedTasksIndexer);
132         closeIndexer(failedTasksIndexer);
133     }
134     
135     /* (non-Javadoc)
136      * @see net.sf.mybatchfwk.IHistoryStorage#isCompletedTask(java.lang.String)
137      */

138     public synchronized boolean isCompletedTask(String JavaDoc id) throws BatchException {
139         boolean result = false;
140         try {
141             if (oldCompletedTasksIndexer != null) {
142                 result = oldCompletedTasksIndexer.containsKey(id);
143             }
144             if (!result) {
145                 result = completedTasksIndexer.containsKey(id);
146             }
147         } catch (IOException JavaDoc e) {
148             throw new BatchException(e);
149         }
150         return result;
151     }
152
153     /* (non-Javadoc)
154      * @see net.sf.mybatchfwk.IHistoryStorage#isFailedTask(java.lang.String)
155      */

156     public synchronized boolean isFailedTask(String JavaDoc id) throws BatchException {
157         boolean result = false;
158         try {
159             if (oldFailedTasksIndexer != null) {
160                 result = oldFailedTasksIndexer.containsKey(id);
161             }
162             if (!result) {
163                 result = failedTasksIndexer.containsKey(id);
164             }
165         } catch (IOException JavaDoc e) {
166             throw new BatchException(e);
167         }
168         return result;
169     }
170
171     /* (non-Javadoc)
172      * @see net.sf.mybatchfwk.IHistoryStorage#storeCompletedTaskId(java.lang.String)
173      */

174     public synchronized void storeCompletedTaskId(String JavaDoc id) throws BatchException {
175         try {
176             completedTasksIndexer.addKey(id);
177         } catch (IOException JavaDoc e) {
178             throw new BatchException(e);
179         }
180     }
181
182     /* (non-Javadoc)
183      * @see net.sf.mybatchfwk.IHistoryStorage#storeFailedTaskId(java.lang.String)
184      */

185     public synchronized void storeFailedTaskId(String JavaDoc id) throws BatchException {
186         try {
187             failedTasksIndexer.addKey(id);
188         } catch (IOException JavaDoc e) {
189             throw new BatchException(e);
190         }
191     }
192
193     /* (non-Javadoc)
194      * @see net.sf.mybatchfwk.history.IExecutionHistory#completedTasksIdIterator()
195      */

196     public Iterator JavaDoc completedTasksIdIterator() throws BatchException {
197         try {
198             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(completedTasksFile));
199             return new LineIterator(reader);
200         } catch (FileNotFoundException JavaDoc e) {
201             throw new BatchException(e);
202         }
203     }
204
205     /* (non-Javadoc)
206      * @see net.sf.mybatchfwk.history.IExecutionHistory#failedTasksIdIterator()
207      */

208     public Iterator JavaDoc failedTasksIdIterator() throws BatchException {
209         try {
210             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(failedTasksFile));
211             return new LineIterator(reader);
212         } catch (FileNotFoundException JavaDoc e) {
213             throw new BatchException(e);
214         }
215     }
216 }
217
Popular Tags