001: package org.apache.ojb.broker.prevayler;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.Serializable;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Enumeration;
024: import java.util.Iterator;
025:
026: import org.apache.ojb.broker.Identity;
027: import org.apache.ojb.broker.ManageableCollection;
028: import org.apache.ojb.broker.MtoNImplementor;
029: import org.apache.ojb.broker.PBKey;
030: import org.apache.ojb.broker.PersistenceBrokerException;
031: import org.apache.ojb.broker.PersistenceBrokerFactory;
032: import org.apache.ojb.broker.TransactionAbortedException;
033: import org.apache.ojb.broker.TransactionInProgressException;
034: import org.apache.ojb.broker.TransactionNotInProgressException;
035: import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
036: import org.apache.ojb.broker.core.PersistenceBrokerImpl;
037: import org.apache.ojb.broker.query.Query;
038: import org.apache.ojb.broker.query.QueryByIdentity;
039: import org.apache.ojb.broker.util.BrokerHelper;
040: import org.apache.ojb.broker.util.ObjectModification;
041: import org.prevayler.Command;
042: import org.prevayler.Prevayler;
043: import org.prevayler.implementation.SnapshotPrevayler;
044:
045: /**
046: * An OJB PersistenBroker implementation working against a datastore
047: * that is persisted by Prevayler.
048: * So in other word, this is an OODBMS with a PB API.
049: * Of course you can use OJB/ODMG or OJB/JDO on top of it.
050: *
051: * important note: this implementation is not finished.
052: * Do not try to use it in production environments.
053: *
054: * @author Thomas Mahler
055: */
056: public class PBPrevaylerImpl extends PersistenceBrokerImpl {
057:
058: private transient Database db;
059:
060: private Prevayler prevayler;
061:
062: private ArrayList commandLog = new ArrayList(100);
063:
064: private boolean inTransaction = false;
065:
066: /**
067: * Constructor for PBPrevaylerImpl.
068: * @param key
069: * @param pbf
070: */
071: public PBPrevaylerImpl(PBKey key, PersistenceBrokerFactoryIF pbf) {
072: super (key, pbf);
073: refresh();
074: if (key == null)
075: throw new PersistenceBrokerException(
076: "Could not instantiate broker with PBKey 'null'");
077: this .pbf = pbf;
078: this .setPBKey(key);
079:
080: brokerHelper = new BrokerHelper(this );
081: //connectionManager = ConnectionManagerFactory.getInstance().createConnectionManager(this);
082: //objectCache = ObjectCacheFactory.getInstance().createObjectCache(this);
083: //sequenceManager = SequenceManagerFactory.getSequenceManager(this);
084: //dbAccess = JdbcAccessFactory.getInstance().createJdbcAccess(this);
085: //statementManager = StatementManagerFactory.getInstance().createStatementManager(this);
086: //sqlGenerator = SqlGeneratorFactory.getInstance().createSqlGenerator(connectionManager.getSupportedPlatform());
087:
088: //markedForDelete = new ArrayList();
089: try {
090: prevayler = new SnapshotPrevayler(new Database(),
091: "PrevalenceBase" + File.separator + "Database");
092: db = (Database) prevayler.system();
093: db.setBroker(this );
094: } catch (Exception e) {
095: }
096: }
097:
098: /**
099: * @see org.apache.ojb.broker.PersistenceBroker#abortTransaction()
100: */
101: public void abortTransaction()
102: throws TransactionNotInProgressException {
103: if (!isInTransaction()) {
104: throw new TransactionNotInProgressException();
105: }
106: inTransaction = false;
107: commandLog.clear();
108: }
109:
110: /**
111: * @see org.apache.ojb.broker.PersistenceBroker#beginTransaction()
112: */
113: public void beginTransaction()
114: throws TransactionInProgressException,
115: TransactionAbortedException {
116: if (this .isInTransaction()) {
117: throw new TransactionInProgressException();
118: }
119: inTransaction = true;
120: commandLog.clear();
121: }
122:
123: /**
124: * @see org.apache.ojb.broker.PersistenceBroker#commitTransaction()
125: */
126: public void commitTransaction()
127: throws TransactionNotInProgressException,
128: TransactionAbortedException {
129: if (!isInTransaction()) {
130: throw new TransactionNotInProgressException();
131: }
132:
133: Iterator iter = commandLog.iterator();
134: try {
135: while (iter.hasNext()) {
136: Command cmd = (Command) iter.next();
137: prevayler.executeCommand(cmd);
138: }
139: } catch (Exception e) {
140: this .abortTransaction();
141: }
142: inTransaction = false;
143: commandLog.clear();
144: }
145:
146: /**
147: * @see org.apache.ojb.broker.PersistenceBroker#isInTransaction()
148: */
149: public boolean isInTransaction() throws PersistenceBrokerException {
150: return inTransaction;
151: }
152:
153: /**
154: * @see org.apache.ojb.broker.PersistenceBroker#close()
155: */
156: public boolean close() {
157: if (isInTransaction()) {
158: abortTransaction();
159: }
160: try {
161: ((SnapshotPrevayler) prevayler).takeSnapshot();
162: } catch (IOException e) {
163: }
164: setClosed(true);
165: return true;
166: }
167:
168: /**
169: * @see org.apache.ojb.broker.PersistenceBroker#clearCache()
170: */
171: public void clearCache() throws PersistenceBrokerException {
172: }
173:
174: /**
175: * @see org.apache.ojb.broker.PersistenceBroker#removeFromCache(Object)
176: */
177: public void removeFromCache(Object obj)
178: throws PersistenceBrokerException {
179: }
180:
181: /**
182: * @see org.apache.ojb.broker.PersistenceBroker#store(Object, ObjectModification)
183: */
184: public void store(Object obj, ObjectModification modification)
185: throws PersistenceBrokerException {
186: this .store(obj);
187: }
188:
189: /**
190: * @see org.apache.ojb.broker.PersistenceBroker#store(Object)
191: */
192: public void store(Object obj) throws PersistenceBrokerException {
193: if (!(obj instanceof Serializable)) {
194: throw new PersistenceBrokerException(obj.getClass()
195: .getName()
196: + "does not implement java.io.Serializable.");
197: }
198:
199: CommandStore cmd = new CommandStore(obj);
200: commandLog.add(cmd);
201: }
202:
203: /**
204: * @see org.apache.ojb.broker.PersistenceBroker#delete(Object)
205: */
206: public void delete(Object obj) throws PersistenceBrokerException {
207: Command cmd = new CommandDelete(obj);
208: commandLog.add(cmd);
209: }
210:
211: /**
212: * @see org.apache.ojb.broker.PersistenceBroker#deleteMtoNImplementor(MtoNImplementor)
213: */
214: public void deleteMtoNImplementor(MtoNImplementor m2nImpl)
215: throws PersistenceBrokerException {
216: }
217:
218: /**
219: * @see org.apache.ojb.broker.PersistenceBroker#addMtoNImplementor(MtoNImplementor)
220: */
221: public void addMtoNImplementor(MtoNImplementor m2nImpl)
222: throws PersistenceBrokerException {
223: }
224:
225: /**
226: * @see org.apache.ojb.broker.PersistenceBroker#deleteByQuery(Query)
227: */
228: public void deleteByQuery(Query query)
229: throws PersistenceBrokerException {
230: throw new PersistenceBrokerException("not yet implemented");
231: }
232:
233: /**
234: * @see org.apache.ojb.broker.PersistenceBroker#retrieveAllReferences(Object)
235: */
236: public void retrieveAllReferences(Object pInstance)
237: throws PersistenceBrokerException {
238: }
239:
240: /**
241: * @see org.apache.ojb.broker.PersistenceBroker#retrieveReference(Object, String)
242: */
243: public void retrieveReference(Object pInstance,
244: String pAttributeName) throws PersistenceBrokerException {
245: }
246:
247: /**
248: * @see org.apache.ojb.broker.PersistenceBroker#getCount(Query)
249: */
250: public int getCount(Query query) throws PersistenceBrokerException {
251: throw new PersistenceBrokerException("not yet implemented");
252: }
253:
254: /**
255: * @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Query)
256: */
257: public Collection getCollectionByQuery(Query query)
258: throws PersistenceBrokerException {
259: // needs some more work ;-)
260: return db.getTable().values();
261: }
262:
263: /**
264: * @see org.apache.ojb.broker.PersistenceBroker#getCollectionByQuery(Class, Query)
265: */
266: public ManageableCollection getCollectionByQuery(
267: Class collectionClass, Query query)
268: throws PersistenceBrokerException {
269: throw new PersistenceBrokerException("not yet implemented");
270: }
271:
272: /**
273: * @see org.apache.ojb.broker.PersistenceBroker#getIteratorByQuery(Query)
274: */
275: public Iterator getIteratorByQuery(Query query)
276: throws PersistenceBrokerException {
277: throw new PersistenceBrokerException("not yet implemented");
278: }
279:
280: /**
281: * @see org.apache.ojb.broker.PersistenceBroker#getReportQueryIteratorByQuery(Query)
282: */
283: public Iterator getReportQueryIteratorByQuery(Query query)
284: throws PersistenceBrokerException {
285: throw new PersistenceBrokerException("not yet implemented");
286: }
287:
288: /**
289: * @see org.apache.ojb.broker.PersistenceBroker#getObjectByIdentity(Identity)
290: */
291: public Object getObjectByIdentity(Identity id)
292: throws PersistenceBrokerException {
293: return db.lookupObjectByIdentity(id);
294: }
295:
296: /**
297: * @see org.apache.ojb.broker.PersistenceBroker#getObjectByQuery(Query)
298: */
299: public Object getObjectByQuery(Query query)
300: throws PersistenceBrokerException {
301: if (query instanceof QueryByIdentity) {
302: Object id = ((QueryByIdentity) query).getExampleObject();
303: if (!(id instanceof Identity)) {
304: id = new Identity(id, PersistenceBrokerFactory
305: .defaultPersistenceBroker());
306: }
307: Identity oid = (Identity) id;
308: return db.lookupObjectByIdentity(oid);
309: } else {
310: throw new PersistenceBrokerException("not yet implemented");
311: }
312: }
313:
314: /**
315: * @see org.apache.ojb.broker.PersistenceBroker#getPKEnumerationByQuery(Class, Query)
316: */
317: public Enumeration getPKEnumerationByQuery(Class PrimaryKeyClass,
318: Query query) throws PersistenceBrokerException {
319: throw new PersistenceBrokerException("not yet implemented");
320: }
321:
322: }
|