You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2014/03/24 21:33:06 UTC

svn commit: r1581040 - in /commons/proper/proxy/trunk: core/src/main/java/org/apache/commons/proxy2/serialization/ test/ test/src/test/java/org/apache/commons/proxy2/serialization/

Author: mbenson
Date: Mon Mar 24 20:33:05 2014
New Revision: 1581040

URL: http://svn.apache.org/r1581040
Log:
add support interfaces and unit test by way of example for supporting the serialization proxy pattern when the default Serializable implementations fall short

Added:
    commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/
    commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/ReadResolve.java   (with props)
    commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/WriteReplace.java   (with props)
    commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/package-info.java   (with props)
    commons/proper/proxy/trunk/test/src/test/java/org/apache/commons/proxy2/serialization/
    commons/proper/proxy/trunk/test/src/test/java/org/apache/commons/proxy2/serialization/SerializationProxyTest.java   (with props)
Modified:
    commons/proper/proxy/trunk/test/pom.xml

Added: commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/ReadResolve.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/ReadResolve.java?rev=1581040&view=auto
==============================================================================
--- commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/ReadResolve.java (added)
+++ commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/ReadResolve.java Mon Mar 24 20:33:05 2014
@@ -0,0 +1,32 @@
+/*
+ * 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.commons.proxy2.serialization;
+
+import java.io.Serializable;
+
+/**
+ * Defines a contract around the {@code Object readResolve()} method used by Java deserialization.
+ * 
+ * @since 2.0
+ */
+public interface ReadResolve extends Serializable {
+    /**
+     * Get the deserialized version of this {@link Serializable}.
+     * @return Object
+     */
+    Object readResolve();
+}

Propchange: commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/ReadResolve.java
------------------------------------------------------------------------------
    svn:executable = *

Added: commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/WriteReplace.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/WriteReplace.java?rev=1581040&view=auto
==============================================================================
--- commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/WriteReplace.java (added)
+++ commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/WriteReplace.java Mon Mar 24 20:33:05 2014
@@ -0,0 +1,32 @@
+/*
+ * 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.commons.proxy2.serialization;
+
+import java.io.Serializable;
+
+/**
+ * Defines a contract around the {@code Object writeReplace()} method used by Java deserialization.
+ * 
+ * @since 2.0
+ */
+public interface WriteReplace extends Serializable {
+    /**
+     * Get the serialized version of this object.
+     * @return Object
+     */
+    Object writeReplace();
+}

Propchange: commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/WriteReplace.java
------------------------------------------------------------------------------
    svn:executable = *

Added: commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/package-info.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/package-info.java?rev=1581040&view=auto
==============================================================================
--- commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/package-info.java (added)
+++ commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/package-info.java Mon Mar 24 20:33:05 2014
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+/**
+ * The various {@link org.apache.commons.proxy2.ProxyFactory} implementations create {@link Serializable} proxies;
+ * however it is not always possible or practical to serialize the complete structure of a given proxy object. The
+ * intent of this package is to facilitate the "serialization proxy pattern" by means of the {@code readResolve()} and
+ * {@code writeReplace} methods supported by Java's serialization mechanism. This would normally be problematic with
+ * Commons Proxy because its proxies are generalized to expose only methods declared by superclasses (where applicable)
+ * or proxied interfaces. Therefore we declare the following interfaces:
+ * <ul>
+ *   <li>{@link ReadResolve}</li>
+ *   <li>{@link WriteReplace}</li>
+ * </ul>
+ *
+ * Typically, you should define your proxy to include {@link WriteReplace} among its interfaces, and implement it to
+ * return some object that implements {@link ReadResolve} (or simply declares the {@code Object readResolve()} method
+ * in any scope, but using the interface brings compiler assistance).
+ *
+ * Hint: Your {@link ReadResolve#readResolve()} implementation will typically use serialized information to recreate an
+ * equivalent proxy object, which probably implies some form of {@code static} access.
+ */
+package org.apache.commons.proxy2.serialization;
+import java.io.Serializable;

