2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.test.transactions;
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Proxy;
24 import javax.persistence.EntityManager;
26 import org.wamblee.general.ThreadSpecificProxyFactory;
27 import org.wamblee.test.persistence.JpaBuilder;
28 import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork;
31 * This utility makes sure that each invocation on a certain interface is
32 * carried out within a JPA unit of work. Note that this is equivalent
33 * to the sementics of a requiresNew transaction attribute.
35 * Use {@link #getTransactionScopedEntityManager()} to get the transaction
36 * scoped entity manager to pass to services.
41 * JpaBuilder builder = ...
42 * TransactionProxyFactory<Service> factory = new TransactionProxyFactory<Service>(
43 * builder, Service.class);
44 * Service service = new JpaService(factory.getTransactionScopedEntityManager());
45 * Service proxy = factory.getProxy(service);
46 * proxy.executeMethod(...);
48 * The above example executes the executeMethod() call on the service object within an active transaction.
49 * In the constructor of the service a transaction scoped entity manager is passed.
52 * Type of interface to proxy.
54 * @author Erik Brakkee
56 public class TransactionProxyFactory<T> {
59 * Executes the call on the service within a new transaction.
61 * @author Erik Brakkee
63 * @param <T> Type of the service interface.
65 private class UnitOfWorkInvocationHandler<T> implements InvocationHandler {
69 public UnitOfWorkInvocationHandler(T aService) {
74 public Object invoke(Object aProxy, final Method aMethod,
75 final Object[] aArgs) throws Throwable {
76 return TransactionProxyFactory.this.jpaBuilder
77 .execute(new JpaUnitOfWork<Object>() {
79 public Object execute(EntityManager aEm) throws Exception {
80 EntityManager oldEm = ENTITY_MANAGER.get();
82 ENTITY_MANAGER.set(aEm);
83 return aMethod.invoke(service, aArgs);
84 } catch (InvocationTargetException e) {
85 Throwable cause = e.getCause();
86 if (cause instanceof Exception) {
87 throw (Exception) cause;
88 } else if (cause instanceof Error) {
92 throw new RuntimeException(e);
94 ENTITY_MANAGER.set(oldEm);
102 private static final ThreadSpecificProxyFactory<EntityManager> ENTITY_MANAGER = new ThreadSpecificProxyFactory<EntityManager>(
103 EntityManager.class);
105 private JpaBuilder jpaBuilder;
106 private Class<T> clazz;
109 * Constructs the transaction proxy.
113 public TransactionProxyFactory(JpaBuilder aJpaBuilder, Class<T> aClass) {
114 jpaBuilder = aJpaBuilder;
118 public EntityManager getTransactionScopedEntityManager() {
119 return ENTITY_MANAGER.getProxy();
122 public T getProxy(T aService) {
123 InvocationHandler handler = new UnitOfWorkInvocationHandler<T>(aService);
124 Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
125 new Class[] { clazz });
128 proxy = (T) proxyClass.getConstructor(
129 new Class[] { InvocationHandler.class }).newInstance(
130 new Object[] { handler });
132 } catch (Exception e) {
133 throw new RuntimeException("Could not create proxy for " +