(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 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 serializable proxy that delegates to the object passed in the constructor.
79      * 
80      * @return Proxy.
81      */
82     public T getProxy() {
83         return proxy;
84     }
85
86     private T createProxy() {
87         InvocationHandler handler = new SerializableInvocationHandler(svc,
88             clazz);
89         Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(),
90             new Class[] { clazz });
91         T proxyObj;
92         try {
93             proxyObj = (T) proxyClass.getConstructor(
94                 new Class[] { InvocationHandler.class }).newInstance(
95                 new Object[] { handler });
96             return proxyObj;
97         } catch (Exception e) {
98             throw new RuntimeException("Could not create proxy for " +
99                 clazz.getName(), e);
100         }
101     }
102 }