KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > server > SessionImpl


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.server;
25
26 import org.objectweb.jalisto.se.api.*;
27 import org.objectweb.jalisto.se.api.internal.*;
28 import org.objectweb.jalisto.se.api.internal.multi.LockManager;
29 import org.objectweb.jalisto.se.api.internal.InternalPhysicalFileAccess;
30 import org.objectweb.jalisto.se.api.query.QueryManager;
31 import org.objectweb.jalisto.se.exception.*;
32 import org.objectweb.jalisto.se.impl.trace.Trace;
33 import org.objectweb.jalisto.se.impl.InFileAddress;
34 import org.objectweb.jalisto.se.impl.LogicalOid;
35 import org.objectweb.jalisto.se.impl.ExtentImpl;
36 import org.objectweb.jalisto.se.impl.TransactionImpl;
37 import org.objectweb.jalisto.se.JalistoFactory;
38
39 import java.io.Serializable JavaDoc;
40 import java.util.*;
41
42 public abstract class SessionImpl implements SessionInternal {
43
44     public SessionImpl(JalistoProperties properties) {
45         InternalFactory internalFactory = JalistoFactory.getInternalFactory();
46         trace = internalFactory.getTracer(properties);
47         sessionId = internalFactory.getSessionId(properties, this);
48         trace.println(Trace.SESSION, "{0} : create new instance with properties : {1}", sessionId, properties);
49         cache = internalFactory.getCache(properties, properties.getObjectCacheSize(), "object cache");
50         fileAccess = internalFactory.getLogicalAccess(properties);
51         oidTable = internalFactory.getOidTable(properties);
52         repository = internalFactory.getMetaRepository(properties);
53         props = properties;
54         transaction = createTransaction();
55         checkStateInBase();
56         trace.println(Trace.DEBUG, "properties for session {0} : {1}", sessionId, properties);
57     }
58
59     protected void checkSerialisable(Object JavaDoc[] object) {
60         if (object != null) {
61             for (int i = 0; i < object.length; i++) {
62                 Object JavaDoc o = object[i];
63                 if ((o != null) && !(o instanceof Serializable JavaDoc)) {
64                     throw new JalistoException("not serializabel class in data : "+o.getClass());
65                 }
66             }
67         }
68     }
69
70
71     public void defineClass(ClassDescription classMetaDescription) {
72         trace.println(Trace.SESSION, "{0} : defineClass({1})", sessionId, classMetaDescription);
73         checkIsOpen("defineClass");
74         repository.defineClass(this, classMetaDescription);
75     }
76
77     public void removeClass(String JavaDoc fullClassName) {
78         trace.println(Trace.SESSION, "{0} : removeClass({1})", sessionId, fullClassName);
79         checkIsOpen("removeClass");
80         repository.removeClass(this, fullClassName);
81     }
82
83     public Collection getAllClassNames() {
84         return repository.getAllClassNames();
85     }
86
87     public boolean isClassDefined(String JavaDoc fullQualifiedClassName) {
88         checkIsOpen("isClassDefined");
89         try {
90             repository.getClidFromClassName(fullQualifiedClassName);
91             return true;
92         } catch (SchemaException e) {
93         }
94         return false;
95     }
96
97     public String JavaDoc getClassNameFor(Object JavaDoc oid) {
98         trace.println(Trace.SESSION, "{0} : getClassNameFor({1})", sessionId, oid);
99         checkIsOpen("getClassNameFor");
100         LogicalOid floid = getFloidFromOid(oid);
101         return repository.getClassNameFromClid(new Short JavaDoc(floid.getClid()));
102     }
103
104     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, Class JavaDoc objectClass) {
105         checkSerialisable(objectToCreate);
106         return createObject(makeNewFileOid(objectClass), objectToCreate);
107     }
108
109     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, String JavaDoc objectClassName) {
110         checkSerialisable(objectToCreate);
111         return createObject(makeNewFileOid(objectClassName), objectToCreate);
112     }
113
114     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid) {
115         return readObjectByOid(oid, true);
116     }
117
118     public Collection readObjectsByOids(Collection oids) {
119         Iterator oidsIterator = oids.iterator();
120         ArrayList result = new ArrayList();
121         while(oidsIterator.hasNext()) {
122             result.add(readObjectByOid(oidsIterator.next()));
123         }
124         return result;
125     }
126
127     public boolean contains(Object JavaDoc oid) {
128         trace.println(Trace.SESSION, "{0} contains({1})", sessionId, oid);
129         checkValidity("contains", true);
130         return oidTable.containsFloid(sessionId, (LogicalOid) oid);
131     }
132
133     public Extent getExtent(Class JavaDoc theClass) {
134         String JavaDoc fullClassName = theClass.getName();
135         return getExtent(fullClassName);
136     }
137
138     public Extent getExtent(String JavaDoc fullClassName) {
139         trace.println(Trace.SESSION, "{0} getExtent({1})", sessionId, fullClassName);
140         checkValidity("getExtent", true);
141         return new ExtentImpl(repository.getClidFromClassName(fullClassName), this);
142     }
143
144     public Collection getFloidsFromClid(Object JavaDoc sessionId, Object JavaDoc clid) {
145         return oidTable.getFloidsFromClid(sessionId, clid, false);
146     }
147
148     public Long JavaDoc reserveFloids(short clid, int number) {
149         return new Long JavaDoc(oidTable.getIdentityProvider().reserveFloids(clid, number));
150     }
151
152     public void setOptimistic() {
153     }
154
155     public void setPessimistic() {
156     }
157
158     public void begin() {
159         trace.println(Trace.SESSION, "{0} : begin()", sessionId);
160         checkIsOpen("begin");
161         oidTable.begin(sessionId);
162         fileAccess.begin(sessionId);
163     }
164
165     public void commit() {
166         trace.println(Trace.SESSION, "{0} : commit()", sessionId);
167         checkIsOpen("commit");
168         oidTable.commit(sessionId);
169         fileAccess.commit(sessionId);
170     }
171
172     public void rollback() {
173         trace.println(Trace.SESSION, "{0} : rollback()", sessionId);
174         checkIsOpen("rollback");
175         oidTable.rollback(sessionId);
176         fileAccess.rollback(sessionId);
177     }
178
179     public void openSession() {
180         trace.println(Trace.SESSION, "{0} : openSession()", sessionId);
181         if (isOpen) {
182             throw new TransactionException("Session is already open");
183         }
184         oidTable.open(sessionId);
185         fileAccess.getPhysicalAccess().open();
186         repository.addSession(this);
187         isOpen = true;
188     }
189
190     public void closeSession() {
191         trace.println(Trace.SESSION, "{0} : closeSession()", sessionId);
192         if (!isOpen) {
193             throw new TransactionException("Session is not open");
194         }
195         oidTable.close(sessionId);
196         fileAccess.getPhysicalAccess().close();
197         repository.removeSession(this);
198         isOpen = false;
199     }
200
201     public boolean isOpen() {
202         return isOpen;
203     }
204
205     public void checkValidity(String JavaDoc message, boolean mustBeActive) {
206         checkIsOpen(message);
207         if (mustBeActive) {
208             if (!transaction.isActive()) {
209                 throw new TransactionException("Transaction must be active during " + message);
210             }
211         } else {
212             if (transaction.isActive()) {
213                 throw new TransactionException("Transaction must not be active during " + message);
214             }
215         }
216     }
217
218     public boolean isRemoteSession() {
219         return false;
220     }
221
222     public QueryManager getQueryManager() {
223         checkIsOpen("getQueryManager");
224         if (queryManager == null) {
225             try {
226                 String JavaDoc queryManagerClassName = props.getProperty(JalistoProperties.QUERY_MANAGER_CLASS_KEY);
227                 queryManager = (QueryManager) Class.forName(queryManagerClassName).newInstance();
228                 queryManager.init(this);
229             } catch (InstantiationException JavaDoc e) {
230                 trace.println(Trace.TRACE_ON, "cannot instanciate query manager : {0}", e.getMessage());
231             } catch (IllegalAccessException JavaDoc e) {
232                 trace.println(Trace.TRACE_ON, "cannot initialize query manager : {0}", e.getMessage());
233             } catch (ClassNotFoundException JavaDoc e) {
234                 trace.println(Trace.TRACE_ON, "cannot find query manager class : {0}", e.getMessage());
235             }
236         }
237         return queryManager;
238     }
239
240     public Transaction currentTransaction() {
241         return transaction;
242     }
243
244     public LogicalSystemPageAccess getFileAccess() {
245         return fileAccess;
246     }
247
248     public MetaRepository getMetaRepository() {
249         checkIsOpen("getMetaRepository");
250         return repository;
251     }
252
253     public SessionInternal getInternalSession() {
254         return this;
255     }
256
257     public JalistoProperties getProperties() {
258         return props;
259     }
260
261     public OidTable getOidTable() {
262         return oidTable;
263     }
264
265     public LockManager getLockManager() {
266         return null;
267     }
268
269     public boolean isNewBase() {
270         return fileAccess.getPhysicalAccess().isNewBase();
271     }
272
273     public Object JavaDoc getSessionId() {
274         return sessionId;
275     }
276
277     public void clearObjectCache() {
278         trace.println(Trace.SESSION, "{0} : clearObjectCache()", sessionId);
279         checkIsOpen("clearObjectCache");
280         cache.clear();
281     }
282
283     public Object JavaDoc[] refreshObjectByOid(Object JavaDoc oid) {
284         return readObjectByOid(oid);
285     }
286
287     public void reorganize() {
288         trace.println(Trace.SESSION, "{0} : reorganize()", sessionId);
289         if (!props.allowsSpecialFunctionnalities()) {
290             throw new TransactionException("The reorganize operation is not allowed");
291         }
292         if (isOpen) {
293             throw new TransactionException("Session must not be open during reorganize()");
294         }
295         if (transaction.isActive()) {
296             throw new JalistoException("Transaction must not be active during reorganize()");
297         }
298         repository.checkNoSessions(
299                 "There are active sessions during attempt to reorganize datastore " + getSessionId());
300         try {
301             openSession();
302             transaction.begin();
303             fileAccess.cleanAllNullValue();
304             transaction.commit();
305             fileAccess.getPhysicalAccess().getInternalAccess().reorganize();
306             closeSession();
307         } catch (Exception JavaDoc e) {
308             throw new JalistoException(e);
309         }
310     }
311
312     public void eraseStorage() {
313         trace.println(Trace.SESSION, "{0} : eraseStorage()", sessionId);
314         if (!props.allowsSpecialFunctionnalities()) {
315             throw new TransactionException("The eraseStorage operation is not allowed");
316         }
317         if (isOpen) {
318             throw new TransactionException("Session must not be open during eraseStorage()");
319         }
320         if (transaction.isActive()) {
321             throw new JalistoException("Transaction must not be active during eraseStorage()");
322         }
323         repository.checkNoSessions(
324                 "There are active sessions during attempt to eraseStorage datastore " + getSessionId());
325
326         try {
327             openSession();
328             Iterator classes = (new ArrayList(repository.getAllClassNames())).iterator();
329             while (classes.hasNext()) {
330                 String JavaDoc className = (String JavaDoc) classes.next();
331                 removeClass(className);
332             }
333             transaction.begin();
334             oidTable.getIdentityProvider().resetAll();
335             transaction.commit();
336             closeSession();
337         } catch (Exception JavaDoc e) {
338             throw new JalistoException(e);
339         }
340     }
341
342     public String JavaDoc toString() {
343         return "session " + sessionId;
344     }
345
346     /**
347      * ************************************* PRIVATE *************************************
348      */

