KickJava   Java API By Example, From Geeks To Geeks.

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


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
15 /**
16  * Defines the way to control atomic I/O of a Data Object represented
17  * by a File. The atomicity mechanism is based on shadowing. Each Data
18  * Object is written into a File named "id.ai" where "ai" stands for "after
19  * image". Then, committing a Data Object merely consists in moving this
20  * image into the regular Data Object File named "id".
21  * Deleting an Object File consists in creating a File name "id.dl" where
22  * "dl" stands for deleted. Then commtting the deletion consists in
23  * removing both "id" and "id.dl" files in that order.
24  * @author S. Chassande-Barrioz, P. Déchamboux
25  */

26 public class FosObjectFile {
27     final static String JavaDoc DELETSUFFIX = ".dl";
28     final static String JavaDoc WRITESUFFIX = ".ai";
29     private File JavaDoc fOf;
30     private File JavaDoc shadowOf = null;
31     private File JavaDoc deleteOf = null;
32     private FileOutputStream JavaDoc fosOf = null;
33     private ObjectOutputStream JavaDoc oosOf = null;
34
35     /**
36      * Constructs a Data Object File I/O controler.
37      * @param fn The name of the Data Object File.
38      */

39     FosObjectFile(String JavaDoc fn) {
40         fOf = new File JavaDoc(fn);
41     }
42
43     /**
44      * Recovers Object File status wrt repository status.
45      */

46     void recover() throws Exception JavaDoc {
47         deleteOf = new File JavaDoc(fOf.getPath() + DELETSUFFIX);
48         if (deleteOf.exists())
49             return;
50         deleteOf = null;
51         shadowOf = new File JavaDoc(fOf.getPath() + WRITESUFFIX);
52         if (shadowOf.exists())
53             return;
54         shadowOf = null;
55     }
56
57     /**
58      * Commits any modification performed on this Object File.
59      */

60     void commit() throws Exception JavaDoc {
61         if (deleteOf != null) {
62             // Commits deletion.
63
fOf.delete();
64             deleteOf.delete();
65             deleteOf = null;
66             return;
67         }
68         if (shadowOf != null) {
69             // Commits writing.
70
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     /**
81      * Returns the name of the Object File if it has been modified.
82      */

83     String JavaDoc isModified() {
84         if ((deleteOf != null) || (shadowOf != null))
85             return fOf.getPath();
86         return null;
87     }
88
89     /**
90      * Rollbacks any modification performed on this Object File.
91      */

92     synchronized void rollback() throws Exception JavaDoc {
93         if (deleteOf != null) {
94             // Rollbacks deletion.
95
deleteOf.delete();
96             deleteOf = null;
97             return;
98         }
99         if (shadowOf != null) {
100             // Rollbacks writing.
101
if (fosOf != null) {
102                 fosOf.close();
103                 oosOf = null;
104                 fosOf = null;
105             }
106             shadowOf.delete();
107             shadowOf = null;
108         }
109     }
110
111     /**
112      * Opens a stream in order to read from this Object File. It should be
113      * closed at any time.
114      * @return The stream to read from.
115      */

116     ObjectInputStream JavaDoc openReader() throws Exception JavaDoc {
117         return new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(fOf.getPath()));
118     }
119
120     /**
121      * Opens a stream in order to write to this Object File. Ensures that
122      * there is only one writer at a time.
123      * @return The stream to write to.
124      */

125     synchronized ObjectOutputStream JavaDoc openWriter() throws Exception JavaDoc {
126         if (deleteOf != null) {
127             // There has been a delete operation before. Forgets deletion.
128
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 JavaDoc(fOf.getPath() + WRITESUFFIX);
135         fosOf = new FileOutputStream JavaDoc(shadowOf.getPath());
136         return oosOf = new ObjectOutputStream JavaDoc(fosOf);
137     }
138
139     /**
140      * Closes the stream that has been open for writing. It must be done before
141      * any transaction-related action is performed.
142      */

143     synchronized void closeWriter() throws Exception JavaDoc {
144         oosOf.flush();
145         fosOf.close();
146         oosOf = null;
147         fosOf = null;
148     }
149
150     /**
151      * Verifies if the Data Object File already exists.
152      */

153     boolean exist() {
154         if (deleteOf != null)
155         // Already deleted.
156
return false;
157         if (shadowOf != null) {
158             // There has been a write operation before. Forgets writing.
159
return true;
160         }
161         return fOf.exists();
162     }
163
164     /**
165      * Deletes the Object File.
166      */

167     synchronized void delete() throws Exception JavaDoc {
168         if (deleteOf != null)
169         // Already deleted.
170
return;
171         if (fosOf != null)
172             throw new FosException("Cannot delete while writing - Object File: "
173                 + shadowOf.getPath());
174         if (shadowOf != null) {
175             // There has been a write operation before. Forgets writing.
176
shadowOf.delete();
177             shadowOf = null;
178         }
179         deleteOf = new File JavaDoc(fOf.getPath() + DELETSUFFIX);
180         deleteOf.createNewFile();
181     }
182 }
Popular Tags