KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > connector > ra > fos > FosWriting


1 /**
2  * ObjectWeb Connector: an implementation of JCA Sun specification along
3  * with some extensions of this specification.
4  * Copyright (C) 2001-2002 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Release: 0.1
21  *
22  * Contact: jorm@objectweb.org
23  *
24  * Author: S. Chassande-Barrioz, P. Dechamboux
25  *
26  */

27
28 package org.objectweb.perseus.connector.ra.fos;
29
30 import org.objectweb.perseus.connector.ra.Writing;
31 import org.objectweb.perseus.fos.api.FosAccess;
32 import org.objectweb.perseus.fos.api.FosException;
33 import org.objectweb.perseus.fos.api.FosStructure;
34
35 import java.io.ObjectInputStream JavaDoc;
36 import java.io.ObjectOutputStream JavaDoc;
37 import javax.resource.cci.Connection JavaDoc;
38
39 /**
40  * This class is an implementation of the Writing interface for the FOS case.
41  * This class accesses to a directory that stores objects whose identifier is
42  * pid and value is description.
43  */

44 public class FosWriting implements Writing, FosStructure {
45     private static final String JavaDoc OBJDIR = "objdir";
46     private String JavaDoc pid;
47     private String JavaDoc details;
48     private Connection JavaDoc connection;
49     private FosAccess lastUsedInFS;
50
51     protected FosWriting(int _pid, String JavaDoc _details) {
52         pid = Integer.toString(_pid);
53         details = _details;
54     }
55
56     public void init(Connection JavaDoc co) throws Exception JavaDoc {
57         FosAccess fa = (FosAccess) co;
58         if (fa.exist(OBJDIR, pid))
59             fa.delete(OBJDIR, pid);
60         fa.write(OBJDIR, pid, this, null);
61     }
62
63     public void init() throws Exception JavaDoc {
64         init(connection);
65     }
66
67     /**
68      * Tests if connection is correctly passed through to FosStructure.
69      * @return false if it is not passed through correctly.
70      */

71     public boolean passThrough(Connection JavaDoc co) {
72         try {
73             FosAccess fa = (FosAccess) co;
74             lastUsedInFS = null;
75             fa.write(OBJDIR, pid, this, null);
76             if (lastUsedInFS != fa)
77                 return false;
78             lastUsedInFS = null;
79             fa.read(OBJDIR, pid, this, null);
80             if (lastUsedInFS != fa)
81                 return false;
82             return true;
83         } catch (FosException fe) {
84             fe.printStackTrace();
85             return false;
86         }
87     }
88
89     /**
90      * Read a data from the support, and check the value. The boolean
91      * parameter is used to check the read value.
92      * The method returns true is the data state is right
93      * The method throws Exceptions if an error occures during the test
94      */

95     public boolean read(Connection JavaDoc co, boolean iswritten)
96         throws Exception JavaDoc {
97         FosAccess fa = (FosAccess) co;
98         return fa.exist(OBJDIR, pid) ^ iswritten;
99     }
100
101     public boolean read(boolean iswritten)
102         throws Exception JavaDoc {
103         return read(connection, iswritten);
104     }
105
106     /**
107      * The method throws Exceptions if an error occures during the test
108      */

109     public void write(Connection JavaDoc co) throws Exception JavaDoc {
110         FosAccess fa = (FosAccess) co;
111         if (fa.exist(OBJDIR, pid))
112             fa.delete(OBJDIR, pid);
113     }
114
115     /**
116      * The method throws Exceptions if an error occures during the test
117      */

118     public void write() throws Exception JavaDoc {
119         write(connection);
120     }
121
122     public void setConnection(Connection JavaDoc c) throws Exception JavaDoc {
123         connection = c;
124     }
125
126     /**
127      * Reads the content of a persistent object from the file represented by
128      * the given ObjectInputStream.
129      * @param ois The ObjectInputStream representing the file from which to
130      * read.
131      * @param conn The connection used to access the File Object Store.
132      */

133     public void readFile(ObjectInputStream JavaDoc ois, FosAccess conn, Object JavaDoc ctxt) throws Exception JavaDoc {
134         details = ois.readUTF();
135         lastUsedInFS = conn;
136     }
137
138     /**
139      * Writes the content of a persistent object to the file represented by
140      * the given ObjectOutputStream.
141      * @param oos The ObjectOutputStream representing the file to which to
142      * write.
143      * @param conn The connection used to access the File Object Store.
144      */

145     public void writeFile(ObjectOutputStream JavaDoc oos, FosAccess conn, Object JavaDoc ctxt) throws Exception JavaDoc {
146         oos.writeUTF(details);
147         lastUsedInFS = conn;
148     }
149 }
Popular Tags