KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > client > SessionClientImpl


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.client;
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.api.remote.ClientCommunicationAgent;
32 import org.objectweb.jalisto.se.exception.*;
33 import org.objectweb.jalisto.se.impl.ExtentImpl;
34 import org.objectweb.jalisto.se.impl.LogicalOid;
35 import org.objectweb.jalisto.se.impl.TransactionImpl;
36 import org.objectweb.jalisto.se.impl.trace.Trace;
37
38 import java.util.*;
39
40 public class SessionClientImpl implements SessionInternal {
41
42     public SessionClientImpl(JalistoProperties properties) {
43         this.clientProperties = properties;
44         this.connexion = JalistoFactory.getInternalFactory().getClientCommunicationAgent(properties);
45         this.sessionId = connexion.getSessionId();
46         this.trace = JalistoFactory.getInternalFactory().getTracer(properties);
47         this.cache = JalistoFactory.getInternalFactory().getCache(properties, properties.getObjectCacheSize(), "object cache");
48         this.repository = JalistoFactory.getInternalFactory().getMetaRepository(properties, connexion);
49         this.transaction = createTransaction();
50         this.createdObjects = new HashMap();
51         this.updatedObjects = new HashMap();
52         this.deletedObjects = new HashMap();
53
54         ((JalistoPropertiesClientImpl) clientProperties).setRemoteProperties(connexion.getProperties());
55     }
56
57     public void defineClass(ClassDescription classDescription) {
58         trace.println(Trace.SESSION, "{0} : defineClass({1})", sessionId, classDescription);
59         checkIsOpen("defineClass");
60         checkValidity("defineClass", false);
61         if (!isClassDefined(classDescription.getClassName())) {
62             connexion.defineClass(classDescription);
63             repository.defineClass(this, classDescription);
64             Short JavaDoc clid = (Short JavaDoc) repository.getClidFromClassName(classDescription.getClassName());
65             identityProvider.addClass(clid.shortValue());
66         }
67     }
68
69     public void removeClass(String JavaDoc fullClassName) {
70         trace.println(Trace.SESSION, "{0} : removeClass({1})", sessionId, fullClassName);
71         checkIsOpen("removeClass");
72         checkValidity("removeClass", false);
73         if (isClassDefined(fullClassName)) {
74             repository.removeClass(this, fullClassName);
75             connexion.removeClass(fullClassName);
76         } else {
77             throw new SchemaException("Class " + fullClassName + " is not define in base");
78         }
79     }
80
81     public boolean isClassDefined(String JavaDoc fullQualifiedClassName) {
82         checkIsOpen("isClassDefined");
83         try {
84             return (repository.getClidFromClassName(fullQualifiedClassName) != null);
85         } catch (SchemaException e) {
86         }
87         return false;
88     }
89
90     public String JavaDoc getClassNameFor(Object JavaDoc oid) {
91         trace.println(Trace.SESSION, "{0} : getClassNameFor({1})", sessionId, oid);
92         checkIsOpen("getClassNameFor");
93         LogicalOid floid = getFloidFromOid(oid);
94         return repository.getClassNameFromClid(new Short JavaDoc(floid.getClid()));
95     }
96
97     public Object JavaDoc makeNewFileOid(Class JavaDoc objectClass) {
98         return makeNewFileOid(objectClass.getName());
99     }
100
101     public Object JavaDoc makeNewFileOid(LogicalOid floid) {
102         throw new UnsupportedOperationException JavaDoc();
103     }
104
105     public Long JavaDoc reserveFloids(short clid, int number) {
106         throw new UnsupportedOperationException JavaDoc();
107     }
108
109     public Object JavaDoc makeNewFileOid(String JavaDoc objectClassName) {
110         trace.println(Trace.SESSION, "makeNewFileOid");
111         checkValidity("makeNewFileOid", true);
112         return identityProvider.makeNewFileOid(objectClassName);
113     }
114
115     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, Class JavaDoc objectClass) {
116         return createObject(objectToCreate, objectClass.getName());
117     }
118
119     public Object JavaDoc createObject(Object JavaDoc[] objectToCreate, String JavaDoc objectClassName) {
120         return createObject(makeNewFileOid(objectClassName), objectToCreate);
121     }
122
123     public Object JavaDoc createObject(Object JavaDoc oid, Object JavaDoc[] objectToCreate) {
124         checkValidity("createObject", true);
125         identityProvider.useFloid(oid);
126         createdObjects.put(oid, objectToCreate);
127         return oid;
128     }
129
130     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid) {
131         return readObjectByOid(oid, true);
132     }
133
134     public Collection readObjectsByOids(Collection oids) {
135         trace.println(Trace.SESSION, "{0} : readObjectsByOids({1})", sessionId, oids);
136         checkValidity("readObjectsByOids", true);
137         HashMap readed = new HashMap();
138         ArrayList toRead = new ArrayList();
139         Iterator oidsIterator = oids.iterator();
140         while (oidsIterator.hasNext()) {
141             Object JavaDoc oid = oidsIterator.next();
142             Object JavaDoc result = cache.get(oid);
143             if (result == null) {
144                 result = updatedObjects.get(oid);
145                 if (result == null) {
146                     result = createdObjects.get(oid);
147                 }
148             }
149             if (result == null) {
150                 toRead.add(oid);
151             } else {
152                 readed.put(oid, result);
153             }
154         }
155         Iterator fromServer = connexion.readObjectsByOids(oids).iterator();
156         oidsIterator = oids.iterator();
157         while (fromServer.hasNext() && oidsIterator.hasNext()) {
158             readed.put(oidsIterator.next(), fromServer.next());
159         }
160         ArrayList result = new ArrayList();
161         oidsIterator = oids.iterator();
162         while (oidsIterator.hasNext()) {
163             result.add(readed.get(oidsIterator.next()));
164         }
165         return result;
166     }
167
168     public boolean contains(Object JavaDoc oid) {
169         throw new UnsupportedOperationException JavaDoc("not yet implemented");
170     }
171
172     public Object JavaDoc updateObjectByOid(Object JavaDoc oid, Object JavaDoc[] objectToUpdate) {
173         trace.println(Trace.SESSION, "{0} : readObjectByOid({1}, {2})", sessionId, oid, objectToUpdate);
174         checkValidity("updateObjectByOid", true);
175         if (deletedObjects.containsKey(oid)) {
176             throw new IdentityException("this object has been deleted during the transaction");
177         }
178         cache.remove(oid);
179         identityProvider.useFloid(oid);
180         if (createdObjects.containsKey(oid)) {
181             createdObjects.put(oid, objectToUpdate);
182             return oid;
183         }
184         updatedObjects.put(oid, objectToUpdate);
185         return oid;
186     }
187
188     public void deleteObjectByOid(Object JavaDoc oid) {
189         trace.println(Trace.SESSION, "{0} : deleteObjectByOid({1})", sessionId, oid);
190         checkValidity("deleteObjectByOid", true);
191         if (deletedObjects.containsKey(oid)) {
192             throw new IdentityException("this object has already been deleted during the transaction");
193         }
194         cache.remove(oid);
195         identityProvider.useFloid(oid);
196         createdObjects.remove(oid);
197         updatedObjects.remove(oid);
198         deletedObjects.put(oid, null);
199     }
200
201     public Extent getExtent(String JavaDoc fullClassName) {
202         trace.println(Trace.SESSION, "getExtent(String {0})", fullClassName);
203         checkValidity("getExtent", true);
204         return new ExtentImpl(repository.getClidFromClassName(fullClassName), this);
205     }
206
207     public Extent getExtent(Class JavaDoc theClass) {
208         String JavaDoc fullClassName = theClass.getName();
209         return getExtent(fullClassName);
210     }
211
212     public boolean isNewBase() {
213         return false;
214     }
215
216     public void reorganize() {
217         if (clientProperties.allowsSpecialFunctionnalities()) {
218             trace.println(Trace.SESSION, "{0} : reorganize()", sessionId);
219             if (isOpen) {
220                 throw new TransactionException("Session must not be open during reorganize()");
221             }
222             if (transaction.isActive()) {
223                 throw new JalistoException("Transaction must not be active during reorganize()");
224             }
225             connexion.reorganize();
226         }
227     }
228
229     public QueryManager getQueryManager() {
230         return null;
231     }
232
233     public Transaction currentTransaction() {
234         return transaction;
235     }
236
237     public MetaRepository getMetaRepository() {
238         checkIsOpen("getMetaRepository");
239         return repository;
240     }
241
242     public SessionInternal getInternalSession() {
243         return this;
244     }
245
246     public void openSession() {
247         if (isOpen) {
248             throw new TransactionException("Session is already open");
249         }
250         connexion.openSession();
251         ((InternalMetaRepositoryClientImpl)repository).init();
252         identityProvider = new RemoteIdentityProvider(connexion, repository);
253         isOpen = true;
254     }
255
256     public void closeSession() {
257         if (!isOpen) {
258             throw new TransactionException("Session is not open");
259         }
260         connexion.closeSession();
261         isOpen = false;
262     }
263
264     public boolean isOpen() {
265         return isOpen;
266     }
267
268     public void eraseStorage() {
269         if (clientProperties.allowsSpecialFunctionnalities()) {
270             trace.println(Trace.SESSION, "{0} : eraseStorage()", sessionId);
271             if (isOpen) {
272                 throw new TransactionException("Session must not be open during eraseStorage()");
273             }
274             if (transaction.isActive()) {
275                 throw new JalistoException("Transaction must not be active during eraseStorage()");
276             }
277             connexion.eraseStorage();
278         }
279     }
280
281
282     /**
283      * ***************************** INTERNAL ***************************************************
284      */

