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: */package org.apache.geronimo.persistence;
017:
018: import java.util.concurrent.atomic.AtomicInteger;
019:
020: import javax.ejb.EJBException;
021: import javax.persistence.*;
022: import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
023:
024: /**
025: * InternalCMPEntityManagerExtended is an EntityManager wrapper that CMPEntityManagerExtended wraps the
026: * real EntityManager with and registers with the transaction.
027: *
028: * @version $Rev: 552073 $ $Date: 2007-06-29 18:10:51 -0700 (Fri, 29 Jun 2007) $
029: */
030: public class InternalCMPEntityManagerExtended implements EntityManager,
031: EntityManagerWrapper {
032:
033: private final EntityManager entityManager;
034: private final String persistenceUnit;
035: private final TransactionManagerImpl transactionManager;
036: //Does this need to be thread safe???
037: private final AtomicInteger count = new AtomicInteger();
038:
039: public InternalCMPEntityManagerExtended(
040: EntityManager entityManager, String persistenceUnit,
041: TransactionManagerImpl transactionManager) {
042: this .entityManager = entityManager;
043: this .persistenceUnit = persistenceUnit;
044: this .transactionManager = transactionManager;
045: if (transactionManager.getTransaction() != null) {
046: joinTransaction();
047: }
048: }
049:
050: void registerBean() {
051: count.getAndIncrement();
052: }
053:
054: void beanRemoved() {
055: if (count.decrementAndGet() == 0) {
056: entityManager.close();
057: EntityManagerExtendedRegistry
058: .clearEntityManager(persistenceUnit);
059: }
060:
061: }
062:
063: public EntityManager getEntityManager() {
064: return entityManager;
065: }
066:
067: public void persist(Object o) {
068: entityManager.persist(o);
069: }
070:
071: public <T> T merge(T t) {
072: return entityManager.merge(t);
073: }
074:
075: public void remove(Object o) {
076: entityManager.remove(o);
077: }
078:
079: public <T> T find(Class<T> aClass, Object o) {
080: return entityManager.find(aClass, o);
081: }
082:
083: public <T> T getReference(Class<T> aClass, Object o) {
084: return entityManager.getReference(aClass, o);
085: }
086:
087: public void flush() {
088: entityManager.flush();
089: }
090:
091: public void setFlushMode(FlushModeType flushModeType) {
092: entityManager.setFlushMode(flushModeType);
093: }
094:
095: public FlushModeType getFlushMode() {
096: return entityManager.getFlushMode();
097: }
098:
099: public void lock(Object o, LockModeType lockModeType) {
100: entityManager.lock(o, lockModeType);
101: }
102:
103: public void refresh(Object o) {
104: entityManager.refresh(o);
105: }
106:
107: public void clear() {
108: entityManager.clear();
109: }
110:
111: public boolean contains(Object o) {
112: return entityManager.contains(o);
113: }
114:
115: public Query createQuery(String s) {
116: return entityManager.createQuery(s);
117: }
118:
119: public Query createNamedQuery(String s) {
120: return entityManager.createNamedQuery(s);
121: }
122:
123: public Query createNativeQuery(String s) {
124: return entityManager.createNativeQuery(s);
125: }
126:
127: public Query createNativeQuery(String s, Class aClass) {
128: return entityManager.createNativeQuery(s, aClass);
129: }
130:
131: public Query createNativeQuery(String s, String s1) {
132: return entityManager.createNativeQuery(s, s1);
133: }
134:
135: public void close() {
136: //a no-op
137: }
138:
139: public boolean isOpen() {
140: return true;
141: }
142:
143: public EntityTransaction getTransaction() {
144: throw new IllegalStateException(
145: "You cannot call getTransaction on a container managed EntityManager");
146: }
147:
148: public void joinTransaction() {
149: //This checks section 5.6.3.1, throwing an EJBException if there is already a PersistenceContext.
150: if (transactionManager.getResource(persistenceUnit) != null) {
151: throw new EJBException("EntityManager "
152: + transactionManager.getResource(persistenceUnit)
153: + " for persistenceUnit " + persistenceUnit
154: + " already associated with this transaction "
155: + transactionManager.getTransactionKey());
156: }
157: transactionManager.putResource(persistenceUnit, this );
158: entityManager.joinTransaction();
159: }
160:
161: public Object getDelegate() {
162: return entityManager.getDelegate();
163: }
164:
165: public void beforeCompletion() {
166: }
167:
168: public void afterCompletion(int i) {
169: //close is a no-op
170: }
171: }
|