1
2
3
4
5 package net.sf.crispy.proxy;
6
7 import java.lang.reflect.Method;
8 import java.util.Hashtable;
9 import java.util.Map;
10 import java.util.Properties;
11
12 import net.sf.crispy.InvocationException;
13 import net.sf.crispy.InvocationStrategy;
14 import net.sf.crispy.Property;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 /**
20 * Parent-class from all Proxys.
21 *
22 * @author Linke
23 *
24 */
25 public abstract class Proxy {
26
27 protected static final Log log = LogFactory.getLog (Proxy.class);
28
29
30 private Class proxyClass = null;
31 private Object proxyObject = null;
32 private Properties properties = new Properties();
33
34
35 public void setProxyClass(Class pvClazz) { proxyClass = pvClazz; }
36 public Class getProxyClass() { return proxyClass; }
37
38 public void setProxyObject(Object pvProxyObject) { proxyObject = pvProxyObject; }
39 public Object getProxyObject() { return proxyObject; }
40
41 public void setProperties(Properties pvProperties) { properties = pvProperties; }
42 public Properties getProperties() { return properties; }
43
44 public abstract Object newInstance (Class pvClass);
45
46
47
48
49 public static Object createInvocationStrategy (final InvocationStrategy pvDefaultInvocationStrategy,
50 final Properties pvProperties, final String pvDefaultUrlAndPort,
51 final Class pvProxyClass, final Method pvMethod) throws Exception {
52
53
54 InvocationStrategy lvInvocationStrategy = pvDefaultInvocationStrategy;
55
56 String lvClassStrInvocationStrategy = pvProperties.getProperty(Property.INVOCATION_STRATEGY);
57 if (lvClassStrInvocationStrategy != null) {
58 Class lvStrategyClass = Class.forName(lvClassStrInvocationStrategy);
59 try {
60 lvInvocationStrategy = (InvocationStrategy) lvStrategyClass.newInstance();
61 } catch (Exception e) {
62 throw new InvocationException("Can't create instance for InvocationStrategy-Class: "
63 + lvStrategyClass + " (" + e + ")!");
64 }
65 }
66
67
68 if (lvInvocationStrategy != null) {
69 Map lvInvStrategyProps = new Hashtable(4);
70 lvInvStrategyProps.put(InvocationStrategy.CLASS_NAME, pvProxyClass.getName());
71 if (pvMethod != null) {
72 lvInvStrategyProps.put(InvocationStrategy.METHOD_NAME, pvMethod.getName());
73 }
74 String lvConstNameSpace = pvProperties.getProperty(Property.INVOCATION_STRATEGY_NAMESPACE);
75 if (lvConstNameSpace != null) {
76 lvInvStrategyProps.put(InvocationStrategy.CONST_NAMESPACE, lvConstNameSpace);
77 }
78 String lvUrl = pvProperties.getProperty(Property.REMOTE_URL_AND_PORT);
79 if (lvUrl == null) {
80 lvUrl = pvDefaultUrlAndPort;
81 }
82 lvInvStrategyProps.put(InvocationStrategy.URL, lvUrl);
83 return lvInvocationStrategy.convert(lvInvStrategyProps);
84 }
85
86 if (log.isDebugEnabled()) {
87 log.debug("No InvocationStrategy: " + pvProxyClass + " / " + pvMethod + ")");
88 }
89 return null;
90 }
91
92
93 }