285
286     public void checkValidity(String JavaDoc message, boolean mustBeActive) {
287         checkIsOpen(message);
288         if (mustBeActive) {
289             if (!transaction.isActive()) {
290                 throw new TransactionException("Transaction must be active during " + message);
291             }
292         } else {
293             if (transaction.isActive()) {
294                 throw new TransactionException("Transaction must not be active during " + message);
295             }
296         }
297     }
298
299     public boolean isRemoteSession() {
300         return true;
301     }
302
303     public void begin() {
304         trace.println(Trace.SESSION, "{0} : begin()", sessionId);
305         checkIsOpen("begin");
306         connexion.begin();
307     }
308
309     public void clearObjectCache() {
310         trace.println(Trace.SESSION, "{0} : clearObjectCache()", sessionId);
311         checkIsOpen("clearObjectCache");
312         cache.clear();
313     }
314
315     public void commit() {
316         trace.println(Trace.SESSION, "{0} : commit()", sessionId);
317         checkIsOpen("commit");
318         identityProvider.commit();
319         if (!createdObjects.isEmpty()) {
320             connexion.createObjects(createdObjects);
321             createdObjects.clear();
322         }
323         if (!updatedObjects.isEmpty()) {
324             connexion.updateObjects(updatedObjects);
325             updatedObjects.clear();
326         }
327         if (!deletedObjects.isEmpty()) {
328             connexion.deleteObjects(deletedObjects.keySet());
329             deletedObjects.clear();
330         }
331         connexion.commit();
332     }
333
334     public Collection getAllClassNames() {
335         return repository.getAllClassNames();
336     }
337
338     public LogicalSystemPageAccess getFileAccess() {
339         return null;
340     }
341
342     public LockManager getLockManager() {
343         return null;
344     }
345
346     public OidTable getOidTable() {
347         return null;
348     }
349
350     public JalistoProperties getProperties() {
351         return clientProperties;
352     }
353
354     public JalistoProperties getClientProperties() {
355         return clientProperties;
356     }
357
358     public Object JavaDoc getSessionId() {
359         return sessionId;
360     }
361
362     public Object JavaDoc[] readObjectByOid(Object JavaDoc oid, boolean withCache) {
363         trace.println(Trace.SESSION, "{0} : readObjectByOid({1})", sessionId, oid);
364         checkValidity("readObjectByOid", true);
365         if (deletedObjects.containsKey(oid)) {
366             throw new IdentityException("this object has been deleted during the transaction");
367         }
368         Object JavaDoc result = createdObjects.get(oid);
369         if (result != null) {
370             return (Object JavaDoc[]) result;
371         }
372         result = updatedObjects.get(oid);
373         if (result != null) {
374             return (Object JavaDoc[]) result;
375         }
376         result = cache.get(oid);
377         if (result != null) {
378             return (Object JavaDoc[]) result;
379         }
380         result = connexion.readObjectByOid(oid, withCache);
381         cache.put(oid, result);
382         return (Object JavaDoc[]) result;
383     }
384
385     public Object JavaDoc[] refreshObjectByOid(Object JavaDoc oid) {
386         trace.println(Trace.SESSION, "{0} : refreshObjectByOid({1})", sessionId, oid);
387         checkValidity("refreshObjectByOid", true);
388         if (deletedObjects.containsKey(oid)) {
389             throw new IdentityException("this object has been deleted during the transaction");
390         }
391         Object JavaDoc[] result = connexion.refreshObjectByOid(oid);
392         if (result != null) {
393             if (cache.containsKey(oid)) {
394                 cache.put(oid, result);
395                 return result;
396             }
397             if (createdObjects.containsKey(oid)) {
398                 createdObjects.put(oid, result);
399                 return result;
400             }
401             if (updatedObjects.containsKey(oid)) {
402                 updatedObjects.put(oid, result);
403                 return result;
404             }
405         }
406         return result;
407     }
408
409     public void rollback() {
410         trace.println(Trace.SESSION, "{0} : rollback()", sessionId);
411         checkIsOpen("rollback");
412         identityProvider.rollback();
413         createdObjects.clear();
414         updatedObjects.clear();
415         deletedObjects.clear();
416         connexion.rollback();
417     }
418
419     public void setOptimistic() {
420     }
421
422     public void setPessimistic() {
423     }
424
425     public Collection getFloidsFromClid(Object JavaDoc sessionId, Object JavaDoc clid) {
426         short c = ((Short JavaDoc)clid).shortValue();
427         ArrayList floids = new ArrayList(connexion.getFloidsFromClid(sessionId, clid));
428         identityProvider.addFloidForClid(floids, c);
429         Iterator createdFloids = createdObjects.keySet().iterator();
430         while(createdFloids.hasNext()) {
431             LogicalOid floid = (LogicalOid) createdFloids.next();
432             if (floid.getClid() == c) {
433                 floids.add(floid);
434             }
435         }
436         floids.removeAll(deletedObjects.keySet());
437         return floids;
438     }
439
440     /**
441      * ************************************** PRIVATE ***********************************************
442      */

443
444
445     private Transaction createTransaction() {
446         return new TransactionImpl(this);
447     }
448
449     private void checkIsOpen(String JavaDoc message) {
450         if (!isOpen) {
451             throw new TransactionException("Session must be open during " + message);
452         }
453     }
454
455     protected LogicalOid getFloidFromOid(Object JavaDoc oid) {
456         try {
457             return (LogicalOid) oid;
458         } catch (ClassCastException JavaDoc cce) {
459             throw new IdentityException("oid is not a FileLoid but a " + oid.getClass().getName());
460         }
461     }
462
463
464     private Transaction transaction;
465     private boolean isOpen;
466
467     private JalistoProperties clientProperties;
468     private Object JavaDoc sessionId;
469     private Map cache;
470     private HashMap createdObjects;
471     private HashMap updatedObjects;
472     private HashMap deletedObjects;
473     private InternalMetaRepository repository;
474     private RemoteIdentityProvider identityProvider;
475     private ClientCommunicationAgent connexion;
476     private Trace trace;
477 }
478
Popular Tags