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 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import javax.transaction.xa.Xid ; 17 18 25 public class FosLogFile { 26 private static final String TERMINATOR = "/$/\n"; 27 private File log; 28 private FileOutputStream logFos = null; 29 private ObjectOutputStream logOos = null; 30 private boolean recovered = false; 31 private boolean prepared = false; 32 private ArrayList ofnList; 33 private Xid xid = null; 34 35 39 FosLogFile(String fn) { 40 log = new File (fn); 41 } 42 43 46 void writeXid(Xid xid) throws Exception { 47 if (logFos != null) 48 throw new FosException("Already started to write this log file."); 49 logFos = new FileOutputStream (log.getPath()); 50 logOos = new ObjectOutputStream (logFos); 51 logOos.writeObject(xid); 52 } 53 54 57 void writeObjectFile(String ofn) throws Exception { 58 if (logFos == null) 59 throw new FosException("Not started to write this log file."); 60 if (ofn != null) 61 logOos.writeUTF(ofn); 62 } 63 64 67 void writeEnd() throws Exception { 68 if (logFos == null) 69 throw new FosException("Not started to write this log file."); 70 logOos.writeUTF(TERMINATOR); 71 logOos.flush(); 72 logFos.flush(); 73 logFos.getFD().sync(); 74 logFos.close(); 75 logOos = null; 76 logFos = null; 77 } 78 79 82 void completed() throws Exception { 83 log.delete(); 84 log = null; 85 } 86 87 90 void recover() throws Exception { 91 if (recovered) 92 return; 93 if (!log.exists()) 94 return; 95 ofnList = new ArrayList (); 96 ObjectInputStream ois 97 = new ObjectInputStream (new FileInputStream (log.getPath())); 98 try { 99 xid = (Xid ) ois.readObject(); 100 String fn = ois.readUTF(); 101 while (!fn.equals(TERMINATOR)) { 102 ofnList.add(fn); 103 fn = ois.readUTF(); 104 } 105 prepared = true; 106 recovered = true; 107 } catch (Exception e) { 108 recovered = true; 110 } finally { 111 ois.close(); 112 } 113 } 114 115 118 Xid getXid() throws FosException { 119 if (!recovered) 120 throw new FosException("Operation to be performed after recovery."); 121 return xid; 122 } 123 124 127 boolean isPrepared() throws FosException { 128 if (!recovered) 129 throw new FosException("Operation to be performed after recovery."); 130 return prepared; 131 } 132 133 136 Iterator iterateModifiedOf() throws FosException { 137 if (!recovered) 138 throw new FosException("Operation to be performed after recovery."); 139 return ofnList.iterator(); 140 } 141 } 142 | Popular Tags |