(no commit message)
[utils] / support / general / src / main / java / org / wamblee / general / ThreadSpecificProxyFactory.java
index f7087296796d8a60ae14fc83139aed7603c5ef2e..db51ba1c84f5733865a76507a8606434f7f2f70a 100644 (file)
 package org.wamblee.general;
 
 import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 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>
  * 
- * It is used for instance to pass a transaction scoped entity manager around.
+ * <p>
+ * It can be used for instance to create a contextual reference to an entity
+ * manager that delegates to a thread-specific instance.
+ * </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>
+ * 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>
  * 
- * This class is mostly used by other test tools. 
+ * <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.
@@ -40,21 +54,26 @@ import java.lang.reflect.Proxy;
  * 
  */
 public class ThreadSpecificProxyFactory<T> {
-    private class ThreadSpecificInvocationHandler implements InvocationHandler {
 
-        @Override
-        public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
-            throws Throwable {
-            try {
-                return aMethod.invoke(svc.get(), aArgs);
-            } catch (InvocationTargetException e) {
-                throw e.getCause();
-            }
-        }
+    /**
+     * Optional callback invoked to create the thread-specific object when there
+     * is no object yet associated with the current thread.
+     * 
+     * @author Erik Brakkee
+     * 
+     */
+    public static interface CreationCallback<T> {
+        /**
+         * Creates the object.
+         * 
+         * @return Object.
+         */
+        T create();
     }
 
-    private ThreadLocal<T> svc = new ThreadLocal<T>();
+    private ThreadLocal<T> svc;
     private Class clazz;
+    private T proxy;
 
     /**
      * Constructs the factory.
@@ -63,11 +82,37 @@ public class ThreadSpecificProxyFactory<T> {
      *            Interface class of the service to proxy.
      */
     public ThreadSpecificProxyFactory(Class<T> aClass) {
+        this(aClass, null);
+    }
+
+    /**
+     * Constructs the factory with a callback to create thread-specific objects
+     * automatically.
+     * 
+     * @param aClass
+     *            Interface class of the service to proxy.
+     * @param aCallback
+     *            Callback to create the object if it does not exist. When null,
+     *            then no initialization is done.
+     */
+    public ThreadSpecificProxyFactory(Class<T> aClass,
+        final CreationCallback<T> aCallback) {
         if (!aClass.isInterface()) {
             throw new IllegalArgumentException("Class " + aClass.getName() +
                 " is not an interface");
         }
+        svc = new ThreadLocal<T>() {
+            @Override
+            protected T initialValue() {
+                if (aCallback != null) {
+                    return aCallback.create();
+                }
+                return null;
+            }
+        };
         clazz = aClass;
+        proxy = createProxy();
+
     }
 
     /**
@@ -79,12 +124,15 @@ public class ThreadSpecificProxyFactory<T> {
     public void set(T aService) {
         svc.set(aService);
     }
-    
+
     /**
-     * Gets the current thread-specific service. 
-     * @return Service. 
+     * Gets the current thread-specific service. To get a contextual reference
+     * that can be used by any thread but delegates to a thread-specific
+     * instance, use {@link #getProxy()}.
+     * 
+     * @return Service.
      */
-    public T get() { 
+    public T get() {
         return svc.get();
     }
 
@@ -95,15 +143,20 @@ public class ThreadSpecificProxyFactory<T> {
      * @return Proxy.
      */
     public T getProxy() {
-        InvocationHandler handler = new ThreadSpecificInvocationHandler();
+        return proxy;
+    }
+
+    private T createProxy() {
+        InvocationHandler handler = new ThreadSpecificInvocationHandler(svc,
+            clazz);
         Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
             new Class[] { clazz });
-        T proxy;
+        T proxyObj;
         try {
-            proxy = (T) proxyClass.getConstructor(
+            proxyObj = (T) proxyClass.getConstructor(
                 new Class[] { InvocationHandler.class }).newInstance(
                 new Object[] { handler });
-            return proxy;
+            return proxyObj;
         } catch (Exception e) {
             throw new RuntimeException("Could not create proxy for " +
                 clazz.getName(), e);