001: package org.apache.ojb.odmg;
002:
003: /* Copyright 2002-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 org.apache.ojb.broker.OJBRuntimeException;
019: import org.apache.ojb.broker.util.configuration.ConfigurationException;
020: import org.apache.ojb.broker.util.logging.Logger;
021: import org.apache.ojb.broker.util.logging.LoggerFactory;
022: import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
023: import org.odmg.DArray;
024: import org.odmg.DBag;
025: import org.odmg.DList;
026: import org.odmg.DMap;
027: import org.odmg.DSet;
028: import org.odmg.Database;
029: import org.odmg.Implementation;
030: import org.odmg.Transaction;
031:
032: /**
033: * Implementation of the ODMG {@link Implementation} interface for use in
034: * managed enviroments.
035: *
036: * @version $Id: ImplementationJTAImpl.java,v 1.2.2.3 2005/12/21 22:29:21 tomdz Exp $
037: */
038: public class ImplementationJTAImpl extends ImplementationImpl {
039: private Logger log = LoggerFactory
040: .getLogger(ImplementationJTAImpl.class);
041:
042: protected ImplementationJTAImpl() {
043: super ();
044: }
045:
046: public Database getDatabase(Object obj) {
047: beginInternTransaction();
048: return this .getCurrentDatabase();
049: }
050:
051: public Transaction currentTransaction() {
052: beginInternTransaction();
053: /*
054: we wrap the intern odmg transaction to avoid unauthorised calls
055: since we use proprietary extensions for Transaction interface
056: do cast to enhanced interface
057: */
058: return new NarrowTransaction((TransactionImpl) super
059: .currentTransaction());
060: }
061:
062: public EnhancedOQLQuery newOQLQuery() {
063: beginInternTransaction();
064: return super .newOQLQuery();
065: }
066:
067: public DList newDList() {
068: beginInternTransaction();
069: return super .newDList();
070: }
071:
072: public DBag newDBag() {
073: beginInternTransaction();
074: return super .newDBag();
075: }
076:
077: public DSet newDSet() {
078: beginInternTransaction();
079: return super .newDSet();
080: }
081:
082: public DArray newDArray() {
083: beginInternTransaction();
084: return super .newDArray();
085: }
086:
087: public DMap newDMap() {
088: beginInternTransaction();
089: return super .newDMap();
090: }
091:
092: /**
093: * Here we start a intern odmg-Transaction to hide transaction demarcation
094: * This method could be invoked several times within a transaction, but only
095: * the first call begin a intern odmg transaction
096: */
097: private void beginInternTransaction() {
098: if (log.isDebugEnabled())
099: log.debug("beginInternTransaction was called");
100: J2EETransactionImpl tx = (J2EETransactionImpl) super
101: .currentTransaction();
102: if (tx == null)
103: tx = newInternTransaction();
104: if (!tx.isOpen()) {
105: // start the transaction
106: tx.begin();
107: tx.setInExternTransaction(true);
108: }
109: }
110:
111: /**
112: * Returns a new intern odmg-transaction for the current database.
113: */
114: private J2EETransactionImpl newInternTransaction() {
115: if (log.isDebugEnabled())
116: log.debug("obtain new intern odmg-transaction");
117: J2EETransactionImpl tx = new J2EETransactionImpl(this );
118: try {
119: getConfigurator().configure(tx);
120: } catch (ConfigurationException e) {
121: throw new OJBRuntimeException(
122: "Cannot create new intern odmg transaction", e);
123: }
124: return tx;
125: }
126:
127: /**
128: * Not supported in managed-environment.
129: */
130: public Transaction newTransaction() {
131: throw new UnsupportedOperationException(
132: "Not supported in managed environment");
133: }
134: }
|