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