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