001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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    package org.apache.geronimo.persistence;
019    
020    import java.util.Map;
021    
022    import javax.persistence.EntityManager;
023    import javax.persistence.EntityManagerFactory;
024    import javax.persistence.EntityTransaction;
025    import javax.persistence.FlushModeType;
026    import javax.persistence.LockModeType;
027    import javax.persistence.Query;
028    
029    import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
030    
031    /**
032     * @version $Rev: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
033     */
034    public class CMPEntityManagerExtended implements EntityManager {
035    
036        private final TransactionManagerImpl transactionManager;
037        private final String persistenceUnit;
038        private final EntityManagerFactory entityManagerFactory;
039        private final Map entityManagerProperties;
040        private final InternalCMPEntityManagerExtended entityManager;
041    
042        public CMPEntityManagerExtended(TransactionManagerImpl transactionManager, String persistenceUnit, EntityManagerFactory entityManagerFactory, Map entityManagerProperties) {
043            this.transactionManager = transactionManager;
044            this.persistenceUnit = persistenceUnit;
045            this.entityManagerFactory = entityManagerFactory;
046            this.entityManagerProperties = entityManagerProperties;
047            entityManager = getEntityManager();
048        }
049    
050        private InternalCMPEntityManagerExtended getEntityManager() {
051            InternalCMPEntityManagerExtended entityManager = EntityManagerExtendedRegistry.getEntityManager(persistenceUnit);
052            if (entityManager == null) {
053                entityManager = createEntityManager();
054                EntityManagerExtendedRegistry.putEntityManager(persistenceUnit, entityManager);
055            }
056            entityManager.registerBean();
057            return entityManager;
058        }
059    
060        private InternalCMPEntityManagerExtended createEntityManager() {
061            EntityManager entityManager;
062            if (entityManagerProperties == null) {
063                entityManager = entityManagerFactory.createEntityManager();
064            } else {
065                entityManager = entityManagerFactory.createEntityManager(entityManagerProperties);
066            }
067            return new InternalCMPEntityManagerExtended(entityManager, persistenceUnit, transactionManager);
068        }
069    
070        public void beanRemoved() {
071            entityManager.beanRemoved();
072        }
073    
074    
075        public void persist(Object o) {
076            entityManager.persist(o);
077        }
078    
079        public <T>T merge(T t) {
080            return entityManager.merge(t);
081        }
082    
083        public void remove(Object o) {
084            entityManager.remove(o);
085        }
086    
087        public <T>T find(Class<T> aClass, Object o) {
088            return entityManager.find(aClass, o);
089        }
090    
091        public <T>T getReference(Class<T> aClass, Object o) {
092            return entityManager.getReference(aClass, o);
093        }
094    
095        public void flush() {
096            entityManager.flush();
097        }
098    
099        public void setFlushMode(FlushModeType flushModeType) {
100            entityManager.setFlushMode(flushModeType);
101        }
102    
103        public FlushModeType getFlushMode() {
104            return entityManager.getFlushMode();
105        }
106    
107        public void lock(Object o, LockModeType lockModeType) {
108            entityManager.lock(o, lockModeType);
109        }
110    
111        public void refresh(Object o) {
112            entityManager.refresh(o);
113        }
114    
115        public void clear() {
116            entityManager.clear();
117        }
118    
119        public boolean contains(Object o) {
120            return entityManager.contains(o);
121        }
122    
123        public Query createQuery(String s) {
124            return entityManager.createQuery(s);
125        }
126    
127        public Query createNamedQuery(String s) {
128            return entityManager.createNamedQuery(s);
129        }
130    
131        public Query createNativeQuery(String s) {
132            return entityManager.createNativeQuery(s);
133        }
134    
135        public Query createNativeQuery(String s, Class aClass) {
136            return entityManager.createNativeQuery(s, aClass);
137        }
138    
139        public Query createNativeQuery(String s, String s1) {
140            return entityManager.createNativeQuery(s, s1);
141        }
142    
143        public void close() {
144            throw new IllegalStateException("You cannot call close on a Container Managed Entity Manager");
145        }
146    
147        public boolean isOpen() {
148            return true;
149        }
150    
151        public EntityTransaction getTransaction() {
152            throw new IllegalStateException("You cannot call getTransaction on a container managed EntityManager");
153        }
154    
155        public void joinTransaction() {
156            throw new IllegalStateException("You cannot call joinTransaction on a container managed EntityManager");
157        }
158    
159        public Object getDelegate() {
160            return entityManager.getDelegate();
161        }
162    
163    }