deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject git commit: DELTASPIKE-402 make DependentProvider Serializable (if possible)
Date Fri, 16 Aug 2013 21:07:37 GMT
Updated Branches:
  refs/heads/master ff62a1830 -> 258afc8c8


DELTASPIKE-402 make DependentProvider Serializable (if possible)


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

Branch: refs/heads/master
Commit: 258afc8c809f68bd1b530e115725e0bf511e02ca
Parents: ff62a18
Author: Mark Struberg <struberg@apache.org>
Authored: Fri Aug 16 23:06:16 2013 +0200
Committer: Mark Struberg <struberg@apache.org>
Committed: Fri Aug 16 23:06:16 2013 +0200

----------------------------------------------------------------------
 .../core/api/provider/DependentProvider.java    | 47 +++++++++++-
 .../core/api/message/MessageContextTest.java    |  2 +-
 .../core/api/message/SimpleMessageTest.java     |  2 +-
 .../core/api/provider/BeanProviderTest.java     | 19 +++++
 .../core/api/provider/DependentTestBean.java    |  3 +-
 .../deltaspike/test/category/Serializer.java    | 81 --------------------
 .../deltaspike/test/utils/Serializer.java       | 81 ++++++++++++++++++++
 7 files changed, 149 insertions(+), 86 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/258afc8c/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/DependentProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/DependentProvider.java
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/DependentProvider.java
index a63fe83..ae76a04 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/DependentProvider.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/DependentProvider.java
@@ -20,7 +20,13 @@ package org.apache.deltaspike.core.api.provider;
 
 import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.PassivationCapable;
 import javax.inject.Provider;
+import java.io.IOException;
+import java.io.NotSerializableException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
 
 /**
  * A {@link Provider} for &#064;Dependent scoped contextual instances.
@@ -28,12 +34,17 @@ import javax.inject.Provider;
  * needed anymore via the {@link #destroy()} method.
  *
  * Instances of this class can be retrieved using the {@link BeanProvider}.
+ *
+ * Instances of this class are Serializable if the wrapped contextual instance
+ * is Serializable.
  */
