1 package net.sf.crispy.proxy;
2
3 import java.util.Properties;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 import net.sf.crispy.Property;
9 import net.sf.crispy.concurrent.AsynchronousCallback;
10 import net.sf.crispy.impl.ServiceManager;
11 import net.sf.crispy.util.Util;
12
13 /**
14 * Encapsulate create a instance of DynamicProxy.
15 *
16 * @author Linke
17 *
18 */
19 public class DynamicProxyFactory {
20
21 protected static final Log log = LogFactory.getLog (Proxy.class);
22
23 public static final String DEFAULT_DYNAMIC_PROXY = Property.VALUE_FOR_JDK_DYNAMIC_PROXY;
24
25 private static CommonsProxyFactory commonsProxyFactory = null;
26
27
28 public static DynamicProxy getDefaultDynamicProxy () {
29 return createDynamicProxy (DEFAULT_DYNAMIC_PROXY, null);
30 }
31
32 public static DynamicProxy getDefaultDynamicProxy (final Properties pvProperties) {
33 return createDynamicProxy (DEFAULT_DYNAMIC_PROXY, pvProperties);
34 }
35
36 public static DynamicProxy createDynamicProxy (final String pvName) {
37 return createDynamicProxy(pvName, null);
38 }
39
40 public static DynamicProxy createDynamicProxy (final String pvName, final Properties pvProperties) {
41
42 DynamicProxy lvDynamicProxy = null;
43
44 if ((pvName == null) || (pvName.equals(DEFAULT_DYNAMIC_PROXY)) || (pvName.equals("net.sf.crispy.impl.DynamicJdkProxy"))) {
45 lvDynamicProxy = new DynamicJdkProxy();
46 } else {
47
48
49 try {
50
51 if (commonsProxyFactory == null) {
52 commonsProxyFactory = (CommonsProxyFactory) Util.createObject(CommonsProxyFactory.class.getName());
53 }
54 lvDynamicProxy = commonsProxyFactory.createDynamicProxy(pvName);
55 } catch (Exception e) {
56 if (log.isDebugEnabled()) {
57 log.debug("Can't load CommonsProxyFactory: " + e);
58 }
59 }
60 }
61
62 AsynchronousCallback lvAsynchronousCallback = createAsynchronousCallback(pvProperties);
63 if (lvAsynchronousCallback != null) {
64 int lvMaxSizeOfThreads = getMaxSizeOfThreads(pvProperties);
65 lvDynamicProxy.setAsynchronousCallback(lvAsynchronousCallback, null, lvMaxSizeOfThreads);
66 }
67
68 return lvDynamicProxy;
69 }
70
71 public static AsynchronousCallback createAsynchronousCallback(final Properties pvProperties) {
72 AsynchronousCallback lvAsynchronousCallback = null;
73 if (pvProperties != null) {
74 String lvAsyncCallImplClassString = pvProperties.getProperty(Property.ASYNCHRONOUS_CALLBACK_CLASS);
75 if (lvAsyncCallImplClassString != null) {
76 try {
77 Object o = Util.createObject(lvAsyncCallImplClassString);
78 if (o instanceof AsynchronousCallback) {
79 lvAsynchronousCallback = (AsynchronousCallback) o;
80 } else {
81 throw new IllegalArgumentException (lvAsyncCallImplClassString + " is not a instance of AsynchronousCallback!");
82 }
83 } catch (Exception e) {
84 if (ServiceManager.DEBUG_MODE_ON) {
85 e.printStackTrace();
86 }
87 throw new IllegalArgumentException ("Can't create a instance of class string: " + lvAsyncCallImplClassString
88 + " (" + e + ")");
89 }
90 }
91 }
92
93 return lvAsynchronousCallback;
94 }
95
96 public static int getMaxSizeOfThreads(final Properties pvProperties) {
97 int lvMaxSizeOfThreads = 0;
98 if (pvProperties != null) {
99 String lvMaxSizeOfThreadsString = pvProperties.getProperty(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "0");
100 lvMaxSizeOfThreads = Integer.parseInt(lvMaxSizeOfThreadsString);
101 }
102 return lvMaxSizeOfThreads;
103 }
104
105 }