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