KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > persistence > InternalCMPEntityManagerExtended


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.persistence;
19
20 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.EntityTransaction;
24 import javax.persistence.FlushModeType;
25 import javax.persistence.LockModeType;
26 import javax.persistence.Query;
27
28 import org.apache.geronimo.transaction.manager.TransactionImpl;
29 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
30
31 /**
32  * InternalCMPEntityManagerExtended is an EntityManager wrapper that CMPEntityManagerExtended wraps the
33  * real EntityManager with and registers with the transaction.
34  *
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class InternalCMPEntityManagerExtended implements EntityManager, EntityManagerWrapper {
38
39     private final EntityManager entityManager;
40     private final String JavaDoc persistenceUnit;
41     private final TransactionManagerImpl transactionManager;
42     //Does this need to be thread safe???
43
private final AtomicInteger JavaDoc count = new AtomicInteger JavaDoc();
44
45     public InternalCMPEntityManagerExtended(EntityManager entityManager, String JavaDoc persistenceUnit, TransactionManagerImpl transactionManager) {
46         this.entityManager = entityManager;
47         this.persistenceUnit = persistenceUnit;
48         this.transactionManager = transactionManager;
49         if (transactionManager.getTransaction() != null) {
50             joinTransaction();
51         }
52     }
53
54     void registerBean() {
55         count.getAndIncrement();
56     }
57
58     void beanRemoved() {
59         if (count.decrementAndGet() ==0 ) {
60             entityManager.close();
61             EntityManagerExtendedRegistry.clearEntityManager(persistenceUnit);
62         }
63
64     }
65
66     public EntityManager getEntityManager() {
67         return entityManager;
68     }
69
70     public void persist(Object JavaDoc o) {
71         entityManager.persist(o);
72     }
73
74     public <T>T merge(T t) {
75         return entityManager.merge(t);
76     }
77
78     public void remove(Object JavaDoc o) {
79         entityManager.remove(o);
80     }
81
82     public <T>T find(Class JavaDoc<T> aClass, Object JavaDoc o) {
83         return entityManager.find(aClass, o);
84     }
85
86     public <T>T getReference(Class JavaDoc<T> aClass, Object JavaDoc o) {
87         return entityManager.getReference(aClass, o);
88     }
89
90     public void flush() {
91         entityManager.flush();
92     }
93
94     public void setFlushMode(FlushModeType flushModeType) {
95         entityManager.setFlushMode(flushModeType);
96     }
97
98     public FlushModeType getFlushMode() {
99         return entityManager.getFlushMode();
100     }
101
102     public void lock(Object JavaDoc o, LockModeType lockModeType) {
103         entityManager.lock(o, lockModeType);
104     }
105
106     public void refresh(Object JavaDoc o) {
107         entityManager.refresh(o);
108     }
109
110     public void clear() {
111         entityManager.clear();
112     }
113
114     public boolean contains(Object JavaDoc o) {
115         return entityManager.contains(o);
116     }
117
118     public Query createQuery(String JavaDoc s) {
119         return entityManager.createQuery(s);
120     }
121
122     public Query createNamedQuery(String JavaDoc s) {
123         return entityManager.createNamedQuery(s);
124     }
125
126     public Query createNativeQuery(String JavaDoc s) {
127         return entityManager.createNativeQuery(s);
128     }
129
130     public Query createNativeQuery(String JavaDoc s, Class JavaDoc aClass) {
131         return entityManager.createNativeQuery(s, aClass);
132     }
133
134     public Query createNativeQuery(String JavaDoc s, String JavaDoc s1) {
135         return entityManager.createNativeQuery(s, s1);
136     }
137
138     public void close() {
139         //a no-op
140
}
141
142     public boolean isOpen() {
143         return true;
144     }
145
146     public EntityTransaction getTransaction() {
147         throw new IllegalStateException JavaDoc("You cannot call getTransaction on a container managed EntityManager");
148     }
149
150     public void joinTransaction() {
151             TransactionImpl transaction = (TransactionImpl) transactionManager.getTransaction();
152             //This checks section 5.6.3.1, throwing an EJBException if there is already a PersistenceContext.
153
transaction.setEntityManager(persistenceUnit, this);
154         entityManager.joinTransaction();
155     }
156
157     public Object JavaDoc getDelegate() {
158         return entityManager.getDelegate();
159     }
160
161 }
162
Popular Tags