1 18 package net.sf.mybatchfwk.history; 19 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import java.util.Iterator ; 26 27 import net.sf.mybatchfwk.BatchConfiguration; 28 import net.sf.mybatchfwk.BatchException; 29 30 36 public class FileExecutionHistory implements IExecutionHistory { 37 38 public static final String PROPERTY_STORAGE_FOLDER = "mbf.executionHistory.storageFolder"; 39 40 public static final String COMPLETED_TASKS_FILENAME = "mbf-completedTasks"; 41 42 public static final String FAILED_TASKS_FILENAME = "mbf-failedTasks"; 43 44 private File completedTasksFile; 45 private File failedTasksFile; 46 47 private KeysFileIndexer completedTasksIndexer; 48 private KeysFileIndexer failedTasksIndexer; 49 50 private KeysFileIndexer oldCompletedTasksIndexer; 51 private KeysFileIndexer oldFailedTasksIndexer; 52 53 56 public void initStorage(BatchConfiguration configuration) throws BatchException { 57 58 String 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 file = new File (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 (file, COMPLETED_TASKS_FILENAME + ".txt"); 71 File oldCompletedTasksFile = new File (file, COMPLETED_TASKS_FILENAME + "-old.txt"); 72 rotateFile(completedTasksFile, oldCompletedTasksFile); 73 74 this.failedTasksFile = new File (file, FAILED_TASKS_FILENAME + ".txt"); 75 File oldFailedTasksFile = new File (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 e) { 89 throw new BatchException(e); 90 } 91 } 92 93 99 protected void rotateFile(File file, File 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 117 protected void closeIndexer(KeysFileIndexer indexer) { 118 if (indexer != null) { 119 try { 120 indexer.closeReader(); 121 } catch (Exception e) {} 122 } 123 } 124 125 128 public void closeStorage() throws BatchException { 129 closeIndexer(oldCompletedTasksIndexer); 130 closeIndexer(oldFailedTasksIndexer); 131 closeIndexer(completedTasksIndexer); 132 closeIndexer(failedTasksIndexer); 133 } 134 135 138 public synchronized boolean isCompletedTask(String 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 e) { 148 throw new BatchException(e); 149 } 150 return result; 151 } 152 153 156 public synchronized boolean isFailedTask(String 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 e) { 166 throw new BatchException(e); 167 } 168 return result; 169 } 170 171 174 public synchronized void storeCompletedTaskId(String id) throws BatchException { 175 try { 176 completedTasksIndexer.addKey(id); 177 } catch (IOException e) { 178 throw new BatchException(e); 179 } 180 } 181 182 185 public synchronized void storeFailedTaskId(String id) throws BatchException { 186 try { 187 failedTasksIndexer.addKey(id); 188 } catch (IOException e) { 189 throw new BatchException(e); 190 } 191 } 192 193 196 public Iterator completedTasksIdIterator() throws BatchException { 197 try { 198 BufferedReader reader = new BufferedReader (new FileReader (completedTasksFile)); 199 return new LineIterator(reader); 200 } catch (FileNotFoundException e) { 201 throw new BatchException(e); 202 } 203 } 204 205 208 public Iterator failedTasksIdIterator() throws BatchException { 209 try { 210 BufferedReader reader = new BufferedReader (new FileReader (failedTasksFile)); 211 return new LineIterator(reader); 212 } catch (FileNotFoundException e) { 213 throw new BatchException(e); 214 } 215 } 216 } 217 | Popular Tags |