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.1 2007-08-24 22:17:37 ewestfal Exp $
037     */
038    public class ImplementationJTAImpl extends ImplementationImpl
039    {
040        private Logger log = LoggerFactory.getLogger(ImplementationJTAImpl.class);
041    
042        protected ImplementationJTAImpl()
043        {
044            super();
045        }
046    
047        public Database getDatabase(Object obj)
048        {
049            beginInternTransaction();
050            return this.getCurrentDatabase();
051        }
052    
053        public Transaction currentTransaction()
054        {
055            beginInternTransaction();
056            /*
057            we wrap the intern odmg transaction to avoid unauthorised calls
058            since we use proprietary extensions for Transaction interface
059            do cast to enhanced interface
060            */
061            return new NarrowTransaction((TransactionImpl) super.currentTransaction());
062        }
063    
064        public EnhancedOQLQuery newOQLQuery()
065        {
066            beginInternTransaction();
067            return super.newOQLQuery();
068        }
069    
070        public DList newDList()
071        {
072            beginInternTransaction();
073            return super.newDList();
074        }
075    
076        public DBag newDBag()
077        {
078            beginInternTransaction();
079            return super.newDBag();
080        }
081    
082        public DSet newDSet()
083        {
084            beginInternTransaction();
085            return super.newDSet();
086        }
087    
088        public DArray newDArray()
089        {
090            beginInternTransaction();
091            return super.newDArray();
092        }
093    
094        public DMap newDMap()
095        {
096            beginInternTransaction();
097            return super.newDMap();
098        }
099    
100        /**
101         * Here we start a intern odmg-Transaction to hide transaction demarcation
102         * This method could be invoked several times within a transaction, but only
103         * the first call begin a intern odmg transaction
104         */
105        private void beginInternTransaction()
106        {
107            if (log.isDebugEnabled()) log.debug("beginInternTransaction was called");
108            J2EETransactionImpl tx = (J2EETransactionImpl) super.currentTransaction();
109            if (tx == null) tx = newInternTransaction();
110            if (!tx.isOpen())
111            {
112                // start the transaction
113                tx.begin();
114                tx.setInExternTransaction(true);
115            }
116        }
117    
118        /**
119         * Returns a new intern odmg-transaction for the current database.
120         */
121        private J2EETransactionImpl newInternTransaction()
122        {
123            if (log.isDebugEnabled()) log.debug("obtain new intern odmg-transaction");
124            J2EETransactionImpl tx = new J2EETransactionImpl(this);
125            try
126            {
127                getConfigurator().configure(tx);
128            }
129            catch (ConfigurationException e)
130            {
131                throw new OJBRuntimeException("Cannot create new intern odmg transaction", e);
132            }
133            return tx;
134        }
135    
136        /**
137         * Not supported in managed-environment.
138         */
139        public Transaction newTransaction()
140        {
141            throw new UnsupportedOperationException("Not supported in managed environment");
142        }
143    }