4a3aba550910092379831b6f0e92e3b3d04a80ad
[utils] / support / general / src / main / java / org / wamblee / general / SerializableProxyFactory.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  * Serializable proxy factory that allows to create serializable proxies to
24  * objects that are themselves not serializable.
25  * </p>
26  * 
27  * <p>
28  * This class does not do any cleanup so it is recommended to only use this in
29  * test utilities.
30  * </p>
31  * 
32  * 
33  * @author Erik Brakkee
34  * 
35  * @param <T>
36  */
37 public class SerializableProxyFactory<T> {
38
39     private T svc;
40     private Class clazz;
41     private T proxy;
42
43     /**
44      * Constructs the factory with a callback to create thread-specific objects
45      * automatically.
46      * 
47      * @param aClass
48      *            Interface class of the service to proxy.
49      * @param aCallback
50      *            Callback to create the object if it does not exist. When null,
51      *            then no initialization is done.
52      */
53     public SerializableProxyFactory(Class<T> aClass, T aSvc) {
54         if (!aClass.isInterface()) {
55             throw new IllegalArgumentException("Class " + aClass.getName() +
56                 " is not an interface");
57         }
58         if (!aClass.isInstance(aSvc)) {
59             throw new IllegalArgumentException("Object " + aSvc +
60                 " cannot be cast to " + aSvc.getClass());
61         }
62         svc = aSvc;
63         clazz = aClass;
64         proxy = createProxy();
65
66     }
67
68     /**
69      * Gets the underlying service.
70      * 
71      * @return Service.
72      */
73     public T get() {
74         return svc;
75     }
76
77     /**
78      * Gets the proxy that delegates to the thread-specific instance set by
79      * {@link #set(Object)}
80      * 
81      * @return Proxy.
82      */
83     public T getProxy() {
84         return proxy;
85     }
86
87     private T createProxy() {
88         InvocationHandler handler = new SerializableInvocationHandler(svc,
89             clazz);
90         Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
91             new Class[] { clazz });
92         T proxyObj;
93         try {
94             proxyObj = (T) proxyClass.getConstructor(
95                 new Class[] { InvocationHandler.class }).newInstance(
96                 new Object[] { handler });
97             return proxyObj;
98         } catch (Exception e) {
99             throw new RuntimeException("Could not create proxy for " +
100                 clazz.getName(), e);
101         }
102     }
103 }