All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mmarquee.automation.utils.Canalizer Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package mmarquee.automation.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Wraps an interface instance in a way that all calls to interface methods originate from the same thread.
 *
 * @author Pascal Bihler
 *
 */
public class Canalizer {
    /** The thread where all calls originate from. */
    static final ExecutorService executor = Executors.newSingleThreadExecutor();

    /**
     * Shutdown the ExecutorService nicely.
     */
    public static void shutdown() {
        executor.shutdown();
    }

    /**
     * Wraps an interface instance in a way that all calls to interface methods originate from the same thread.
     *
     * @param  The type of the Object to canalize
     * @param plainInstance The instance to canalize
     * @return the canalized instance
     */
    @SuppressWarnings("unchecked")
    public static  T canalize(final T plainInstance) {
        final CanalizerInvocationHandler invocationHandler = new CanalizerInvocationHandler(executor,plainInstance);
        return (T) java.lang.reflect.Proxy
                .newProxyInstance(plainInstance.getClass().getClassLoader(),
                        getInterfaces(plainInstance),
                        invocationHandler);
    }

    private static Class[] getInterfaces(final Object target) {
        Class base = target.getClass();
        final Set> interfaces = new HashSet<>();
        if (base.isInterface()) {
            interfaces.add(base);
        }
        while (base != null && !Object.class.equals(base)) {
            interfaces.addAll(Arrays.asList(base.getInterfaces()));
            base = base.getSuperclass();
        }
        return interfaces.toArray(new Class[0]);
    }

    static class CanalizerInvocationHandler implements InvocationHandler {
        private ExecutorService executor;
        private Object underlying;

        public CanalizerInvocationHandler(final ExecutorService executor, final Object underlying) {
            this.executor = executor;
            this.underlying = underlying;
        }

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable
        {
            final Callable task = new Callable() {
                @Override
                public Object call() throws Exception
                {
                    return method.invoke(underlying, args);
                }
            };

            final Future result = executor.submit(task);

            try {
                return result.get();
            } catch (final ExecutionException ex) {
                throw ex.getCause();
            }
        }

    }

}