X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FTransactionProxyFactory.java;fp=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FTransactionProxyFactory.java;h=4e72f4845d4a377b7075f62a11e0b8856b0e6783;hb=4c4b45259c4af24c2551c40df72ad776dd037cbe;hp=0000000000000000000000000000000000000000;hpb=dd174318e1f8b2afa8a17ffc7e3775d65d0401ca;p=utils diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/TransactionProxyFactory.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/TransactionProxyFactory.java new file mode 100644 index 00000000..4e72f484 --- /dev/null +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/TransactionProxyFactory.java @@ -0,0 +1,105 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.support.persistence; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +import javax.persistence.EntityManager; + +import org.wamblee.support.ThreadSpecificProxyFactory; +import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork; + +/** + * This utility makes sure that each invocation on a certain interface is + * carried out within a JPA unit of work. + * + * Use {@link #getTransactionScopedEntityManager()} to get the transaction scoped + * entity manager to pass to services. + * + * @param T + * Type of interface to proxy. + * + * @author Erik Brakkee + */ +public class TransactionProxyFactory { + + private class UnitOfWorkInvocationHandler implements InvocationHandler { + + private T service; + + public UnitOfWorkInvocationHandler(T aService) { + service = aService; + } + + @Override + public Object invoke(Object aProxy, final Method aMethod, + final Object[] aArgs) throws Throwable { + return TransactionProxyFactory.this.jpaBuilder + .execute(new JpaUnitOfWork() { + @Override + public Object execute(EntityManager aEm) throws Exception { + try { + ENTITY_MANAGER.set(aEm); + return aMethod.invoke(service, aArgs); + } catch (InvocationTargetException e) { + throw (Exception)e.getCause(); + } finally { + ENTITY_MANAGER.set(null); + } + } + }); + } + + } + + private static final ThreadSpecificProxyFactory ENTITY_MANAGER = + new ThreadSpecificProxyFactory(EntityManager.class); + + private JpaBuilder jpaBuilder; + private Class clazz; + + /** + * Constructs the transaction proxy. + * + * @param aJpaBuilder + */ + public TransactionProxyFactory(JpaBuilder aJpaBuilder, Class aClass) { + jpaBuilder = aJpaBuilder; + clazz = aClass; + } + + public EntityManager getTransactionScopedEntityManager() { + return ENTITY_MANAGER.getProxy(); + } + + public T getProxy(T aService) { + InvocationHandler handler = new UnitOfWorkInvocationHandler(aService); + Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(), + new Class[] { clazz }); + T proxy; + try { + proxy = (T) proxyClass.getConstructor( + new Class[] { InvocationHandler.class }).newInstance( + new Object[] { handler }); + return proxy; + } catch (Exception e) { + throw new RuntimeException("Could not create proxy for " + clazz.getName(), e); + } + } +}