1 4 5 package org.objectweb.perseus.fos.lib; 6 7 import org.objectweb.perseus.fos.api.FosException; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.ObjectInputStream ; 13 import java.io.ObjectOutputStream ; 14 15 26 public class FosObjectFile { 27 final static String DELETSUFFIX = ".dl"; 28 final static String WRITESUFFIX = ".ai"; 29 private File fOf; 30 private File shadowOf = null; 31 private File deleteOf = null; 32 private FileOutputStream fosOf = null; 33 private ObjectOutputStream oosOf = null; 34 35 39 FosObjectFile(String fn) { 40 fOf = new File (fn); 41 } 42 43 46 void recover() throws Exception { 47 deleteOf = new File (fOf.getPath() + DELETSUFFIX); 48 if (deleteOf.exists()) 49 return; 50 deleteOf = null; 51 shadowOf = new File (fOf.getPath() + WRITESUFFIX); 52 if (shadowOf.exists()) 53 return; 54 shadowOf = null; 55 } 56 57 60 void commit() throws Exception { 61 if (deleteOf != null) { 62 fOf.delete(); 64 deleteOf.delete(); 65 deleteOf = null; 66 return; 67 } 68 if (shadowOf != null) { 69 if (fosOf != null) 71 throw new FosException("Cannot commit while writing - Object File: " 72 + shadowOf.getPath()); 73 fOf.delete(); 74 if (!shadowOf.renameTo(fOf)) 75 throw new FosException("Cannot move after image into regular one."); 76 shadowOf = null; 77 } 78 } 79 80 83 String isModified() { 84 if ((deleteOf != null) || (shadowOf != null)) 85 return fOf.getPath(); 86 return null; 87 } 88 89 92 synchronized void rollback() throws Exception { 93 if (deleteOf != null) { 94 deleteOf.delete(); 96 deleteOf = null; 97 return; 98 } 99 if (shadowOf != null) { 100 if (fosOf != null) { 102 fosOf.close(); 103 oosOf = null; 104 fosOf = null; 105 } 106 shadowOf.delete(); 107 shadowOf = null; 108 } 109 } 110 111 116 ObjectInputStream openReader() throws Exception { 117 return new ObjectInputStream (new FileInputStream (fOf.getPath())); 118 } 119 120 125 synchronized ObjectOutputStream openWriter() throws Exception { 126 if (deleteOf != null) { 127 deleteOf.delete(); 129 deleteOf = null; 130 } else if (fosOf != null) 131 throw new FosException("Cannot write in parallel - Object File: " 132 + shadowOf.getPath()); 133 if (shadowOf == null) 134 shadowOf = new File (fOf.getPath() + WRITESUFFIX); 135 fosOf = new FileOutputStream (shadowOf.getPath()); 136 return oosOf = new ObjectOutputStream (fosOf); 137 } 138 139 143 synchronized void closeWriter() throws Exception { 144 oosOf.flush(); 145 fosOf.close(); 146 oosOf = null; 147 fosOf = null; 148 } 149 150 153 boolean exist() { 154 if (deleteOf != null) 155 return false; 157 if (shadowOf != null) { 158 return true; 160 } 161 return fOf.exists(); 162 } 163 164 167 synchronized void delete() throws Exception { 168 if (deleteOf != null) 169 return; 171 if (fosOf != null) 172 throw new FosException("Cannot delete while writing - Object File: " 173 + shadowOf.getPath()); 174 if (shadowOf != null) { 175 shadowOf.delete(); 177 shadowOf = null; 178 } 179 deleteOf = new File (fOf.getPath() + DELETSUFFIX); 180 deleteOf.createNewFile(); 181 } 182 } | Popular Tags |