-public class DependentProvider<T> implements Provider<T>
+public class DependentProvider<T> implements Provider<T>, Serializable
 {
+    private static final long serialVersionUID = 23423413412001L;
+
     private T instance;
     private CreationalContext<T> creationalContext;
-    private Bean<T> bean;
+    private transient Bean<T> bean;
 
     DependentProvider(Bean<T> bean, CreationalContext<T> creationalContext, T
instance)
     {
@@ -52,4 +63,36 @@ public class DependentProvider<T> implements Provider<T>
     {
         bean.destroy(instance, creationalContext);
     }
+
+    private void writeObject(ObjectOutputStream out) throws IOException
+    {
+        if (!(bean instanceof PassivationCapable))
+        {
+            throw new NotSerializableException("Bean is not PassivationCapable: " + bean.toString());
+        }
+        String passivationId = ((PassivationCapable) bean).getId();
+        if (passivationId == null)
+        {
+            throw new NotSerializableException(bean.toString());
+        }
+
+        out.writeLong(serialVersionUID);
+        out.writeObject(passivationId);
+        out.writeObject(instance);
+        out.writeObject(creationalContext);
+    }
+
+    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
+    {
+        long oldSerialId = in.readLong();
+        if (oldSerialId != serialVersionUID)
+        {
+            throw new NotSerializableException(getClass().getName() + " serialVersion does
not match");
+        }
+        String passivationId = (String) in.readObject();
+        bean = (Bean<T>) BeanManagerProvider.getInstance().getBeanManager().getPassivationCapableBean(passivationId);
+        instance = (T) in.readObject();
+        creationalContext = (CreationalContext<T>) in.readObject();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/258afc8c/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/MessageContextTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/MessageContextTest.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/MessageContextTest.java
index c4bde2f..a0893d0 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/MessageContextTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/MessageContextTest.java
@@ -28,7 +28,7 @@ import org.apache.deltaspike.core.api.message.Message;
 import org.apache.deltaspike.core.api.message.MessageContext;
 import org.apache.deltaspike.core.impl.message.MessageBundleExtension;
 import org.apache.deltaspike.test.category.SeCategory;
-import org.apache.deltaspike.test.category.Serializer;
+import org.apache.deltaspike.test.utils.Serializer;
 import org.apache.deltaspike.test.util.ArchiveUtils;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/258afc8c/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/SimpleMessageTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/SimpleMessageTest.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/SimpleMessageTest.java
index e5dadd8..8acc095 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/SimpleMessageTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/SimpleMessageTest.java
@@ -21,7 +21,7 @@ package org.apache.deltaspike.test.core.api.message;
 import org.apache.deltaspike.core.api.message.LocaleResolver;
 import org.apache.deltaspike.core.impl.message.MessageBundleExtension;
 import org.apache.deltaspike.test.category.SeCategory;
-import org.apache.deltaspike.test.category.Serializer;
+import org.apache.deltaspike.test.utils.Serializer;
 import org.apache.deltaspike.test.util.ArchiveUtils;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/258afc8c/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
index abbeb98..d9c9feb 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
@@ -22,6 +22,8 @@ package org.apache.deltaspike.test.core.api.provider;
 import org.apache.deltaspike.core.api.provider.BeanProvider;
 import org.apache.deltaspike.core.api.provider.DependentProvider;
 import org.apache.deltaspike.test.util.ArchiveUtils;
+import org.apache.deltaspike.test.utils.CdiContainerUnderTest;
+import org.apache.deltaspike.test.utils.Serializer;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;
 import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -29,6 +31,7 @@ import org.jboss.shrinkwrap.api.asset.EmptyAsset;
 import org.jboss.shrinkwrap.api.spec.JavaArchive;
 import org.jboss.shrinkwrap.api.spec.WebArchive;
 import org.junit.Assert;
+import org.junit.Assume;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -38,6 +41,9 @@ import java.util.concurrent.ConcurrentHashMap;
 @RunWith(Arquillian.class)
 public class BeanProviderTest
 {
+    private static final String CONTAINER_OWB_1_1_x = "owb-1\\.1\\..*";
+
+
     /**
      *X TODO creating a WebArchive is only a workaround because JavaArchive cannot contain
other archives.
      */
@@ -210,6 +216,19 @@ public class BeanProviderTest
         checkDependentProvider(dependentTestBeanProvider);
     }
 
+    @Test
+    public void testDependentBeanSerialization() throws Exception
+    {
+        // this test is known to not work under owb-1.1.x as ManagedBean for Dependent scoped
classes did not implement PassivationCapable.
+        Assume.assumeTrue(!CdiContainerUnderTest.is(CONTAINER_OWB_1_1_x));
+
+        DependentProvider<DependentTestBean> dependentTestBeanProvider = BeanProvider.getDependent(DependentTestBean.class);
+
+        Serializer<DependentProvider<DependentTestBean>> serializer = new Serializer<DependentProvider<DependentTestBean>>();
+        DependentProvider<DependentTestBean> provider2 = serializer.roundTrip(dependentTestBeanProvider);
+        Assert.assertNotNull(provider2);
+    }
+
     private void checkDependentProvider(DependentProvider<DependentTestBean> dependentTestBeanProvider)
     {
         Assert.assertNotNull(dependentTestBeanProvider);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/258afc8c/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
index 1bda0b4..b5862e8 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
@@ -21,10 +21,11 @@ package org.apache.deltaspike.test.core.api.provider;
 import javax.annotation.PreDestroy;
 import javax.enterprise.context.Dependent;
 import javax.inject.Named;
+import java.io.Serializable;
 
 @Dependent
 @Named
-public class DependentTestBean
+public class DependentTestBean implements Serializable
 {
     private int i = 42;
     private boolean destroyed = false;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/258afc8c/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/category/Serializer.java
----------------------------------------------------------------------
diff --git a/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/category/Serializer.java
b/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/category/Serializer.java
deleted file mode 100644
index bd03e94..0000000
--- a/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/category/Serializer.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * 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.deltaspike.test.category;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-
-/**
- * A few helper methods for testing serialisation.
- * They help serializing to a byte[] and back to the object
- */
-public class Serializer<T>
-{
-
-    /**
-     * Serializes the given instance to a byte[] and immediately
-     * de-serialize it back.
-     * @param original instance
-     * @return the deserialized new instance
-     */
-    public T roundTrip(T original)
-    {
-        return deserialize(serialize(original));
-    }
-
-    /**
-     * Serializes the given instance to a byte[].
-     */
-    public byte[] serialize(T o)
-    {
-        try
-        {
-            ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            ObjectOutputStream oos = null;
-            oos = new ObjectOutputStream(baos);
-            oos.writeObject(o);
-            return baos.toByteArray();
-        }
-        catch (IOException e)
-        {
-            throw new RuntimeException(e);
-        }
-    }
-
-    /**
-     * De-serializes the given byte[] to an instance of T.
-     */
-    public T deserialize(byte[] serial)
-    {
-        try
-        {
-            ByteArrayInputStream bais = new ByteArrayInputStream(serial);
-            ObjectInputStream ois = new ObjectInputStream(bais);
-            return (T) ois.readObject();
-        }
-        catch (Exception e)
-        {
-            throw new RuntimeException(e);
-        }
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/258afc8c/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/Serializer.java
----------------------------------------------------------------------
diff --git a/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/Serializer.java
b/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/Serializer.java
new file mode 100644
index 0000000..ff0d645
--- /dev/null
+++ b/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/Serializer.java
@@ -0,0 +1,81 @@
+/*
+ * 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.deltaspike.test.utils;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+/**
+ * A few helper methods for testing serialisation.
+ * They help serializing to a byte[] and back to the object
+ */
+public class Serializer<T>
+{
+
+    /**
+     * Serializes the given instance to a byte[] and immediately
+     * de-serialize it back.
+     * @param original instance
+     * @return the deserialized new instance
+     */
+    public T roundTrip(T original)
+    {
+        return deserialize(serialize(original));
+    }
+
+    /**
+     * Serializes the given instance to a byte[].
+     */
+    public byte[] serialize(T o)
+    {
+        try
+        {
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            ObjectOutputStream oos = null;
+            oos = new ObjectOutputStream(baos);
+            oos.writeObject(o);
+            return baos.toByteArray();
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * De-serializes the given byte[] to an instance of T.
+     */
+    public T deserialize(byte[] serial)
+    {
+        try
+        {
+            ByteArrayInputStream bais = new ByteArrayInputStream(serial);
+            ObjectInputStream ois = new ObjectInputStream(bais);
+            return (T) ois.readObject();
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+}


Mime
View raw message