KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > prevayler > PBPrevaylerImpl


1 package org.apache.ojb.broker.prevayler;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.ojb.broker.Identity;
27 import org.apache.ojb.broker.ManageableCollection;
28 import org.apache.ojb.broker.MtoNImplementor;
29 import org.apache.ojb.broker.PBKey;
30 import org.apache.ojb.broker.PersistenceBrokerException;
31 import org.apache.ojb.broker.PersistenceBrokerFactory;
32 import org.apache.ojb.broker.TransactionAbortedException;
33 import org.apache.ojb.broker.TransactionInProgressException;
34 import org.apache.ojb.broker.TransactionNotInProgressException;
35 import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
36 import org.apache.ojb.broker.core.PersistenceBrokerImpl;
37 import org.apache.ojb.broker.query.Query;
38 import org.apache.ojb.broker.query.QueryByIdentity;
39 import org.apache.ojb.broker.util.BrokerHelper;
40 import org.apache.ojb.broker.util.ObjectModification;
41 import org.prevayler.Command;
42 import org.prevayler.Prevayler;
43 import org.prevayler.implementation.SnapshotPrevayler;
44
45
46 /**
47  * An OJB PersistenBroker implementation working against a datastore
48  * that is persisted by Prevayler.
49  * So in other word, this is an OODBMS with a PB API.
50  * Of course you can use OJB/ODMG or OJB/JDO on top of it.
51  *
52  * important note: this implementation is not finished.
53  * Do not try to use it in production environments.
54  *
55  * @author Thomas Mahler
56  */

