001: /*
002: * $Id: SpringRemoteInvokerComponent.java 10866 2008-02-18 20:29:34Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.module.spring.remoting;
012:
013: import org.mule.api.MuleEventContext;
014: import org.mule.api.lifecycle.Callable;
015: import org.mule.api.lifecycle.Initialisable;
016: import org.mule.api.lifecycle.InitialisationException;
017: import org.mule.api.lifecycle.LifecycleTransitionResult;
018: import org.mule.config.i18n.CoreMessages;
019: import org.mule.util.ClassUtils;
020:
021: import org.springframework.beans.factory.InitializingBean;
022: import org.springframework.remoting.support.RemoteInvocation;
023: import org.springframework.remoting.support.RemoteInvocationBasedExporter;
024: import org.springframework.remoting.support.RemoteInvocationExecutor;
025: import org.springframework.remoting.support.RemoteInvocationResult;
026:
027: public class SpringRemoteInvokerComponent implements Initialisable,
028: Callable {
029: private Delegate delegate;
030: private Class serviceClass;
031: private Class serviceInterface;
032: private Object serviceBean;
033: private boolean registerTraceInterceptor = false;
034: private RemoteInvocationExecutor remoteInvocationExecutor;
035:
036: private class Delegate extends RemoteInvocationBasedExporter
037: implements InitializingBean {
038: private Object proxy;
039:
040: public void afterPropertiesSet() {
041: this .proxy = getProxyForService();
042: }
043:
044: public Object execute(RemoteInvocation invocation) {
045: try {
046: Object value = invoke(invocation, proxy);
047: return value;
048: } catch (Throwable ex) {
049: ex.printStackTrace();
050: return new RemoteInvocationResult(ex);
051: }
052: }
053: }
054:
055: public SpringRemoteInvokerComponent() {
056: delegate = new Delegate();
057: }
058:
059: public LifecycleTransitionResult initialise()
060: throws InitialisationException {
061: if (serviceClass == null && serviceBean == null) {
062: throw new InitialisationException(CoreMessages
063: .propertiesNotSet("serviceClass or serviceBean"),
064: this );
065: }
066: if (serviceInterface == null) {
067: throw new InitialisationException(CoreMessages
068: .propertiesNotSet("serviceInterface"), this );
069: }
070:
071: if (serviceClass != null) {
072: Object service = null;
073: try {
074: service = ClassUtils.instanciateClass(serviceClass,
075: null);
076: } catch (Exception e) {
077: throw new InitialisationException(e, this );
078: }
079: delegate.setService(service);
080: } else if (serviceBean != null) {
081: delegate.setService(serviceBean);
082: }
083: delegate.setServiceInterface(serviceInterface);
084: delegate.setRegisterTraceInterceptor(registerTraceInterceptor);
085: if (remoteInvocationExecutor != null) {
086: delegate
087: .setRemoteInvocationExecutor(remoteInvocationExecutor);
088: }
089: delegate.afterPropertiesSet();
090: return LifecycleTransitionResult.OK;
091: }
092:
093: public Class getServiceClass() {
094: return serviceClass;
095: }
096:
097: public void setServiceClass(Class serviceClass) {
098: this .serviceClass = serviceClass;
099: }
100:
101: public Object getServiceBean() {
102: return serviceBean;
103: }
104:
105: public void setServiceBean(Object serviceBean) {
106: this .serviceBean = serviceBean;
107: }
108:
109: public Class getServiceInterface() {
110: return serviceInterface;
111: }
112:
113: public void setServiceInterface(Class serviceInterface) {
114: this .serviceInterface = serviceInterface;
115: }
116:
117: public boolean isRegisterTraceInterceptor() {
118: return registerTraceInterceptor;
119: }
120:
121: public void setRegisterTraceInterceptor(
122: boolean registerTraceInterceptor) {
123: this .registerTraceInterceptor = registerTraceInterceptor;
124: }
125:
126: public RemoteInvocationExecutor getRemoteInvocationExecutor() {
127: return remoteInvocationExecutor;
128: }
129:
130: public void setRemoteInvocationExecutor(
131: RemoteInvocationExecutor remoteInvocationExecutor) {
132: this .remoteInvocationExecutor = remoteInvocationExecutor;
133: }
134:
135: public Object onCall(MuleEventContext eventContext)
136: throws Exception {
137: Object transformedMessage = eventContext.transformMessage();
138: RemoteInvocation ri = (RemoteInvocation) transformedMessage;
139: Object rval = delegate.execute(ri);
140: return rval;
141: }
142: }
|