KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > fos > lib > FosLogFile


1 /**
2  * Copyright (C) 2000
3  */

4
5 package org.objectweb.perseus.fos.lib;
6
7 import org.objectweb.perseus.fos.api.FosException;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.ObjectInputStream JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import javax.transaction.xa.Xid JavaDoc;
17
18 /**
19  * Defines the file that stores the log of a transaction. It mainly stores
20  * the XID of the DTP transaction it is involved in (if any), or null. Then
21  * it stores the name of all Object Files that have been modified by this
22  * transaction.
23  * @author S. Chassande-Barrioz, P. Déchamboux
24  */

25 public class FosLogFile {
26     private static final String JavaDoc TERMINATOR = "/$/\n";
27     private File JavaDoc log;
28     private FileOutputStream JavaDoc logFos = null;
29     private ObjectOutputStream JavaDoc logOos = null;
30     private boolean recovered = false;
31     private boolean prepared = false;
32     private ArrayList JavaDoc ofnList;
33     private Xid JavaDoc xid = null;
34
35     /**
36      * Constructs a log file controler.
37      * @param fn The name of the log file.
38      */

39     FosLogFile(String JavaDoc fn) {
40         log = new File JavaDoc(fn);
41     }
42
43     /**
44      *
45      */

46     void writeXid(Xid JavaDoc xid) throws Exception JavaDoc {
47         if (logFos != null)
48             throw new FosException("Already started to write this log file.");
49         logFos = new FileOutputStream JavaDoc(log.getPath());
50         logOos = new ObjectOutputStream JavaDoc(logFos);
51         logOos.writeObject(xid);
52     }
53
54     /**
55      *
56      */

57     void writeObjectFile(String JavaDoc ofn) throws Exception JavaDoc {
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     /**
65      *
66      */

67     void writeEnd() throws Exception JavaDoc {
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     /**
80      *
81      */

82     void completed() throws Exception JavaDoc {
83         log.delete();
84         log = null;
85     }
86
87     /**
88      *
89      */

90     void recover() throws Exception JavaDoc {
91         if (recovered)
92             return;
93         if (!log.exists())
94             return;
95         ofnList = new ArrayList JavaDoc();
96         ObjectInputStream JavaDoc ois
97             = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(log.getPath()));
98         try {
99             xid = (Xid JavaDoc) ois.readObject();
100             String JavaDoc 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 JavaDoc e) {
108             // log file not consistent: not prepared correctly
109
recovered = true;
110         } finally {
111             ois.close();
112         }
113     }
114
115     /**
116      *
117      */

118     Xid JavaDoc getXid() throws FosException {
119         if (!recovered)
120             throw new FosException("Operation to be performed after recovery.");
121         return xid;
122     }
123
124     /**
125      *
126      */

127     boolean isPrepared() throws FosException {
128         if (!recovered)
129             throw new FosException("Operation to be performed after recovery.");
130         return prepared;
131     }
132
133     /**
134      *
135      */

136     Iterator JavaDoc 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