You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/11/07 12:32:52 UTC

[3/3] tomee git commit: TOMEE-1965 making entity manager factory callable serializable

TOMEE-1965 making entity manager factory callable serializable


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/c6f924a2
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/c6f924a2
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/c6f924a2

Branch: refs/heads/master
Commit: c6f924a27d39f32729a6d7b82b103e20927b22dc
Parents: f479249
Author: rmannibucau <rm...@apache.org>
Authored: Mon Nov 7 13:32:29 2016 +0100
Committer: rmannibucau <rm...@apache.org>
Committed: Mon Nov 7 13:32:29 2016 +0100

----------------------------------------------------------------------
 .../classic/EntityManagerFactoryCallable.java   | 49 ++++++++++++--------
 ...ManagerFactoryCallableSerializationTest.java | 49 ++++++++++++++++++++
 2 files changed, 78 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/c6f924a2/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EntityManagerFactoryCallable.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EntityManagerFactoryCallable.java b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EntityManagerFactoryCallable.java
index 6ef05e9..777f412 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EntityManagerFactoryCallable.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EntityManagerFactoryCallable.java
@@ -31,6 +31,7 @@ import javax.persistence.ValidationMode;
 import javax.persistence.spi.PersistenceProvider;
 import javax.transaction.Transaction;
 import javax.validation.ValidatorFactory;
+import java.io.Serializable;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -86,26 +87,8 @@ public class EntityManagerFactoryCallable implements Callable<EntityManagerFacto
             }
             if (cdi && "true".equalsIgnoreCase(unitInfo.getProperties().getProperty("tomee.jpa.cdi", "true"))
                     && "true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.jpa.cdi", "true"))) {
-                properties.put("javax.persistence.bean.manager", // TODO: impl a passthrough BM?
-                        Proxy.newProxyInstance(appClassLoader, new Class<?>[]{BeanManager.class}, new InvocationHandler() {
-                            private volatile BeanManager bm;
-
-                            @Override
-                            public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
-                                try {
-                                    return method.invoke(findBm(), args);
-                                } catch (final InvocationTargetException ite) {
-                                    Logger.getInstance(LogCategory.OPENEJB_JPA, EntityManagerFactoryCallable.class)
-                                            .warning("Exception calling CDI, if a lifecycle issue you should maybe set tomee.jpa.factory.lazy=true", ite.getCause());
-                                    throw ite.getCause();
-                                }
-                            }
-
-                            private Object findBm() {
-                                return bm != null ? bm : (bm = new InjectableBeanManager(WebBeansContext.currentInstance().getBeanManagerImpl()));
-                            }
-                        })
-                );
+                properties.put("javax.persistence.bean.manager",
+                        Proxy.newProxyInstance(appClassLoader, new Class<?>[]{BeanManager.class}, new BmHandler()));
             }
 
             customizeProperties(properties);
@@ -161,4 +144,30 @@ public class EntityManagerFactoryCallable implements Callable<EntityManagerFacto
     public void overrideClassLoader(final ClassLoader loader) {
         appClassLoader = loader;
     }
+
+    private static class BmHandler implements InvocationHandler, Serializable {
+        private transient volatile BeanManager bm;
+
+        @Override
+        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+            try {
+                return method.invoke(findBm(), args);
+            } catch (final InvocationTargetException ite) {
+                Logger.getInstance(LogCategory.OPENEJB_JPA, EntityManagerFactoryCallable.class)
+                        .warning("Exception calling CDI, if a lifecycle issue you should maybe set tomee.jpa.factory.lazy=true", ite.getCause());
+                throw ite.getCause();
+            }
+        }
+
+        private Object findBm() {
+            if (bm == null) {
+                synchronized (this) {
+                    if (bm == null) {
+                        bm = new InjectableBeanManager(WebBeansContext.currentInstance().getBeanManagerImpl());
+                    }
+                }
+            }
+            return bm;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/c6f924a2/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/EntityManagerFactoryCallableSerializationTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/EntityManagerFactoryCallableSerializationTest.java b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/EntityManagerFactoryCallableSerializationTest.java
new file mode 100644
index 0000000..e75e26b
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/EntityManagerFactoryCallableSerializationTest.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.openejb.assembler.classic;
+
+import org.apache.commons.lang3.SerializationUtils;
+import org.apache.openejb.api.configuration.PersistenceUnitDefinition;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.util.reflection.Reflections;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import java.io.Serializable;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+@Classes(cdi = true)
+@PersistenceUnitDefinition
+@RunWith(ApplicationComposer.class)
+public class EntityManagerFactoryCallableSerializationTest {
+    @PersistenceContext
+    private EntityManager em;
+
+    @Test
+    public void serializationRoundTrip() {
+        final Object em = SerializationUtils.deserialize(SerializationUtils.serialize(Serializable.class.cast(this.em)));
+        assertTrue(EntityManager.class.isInstance(em));
+        final ReloadableEntityManagerFactory factory = ReloadableEntityManagerFactory.class.cast(Reflections.get(em, "entityManagerFactory"));
+        assertNotNull(factory.getDelegate());
+        assertNotNull(Reflections.get(factory, "entityManagerFactoryCallable"));
+    }
+}