diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-01-20 23:19:46 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-01-20 23:19:46 +0000 |
commit | d04fa7201d8a91fbe5369eefe6569b00009352ce (patch) | |
tree | e6dc067942ae95952d51fd0c8a22269e0bfee886 /java/sca/modules/binding-rmi-runtime/src/main | |
parent | ceee3ad6fb060a7b649e352997a84556b7e768f4 (diff) |
TUSCANY-2270 - Reverting changes from TUSCANY-2270 as OASIS Specification voted today to remove conversation from next version of specification
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@736166 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/binding-rmi-runtime/src/main')
3 files changed, 237 insertions, 333 deletions
diff --git a/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIBindingInvoker.java b/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIBindingInvoker.java index f06dfbc83b..7754fb5485 100644 --- a/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIBindingInvoker.java +++ b/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIBindingInvoker.java @@ -23,193 +23,61 @@ import java.lang.reflect.Method; import java.rmi.Remote; import org.apache.tuscany.sca.host.rmi.RMIHost; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; -import org.apache.tuscany.sca.invocation.DataExchangeSemantics; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.DataExchangeSemantics; /** * Invoker for RMI References. - * - * When invoke is fire, the Service Interface can be Conversational or not, when - * Service Interface is conversational, so the Conversational Service Interface - * is created and loaded. Conversational Service Interface is responsable to - * carry conversationId to server side. - * + * * @version $Rev$ $Date$ */ public class RMIBindingInvoker implements Invoker, DataExchangeSemantics { - private RMIHost rmiHost; - private String uri; - private Method referenceMethod; - private Remote proxy; - private Class<?> serviceInterface; + private RMIHost rmiHost; + private String uri; + private Method remoteMethod; + private Remote proxy; - public RMIBindingInvoker(RMIHost rmiHost, String uri, - Class<?> serviceInterface, Operation operation) { - this.rmiHost = rmiHost; - this.uri = uri; - this.serviceInterface = serviceInterface; - try { - this.referenceMethod = JavaInterfaceUtil.findMethod( - serviceInterface, operation); - } catch (NoSuchMethodException e) { - throw new IllegalArgumentException(e); - } - ; - } + public RMIBindingInvoker(RMIHost rmiHost, String uri, Method remoteMethod) { + this.rmiHost = rmiHost; + this.remoteMethod = remoteMethod; + this.uri = uri; + } - public Message invoke(Message msg) { - try { - Object conversationId = msg.getFrom().getReferenceParameters().getConversationID(); - boolean isConversational = conversationId != null; - Object resp; - if (isConversational) { - initProxy(serviceInterface, true); - resp = invokeConversational(conversationId, msg.getBody()); - } else { - initProxy(serviceInterface, false); - resp = invokeTarget(msg.getBody()); - } - msg.setBody(resp); - } catch (InvocationTargetException e) { - Throwable cause = getCause(e); - if (cause == null) { - cause = e.getCause(); - } - msg.setFaultBody(cause); - } catch (Throwable e) { - msg.setFaultBody(e); - } - return msg; - } + public Message invoke(Message msg) { + try { - /** - * Find Remote Service - * - * If Service Interface is conversational the Conversational Service - * Interface is created and loaded, this is necessary because RMI looking - * for Conversational Service Interface in client side, when any method in - * conversational service interface is called. - * - * @param serviceInterface - * The Service Interface - * @param isConversational - * Indicate if Service Interface is Conversational. - */ - private void initProxy(Class<?> serviceInterface, boolean isConversational) { - if (proxy == null) { - if (isConversational) { - ConversationalServiceInterfaceFactory.create(serviceInterface); - } - proxy = rmiHost.findService(uri); - } - } + Object[] args = msg.getBody(); + Object resp = invokeTarget(args); + msg.setBody(resp); + + } catch (InvocationTargetException e) { + msg.setFaultBody(e.getCause()); + } catch (Throwable e) { + msg.setFaultBody(e); + } - /** - * Get the original throwable. - * - * If throwable was throws by Service Interface, than original throwable is - * returned, else null is returned. - * - * @param e The final Throwable - * @return The Original Throwable or null - */ - private Throwable getCause(Throwable e) { - Throwable cause = e.getCause(); - Throwable expectedCause = null; - for (Class<?> declaredType : referenceMethod.getExceptionTypes()) { - if (declaredType.isInstance(cause)) { - expectedCause = cause; - break; - } - } - if (expectedCause == null) { - expectedCause = getCause(cause); - } - return expectedCause; - } + return msg; + } - /** - * Invoke the Method in Conversational Service Interface. This method verify - * the parameters of reference method and create a new argument, that take the - * conversationId. - * - * @param conversationId - * The ConversationId generated by SCA - * @param args - * The parameters of Method of Service Interface - * @return The result of Method executed. - * @throws InvocationTargetException - * @throws SecurityException - * @throws NoSuchMethodException - * @throws IllegalArgumentException - * @throws IllegalAccessException - */ - private Object invokeConversational(Object conversationId, Object args) - throws InvocationTargetException, SecurityException, - NoSuchMethodException, IllegalArgumentException, - IllegalAccessException { - Object[] newParameters = null; - Class<?>[] newParametersType = null; - if (isWithoutParameters(args)) { - newParameters = new Object[1]; - newParametersType = new Class[1]; - } else { - Object[] parameters = (Object[]) args; - newParameters = new Object[parameters.length + 1]; - System.arraycopy(args, 0, newParameters, 0, parameters.length); - Class<?>[] refParameters = referenceMethod.getParameterTypes(); - newParametersType = new Class[refParameters.length + 1]; - System.arraycopy(refParameters, 0, newParametersType, 0, refParameters.length); - } - newParameters[newParameters.length - 1] = new ConversationalParameter(conversationId); - newParametersType[newParametersType.length - 1] = ConversationalParameter.class; - Method conversationalMethod = proxy.getClass().getMethod(referenceMethod.getName(), newParametersType); - return invoke(conversationalMethod, proxy, newParameters); - } + public Object invokeTarget(final Object payload) throws InvocationTargetException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException { + if (proxy == null) { + proxy = rmiHost.findService(uri); + } - /** - * Invoke the Method in Service Interface - * - * @param parameters - * The parameters of Method of Service Interface - * @return The result of Method executed. - * @throws InvocationTargetException - * @throws SecurityException - * @throws NoSuchMethodException - * @throws IllegalArgumentException - * @throws IllegalAccessException - */ - private Object invokeTarget(final Object parameters) - throws InvocationTargetException, SecurityException, - NoSuchMethodException, IllegalArgumentException, - IllegalAccessException { - - Method remoteMethod = proxy.getClass().getMethod(referenceMethod.getName(), referenceMethod.getParameterTypes()); - return invoke(remoteMethod, proxy, parameters); - } + remoteMethod = proxy.getClass().getMethod(remoteMethod.getName(), remoteMethod.getParameterTypes()); - private Object invoke(Method remoteMethod, Remote proxy, - final Object parameters) throws InvocationTargetException, - SecurityException, NoSuchMethodException, IllegalArgumentException, - IllegalAccessException { - - if (parameters != null && !parameters.getClass().isArray()) { - return remoteMethod.invoke(proxy, parameters); - } else { - return remoteMethod.invoke(proxy, (Object[]) parameters); - } - } + if (payload != null && !payload.getClass().isArray()) { + return remoteMethod.invoke(proxy, payload); + } else { + return remoteMethod.invoke(proxy, (Object[])payload); + } + } - public boolean allowsPassByReference() { - // RMI always pass by value - return true; - } + public boolean allowsPassByReference() { + // RMI always pass by value + return true; + } - private boolean isWithoutParameters(Object parameters) { - return (parameters == null || (parameters.getClass().isArray() && ((Object[]) parameters).length == 0)); - } } diff --git a/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIReferenceBindingProvider.java b/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIReferenceBindingProvider.java index c9b6927222..8a368a26c3 100644 --- a/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIReferenceBindingProvider.java +++ b/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIReferenceBindingProvider.java @@ -19,11 +19,14 @@ package org.apache.tuscany.sca.binding.rmi.provider; +import java.lang.reflect.Method; + import org.apache.tuscany.sca.binding.rmi.RMIBinding; import org.apache.tuscany.sca.host.rmi.RMIHost; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.provider.ReferenceBindingProvider; import org.apache.tuscany.sca.runtime.RuntimeComponent; @@ -36,37 +39,43 @@ import org.apache.tuscany.sca.runtime.RuntimeComponentReference; */ public class RMIReferenceBindingProvider implements ReferenceBindingProvider { - private RuntimeComponentReference reference; - private RMIBinding binding; - private RMIHost rmiHost; - - public RMIReferenceBindingProvider(RuntimeComponent component, - RuntimeComponentReference reference, RMIBinding binding, - RMIHost rmiHost) { - this.reference = reference; - this.binding = binding; - this.rmiHost = rmiHost; - } + private RuntimeComponentReference reference; + private RMIBinding binding; + private RMIHost rmiHost; + + public RMIReferenceBindingProvider(RuntimeComponent component, + RuntimeComponentReference reference, + RMIBinding binding, + RMIHost rmiHost) { + this.reference = reference; + this.binding = binding; + this.rmiHost = rmiHost; + } - public InterfaceContract getBindingInterfaceContract() { - return reference.getInterfaceContract(); - } + public InterfaceContract getBindingInterfaceContract() { + return reference.getInterfaceContract(); + } + + public Invoker createInvoker(Operation operation) { + Class<?> iface = ((JavaInterface)reference.getInterfaceContract().getInterface()).getJavaClass(); + Method remoteMethod; + try { + remoteMethod = JavaInterfaceUtil.findMethod(iface, operation); + } catch (NoSuchMethodException e) { + throw new IllegalArgumentException(e); + } - public Invoker createInvoker(Operation operation) { - Class<?> serviceInterface = ((JavaInterface) reference - .getInterfaceContract().getInterface()).getJavaClass(); - return new RMIBindingInvoker(rmiHost, binding.getURI(), - serviceInterface, operation); - } + return new RMIBindingInvoker(rmiHost, binding.getURI(), remoteMethod); + } - public void start() { - } + public void start() { + } - public void stop() { - } + public void stop() { + } - public boolean supportsOneWayInvocation() { - return false; - } + public boolean supportsOneWayInvocation() { + return false; + } } diff --git a/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIServiceBindingProvider.java b/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIServiceBindingProvider.java index f8aa7dcf4a..e277160aae 100644 --- a/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIServiceBindingProvider.java +++ b/java/sca/modules/binding-rmi-runtime/src/main/java/org/apache/tuscany/sca/binding/rmi/provider/RMIServiceBindingProvider.java @@ -19,24 +19,29 @@ package org.apache.tuscany.sca.binding.rmi.provider; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.rmi.Remote; -import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; -import java.util.ArrayList; -import java.util.List; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; import net.sf.cglib.asm.ClassWriter; import net.sf.cglib.asm.Type; import net.sf.cglib.core.Constants; import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.MethodInterceptor; +import net.sf.cglib.proxy.MethodProxy; import org.apache.tuscany.sca.binding.rmi.RMIBinding; import org.apache.tuscany.sca.host.rmi.RMIHost; import org.apache.tuscany.sca.host.rmi.RMIHostException; import org.apache.tuscany.sca.interfacedef.Interface; import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.java.impl.JavaInterfaceUtil; import org.apache.tuscany.sca.provider.ServiceBindingProvider; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentService; @@ -45,140 +50,162 @@ import org.osoa.sca.ServiceRuntimeException; /** * Implementation of a Service for the RMIBinding. - * + * * @version $Rev$ $Date$ */ public class RMIServiceBindingProvider implements ServiceBindingProvider { - private RuntimeComponent component; - private RuntimeComponentService service; - private RMIBinding binding; - private RMIHost rmiHost; - private RuntimeWire wire; - - public RMIServiceBindingProvider(RuntimeComponent component, - RuntimeComponentService service, RMIBinding binding, RMIHost rmiHost) { - this.component = component; - this.service = service; - this.binding = binding; - this.rmiHost = rmiHost; - } - - public void start() { - wire = service.getRuntimeWire(binding); - Interface serviceInterface = service.getInterfaceContract() - .getInterface(); - Remote rmiProxy = createRmiService(serviceInterface); - try { - rmiHost.registerService(binding.getURI(), rmiProxy); - } catch (RMIHostException e) { - throw new ServiceRuntimeException(e); - } - } - - public void stop() { - rmiHost.unregisterService(binding.getURI()); - } - - protected Remote createRmiService(final Interface serviceInterface) { - Enhancer enhancer = new Enhancer(); - enhancer.setSuperclass(UnicastRemoteObject.class); - enhancer.setCallback(new RMIMethodInterceptor(wire, serviceInterface)); - Class<?> targetJavaInterface = getTargetJavaClass(serviceInterface); - List<Class<?>> interfaces = new ArrayList<Class<?>>(); - RMIServiceClassLoader classloader = new RMIServiceClassLoader( - targetJavaInterface.getClassLoader()); - if (!Remote.class.isAssignableFrom(targetJavaInterface)) { - final byte[] byteCode = generateRemoteInterface(targetJavaInterface); - targetJavaInterface = classloader.defineClass(targetJavaInterface - .getName(), byteCode); - } - interfaces.add(targetJavaInterface); - if (serviceInterface.isConversational()) { - Class<?> conversationalInterface = ConversationalServiceInterfaceFactory - .create(targetJavaInterface); - final byte[] byteCode = generateRemoteInterface(conversationalInterface); - conversationalInterface = classloader.defineClass( - conversationalInterface.getName(), byteCode); - interfaces.add(conversationalInterface); - } - enhancer.setClassLoader(classloader); - enhancer.setInterfaces(interfaces.toArray(new Class[] {})); - return (Remote) enhancer.create(); - } - - /** - * if the interface of the component whose serviceBindings must be exposed - * as RMI Service, does not implement java.rmi.Remote, then generate such an - * interface. This method will stop with just generating the bytecode. - * Defining the class from the byte code must be the responsibility of the - * caller of this method, since it requires a ClassLoader to be created to - * define and load this interface. - */ - private byte[] generateRemoteInterface(Class serviceInterface) { - String interfazeName = serviceInterface.getName(); - ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - - cw.visit(Constants.V1_5, Constants.ACC_PUBLIC + Constants.ACC_ABSTRACT - + Constants.ACC_INTERFACE, interfazeName.replace('.', '/'), - null, "java/lang/Object", new String[] { "java/rmi/Remote" }); - StringBuffer argsAndReturn = null; - Method[] methods = serviceInterface.getMethods(); - for (Method method : methods) { - argsAndReturn = new StringBuffer("("); - Class<?>[] paramTypes = method.getParameterTypes(); - Class<?> returnType = method.getReturnType(); - - for (Class<?> paramType : paramTypes) { - argsAndReturn.append(Type.getType(paramType)); - } - argsAndReturn.append(")"); - argsAndReturn.append(Type.getType(returnType)); - - boolean hasRemoteException = false; - List<String> exceptions = new ArrayList<String>(); - Class<?>[] exceptionTypes = method.getExceptionTypes(); - for (int i = 0, s = exceptionTypes.length; i < s; i++) { - if (exceptionTypes[i].equals(RemoteException.class)) { - hasRemoteException = true; - } - exceptions.add(exceptionTypes[i].getName().replace('.', '/')); - } - if (!hasRemoteException) { - exceptions.add("java/rmi/RemoteException"); - } - cw.visitMethod(Constants.ACC_PUBLIC + Constants.ACC_ABSTRACT, - method.getName(), argsAndReturn.toString(), null, - exceptions.toArray(new String[] {})); - } - cw.visitEnd(); - return cw.toByteArray(); - } - - private Class<?> getTargetJavaClass(Interface targetInterface) { - // TODO: right now assume that the target is always a Java - // Implementation. Need to figure out - // how to generate Java Interface in cases where the target is not a - // Java Implementation - return ((JavaInterface) targetInterface).getJavaClass(); - } - - protected class RMIServiceClassLoader extends ClassLoader { - public RMIServiceClassLoader(ClassLoader parent) { - super(parent); - } - - public Class defineClass(String name, byte[] byteArray) { - return defineClass(name, byteArray, 0, byteArray.length); - } - } - - public InterfaceContract getBindingInterfaceContract() { - return service.getInterfaceContract(); - } - - public boolean supportsOneWayInvocation() { - return false; - } - + private RuntimeComponent component; + private RuntimeComponentService service; + private RMIBinding binding; + private RMIHost rmiHost; + private RuntimeWire wire; + + public RMIServiceBindingProvider(RuntimeComponent rc, RuntimeComponentService rcs, RMIBinding binding, RMIHost rmiHost) { + this.component = rc; + this.service = rcs; + this.binding = binding; + this.rmiHost = rmiHost; + } + + public void start() { + // URI uri = URI.create(component.getURI() + "/" + binding.getName()); + // binding.setURI(uri.toString()); + + wire = service.getRuntimeWire(binding); + Interface serviceInterface = service.getInterfaceContract().getInterface(); + + Remote rmiProxy = createRmiService(serviceInterface); + + try { + + rmiHost.registerService(binding.getURI(), rmiProxy); + + } catch (RMIHostException e) { + throw new ServiceRuntimeException(e); + } + } + + public void stop() { + rmiHost.unregisterService(binding.getURI()); + } + + private int getPort(String port) { + int portNumber = RMIHost.RMI_DEFAULT_PORT; + if (port != null && port.length() > 0) { + portNumber = Integer.decode(port); + } + return portNumber; + } + + private Remote createRmiService(final Interface serviceInterface) { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(UnicastRemoteObject.class); + enhancer.setCallback(new MethodInterceptor() { + public Object intercept(Object arg0, Method method, Object[] args, MethodProxy arg3) throws Throwable { + try { + return invokeTarget(JavaInterfaceUtil.findOperation(method, serviceInterface.getOperations()), args); + } catch (InvocationTargetException e) { + final Throwable cause = e.getCause(); + for (Class<?> declaredType : method.getExceptionTypes()) { + if (declaredType.isInstance(cause)) { + throw e; + } + } + + if (cause.getCause() != null) { + // TUSCANY-2545: don't inlcude nested cause object + AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { + public Object run() throws Exception { + Field field = Throwable.class.getDeclaredField("cause"); + field.setAccessible(true); + field.set(cause, null); + field.setAccessible(false); + return null; + } + }); + } + + throw cause; + } + } + }); + Class targetJavaInterface = getTargetJavaClass(serviceInterface); + if (!Remote.class.isAssignableFrom(targetJavaInterface)) { + RMIServiceClassLoader classloader = new RMIServiceClassLoader(targetJavaInterface.getClassLoader()); + final byte[] byteCode = generateRemoteInterface(targetJavaInterface); + targetJavaInterface = classloader.defineClass(targetJavaInterface.getName(), byteCode); + enhancer.setClassLoader(classloader); + } + enhancer.setInterfaces(new Class[] {targetJavaInterface}); + return (Remote)enhancer.create(); + } + + private Object invokeTarget(Operation op, Object[] args) throws InvocationTargetException { + return wire.invoke(op, args); + } + + /** + * if the interface of the component whose serviceBindings must be exposed as RMI Service, does not + * implement java.rmi.Remote, then generate such an interface. This method will stop with just + * generating the bytecode. Defining the class from the byte code must be the responsibility of the + * caller of this method, since it requires a ClassLoader to be created to define and load this interface. + */ + private byte[] generateRemoteInterface(Class serviceInterface) { + String interfazeName = serviceInterface.getName(); + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); + + String simpleName = serviceInterface.getSimpleName(); + cw.visit(Constants.V1_5, Constants.ACC_PUBLIC + Constants.ACC_ABSTRACT + Constants.ACC_INTERFACE, interfazeName + .replace('.', '/'), null, "java/lang/Object", new String[] {"java/rmi/Remote"}); + + StringBuffer argsAndReturn = null; + Method[] methods = serviceInterface.getMethods(); + for (Method method : methods) { + argsAndReturn = new StringBuffer("("); + Class[] paramTypes = method.getParameterTypes(); + Class returnType = method.getReturnType(); + + for (Class paramType : paramTypes) { + argsAndReturn.append(Type.getType(paramType)); + } + argsAndReturn.append(")"); + argsAndReturn.append(Type.getType(returnType)); + + cw.visitMethod(Constants.ACC_PUBLIC + Constants.ACC_ABSTRACT, + method.getName(), + argsAndReturn.toString(), + null, + new String[] {"java/rmi/RemoteException"}); + } + cw.visitEnd(); + return cw.toByteArray(); + } + + private Class<?> getTargetJavaClass(Interface targetInterface) { + // TODO: right now assume that the target is always a Java + // Implementation. Need to figure out + // how to generate Java Interface in cases where the target is not a + // Java Implementation + return ((JavaInterface)targetInterface).getJavaClass(); + } + + protected class RMIServiceClassLoader extends ClassLoader { + public RMIServiceClassLoader(ClassLoader parent) { + super(parent); + } + + public Class defineClass(String name, byte[] byteArray) { + return defineClass(name, byteArray, 0, byteArray.length); + } + } + + public InterfaceContract getBindingInterfaceContract() { + return service.getInterfaceContract(); + } + + public boolean supportsOneWayInvocation() { + return false; + } + } |