KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > readonly > SessionReadOnlyImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl.readonly;
25
26 import org.objectweb.jalisto.se.JalistoFactory;
27 import org.objectweb.jalisto.se.api.*;
28 import org.objectweb.jalisto.se.api.internal.*;
29 import org.objectweb.jalisto.se.api.internal.multi.LockManager;
30 import org.objectweb.jalisto.se.api.query.QueryManager;
31 import org.objectweb.jalisto.se.exception.IdentityException;
32 import org.objectweb.jalisto.se.exception.SchemaException;
33 import org.objectweb.jalisto.se.exception.TransactionException;
34 import org.objectweb.jalisto.se.impl.*;
35 import org.objectweb.jalisto.se.impl.InFileAddress;
36 import org.objectweb.jalisto.se.impl.server.PhysicalOid;
37 import org.objectweb.jalisto.se.impl.trace.Trace;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43 public class SessionReadOnlyImpl implements SessionInternal {
44
45     public SessionReadOnlyImpl(JalistoProperties properties) {
46         InternalFactory internalFactory = JalistoFactory.getInternalFactory();
47         trace = internalFactory.getTracer(properties);
48         sessionId = internalFactory.getSessionId(properties, this);
49         trace.println(Trace.DEBUG, "CREATE A READ ONLY SESSION, id = {0}", sessionId);
50         trace.println(Trace.SESSION, "{0} : create new instance with properties : {1}", sessionId, properties);
51         fileAccess = internalFactory.getLogicalAccess(properties);
52         oidTable = internalFactory.getOidTable(properties);
53         repository = internalFactory.getMetaRepository(properties);
54         props = properties;
55         transaction = createTransaction();
56     }
57
58
59     public void defineClass(ClassDescription classMetaDescription) {
60         throw new UnsupportedOperationException JavaDoc();
61     }
62
63     public void removeClass(String JavaDoc fullClassName) {
64         throw new UnsupportedOperationException JavaDoc();
65     }
66
67     public Collection JavaDoc getAllClassNames() {
68         return repository.getAllClassNames();
69     }
70
71     public boolean isClassDefined(String JavaDoc fullQualifiedClassName) {
72         try {
73             repository.getClidFromClassName(fullQualifiedClassName);
74             return true;
75         } catch (SchemaException e) {
76         }
77         return false;
78     }
79
80     public String JavaDoc getClassNameFor(Object JavaDoc oid) {
81         trace.println(Trace.SESSION, "{0} : getClassNameFor({1})", sessionId, oid);
82         LogicalOid floid = getFloidFromOid(oid);
83         return repository.getClassNameFromClid(new Short JavaDoc(floid.getClid()));
84     }
85
86     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, Class JavaDoc objectClass) {
87         throw new UnsupportedOperationException JavaDoc();
88     }
89
90     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, String JavaDoc objectClassName) {
91         throw new UnsupportedOperationException JavaDoc();
92     }
93
94     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid) {
95         return readObjectByOid(oid, true);
96     }
97
98     public Collection JavaDoc readObjectsByOids(Collection JavaDoc oids) {
99         Iterator JavaDoc oidsIterator = oids.iterator();
100         ArrayList JavaDoc result = new ArrayList JavaDoc();
101         while(oidsIterator.hasNext()) {
102             result.add(readObjectByOid(oidsIterator.next(), true));
103         }
104         return result;
105     }
106
107     public boolean contains(Object JavaDoc oid) {
108         trace.println(Trace.SESSION, "{0} contains({1})", sessionId, oid);
109         checkValidity("contains", true);
110         return oidTable.containsFloid(sessionId, (LogicalOid) oid);
111     }
112
113     public Extent getExtent(Class JavaDoc theClass) {
114         String JavaDoc fullClassName = theClass.getName();
115         return getExtent(fullClassName);
116     }
117
118     public Collection JavaDoc getFloidsFromClid(Object JavaDoc sessionId, Object JavaDoc clid) {
119         return oidTable.getFloidsFromClid(sessionId, clid, false);
120     }
121
122     public Long JavaDoc reserveFloids(short clid, int number) {
123         throw new UnsupportedOperationException JavaDoc();
124     }
125
126
127     public void setOptimistic() {
128         throw new UnsupportedOperationException JavaDoc();
129     }
130
131     public void setPessimistic() {
132         throw new UnsupportedOperationException JavaDoc();
133     }
134
135     public void begin() {
136         trace.println(Trace.SESSION, "{0} : begin()", sessionId);
137         fileAccess.begin(sessionId);
138     }
139
140     public void commit() {
141         trace.println(Trace.SESSION, "{0} : commit()", sessionId);
142         fileAccess.commit(sessionId);
143     }
144
145     public void rollback() {
146         trace.println(Trace.SESSION, "{0} : rollback()", sessionId);
147         fileAccess.rollback(sessionId);
148     }
149
150     public void openSession() {
151         repository.addSession(this);
152         isOpen = true;
153     }
154
155     public void closeSession() {
156         repository.removeSession(this);
157         isOpen = false;
158     }
159
160     public boolean isOpen() {
161         return isOpen;
162     }
163
164     public QueryManager getQueryManager() {
165         if (queryManager == null) {
166             try {
167                 String JavaDoc queryManagerClassName = props.getProperty(JalistoProperties.QUERY_MANAGER_CLASS_KEY);
168                 queryManager = (QueryManager) Class.forName(queryManagerClassName).newInstance();
169                 queryManager.init(this);
170             } catch (InstantiationException JavaDoc e) {
171                 trace.println(Trace.TRACE_ON, "cannot instanciate query manager : {0}", e.getMessage());
172             } catch (IllegalAccessException JavaDoc e) {
173                 trace.println(Trace.TRACE_ON, "cannot initialize query manager : {0}", e.getMessage());
174             } catch (ClassNotFoundException JavaDoc e) {
175                 trace.println(Trace.TRACE_ON, "cannot find query manager class : {0}", e.getMessage());
176             }
177         }
178         return queryManager;
179     }
180
181     public Transaction currentTransaction() {
182         return transaction;
183     }
184
185     public LogicalSystemPageAccess getFileAccess() {
186         return fileAccess;
187     }
188
189     public MetaRepository getMetaRepository() {
190         return repository;
191     }
192
193     public SessionInternal getInternalSession() {
194         return this;
195     }
196
197     public JalistoProperties getProperties() {
198         return props;
199     }
200
201     public OidTable getOidTable() {
202         return oidTable;
203     }
204
205     public LockManager getLockManager() {
206         throw new UnsupportedOperationException JavaDoc();
207     }
208
209     public boolean isNewBase() {
210         return fileAccess.getPhysicalAccess().isNewBase();
211     }
212
213     public Object JavaDoc getSessionId() {
214         return sessionId;
215     }
216
217     public void clearObjectCache() {
218     }
219
220     public Object JavaDoc makeNewFileOid(LogicalOid floid) {
221         throw new UnsupportedOperationException JavaDoc();
222     }
223
224     public void reorganize() {
225         throw new UnsupportedOperationException JavaDoc();
226     }
227
228     public String JavaDoc toString() {
229         return "session " + sessionId;
230     }
231
232     public void checkValidity(String JavaDoc message, boolean mustBeActive) {
233         if (mustBeActive) {
234             if (!transaction.isActive()) {
235                 throw new TransactionException("Transaction must be active during " + message);
236             }
237         } else {
238             if (transaction.isActive()) {
239                 throw new TransactionException("Transaction must not be active during " + message);
240             }
241         }
242     }
243
244     public boolean isRemoteSession() {
245         return false;
246     }
247
248     /**
249      * ******************************************************************************************
250      */

