|
1 |
| package net.sf.crispy; |
|
2 |
| |
|
3 |
| import java.lang.reflect.Method; |
|
4 |
| import java.util.Collection; |
|
5 |
| |
|
6 |
| import net.sf.crispy.proxy.Proxy; |
|
7 |
| import net.sf.crispy.util.Converter; |
|
8 |
| import net.sf.crispy.util.Invoker; |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| public class ExecutorDecorator extends Executor { |
|
18 |
| |
|
19 |
| private transient final Executor executor; |
|
20 |
| |
|
21 |
243
| public ExecutorDecorator(Executor pvExecutor) {
|
|
22 |
243
| super();
|
|
23 |
243
| executor = pvExecutor;
|
|
24 |
243
| this.setProperties(executor.getProperties());
|
|
25 |
| } |
|
26 |
| |
|
27 |
3
| public Executor getExecutor() { return executor; }
|
|
28 |
| |
|
29 |
939
| public static Object notRemoteMethod (final Method pvMethod, final Class pvProxyClass) {
|
|
30 |
939
| final String lvMethodName = pvMethod.getName();
|
|
31 |
939
| Object lvReturn = null;
|
|
32 |
939
| if ("toString".equals(lvMethodName)) {
|
|
33 |
5
| lvReturn = "toString: " + pvProxyClass.getName();
|
|
34 |
| } |
|
35 |
934
| else if ("getClass".equals(lvMethodName)) {
|
|
36 |
1
| lvReturn = pvProxyClass;
|
|
37 |
| } |
|
38 |
933
| else if ("equals".equals(lvMethodName)) {
|
|
39 |
2
| lvReturn = Boolean.FALSE;
|
|
40 |
| } |
|
41 |
931
| else if ("hashCode".equals(lvMethodName)) {
|
|
42 |
4
| lvReturn = Integer.valueOf("0");
|
|
43 |
| } |
|
44 |
| |
|
45 |
939
| return lvReturn;
|
|
46 |
| } |
|
47 |
| |
|
48 |
| |
|
49 |
365
| public Object createInvocationStrategy (final Class pvProxyClass, final Method pvMethod) throws Exception {
|
|
50 |
| |
|
51 |
365
| executor.setInvocationStrategy(null);
|
|
52 |
365
| return Proxy.createInvocationStrategy(executor.getDefaultInvocationStrategy(),
|
|
53 |
| getProperties(), executor.getDefaultUrlAndPort(), |
|
54 |
| pvProxyClass, pvMethod); |
|
55 |
| } |
|
56 |
| |
|
57 |
217
| public String getDefaultUrlAndPort() { return executor.getDefaultUrlAndPort(); }
|
|
58 |
1
| public InvocationStrategy getDefaultInvocationStrategy() { return executor.getDefaultInvocationStrategy(); }
|
|
59 |
| |
|
60 |
359
| public Object execute(final Class pvProxyClass, final Object pvProxy, final Method pvMethod, final Object[] pvArgs) throws Exception {
|
|
61 |
359
| final Object isPermittedMethod = notRemoteMethod(pvMethod, pvProxyClass);
|
|
62 |
359
| if (isPermittedMethod == null) {
|
|
63 |
357
| if ((executor.getProperties() != null) && (executor.getUrlAndPort() == null)) {
|
|
64 |
217
| executor.setUrlAndPort(getProperties().getProperty(Property.REMOTE_URL_AND_PORT, getDefaultUrlAndPort()));
|
|
65 |
| } |
|
66 |
| } else { |
|
67 |
| if (log.isDebugEnabled()) { log.debug("PermittedMethod: " + pvMethod); } |
|
68 |
2
| return isPermittedMethod;
|
|
69 |
| } |
|
70 |
| |
|
71 |
| |
|
72 |
357
| final Object lvInvocationStrategy = createInvocationStrategy(pvProxyClass, pvMethod);
|
|
73 |
357
| executor.setInvocationStrategy(lvInvocationStrategy);
|
|
74 |
| |
|
75 |
| |
|
76 |
357
| boolean lvWithConverter = executor.withConverter();
|
|
77 |
357
| String lvStrWithConverter = getProperties().getProperty(Property.WITH_CONVERTER, Boolean.valueOf(lvWithConverter).toString());
|
|
78 |
357
| lvWithConverter = Boolean.valueOf(lvStrWithConverter).booleanValue();
|
|
79 |
| |
|
80 |
| |
|
81 |
357
| Object lvParams[] = pvArgs;
|
|
82 |
357
| Converter lvConverter = null;
|
|
83 |
357
| if (lvWithConverter == true) {
|
|
84 |
135
| String lvPropertyNullValue = getProperties().getProperty(Property.NULL_VALUE, null);
|
|
85 |
135
| lvConverter = new Converter(lvPropertyNullValue);
|
|
86 |
135
| lvConverter.setWithSimpleKeyMapper(true);
|
|
87 |
135
| lvParams = Invoker.array2SimpleArray(pvArgs, lvConverter);
|
|
88 |
| } |
|
89 |
| |
|
90 |
357
| Object lvResult = executor.execute(pvProxyClass, pvProxy, pvMethod, lvParams);
|
|
91 |
| |
|
92 |
| |
|
93 |
340
| if (lvWithConverter == true) {
|
|
94 |
128
| if ((lvResult instanceof Collection) && (pvMethod.getReturnType().isArray())) {
|
|
95 |
6
| Class lvTypeInArray = pvMethod.getReturnType().getComponentType();
|
|
96 |
6
| lvResult = lvConverter.makeComplex(lvResult, pvMethod.getReturnType(), lvTypeInArray);
|
|
97 |
| } else { |
|
98 |
122
| lvResult = lvConverter.makeComplex(lvResult, pvMethod.getReturnType());
|
|
99 |
| } |
|
100 |
| } |
|
101 |
| |
|
102 |
340
| return lvResult;
|
|
103 |
| } |
|
104 |
| |
|
105 |
| } |