|
1 |
| package net.sf.crispy.proxy; |
|
2 |
| |
|
3 |
| import java.util.Hashtable; |
|
4 |
| import java.util.Map; |
|
5 |
| |
|
6 |
| import net.sf.crispy.Property; |
|
7 |
| |
|
8 |
| import org.apache.commons.proxy.Invoker; |
|
9 |
| import org.apache.commons.proxy.ProxyFactory; |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| public class CommonsProxyFactory extends DynamicProxy implements Invoker { |
|
18 |
| |
|
19 |
| public static final String DEFAULT_DYNAMIC_PROXY = Property.VALUE_FOR_JDK_DYNAMIC_PROXY; |
|
20 |
| |
|
21 |
| private ProxyFactory proxyFactory = null; |
|
22 |
| private static Map proxyClassMap = null; |
|
23 |
| |
|
24 |
11
| public CommonsProxyFactory() {
|
|
25 |
| |
|
26 |
| } |
|
27 |
| |
|
28 |
15
| public CommonsProxyFactory(ProxyFactory pvProxyFactory) {
|
|
29 |
15
| proxyFactory = pvProxyFactory;
|
|
30 |
| } |
|
31 |
| |
|
32 |
| |
|
33 |
15
| public DynamicProxy createDynamicProxy (String pvName) {
|
|
34 |
15
| String lvName = pvName;
|
|
35 |
| |
|
36 |
1
| if (lvName == null) { lvName = DEFAULT_DYNAMIC_PROXY; }
|
|
37 |
| |
|
38 |
15
| if (proxyClassMap == null) {
|
|
39 |
10
| proxyClassMap = new Hashtable();
|
|
40 |
| |
|
41 |
10
| proxyClassMap.put("net.sf.crispy.impl.DynamicCglibProxy", "org.apache.commons.proxy.factory.cglib.CglibProxyFactory");
|
|
42 |
10
| proxyClassMap.put("net.sf.crispy.impl.DynamicJdkProxy", "org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory");
|
|
43 |
| |
|
44 |
10
| proxyClassMap.put(Property.VALUE_FOR_CGLIB_DYNAMIC_PROXY, "org.apache.commons.proxy.factory.cglib.CglibProxyFactory");
|
|
45 |
10
| proxyClassMap.put(Property.VALUE_FOR_JDK_DYNAMIC_PROXY, "org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory");
|
|
46 |
10
| proxyClassMap.put(Property.VALUE_FOR_JAVASSIST_DYNAMIC_PROXY, "org.apache.commons.proxy.factory.javassist.JavassistProxyFactory");
|
|
47 |
| } |
|
48 |
| |
|
49 |
15
| ProxyFactory lvProxyFactory = null;
|
|
50 |
15
| String lvProxyFactoryClassString = (String) proxyClassMap.get(lvName);
|
|
51 |
15
| if (lvProxyFactoryClassString == null) {
|
|
52 |
3
| try {
|
|
53 |
3
| lvProxyFactory = (ProxyFactory) Class.forName(lvName).newInstance();
|
|
54 |
| } catch (Exception e) { |
|
55 |
2
| lvProxyFactory = new ProxyFactory();
|
|
56 |
| } |
|
57 |
| } else { |
|
58 |
12
| try {
|
|
59 |
12
| lvProxyFactory = (ProxyFactory) Class.forName(lvProxyFactoryClassString).newInstance();
|
|
60 |
| } catch (Exception e) { |
|
61 |
1
| lvProxyFactory = new ProxyFactory();
|
|
62 |
| } |
|
63 |
| } |
|
64 |
15
| return new CommonsProxyFactory(lvProxyFactory);
|
|
65 |
| } |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
4
| public ProxyFactory getProxyFactory() { return proxyFactory; }
|
|
70 |
| |
|
71 |
10
| public Object newInstance(Class pvClass) {
|
|
72 |
10
| super.newInstance(pvClass);
|
|
73 |
8
| Object o = proxyFactory.createInvokerProxy(pvClass.getClassLoader(), this, new Class[] {pvClass});
|
|
74 |
| if (log.isDebugEnabled()) { |
|
75 |
| log.debug("Generated proxy for class: " + pvClass); |
|
76 |
| } |
|
77 |
8
| return o;
|
|
78 |
| } |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| } |