251
252     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid, boolean withCache) {
253         trace.println(Trace.SESSION, "{0} : readObjectByOid({1})", sessionId, oid);
254         checkValidity("readObjectByOid", true);
255         LogicalOid floid = getFloidFromOid(oid);
256         PhysicalOid fpoid = oidTable.getFpoid(sessionId, floid);
257         if (fpoid != null) {
258             DataWrapper datas = fileAccess.readDatas(sessionId, fpoid);
259             if (datas != null) {
260                 return datas.getDatas();
261             }
262         }
263         throw new IdentityException("the given oid " + oid + " doesn't exist in base");
264     }
265
266     public Object JavaDoc[] refreshObjectByOid(Object JavaDoc oid) {
267         return readObjectByOid(oid);
268     }
269
270     public Object JavaDoc createObject(Object JavaDoc oid, Object JavaDoc[] objectToCreate) {
271         throw new UnsupportedOperationException JavaDoc();
272     }
273
274     public void deleteObjectByOid(Object JavaDoc oid) {
275         throw new UnsupportedOperationException JavaDoc();
276     }
277
278     public Extent getExtent(String JavaDoc fullClassName) {
279         trace.println(Trace.SESSION, "{0} : getExtent({1})", sessionId, fullClassName);
280         checkValidity("getExtent", true);
281         return new ExtentImpl(repository.getClidFromClassName(fullClassName), this);
282     }
283
284     public Object JavaDoc makeNewFileOid(Class JavaDoc objectClass) {
285         throw new UnsupportedOperationException JavaDoc();
286     }
287
288     public Object JavaDoc makeNewFileOid(String JavaDoc objectClassName) {
289         throw new UnsupportedOperationException JavaDoc();
290     }
291
292     public Object JavaDoc updateObjectByOid(Object JavaDoc oid, Object JavaDoc[] objectToUpdate) {
293         throw new UnsupportedOperationException JavaDoc();
294     }
295
296     public void eraseStorage() {
297         throw new UnsupportedOperationException JavaDoc();
298     }
299
300     /**
301      * ************************************* PRIVATE *************************************
302      */

303
304     private LogicalOid getFloidFromOid(Object JavaDoc oid) {
305         // trace.println(Trace.SESSION, "{0} : getFloidFromOid({1})", sessionId, oid);
306
try {
307             return (LogicalOid) oid;
308         } catch (ClassCastException JavaDoc cce) {
309             throw new IdentityException("oid is not a FileLoid but a " + oid.getClass().getName());
310         }
311     }
312
313     protected Transaction createTransaction() {
314         return new TransactionImpl(this);
315     }
316
317     private OidTable oidTable;
318     private LogicalSystemPageAccess fileAccess;
319     private InternalMetaRepository repository;
320     private QueryManager queryManager;
321     private Transaction transaction;
322     private boolean isOpen = false;
323
324     protected JalistoProperties props;
325     private Object JavaDoc sessionId;
326
327     protected Trace trace;
328
329     public static final InFileAddress BSTATE_IFA = new InFileAddress("bState");
330 }
331
Popular Tags