1 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 serviceClass; 31 private Class serviceInterface; 32 private Object serviceBean; 33 private boolean registerTraceInterceptor = false; 34 private RemoteInvocationExecutor remoteInvocationExecutor; 35 36 private class Delegate extends RemoteInvocationBasedExporter implements InitializingBean 37 { 38 private Object proxy; 39 40 public void afterPropertiesSet() 41 { 42 this.proxy = getProxyForService(); 43 } 44 45 public Object execute(RemoteInvocation invocation) 46 { 47 try 48 { 49 Object value = invoke(invocation, proxy); 50 return value; 51 } 52 catch (Throwable 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 service = null; 81 try 82 { 83 service = ClassUtils.instanciateClass(serviceClass, null); 84 } 85 catch (Exception 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 getServiceClass() 105 { 106 return serviceClass; 107 } 108 109 public void setServiceClass(Class serviceClass) 110 { 111 this.serviceClass = serviceClass; 112 } 113 114 public Object getServiceBean() 115 { 116 return serviceBean; 117 } 118 119 public void setServiceBean(Object serviceBean) 120 { 121 this.serviceBean = serviceBean; 122 } 123 124 public Class getServiceInterface() 125 { 126 return serviceInterface; 127 } 128 129 public void setServiceInterface(Class 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 onCall(UMOEventContext eventContext) throws Exception 155 { 156 Object transformedMessage = eventContext.getTransformedMessage(); 157 RemoteInvocation ri = (RemoteInvocation)transformedMessage; 158 Object rval = delegate.execute(ri); 159 return rval; 160 } 161 } 162 | Popular Tags |