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 * Is a special DynamicProxy. He is to encapsulate the Commons Proxy library.
13 *
14 * @author Linke
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 public CommonsProxyFactory() {
25
26 }
27
28 public CommonsProxyFactory(ProxyFactory pvProxyFactory) {
29 proxyFactory = pvProxyFactory;
30 }
31
32
33 public DynamicProxy createDynamicProxy (String pvName) {
34 String lvName = pvName;
35
36 if (lvName == null) { lvName = DEFAULT_DYNAMIC_PROXY; }
37
38 if (proxyClassMap == null) {
39 proxyClassMap = new Hashtable();
40
41 proxyClassMap.put("net.sf.crispy.impl.DynamicCglibProxy", "org.apache.commons.proxy.factory.cglib.CglibProxyFactory");
42 proxyClassMap.put("net.sf.crispy.impl.DynamicJdkProxy", "org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory");
43
44 proxyClassMap.put(Property.VALUE_FOR_CGLIB_DYNAMIC_PROXY, "org.apache.commons.proxy.factory.cglib.CglibProxyFactory");
45 proxyClassMap.put(Property.VALUE_FOR_JDK_DYNAMIC_PROXY, "org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory");
46 proxyClassMap.put(Property.VALUE_FOR_JAVASSIST_DYNAMIC_PROXY, "org.apache.commons.proxy.factory.javassist.JavassistProxyFactory");
47 }
48
49 ProxyFactory lvProxyFactory = null;
50 String lvProxyFactoryClassString = (String) proxyClassMap.get(lvName);
51 if (lvProxyFactoryClassString == null) {
52 try {
53 lvProxyFactory = (ProxyFactory) Class.forName(lvName).newInstance();
54 } catch (Exception e) {
55 lvProxyFactory = new ProxyFactory();
56 }
57 } else {
58 try {
59 lvProxyFactory = (ProxyFactory) Class.forName(lvProxyFactoryClassString).newInstance();
60 } catch (Exception e) {
61 lvProxyFactory = new ProxyFactory();
62 }
63 }
64 return new CommonsProxyFactory(lvProxyFactory);
65 }
66
67
68
69 public ProxyFactory getProxyFactory() { return proxyFactory; }
70
71 public Object newInstance(Class pvClass) {
72 super.newInstance(pvClass);
73 Object o = proxyFactory.createInvokerProxy(pvClass.getClassLoader(), this, new Class[] {pvClass});
74 if (log.isDebugEnabled()) {
75 log.debug("Generated proxy for class: " + pvClass);
76 }
77 return o;
78 }
79
80
81
82
83
84
85 }