javadoc updates
[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  * The {@link #set(Object)} method sets the current service instance for the current thread. 
30  * The {@link #get()} method gets the current service instance for the current thread. 
31  * The {@link #getProxy()} method gets a proxy that will delegate at runtime to the thread-specific 
32  * instance. The result from this method can be passed at construction of an object that will be used
33  * by multiple threads. 
34  * 
35  * @param T
36  *            Interface to proxy.
37  * @author Erik Brakkee
38  * 
39  */
40 public class ThreadSpecificProxyFactory<T> {
41     private class ThreadSpecificInvocationHandler implements InvocationHandler {
42
43         @Override
44         public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
45             throws Throwable {
46             try {
47                 return aMethod.invoke(svc.get(), aArgs);
48             } catch (InvocationTargetException e) {
49                 throw e.getCause();
50             }
51         }
52     }
53
54     private ThreadLocal<T> svc = new ThreadLocal<T>();
55     private Class clazz;
56
57     /**
58      * Constructs the factory.
59      * 
60      * @param aClass
61      *            Interface class of the service to proxy.
62      */
63     public ThreadSpecificProxyFactory(Class<T> aClass) {
64         if (!aClass.isInterface()) {
65             throw new IllegalArgumentException("Class " + aClass.getName() +
66                 " is not an interface");
67         }
68         clazz = aClass;
69     }
70
71     /**
72      * Sets the thread-specific service.
73      * 
74      * @param aService
75      *            Service, use null value to reset.
76      */
77     public void set(T aService) {
78         svc.set(aService);
79     }
80     
81     /**
82      * Gets the current thread-specific service. 
83      * @return Service. 
84      */
85     public T get() { 
86         return svc.get();
87     }
88
89     /**
90      * Gets the proxy that delegates to the thread-specific instance set by
91      * {@link #set(Object)}
92      * 
93      * @return Proxy.
94      */
95     public T getProxy() {
96         InvocationHandler handler = new ThreadSpecificInvocationHandler();
97         Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
98             new Class[] { clazz });
99         T proxy;
100         try {
101             proxy = (T) proxyClass.getConstructor(
102                 new Class[] { InvocationHandler.class }).newInstance(
103                 new Object[] { handler });
104             return proxy;
105         } catch (Exception e) {
106             throw new RuntimeException("Could not create proxy for " +
107                 clazz.getName(), e);
108         }
109     }
110 }