349
350     protected void checkIsOpen(String JavaDoc message) {
351         if (!isOpen) {
352             throw new TransactionException("Session must be open during " + message);
353         }
354     }
355
356     protected LogicalOid getFloidFromOid(Object JavaDoc oid) {
357         try {
358             return (LogicalOid) oid;
359         } catch (ClassCastException JavaDoc cce) {
360             throw new IdentityException("oid is not a FileLoid but a " + oid.getClass().getName());
361         }
362     }
363
364     private void checkStateInBase() {
365         trace.println(Trace.SESSION, "{0} : checkStateInBase()", sessionId);
366         JalistoProperties state;
367         InternalPhysicalFileAccess physical = fileAccess.getPhysicalAccess();
368
369         try {
370             state = (JalistoProperties) physical.readFileObjectAt(BSTATE_IFA).getData();
371         } catch (JalistoException e) {
372             physical.writeObjectInBase(BSTATE_IFA, props.getClone(), false);
373             return;
374         }
375         props.compareProperties(state);
376     }
377
378     protected Transaction createTransaction() {
379         return new TransactionImpl(this);
380     }
381
382
383     protected Map cache;
384     protected OidTable oidTable;
385     protected LogicalSystemPageAccess fileAccess;
386     protected InternalMetaRepository repository;
387     protected QueryManager queryManager;
388
389     protected Transaction transaction;
390     protected boolean isOpen;
391
392     protected JalistoProperties props;
393     protected Object JavaDoc sessionId;
394
395     protected Trace trace;
396
397     public static final InFileAddress BSTATE_IFA = new InFileAddress("bState");
398 }
399
Popular Tags