Propchange: commons/proper/proxy/trunk/core/src/main/java/org/apache/commons/proxy2/serialization/package-info.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: commons/proper/proxy/trunk/test/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/test/pom.xml?rev=1581040&r1=1581039&r2=1581040&view=diff
==============================================================================
--- commons/proper/proxy/trunk/test/pom.xml (original)
+++ commons/proper/proxy/trunk/test/pom.xml Mon Mar 24 20:33:05 2014
@@ -55,5 +55,10 @@
             <artifactId>commons-proxy2-asm4</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Added: commons/proper/proxy/trunk/test/src/test/java/org/apache/commons/proxy2/serialization/SerializationProxyTest.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/test/src/test/java/org/apache/commons/proxy2/serialization/SerializationProxyTest.java?rev=1581040&view=auto
==============================================================================
--- commons/proper/proxy/trunk/test/src/test/java/org/apache/commons/proxy2/serialization/SerializationProxyTest.java (added)
+++ commons/proper/proxy/trunk/test/src/test/java/org/apache/commons/proxy2/serialization/SerializationProxyTest.java Mon Mar 24 20:33:05 2014
@@ -0,0 +1,111 @@
+/*
+ * 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.commons.proxy2.serialization;
+
+import static org.junit.Assert.*;
+
+import java.io.Serializable;
+
+import org.apache.commons.lang3.SerializationException;
+import org.apache.commons.lang3.SerializationUtils;
+import org.apache.commons.proxy2.ProxyFactory;
+import org.apache.commons.proxy2.interceptor.InterceptorUtils;
+import org.apache.commons.proxy2.interceptor.SwitchInterceptor;
+import org.apache.commons.proxy2.interceptor.matcher.invocation.DeclaredByMatcher;
+import org.apache.commons.proxy2.stub.AbstractProxyFactoryAgnosticTest;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SerializationProxyTest extends AbstractProxyFactoryAgnosticTest
+{
+    public static class NonSerializableStringWrapper
+    {
+        private final String value;
+
+        NonSerializableStringWrapper(String value)
+        {
+            this.value = value;
+        }
+
+        public String getValue()
+        {
+            return value;
+        }
+    }
+
+    public interface Provider
+    {
+        NonSerializableStringWrapper getObject();
+    }
+
+    private static final ThreadLocal<ProxyFactory> PROXY_FACTORY = new ThreadLocal<ProxyFactory>();
+
+    private static SwitchInterceptor implementProvider(String value)
+    {
+        return new SwitchInterceptor().when(new DeclaredByMatcher(Provider.class)).then(
+            InterceptorUtils.constant(new NonSerializableStringWrapper(value)));
+    }
+
+    private static Provider serializableProvider(final String value)
+    {
+        return PROXY_FACTORY.get().createInterceptorProxy(
+            null,
+            implementProvider(value).when(new DeclaredByMatcher(WriteReplace.class)).then(
+                InterceptorUtils.constant(new ReadResolve()
+                {
+                    private static final long serialVersionUID = 1L;
+
+                    @Override
+                    public Object readResolve()
+                    {
+                        return serializableProvider(value);
+                    }
+                })), Provider.class, WriteReplace.class);
+    }
+
+    @Before
+    public void captureProxyFactory()
+    {
+        PROXY_FACTORY.set(proxyFactory);
+    }
+
+    @After
+    public void clearProxyFactory()
+    {
+        PROXY_FACTORY.remove();
+    }
+
+    @Test(expected = SerializationException.class)
+    public void testNaive()
+    {
+        final Provider proxy =
+            proxyFactory.createInterceptorProxy(null, implementProvider("foo"), Provider.class, Serializable.class);
+        assertEquals("foo", proxy.getObject().getValue());
+        assertTrue(Serializable.class.isInstance(proxy));
+        SerializationUtils.roundtrip((Serializable) proxy);
+    }
+
+    @Test
+    public void testSerializationProxy() {
+        final Provider proxy = serializableProvider("foo");
+        assertEquals("foo", proxy.getObject().getValue());
+        assertTrue(Serializable.class.isInstance(proxy));
+        final Provider proxy2 = (Provider) SerializationUtils.roundtrip((Serializable) proxy);
+        assertEquals("foo", proxy2.getObject().getValue());
+    }
+}

Propchange: commons/proper/proxy/trunk/test/src/test/java/org/apache/commons/proxy2/serialization/SerializationProxyTest.java
------------------------------------------------------------------------------
    svn:executable = *