57 public class PBPrevaylerImpl extends PersistenceBrokerImpl
58 {
59
60     private transient Database db;
61     
62     private Prevayler prevayler;
63
64     private ArrayList JavaDoc commandLog = new ArrayList JavaDoc(100);
65     
66     private boolean inTransaction = false;
67
68
69     /**
70      * Constructor for PBPrevaylerImpl.
71      * @param key
72      * @param pbf
73      */

74     public PBPrevaylerImpl(PBKey key, PersistenceBrokerFactoryIF pbf)
75     {
76         super(key, pbf);
77         refresh();
78         if(key == null) throw new PersistenceBrokerException("Could not instantiate broker with PBKey 'null'");
79         this.pbf = pbf;
80         this.setPBKey(key);
81
82         brokerHelper = new BrokerHelper(this);
83         //connectionManager = ConnectionManagerFactory.getInstance().createConnectionManager(this);
84
//objectCache = ObjectCacheFactory.getInstance().createObjectCache(this);
85
//sequenceManager = SequenceManagerFactory.getSequenceManager(this);
86
//dbAccess = JdbcAccessFactory.getInstance().createJdbcAccess(this);
87
//statementManager = StatementManagerFactory.getInstance().createStatementManager(this);
88
//sqlGenerator = SqlGeneratorFactory.getInstance().createSqlGenerator(connectionManager.getSupportedPlatform());
89

90         //markedForDelete = new ArrayList();
91
try
92         {
93              prevayler = new SnapshotPrevayler(new Database(), "PrevalenceBase" + File.separator + "Database");
94              db = (Database) prevayler.system();
95              db.setBroker(this);
96         }
97         catch (Exception JavaDoc e)
98         {
99         }
100     }
101
102
103     /**
104      * @see org.apache.ojb.broker.PersistenceBroker#abortTransaction()
105      */

106     public void abortTransaction() throws TransactionNotInProgressException
107     {
108         if (! isInTransaction())
109         {
110             throw new TransactionNotInProgressException();
111         }
112         inTransaction = false;
113         commandLog.clear();
114     }
115
116     /**
117      * @see org.apache.ojb.broker.PersistenceBroker#beginTransaction()
118      */

119     public void beginTransaction()
120         throws TransactionInProgressException, TransactionAbortedException
121     {
122         if (this.isInTransaction())
123         {
124             throw new TransactionInProgressException();
125         }
126         inTransaction = true;
127         commandLog.clear();
128     }
129
130     /**
131      * @see org.apache.ojb.broker.PersistenceBroker#commitTransaction()
132      */

133     public void commitTransaction()
134         throws TransactionNotInProgressException, TransactionAbortedException
135     {
136         if (!isInTransaction())
137         {
138             throw new TransactionNotInProgressException();
139         }
140
141         Iterator JavaDoc iter = commandLog.iterator();
142         try
143         {
144             while (iter.hasNext())
145             {
146                 Command cmd = (Command) iter.next();
147                 prevayler.executeCommand(cmd);
148             }
149         }
150         catch (Exception JavaDoc e)
151         {
152             this.abortTransaction();
153         }
154         inTransaction = false;
155         commandLog.clear();
156     }
157
158     /**
159      * @see org.apache.ojb.broker.PersistenceBroker#isInTransaction()
160      */

161     public boolean isInTransaction() throws PersistenceBrokerException
162     {
163         return inTransaction;
164     }
165
166     /**
167      * @see org.apache.ojb.broker.PersistenceBroker#close()
168      */

169     public boolean close()
170     {
171         if (isInTransaction())
172         {
173             abortTransaction();
174         }
175         try
176         {
177             ((SnapshotPrevayler)prevayler).takeSnapshot();
178         }
179         catch (IOException JavaDoc e)
180         {
181         }
182         setClosed(true);
183         return true;
184     }
185
186     /**
187      * @see org.apache.ojb.broker.PersistenceBroker#clearCache()
188      */

189     public void clearCache() throws PersistenceBrokerException
190     {
191     }
192
193     /**
194      * @see org.apache.ojb.broker.PersistenceBroker#removeFromCache(Object)
195      */

196     public void removeFromCache(Object JavaDoc obj) throws PersistenceBrokerException
197     {
198     }
199
200     /**
201      * @see org.apache.ojb.broker.PersistenceBroker#store(Object, ObjectModification)
202      */

203     public void store(Object JavaDoc obj, ObjectModification modification)
204         throws PersistenceBrokerException
205     {
206         this.store(obj);
207     }
208
209     /**
210      * @see org.apache.ojb.broker.PersistenceBroker#store(Object)
211      */

212     public void store(Object JavaDoc obj) throws PersistenceBrokerException
213     {
214         if (! (obj instanceof Serializable JavaDoc))
215         {
216             throw new PersistenceBrokerException(obj.getClass().getName() + "does not implement java.io.Serializable.");
217         }
218         
219         CommandStore cmd = new CommandStore(obj);
220         commandLog.add(cmd);
221     }
222
223     /**
224      * @see org.apache.ojb.broker.PersistenceBroker#delete(Object)
225      */

226     public void delete(Object JavaDoc obj) throws PersistenceBrokerException
227     {
228         Command cmd = new CommandDelete(obj);
229         commandLog.add(cmd);
230     }
231
232     /**
233      * @see org.apache.ojb.broker.PersistenceBroker#deleteMtoNImplementor(MtoNImplementor)
234      */

235     public void deleteMtoNImplementor(MtoNImplementor m2nImpl)
236         throws PersistenceBrokerException
237     {
238     }
239
240     /**
241      * @see org.apache.ojb.broker.PersistenceBroker#addMtoNImplementor(MtoNImplementor)
242      */

243     public void addMtoNImplementor(MtoNImplementor m2nImpl)
244         throws PersistenceBrokerException
245     {
246     }
247
248     /**
249      * @see org.apache.ojb.broker.PersistenceBroker#deleteByQuery(Query)
250      */

251     public void deleteByQuery(Query query) throws PersistenceBrokerException
252     {
253         throw new PersistenceBrokerException("not yet implemented");
254     }
255
256     /**
257      * @see org.apache.ojb.broker.PersistenceBroker#retrieveAllReferences(Object)
258      */

259     public void retrieveAllReferences(Object JavaDoc pInstance)
260         throws PersistenceBrokerException
261     {
262     }
263
264     /**
265      * @see org.apache.ojb.broker.PersistenceBroker#retrieveReference(Object, String)
266      */

267     public void retrieveReference(Object JavaDoc pInstance, String JavaDoc pAttributeName)
268         throws PersistenceBrokerException
269     {
270     }
271
272     /**
273      * @see org.apache.ojb.broker.PersistenceBroker#getCount(Query)
274      */

275     public int getCount(Query query) throws PersistenceBrokerException
276     {
277         throw new PersistenceBrokerException("not yet implemented");
278     }
279
280     /**
281      * @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Query)
282      */

283     public Collection JavaDoc getCollectionByQuery(Query query)
284         throws PersistenceBrokerException
285     {
286         // needs some more work ;-)
287
return db.getTable().values();
288     }
289
290     /**
291      * @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Class, Query)
292      */

293     public ManageableCollection getCollectionByQuery(
294         Class JavaDoc collectionClass,
295         Query query)
296         throws PersistenceBrokerException
297     {
298         throw new PersistenceBrokerException("not yet implemented");
299     }
300
301     /**
302      * @see org.apache.ojb.broker.PersistenceBroker#getIteratorByQuery(Query)
303      */

304     public Iterator JavaDoc getIteratorByQuery(Query query)
305         throws PersistenceBrokerException
306     {
307         throw new PersistenceBrokerException("not yet implemented");
308     }
309
310     /**
311      * @see org.apache.ojb.broker.PersistenceBroker#getReportQueryIteratorByQuery(Query)
312      */

313     public Iterator JavaDoc getReportQueryIteratorByQuery(Query query)
314         throws PersistenceBrokerException
315     {
316         throw new PersistenceBrokerException("not yet implemented");
317     }
318
319     /**
320      * @see org.apache.ojb.broker.PersistenceBroker#getObjectByIdentity(Identity)
321      */

322     public Object JavaDoc getObjectByIdentity(Identity id)
323         throws PersistenceBrokerException
324     {
325         return db.lookupObjectByIdentity(id);
326     }
327
328     /**
329      * @see org.apache.ojb.broker.PersistenceBroker#getObjectByQuery(Query)
330      */

331     public Object JavaDoc getObjectByQuery(Query query)
332         throws PersistenceBrokerException
333     {
334         if (query instanceof QueryByIdentity)
335         {
336             Object JavaDoc id = ((QueryByIdentity) query).getExampleObject();
337             if (! (id instanceof Identity))
338             {
339                 id = new Identity(id,PersistenceBrokerFactory.defaultPersistenceBroker());
340             }
341             Identity oid = (Identity) id;
342             return db.lookupObjectByIdentity(oid);
343         }
344         else
345         {
346             throw new PersistenceBrokerException("not yet implemented");
347         }
348     }
349
350     /**
351      * @see org.apache.ojb.broker.PersistenceBroker#getPKEnumerationByQuery(Class, Query)
352      */

353     public Enumeration JavaDoc getPKEnumerationByQuery(
354         Class JavaDoc PrimaryKeyClass,
355         Query query)
356         throws PersistenceBrokerException
357     {
358         throw new PersistenceBrokerException("not yet implemented");
359     }
360
361
362 }
363
Popular Tags