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.Map;
019:
020: import javax.persistence.*;
021: import javax.persistence.TransactionRequiredException;
022: import javax.transaction.*;
023: import org.apache.geronimo.transaction.manager.TransactionImpl;
024: import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
025:
026: /**
027: * @version $Rev: 552073 $ $Date: 2007-06-29 18:10:51 -0700 (Fri, 29 Jun 2007) $
028: */
029: public class CMPEntityManagerTxScoped implements EntityManager {
030:
031: private final TransactionManagerImpl transactionManager;
032: private final String persistenceUnit;
033: private final EntityManagerFactory entityManagerFactory;
034: private final Map entityManagerProperties;
035:
036: public CMPEntityManagerTxScoped(
037: TransactionManagerImpl transactionManager,
038: String persistenceUnit,
039: EntityManagerFactory entityManagerFactory,
040: Map entityManagerProperties) {
041: this .transactionManager = transactionManager;
042: this .persistenceUnit = persistenceUnit;
043: this .entityManagerFactory = entityManagerFactory;
044: this .entityManagerProperties = entityManagerProperties;
045: }
046:
047: private EntityManager getEntityManager(boolean activeRequired) {
048: TransactionImpl transaction = (TransactionImpl) transactionManager
049: .getTransaction();
050: if (activeRequired
051: && (transaction == null || transaction.getStatus() != Status.STATUS_ACTIVE)) {
052: throw new TransactionRequiredException(
053: "No active transaction");
054: }
055: if (transaction == null) {
056: return null;
057: }
058: EntityManagerWrapper entityManagerWrapper = (EntityManagerWrapper) transactionManager
059: .getResource(persistenceUnit);
060: if (entityManagerWrapper == null) {
061: EntityManager entityManager = createEntityManager();
062: entityManagerWrapper = new EntityManagerWrapperTxScoped(
063: entityManager);
064: transactionManager.putResource(persistenceUnit,
065: entityManagerWrapper);
066: try {
067: transaction
068: .registerSynchronization(entityManagerWrapper);
069: } catch (javax.transaction.RollbackException e) {
070: throw (TransactionRequiredException) new TransactionRequiredException(
071: "No active transaction").initCause(e);
072: } catch (SystemException e) {
073: throw (TransactionRequiredException) new TransactionRequiredException(
074: "No active transaction").initCause(e);
075: }
076: }
077: return entityManagerWrapper.getEntityManager();
078: }
079:
080: private EntityManager createEntityManager() {
081: EntityManager entityManager;
082: if (entityManagerProperties == null) {
083: entityManager = entityManagerFactory.createEntityManager();
084: } else {
085: entityManager = entityManagerFactory
086: .createEntityManager(entityManagerProperties);
087: }
088: return entityManager;
089: }
090:
091: public void persist(Object o) {
092: EntityManager entityManager = getEntityManager(true);
093: if (entityManager != null) {
094: entityManager.persist(o);
095: } else {
096: entityManager = createEntityManager();
097: try {
098: entityManager.persist(o);
099: } finally {
100: entityManager.close();
101: }
102: }
103: }
104:
105: public <T> T merge(T t) {
106: EntityManager entityManager = getEntityManager(true);
107: if (entityManager != null) {
108: return entityManager.merge(t);
109: } else {
110: entityManager = createEntityManager();
111: try {
112: return entityManager.merge(t);
113: } finally {
114: entityManager.close();
115: }
116: }
117: }
118:
119: public void remove(Object o) {
120: EntityManager entityManager = getEntityManager(true);
121: if (entityManager != null) {
122: entityManager.remove(o);
123: } else {
124: entityManager = createEntityManager();
125: try {
126: entityManager.remove(o);
127: } finally {
128: entityManager.close();
129: }
130: }
131: }
132:
133: public <T> T find(Class<T> aClass, Object o) {
134: EntityManager entityManager = getEntityManager(false);
135: if (entityManager != null) {
136: return entityManager.find(aClass, o);
137: } else {
138: entityManager = createEntityManager();
139: try {
140: return entityManager.find(aClass, o);
141: } finally {
142: entityManager.close();
143: }
144: }
145: }
146:
147: public <T> T getReference(Class<T> aClass, Object o) {
148: EntityManager entityManager = getEntityManager(false);
149: if (entityManager != null) {
150: return entityManager.getReference(aClass, o);
151: } else {
152: entityManager = createEntityManager();
153: try {
154: return entityManager.getReference(aClass, o);
155: } finally {
156: entityManager.close();
157: }
158: }
159: }
160:
161: public void flush() {
162: EntityManager entityManager = getEntityManager(false);
163: if (entityManager != null) {
164: entityManager.flush();
165: } else {
166: entityManager = createEntityManager();
167: try {
168: entityManager.flush();
169: } finally {
170: entityManager.close();
171: }
172: }
173: }
174:
175: public void setFlushMode(FlushModeType flushModeType) {
176: EntityManager entityManager = getEntityManager(false);
177: if (entityManager != null) {
178: entityManager.setFlushMode(flushModeType);
179: } else {
180: entityManager = createEntityManager();
181: try {
182: entityManager.setFlushMode(flushModeType);
183: } finally {
184: entityManager.close();
185: }
186: }
187: }
188:
189: public FlushModeType getFlushMode() {
190: EntityManager entityManager = getEntityManager(false);
191: if (entityManager != null) {
192: return entityManager.getFlushMode();
193: } else {
194: entityManager = createEntityManager();
195: try {
196: return entityManager.getFlushMode();
197: } finally {
198: entityManager.close();
199: }
200: }
201: }
202:
203: public void lock(Object o, LockModeType lockModeType) {
204: EntityManager entityManager = getEntityManager(false);
205: if (entityManager != null) {
206: entityManager.lock(o, lockModeType);
207: } else {
208: entityManager = createEntityManager();
209: try {
210: entityManager.lock(o, lockModeType);
211: } finally {
212: entityManager.close();
213: }
214: }
215: }
216:
217: public void refresh(Object o) {
218: EntityManager entityManager = getEntityManager(true);
219: if (entityManager != null) {
220: entityManager.refresh(o);
221: } else {
222: entityManager = createEntityManager();
223: try {
224: entityManager.refresh(o);
225: } finally {
226: entityManager.close();
227: }
228: }
229: }
230:
231: public void clear() {
232: EntityManager entityManager = getEntityManager(false);
233: if (entityManager != null) {
234: entityManager.clear();
235: } else {
236: entityManager = createEntityManager();
237: try {
238: entityManager.clear();
239: } finally {
240: entityManager.close();
241: }
242: }
243: }
244:
245: public boolean contains(Object o) {
246: EntityManager entityManager = getEntityManager(false);
247: if (entityManager != null) {
248: return entityManager.contains(o);
249: } else {
250: entityManager = createEntityManager();
251: try {
252: return entityManager.contains(o);
253: } finally {
254: entityManager.close();
255: }
256: }
257: }
258:
259: public Query createQuery(String s) {
260: EntityManager entityManager = getEntityManager(false);
261: if (entityManager != null) {
262: return entityManager.createQuery(s);
263: } else {
264: entityManager = createEntityManager();
265: return new NoTxQueryWrapper(entityManager, entityManager
266: .createQuery(s));
267: }
268: }
269:
270: public Query createNamedQuery(String s) {
271: EntityManager entityManager = getEntityManager(false);
272: if (entityManager != null) {
273: return entityManager.createNamedQuery(s);
274: } else {
275: entityManager = createEntityManager();
276: return new NoTxQueryWrapper(entityManager, entityManager
277: .createNamedQuery(s));
278: }
279: }
280:
281: public Query createNativeQuery(String s) {
282: EntityManager entityManager = getEntityManager(false);
283: if (entityManager != null) {
284: return entityManager.createNativeQuery(s);
285: } else {
286: entityManager = createEntityManager();
287: return new NoTxQueryWrapper(entityManager, entityManager
288: .createNativeQuery(s));
289: }
290: }
291:
292: public Query createNativeQuery(String s, Class aClass) {
293: EntityManager entityManager = getEntityManager(false);
294: if (entityManager != null) {
295: return entityManager.createNativeQuery(s, aClass);
296: } else {
297: entityManager = createEntityManager();
298: return new NoTxQueryWrapper(entityManager, entityManager
299: .createNativeQuery(s, aClass));
300: }
301: }
302:
303: public Query createNativeQuery(String s, String s1) {
304: EntityManager entityManager = getEntityManager(false);
305: if (entityManager != null) {
306: return entityManager.createNativeQuery(s, s1);
307: } else {
308: entityManager = createEntityManager();
309: return new NoTxQueryWrapper(entityManager, entityManager
310: .createNativeQuery(s, s1));
311: }
312: }
313:
314: public void close() {
315: throw new IllegalStateException(
316: "You cannot call close on a Container Managed Entity Manager");
317: }
318:
319: public boolean isOpen() {
320: return true;
321: }
322:
323: public EntityTransaction getTransaction() {
324: throw new IllegalStateException(
325: "You cannot call getTransaction on a container managed EntityManager");
326: }
327:
328: public void joinTransaction() {
329: throw new IllegalStateException(
330: "You cannot call joinTransaction on a container managed EntityManager");
331: }
332:
333: public Object 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
348: EntityManagerWrapper, Synchronization {
349: private final EntityManager entityManager;
350:
351: public EntityManagerWrapperTxScoped(EntityManager entityManager) {
352: if (entityManager == null) {
353: throw new IllegalArgumentException(
354: "Need a non-null entity manager");
355: }
356: this .entityManager = entityManager;
357: }
358:
359: public void close() {
360: entityManager.close();
361: }
362:
363: public EntityManager getEntityManager() {
364: return entityManager;
365: }
366:
367: public void beforeCompletion() {
368: }
369:
370: public void afterCompletion(int i) {
371: close();
372: }
373: }
374: }
|