(no commit message)
[utils] / test / enterprise / src / main / java / org / wamblee / support / ThreadSpecificProxyFactory.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.support;
17
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Proxy;
22
23 /**
24  * Thread-specific proxy is used to create implementations of interfaces that
25  * delegate to a thread-specific implementation of the service.
26  * 
27  * It is used for instance to pass a transaction scoped entity manager around.
28  * 
29  * @param T
30  *            Interface to proxy.
31  * @author Erik Brakkee
32  * 
33  */
34 public class ThreadSpecificProxyFactory<T> {
35     private class ThreadSpecificInvocationHandler implements InvocationHandler {
36
37         @Override
38         public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
39             throws Throwable {
40             try {
41                 return aMethod.invoke(svc.get(), aArgs);
42             } catch (InvocationTargetException e) {
43                 throw e.getCause();
44             }
45         }
46     }
47
48     private ThreadLocal<T> svc = new ThreadLocal<T>();
49     private Class clazz;
50
51     /**
52      * Constructs the factory.
53      * 
54      * @param aClass
55      *            Interface class of the service to proxy.
56      */
57     public ThreadSpecificProxyFactory(Class<T> aClass) {
58         if (!aClass.isInterface()) {
59             throw new IllegalArgumentException("Class " + aClass.getName() +
60                 " is not an interface");
61         }
62         clazz = aClass;
63     }
64
65     /**
66      * Sets the thread-specific service.
67      * 
68      * @param aService
69      *            Service, use null value to reset.
70      */
71     public void set(T aService) {
72         svc.set(aService);
73     }
74     
75     /**
76      * Gets the current thread-specific service. 
77      * @return Service. 
78      */
79     public T get() { 
80         return svc.get();
81     }
82
83     /**
84      * Gets the proxy that delegates to the thread-specific instance set by
85      * {@link #set(Object)}
86      * 
87      * @return Proxy.
88      */
89     public T getProxy() {
90         InvocationHandler handler = new ThreadSpecificInvocationHandler();
91         Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
92             new Class[] { clazz });
93         T proxy;
94         try {
95             proxy = (T) proxyClass.getConstructor(
96                 new Class[] { InvocationHandler.class }).newInstance(
97                 new Object[] { handler });
98             return proxy;
99         } catch (Exception e) {
100             throw new RuntimeException("Could not create proxy for " +
101                 clazz.getName(), e);
102         }
103     }
104 }