1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.spring.remoting; |
12 | |
|
13 | |
import org.mule.api.MuleEventContext; |
14 | |
import org.mule.api.lifecycle.Callable; |
15 | |
import org.mule.api.lifecycle.Initialisable; |
16 | |
import org.mule.api.lifecycle.InitialisationException; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.util.ClassUtils; |
19 | |
|
20 | |
import org.springframework.beans.factory.InitializingBean; |
21 | |
import org.springframework.remoting.support.RemoteInvocation; |
22 | |
import org.springframework.remoting.support.RemoteInvocationBasedExporter; |
23 | |
import org.springframework.remoting.support.RemoteInvocationExecutor; |
24 | |
import org.springframework.remoting.support.RemoteInvocationResult; |
25 | |
|
26 | |
public class SpringRemoteInvokerComponent implements Initialisable, Callable |
27 | |
{ |
28 | |
private Delegate delegate; |
29 | |
private Class serviceClass; |
30 | |
private Class serviceInterface; |
31 | |
private Object serviceBean; |
32 | 0 | private boolean registerTraceInterceptor = false; |
33 | |
private RemoteInvocationExecutor remoteInvocationExecutor; |
34 | |
|
35 | 0 | private class Delegate extends RemoteInvocationBasedExporter implements InitializingBean |
36 | |
{ |
37 | |
private Object proxy; |
38 | |
|
39 | |
public void afterPropertiesSet() |
40 | |
{ |
41 | 0 | this.proxy = getProxyForService(); |
42 | 0 | } |
43 | |
|
44 | |
public Object execute(RemoteInvocation invocation) |
45 | |
{ |
46 | |
try |
47 | |
{ |
48 | 0 | Object value = invoke(invocation, proxy); |
49 | 0 | return value; |
50 | |
} |
51 | 0 | catch (Throwable ex) |
52 | |
{ |
53 | 0 | return new RemoteInvocationResult(ex); |
54 | |
} |
55 | |
} |
56 | |
} |
57 | |
|
58 | |
public SpringRemoteInvokerComponent() |
59 | 0 | { |
60 | 0 | delegate = new Delegate(); |
61 | 0 | } |
62 | |
|
63 | |
public void initialise() throws InitialisationException |
64 | |
{ |
65 | 0 | if (serviceClass == null && serviceBean == null) |
66 | |
{ |
67 | 0 | throw new InitialisationException(CoreMessages.propertiesNotSet("serviceClass or serviceBean"), |
68 | |
this); |
69 | |
} |
70 | 0 | if (serviceInterface == null) |
71 | |
{ |
72 | 0 | throw new InitialisationException(CoreMessages.propertiesNotSet("serviceInterface"), this); |
73 | |
} |
74 | |
|
75 | 0 | if (serviceClass != null) |
76 | |
{ |
77 | 0 | Object service = null; |
78 | |
try |
79 | |
{ |
80 | 0 | service = ClassUtils.instanciateClass(serviceClass, (Object[]) null); |
81 | |
} |
82 | 0 | catch (Exception e) |
83 | |
{ |
84 | 0 | throw new InitialisationException(e, this); |
85 | 0 | } |
86 | 0 | delegate.setService(service); |
87 | 0 | } |
88 | 0 | else if (serviceBean != null) |
89 | |
{ |
90 | 0 | delegate.setService(serviceBean); |
91 | |
} |
92 | 0 | delegate.setServiceInterface(serviceInterface); |
93 | 0 | delegate.setRegisterTraceInterceptor(registerTraceInterceptor); |
94 | 0 | if (remoteInvocationExecutor != null) |
95 | |
{ |
96 | 0 | delegate.setRemoteInvocationExecutor(remoteInvocationExecutor); |
97 | |
} |
98 | 0 | delegate.afterPropertiesSet(); |
99 | 0 | } |
100 | |
|
101 | |
public Class getServiceClass() |
102 | |
{ |
103 | 0 | return serviceClass; |
104 | |
} |
105 | |
|
106 | |
public void setServiceClass(Class serviceClass) |
107 | |
{ |
108 | 0 | this.serviceClass = serviceClass; |
109 | 0 | } |
110 | |
|
111 | |
public Object getServiceBean() |
112 | |
{ |
113 | 0 | return serviceBean; |
114 | |
} |
115 | |
|
116 | |
public void setServiceBean(Object serviceBean) |
117 | |
{ |
118 | 0 | this.serviceBean = serviceBean; |
119 | 0 | } |
120 | |
|
121 | |
public Class getServiceInterface() |
122 | |
{ |
123 | 0 | return serviceInterface; |
124 | |
} |
125 | |
|
126 | |
public void setServiceInterface(Class serviceInterface) |
127 | |
{ |
128 | 0 | this.serviceInterface = serviceInterface; |
129 | 0 | } |
130 | |
|
131 | |
public boolean isRegisterTraceInterceptor() |
132 | |
{ |
133 | 0 | return registerTraceInterceptor; |
134 | |
} |
135 | |
|
136 | |
public void setRegisterTraceInterceptor(boolean registerTraceInterceptor) |
137 | |
{ |
138 | 0 | this.registerTraceInterceptor = registerTraceInterceptor; |
139 | 0 | } |
140 | |
|
141 | |
public RemoteInvocationExecutor getRemoteInvocationExecutor() |
142 | |
{ |
143 | 0 | return remoteInvocationExecutor; |
144 | |
} |
145 | |
|
146 | |
public void setRemoteInvocationExecutor(RemoteInvocationExecutor remoteInvocationExecutor) |
147 | |
{ |
148 | 0 | this.remoteInvocationExecutor = remoteInvocationExecutor; |
149 | 0 | } |
150 | |
|
151 | |
public Object onCall(MuleEventContext eventContext) throws Exception |
152 | |
{ |
153 | 0 | Object payload = eventContext.getMessage().getPayload(); |
154 | 0 | RemoteInvocation ri = (RemoteInvocation) payload; |
155 | 0 | Object rval = delegate.execute(ri); |
156 | 0 | return rval; |
157 | |
} |
158 | |
} |