jpauserset now working.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Fri, 30 Apr 2010 14:19:35 +0000 (14:19 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Fri, 30 Apr 2010 14:19:35 +0000 (14:19 +0000)
test/enterprise/src/main/java/org/wamblee/support/persistence/TransactionProxyFactory.java

index 4e72f4845d4a377b7075f62a11e0b8856b0e6783..d3d2884b79b5db87bcba6475455f9225d4a8bc3f 100644 (file)
@@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 
+import javax.management.RuntimeErrorException;
 import javax.persistence.EntityManager;
 
 import org.wamblee.support.ThreadSpecificProxyFactory;
@@ -29,8 +30,8 @@ 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. 
+ * Use {@link #getTransactionScopedEntityManager()} to get the transaction
+ * scoped entity manager to pass to services.
  * 
  * @param T
  *            Type of interface to proxy.
@@ -58,8 +59,15 @@ public class TransactionProxyFactory<T> {
                             ENTITY_MANAGER.set(aEm);
                             return aMethod.invoke(service, aArgs);
                         } catch (InvocationTargetException e) {
-                            throw (Exception)e.getCause();
-                        } finally { 
+                            Throwable cause = e.getCause();
+                            if (cause instanceof Exception) {
+                                throw (Exception) cause;
+                            } else if (cause instanceof Error) {
+                                throw (Error) cause;
+                            }
+                            // last resort.
+                            throw new RuntimeException(e);
+                        } finally {
                             ENTITY_MANAGER.set(null);
                         }
                     }
@@ -68,9 +76,9 @@ public class TransactionProxyFactory<T> {
 
     }
 
-    private static final ThreadSpecificProxyFactory<EntityManager> ENTITY_MANAGER = 
-        new ThreadSpecificProxyFactory<EntityManager>(EntityManager.class);
-    
+    private static final ThreadSpecificProxyFactory<EntityManager> ENTITY_MANAGER = new ThreadSpecificProxyFactory<EntityManager>(
+        EntityManager.class);
+
     private JpaBuilder jpaBuilder;
     private Class<T> clazz;
 
@@ -83,8 +91,8 @@ public class TransactionProxyFactory<T> {
         jpaBuilder = aJpaBuilder;
         clazz = aClass;
     }
-    
-    public EntityManager getTransactionScopedEntityManager() { 
+
+    public EntityManager getTransactionScopedEntityManager() {
         return ENTITY_MANAGER.getProxy();
     }
 
@@ -98,8 +106,9 @@ public class TransactionProxyFactory<T> {
                 new Class[] { InvocationHandler.class }).newInstance(
                 new Object[] { handler });
             return proxy;
-        } catch (Exception e) { 
-            throw new RuntimeException("Could not create proxy for " + clazz.getName(), e);
+        } catch (Exception e) {
+            throw new RuntimeException("Could not create proxy for " +
+                clazz.getName(), e);
         }
     }
 }