KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > spring > remoting > SpringRemoteInvokerComponent


1 /*
2  * $Id: SpringRemoteInvokerComponent.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

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