*/
package org.wamblee.general;
+import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Proxy factory that can provide contextual references to objects retrieved
- * through a lookup mechanism.
+ * through a lookup mechanism. The returned proxies are serializable.
*
* @param T
* Interface to proxy.
*
* @author Erik Brakkee
*/
- public static interface Lookup {
+ public static interface Lookup extends Serializable {
/**
* Looks up the object.
* @return Object (non-null)
*
* @author Erik Brakkee
*/
- private class LookupInvocationHandler implements InvocationHandler {
+ private static class LookupInvocationHandler<T> implements InvocationHandler, Serializable {
+
+ private Class clazz;
+ private Lookup lookup;
+
+ /**
+ * Constructs the invocation handler.
+ * @param aLookup Lookup class.
+ */
+ public LookupInvocationHandler(Class aClass, Lookup aLookup) {
+ clazz = aClass;
+ lookup = aLookup;
+ }
@Override
/**
"Error looking up object", e);
}
if (svcObj == null) {
- throw new LookupException("Object at is null");
+ throw new LookupException("Object is null");
}
if (!clazz.isInstance(svcObj)) {
throw new LookupException("Object '" + svcObj + "' is not of type " + clazz.getName() +
* @return Proxy.
*/
public T getProxy() {
- InvocationHandler handler = new LookupInvocationHandler();
+ InvocationHandler handler = new LookupInvocationHandler(clazz, lookup);
Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
new Class[] { clazz });
T proxy;
--- /dev/null
+/*
+ * 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.general;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+/**
+ * Utility for serializating and deserializing objects.
+ *
+ * @author Erik Brakkee
+ */
+public class ObjectSerializationUtils {
+
+ /**
+ * Serialize an object to a byte array.
+ * @param aObject Object ot serialize.
+ * @return Byte array.
+ * @throws IOException
+ */
+ public static byte[] serialize(Object aObject) throws IOException {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ ObjectOutputStream os = new ObjectOutputStream(bos);
+ os.writeObject(aObject);
+ os.flush();
+ return bos.toByteArray();
+ }
+
+ /**
+ * Desrializes an object from a byte array.
+ * @param <T> Type of the object.
+ * @param aData Serialized data.
+ * @param aType Type of the object.
+ * @return Object.
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ public static <T> T deserialize(byte[] aData, Class<T> aType) throws IOException, ClassNotFoundException {
+ ByteArrayInputStream bis = new ByteArrayInputStream(aData);
+ ObjectInputStream os = new ObjectInputStream(bis);
+ return (T)os.readObject();
+ }
+}
import java.lang.reflect.Proxy;
/**
+ * <p>
* Thread-specific proxy is used to create implementations of interfaces that
* delegate to a thread-specific implementation of the service.
+ * </p>
*
+ * <p>
* It can be used for instance to create a contextual reference to an entity
* manager that delegates to a thread-specific instance.
+ * </p>
*
+ * <p>
* The {@link #set(Object)} method sets the current service instance for the
* current thread. The {@link #get()} method gets the current service instance
* for the current thread. The {@link #getProxy()} method gets a proxy that will
* delegate at runtime to the thread-specific instance. The result from this
* method can be passed at construction of an object that will be used by
* multiple threads.
- *
+ * </p>
+ *
+ * <p>
* This class is mostly used by infrastructure code (utilities) and test tools.
+ * </p>
*
+ * <p>
* Care has been taken so that the invocation handler is serializable. However,
* it is only serializable within one virtual machine. It cannot be used in a
* distributed context where it can be sent to another JVM.
+ * </p>
*
* @param T
* Interface to proxy.
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
+import java.io.Serializable;
+
import javax.naming.InitialContext;
import org.junit.After;
assertEquals(NAA_NA_NA_NAA_NA, e.getMessage());
}
}
+
+ private static final class MyLookup implements Lookup {
+
+ @Override
+ public Object lookup() throws Exception {
+ return new MyInterface() {
+
+ @Override
+ public int execute() {
+ return 10;
+ }
+ };
+ }
+ }
+
+ @Test
+ public void testProxyMustBerializable() throws Exception {
+ lookup = new MyLookup();
+ LookupProxyFactory<MyInterface> factory = new LookupProxyFactory<MyInterface>(
+ MyInterface.class, lookup);
+ proxy = factory.getProxy();
+
+ assertEquals(10, proxy.execute());
+
+ assertTrue(proxy instanceof Serializable);
+
+ proxy = ObjectSerializationUtils.deserialize(ObjectSerializationUtils.serialize(proxy),
+ MyInterface.class);
+
+ // and it should still work
+ assertEquals(10, proxy.execute());
+ }
+
}
--- /dev/null
+/*
+ * 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.general;
+
+import static junit.framework.Assert.*;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class ObjectSerializationUtilsTest {
+
+ @Test
+ public void testSerialize() throws Exception {
+ String s = "hello world";
+ byte[] data = ObjectSerializationUtils.serialize(s);
+ assertNotNull(data);
+ String t = ObjectSerializationUtils.deserialize(data, String.class);
+ assertEquals(s, t);
+ }
+}