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