(no commit message)
[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.
45      * 
46      * @param aClass
47      *            Interface class of the service to proxy.
48      */
49     public SerializableProxyFactory(Class<T> aClass) {
50         this(aClass, null);
51     }
52
53     /**
54      * Constructs the factory with a callback to create thread-specific objects
55      * automatically.
56      * 
57      * @param aClass
58      *            Interface class of the service to proxy.
59      * @param aCallback
60      *            Callback to create the object if it does not exist. When null,
61      *            then no initialization is done.
62      */
63     public SerializableProxyFactory(Class<T> aClass, T aSvc) {
64         if (!aClass.isInterface()) {
65             throw new IllegalArgumentException("Class " + aClass.getName() +
66                 " is not an interface");
67         }
68         if (!aClass.isInstance(aSvc)) {
69             throw new IllegalArgumentException("Object " + aSvc +
70                 " cannot be cast to " + aSvc.getClass());
71         }
72         svc = aSvc;
73         clazz = aClass;
74         proxy = createProxy();
75
76     }
77
78     /**
79      * Gets the underlying service.
80      * 
81      * @return Service.
82      */
83     public T get() {
84         return svc;
85     }
86
87     /**
88      * Gets the proxy that delegates to the thread-specific instance set by
89      * {@link #set(Object)}
90      * 
91      * @return Proxy.
92      */
93     public T getProxy() {
94         return proxy;
95     }
96
97     private T createProxy() {
98         InvocationHandler handler = new SerializableInvocationHandler(svc,
99             clazz);
100         Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
101             new Class[] { clazz });
102         T proxyObj;
103         try {
104             proxyObj = (T) proxyClass.getConstructor(
105                 new Class[] { InvocationHandler.class }).newInstance(
106                 new Object[] { handler });
107             return proxyObj;
108         } catch (Exception e) {
109             throw new RuntimeException("Could not create proxy for " +
110                 clazz.getName(), e);
111         }
112     }
113 }