KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.FlushModeType;
24 import javax.persistence.LockModeType;
25 import javax.persistence.Query;
26 import javax.persistence.EntityTransaction;
27 import javax.persistence.EntityManagerFactory;
28 import javax.persistence.TransactionRequiredException;
29 import javax.transaction.Status JavaDoc;
30
31 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
32 import org.apache.geronimo.transaction.manager.TransactionImpl;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class CMPEntityManagerTxScoped implements EntityManager {
38
39     private final TransactionManagerImpl transactionManager;
40     private final String JavaDoc persistenceUnit;
41     private final EntityManagerFactory entityManagerFactory;
42     private final Map JavaDoc entityManagerProperties;
43
44     public CMPEntityManagerTxScoped(TransactionManagerImpl transactionManager, String JavaDoc persistenceUnit, EntityManagerFactory entityManagerFactory, Map JavaDoc entityManagerProperties) {
45         this.transactionManager = transactionManager;
46         this.persistenceUnit = persistenceUnit;
47         this.entityManagerFactory = entityManagerFactory;
48         this.entityManagerProperties = entityManagerProperties;
49     }
50
51     private EntityManager getEntityManager(boolean activeRequired) {
52         TransactionImpl transaction = (TransactionImpl) transactionManager.getTransaction();
53         if (activeRequired && (transaction == null || transaction.getStatus() != Status.STATUS_ACTIVE)) {
54             throw new TransactionRequiredException("No active transaction");
55         }
56         if (transaction == null) {
57             return null;
58         }
59         EntityManagerWrapper entityManagerWrapper = (EntityManagerWrapper) transaction.getEntityManager(persistenceUnit);
60         if (entityManagerWrapper == null) {
61             EntityManager entityManager = createEntityManager();
62             entityManagerWrapper = new EntityManagerWrapperTxScoped(entityManager);
63             transaction.setEntityManager(persistenceUnit, entityManagerWrapper);
64         }
65         return entityManagerWrapper.getEntityManager();
66     }
67
68     private EntityManager createEntityManager() {
69         EntityManager entityManager;
70         if (entityManagerProperties == null) {
71             entityManager = entityManagerFactory.createEntityManager();
72         } else {
73             entityManager = entityManagerFactory.createEntityManager(entityManagerProperties);
74         }
75         return entityManager;
76     }
77
78
79     public void persist(Object JavaDoc o) {
80         EntityManager entityManager = getEntityManager(true);
81         if (entityManager != null) {
82             entityManager.persist(o);
83         } else {
84             entityManager = createEntityManager();
85             try {
86                 entityManager.persist(o);
87             } finally {
88                 entityManager.close();
89             }
90         }
91     }
92
93     public <T>T merge(T t) {
94         EntityManager entityManager = getEntityManager(true);
95         if (entityManager != null) {
96             return entityManager.merge(t);
97         } else {
98             entityManager = createEntityManager();
99             try {
100                 return entityManager.merge(t);
101             } finally {
102                 entityManager.close();
103             }
104         }
105     }
106
107     public void remove(Object JavaDoc o) {
108         EntityManager entityManager = getEntityManager(true);
109         if (entityManager != null) {
110             entityManager.remove(o);
111         } else {
112             entityManager = createEntityManager();
113             try {
114                 entityManager.remove(o);
115             } finally {
116                 entityManager.close();
117             }
118         }
119     }
120
121     public <T>T find(Class JavaDoc<T> aClass, Object JavaDoc o) {
122         EntityManager entityManager = getEntityManager(false);
123         if (entityManager != null) {
124             return entityManager.find(aClass, o);
125         } else {
126             entityManager = createEntityManager();
127             try {
128                 return entityManager.find(aClass, o);
129             } finally {
130                 entityManager.close();
131             }
132         }
133     }
134
135     public <T>T getReference(Class JavaDoc<T> aClass, Object JavaDoc o) {
136         EntityManager entityManager = getEntityManager(false);
137         if (entityManager != null) {
138             return entityManager.getReference(aClass, o);
139         } else {
140             entityManager = createEntityManager();
141             try {
142                 return entityManager.getReference(aClass, o);
143             } finally {
144                 entityManager.close();
145             }
146         }
147     }
148
149     public void flush() {
150         EntityManager entityManager = getEntityManager(false);
151         if (entityManager != null) {
152             entityManager.flush();
153         } else {
154             entityManager = createEntityManager();
155             try {
156                 entityManager.flush();
157             } finally {
158                 entityManager.close();
159             }
160         }
161     }
162
163     public void setFlushMode(FlushModeType flushModeType) {
164         EntityManager entityManager = getEntityManager(false);
165         if (entityManager != null) {
166              entityManager.setFlushMode(flushModeType);
167         } else {
168             entityManager = createEntityManager();
169             try {
170                  entityManager.setFlushMode(flushModeType);
171             } finally {
172                 entityManager.close();
173             }
174         }
175     }
176
177     public FlushModeType getFlushMode() {
178         EntityManager entityManager = getEntityManager(false);
179         if (entityManager != null) {
180             return entityManager.getFlushMode();
181         } else {
182             entityManager = createEntityManager();
183             try {
184                 return entityManager.getFlushMode();
185             } finally {
186                 entityManager.close();
187             }
188         }
189     }
190
191     public void lock(Object JavaDoc o, LockModeType lockModeType) {
192         EntityManager entityManager = getEntityManager(false);
193         if (entityManager != null) {
194              entityManager.lock(o, lockModeType);
195         } else {
196             entityManager = createEntityManager();
197             try {
198                  entityManager.lock(o, lockModeType);
199             } finally {
200                 entityManager.close();
201             }
202         }
203     }
204
205     public void refresh(Object JavaDoc o) {
206         EntityManager entityManager = getEntityManager(true);
207         if (entityManager != null) {
208              entityManager.refresh(o);
209         } else {
210             entityManager = createEntityManager();
211             try {
212                  entityManager.refresh(o);
213             } finally {
214                 entityManager.close();
215             }
216         }
217     }
218
219     public void clear() {
220         EntityManager entityManager = getEntityManager(false);
221         if (entityManager != null) {
222              entityManager.clear();
223         } else {
224             entityManager = createEntityManager();
225             try {
226                  entityManager.clear();
227             } finally {
228                 entityManager.close();
229             }
230         }
231     }
232
233     public boolean contains(Object JavaDoc o) {
234         EntityManager entityManager = getEntityManager(false);
235         if (entityManager != null) {
236             return entityManager.contains(o);
237         } else {
238             entityManager = createEntityManager();
239             try {
240                 return entityManager.contains(o);
241             } finally {
242                 entityManager.close();
243             }
244         }
245     }
246
247     public Query createQuery(String JavaDoc s) {
248         EntityManager entityManager = getEntityManager(false);
249         if (entityManager != null) {
250             return entityManager.createQuery(s);
251         } else {
252             entityManager = createEntityManager();
253             try {
254                 return entityManager.createQuery(s);
255             } finally {
256                 entityManager.close();
257             }
258         }
259     }
260
261     public Query createNamedQuery(String JavaDoc s) {
262         EntityManager entityManager = getEntityManager(false);
263         if (entityManager != null) {
264             return entityManager.createNamedQuery(s);
265         } else {
266             entityManager = createEntityManager();
267             try {
268                 return entityManager.createNamedQuery(s);
269             } finally {
270                 entityManager.close();
271             }
272         }
273     }
274
275     public Query createNativeQuery(String JavaDoc s) {
276         EntityManager entityManager = getEntityManager(false);
277         if (entityManager != null) {
278             return entityManager.createNativeQuery(s);
279         } else {
280             entityManager = createEntityManager();
281             try {
282                 return entityManager.createNativeQuery(s);
283             } finally {
284                 entityManager.close();
285             }
286         }
287     }
288
289     public Query createNativeQuery(String JavaDoc s, Class JavaDoc aClass) {
290         EntityManager entityManager = getEntityManager(false);
291         if (entityManager != null) {
292             return entityManager.createNativeQuery(s, aClass);
293         } else {
294             entityManager = createEntityManager();
295             try {
296                 return entityManager.createNativeQuery(s, aClass);
297             } finally {
298                 entityManager.close();
299             }
300         }
301     }
302
303     public Query createNativeQuery(String JavaDoc s, String JavaDoc s1) {
304         EntityManager entityManager = getEntityManager(false);
305         if (entityManager != null) {
306             return entityManager.createNativeQuery(s, s1);
307         } else {
308             entityManager = createEntityManager();
309             try {
310                 return entityManager.createNativeQuery(s, s1);
311             } finally {
312                 entityManager.close();
313             }
314         }
315     }
316
317     public void close() {
318         throw new IllegalStateException JavaDoc("You cannot call close on a Container Managed Entity Manager");
319     }
320
321     public boolean isOpen() {
322         return true;
323     }
324
325     public EntityTransaction getTransaction() {
326         throw new IllegalStateException JavaDoc("You cannot call getTransaction on a container managed EntityManager");
327     }
328
329     public void joinTransaction() {
330         throw new IllegalStateException JavaDoc("You cannot call joinTransaction on a container managed EntityManager");
331     }
332
333     public Object JavaDoc getDelegate() {
334         EntityManager entityManager = getEntityManager(false);
335         if (entityManager != null) {
336             return entityManager.getDelegate();
337         } else {
338             entityManager = createEntityManager();
339             try {
340                 return entityManager.getDelegate();
341             } finally {
342                 entityManager.close();
343             }
344         }
345     }
346
347     private static class EntityManagerWrapperTxScoped implements EntityManagerWrapper {
348         private final EntityManager entityManager;
349
350         public EntityManagerWrapperTxScoped(EntityManager entityManager) {
351             if (entityManager == null) {
352                 throw new IllegalArgumentException JavaDoc("Need a non-null entity manager");
353             }
354             this.entityManager = entityManager;
355         }
356
357         public void close() {
358                 entityManager.close();
359         }
360
361         public EntityManager getEntityManager() {
362             return entityManager;
363         }
364     }
365 }
366
Popular Tags