View Javadoc

1   /*
2    * $Id: SpringRemoteInvokerComponent.java 11517 2008-03-31 21:34:19Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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                  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      {
66          if (serviceClass == null && serviceBean == null)
67          {
68              throw new InitialisationException(CoreMessages.propertiesNotSet("serviceClass or serviceBean"),
69                  this);
70          }
71          if (serviceInterface == null)
72          {
73              throw new InitialisationException(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(MuleEventContext eventContext) throws Exception
153     {
154         Object transformedMessage = eventContext.transformMessage();
155         RemoteInvocation ri = (RemoteInvocation) transformedMessage;
156         Object rval = delegate.execute(ri);
157         return rval;
158     }
159 }