(no commit message)
[utils] / support / general / src / main / java / org / wamblee / general / 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.general;
17
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.Proxy;
20
21 /**
22  * <p>
23  * Thread-specific proxy is used to create implementations of interfaces that
24  * delegate to a thread-specific implementation of the service.
25  * </p>
26  * 
27  * <p>
28  * It can be used for instance to create a contextual reference to an entity
29  * manager that delegates to a thread-specific instance.
30  * </p>
31  * 
32  * <p>
33  * The {@link #set(Object)} method sets the current service instance for the
34  * current thread. The {@link #get()} method gets the current service instance
35  * for the current thread. The {@link #getProxy()} method gets a proxy that will
36  * delegate at runtime to the thread-specific instance. The result from this
37  * method can be passed at construction of an object that will be used by
38  * multiple threads.
39  * </p>
40  * 
41  * <p>
42  * This class is mostly used by infrastructure code (utilities) and test tools.
43  * </p>
44  * 
45  * <p>
46  * Care has been taken so that the invocation handler is serializable. However,
47  * it is only serializable within one virtual machine. It cannot be used in a
48  * distributed context where it can be sent to another JVM.
49  * </p>
50  * 
51  * <p>
52  * This class currently does not do any cleanup. So it should not be used in production code
53  * but only in test utilities. 
54  * </p>
55  * 
56  * @param T
57  *            Interface to proxy.
58  * @author Erik Brakkee
59  * 
60  */
61 public class ThreadSpecificProxyFactory<T> {
62
63     /**
64      * Optional callback invoked to create the thread-specific object when there
65      * is no object yet associated with the current thread.
66      * 
67      * @author Erik Brakkee
68      * 
69      */
70     public static interface CreationCallback<T> {
71         /**
72          * Creates the object.
73          * 
74          * @return Object.
75          */
76         T create();
77     }
78
79     private ThreadLocal<T> svc;
80     private Class clazz;
81     private T proxy;
82
83     /**
84      * Constructs the factory.
85      * 
86      * @param aClass
87      *            Interface class of the service to proxy.
88      */
89     public ThreadSpecificProxyFactory(Class<T> aClass) {
90         this(aClass, null);
91     }
92
93     /**
94      * Constructs the factory with a callback to create thread-specific objects
95      * automatically.
96      * 
97      * @param aClass
98      *            Interface class of the service to proxy.
99      * @param aCallback
100      *            Callback to create the object if it does not exist. When null,
101      *            then no initialization is done.
102      */
103     public ThreadSpecificProxyFactory(Class<T> aClass,
104         final CreationCallback<T> aCallback) {
105         if (!aClass.isInterface()) {
106             throw new IllegalArgumentException("Class " + aClass.getName() +
107                 " is not an interface");
108         }
109         svc = new ThreadLocal<T>() {
110             @Override
111             protected T initialValue() {
112                 if (aCallback != null) {
113                     return aCallback.create();
114                 }
115                 return null;
116             }
117         };
118         clazz = aClass;
119         proxy = createProxy();
120
121     }
122
123     /**
124      * Sets the thread-specific service.
125      * 
126      * @param aService
127      *            Service, use null value to reset.
128      */
129     public void set(T aService) {
130         svc.set(aService);
131     }
132
133     /**
134      * Gets the current thread-specific service. To get a contextual reference
135      * that can be used by any thread but delegates to a thread-specific
136      * instance, use {@link #getProxy()}.
137      * 
138      * @return Service.
139      */
140     public T get() {
141         return svc.get();
142     }
143
144     /**
145      * Gets the proxy that delegates to the thread-specific instance set by
146      * {@link #set(Object)}
147      * 
148      * @return Proxy.
149      */
150     public T getProxy() {
151         return proxy;
152     }
153
154     private T createProxy() {
155         InvocationHandler handler = new ThreadSpecificInvocationHandler(svc,
156             clazz);
157         Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
158             new Class[] { clazz });
159         T proxyObj;
160         try {
161             proxyObj = (T) proxyClass.getConstructor(
162                 new Class[] { InvocationHandler.class }).newInstance(
163                 new Object[] { handler });
164             return proxyObj;
165         } catch (Exception e) {
166             throw new RuntimeException("Could not create proxy for " +
167                 clazz.getName(), e);
168         }
169     }
170 }