View Javadoc

1   /*
2    * $Id: SpringRemoteInvokerComponent.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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      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                  return new RemoteInvocationResult(ex);
54              }
55          }
56      }
57  
58      public SpringRemoteInvokerComponent()
59      {
60          delegate = new Delegate();
61      }
62  
63      public void initialise() throws InitialisationException
64      {
65          if (serviceClass == null && serviceBean == null)
66          {
67              throw new InitialisationException(CoreMessages.propertiesNotSet("serviceClass or serviceBean"),
68                  this);
69          }
70          if (serviceInterface == null)
71          {
72              throw new InitialisationException(CoreMessages.propertiesNotSet("serviceInterface"), this);
73          }
74  
75          if (serviceClass != null)
76          {
77              Object service = null;
78              try
79              {
80                  service = ClassUtils.instanciateClass(serviceClass, (Object[]) null);
81              }
82              catch (Exception e)
83              {
84                  throw new InitialisationException(e, this);
85              }
86              delegate.setService(service);
87          }
88          else if (serviceBean != null)
89          {
90              delegate.setService(serviceBean);
91          }
92          delegate.setServiceInterface(serviceInterface);
93          delegate.setRegisterTraceInterceptor(registerTraceInterceptor);
94          if (remoteInvocationExecutor != null)
95          {
96              delegate.setRemoteInvocationExecutor(remoteInvocationExecutor);
97          }
98          delegate.afterPropertiesSet();
99      }
100 
101     public Class getServiceClass()
102     {
103         return serviceClass;
104     }
105 
106     public void setServiceClass(Class serviceClass)
107     {
108         this.serviceClass = serviceClass;
109     }
110 
111     public Object getServiceBean()
112     {
113         return serviceBean;
114     }
115 
116     public void setServiceBean(Object serviceBean)
117     {
118         this.serviceBean = serviceBean;
119     }
120 
121     public Class getServiceInterface()
122     {
123         return serviceInterface;
124     }
125 
126     public void setServiceInterface(Class serviceInterface)
127     {
128         this.serviceInterface = serviceInterface;
129     }
130 
131     public boolean isRegisterTraceInterceptor()
132     {
133         return registerTraceInterceptor;
134     }
135 
136     public void setRegisterTraceInterceptor(boolean registerTraceInterceptor)
137     {
138         this.registerTraceInterceptor = registerTraceInterceptor;
139     }
140 
141     public RemoteInvocationExecutor getRemoteInvocationExecutor()
142     {
143         return remoteInvocationExecutor;
144     }
145 
146     public void setRemoteInvocationExecutor(RemoteInvocationExecutor remoteInvocationExecutor)
147     {
148         this.remoteInvocationExecutor = remoteInvocationExecutor;
149     }
150 
151     public Object onCall(MuleEventContext eventContext) throws Exception
152     {
153         Object payload = eventContext.getMessage().getPayload();
154         RemoteInvocation ri = (RemoteInvocation) payload;
155         Object rval = delegate.execute(ri);
156         return rval;
157     }
158 }