From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../binding/rmi/NoRemoteMethodException.java | 37 +++++ .../binding/rmi/NoRemoteServiceException.java | 37 +++++ .../org/apache/tuscany/binding/rmi/RMIBinding.java | 57 ++++++++ .../tuscany/binding/rmi/RMIBindingBuilder.java | 78 ++++++++++ .../tuscany/binding/rmi/RMIBindingLoader.java | 64 +++++++++ .../org/apache/tuscany/binding/rmi/RMIInvoker.java | 66 +++++++++ .../apache/tuscany/binding/rmi/RMIReference.java | 87 +++++++++++ .../tuscany/binding/rmi/RMIRuntimeException.java | 39 +++++ .../org/apache/tuscany/binding/rmi/RMIService.java | 160 +++++++++++++++++++++ .../tuscany/binding/rmi/RemoteMethodHandler.java | 47 ++++++ .../tuscany/binding/rmi/host/RMIHostImpl.java | 136 ++++++++++++++++++ 11 files changed, 808 insertions(+) create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/NoRemoteMethodException.java create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/NoRemoteServiceException.java create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBinding.java create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBindingBuilder.java create mode 100755 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBindingLoader.java create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIInvoker.java create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIReference.java create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIRuntimeException.java create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIService.java create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RemoteMethodHandler.java create mode 100644 branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/host/RMIHostImpl.java (limited to 'branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany') diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/NoRemoteMethodException.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/NoRemoteMethodException.java new file mode 100644 index 0000000000..45404f087e --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/NoRemoteMethodException.java @@ -0,0 +1,37 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +/** + * @version $Rev$ $Date$ + */ +public class NoRemoteMethodException extends RMIRuntimeException { + public NoRemoteMethodException() { + } + + public NoRemoteMethodException(String message) { + super(message); + } + + public NoRemoteMethodException(String message, Throwable cause) { + super(message, cause); + } + + public NoRemoteMethodException(Throwable cause) { + super(cause); + } +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/NoRemoteServiceException.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/NoRemoteServiceException.java new file mode 100644 index 0000000000..af2d2fd582 --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/NoRemoteServiceException.java @@ -0,0 +1,37 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +/** + * @version $Rev$ $Date$ + */ +public class NoRemoteServiceException extends RMIRuntimeException { + public NoRemoteServiceException() { + } + + public NoRemoteServiceException(String message) { + super(message); + } + + public NoRemoteServiceException(String message, Throwable cause) { + super(message, cause); + } + + public NoRemoteServiceException(Throwable cause) { + super(cause); + } +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBinding.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBinding.java new file mode 100644 index 0000000000..bc8c69851e --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBinding.java @@ -0,0 +1,57 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +import org.apache.tuscany.spi.model.Binding; + +/** + * Represents a binding to an RMI service. + * + * @version $Rev$ $Date$ + */ +public class RMIBinding extends Binding { + private String host; + + private String port; + + private String serviceName; + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public String getPort() { + return port; + } + + public void setPort(String port) { + this.port = port; + } + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBindingBuilder.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBindingBuilder.java new file mode 100644 index 0000000000..1e2eb9633a --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBindingBuilder.java @@ -0,0 +1,78 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +import java.rmi.Remote; + +import org.apache.tuscany.host.rmi.RMIHost; +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.Reference; +import org.apache.tuscany.spi.component.Service; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.BindingBuilderExtension; +import org.apache.tuscany.spi.model.BoundReferenceDefinition; +import org.apache.tuscany.spi.model.BoundServiceDefinition; +import org.osoa.sca.annotations.Constructor; + +/** + * Builds a Service or Reference for an RMI binding. + * + * @version $Rev$ $Date$ + */ + +public class RMIBindingBuilder extends BindingBuilderExtension { + + private RMIHost rmiHost; + + @Constructor({"rmiHost"}) + public RMIBindingBuilder(@Autowire RMIHost rHost) { + this.rmiHost = rHost; + } + + protected Class getBindingType() { + return RMIBinding.class; + } + + @SuppressWarnings({"unchecked"}) + public Service build(CompositeComponent parent, + BoundServiceDefinition boundServiceDefinition, + DeploymentContext deploymentContext) { + + Class intf = boundServiceDefinition.getServiceContract().getInterfaceClass(); + + return new RMIService(boundServiceDefinition.getName(), parent, wireService, rmiHost, + boundServiceDefinition.getBinding().getHost(), boundServiceDefinition.getBinding().getPort(), + boundServiceDefinition.getBinding().getServiceName(), intf); + } + + @SuppressWarnings({"unchecked"}) + public Reference build(CompositeComponent parent, + BoundReferenceDefinition boundReferenceDefinition, + DeploymentContext deploymentContext) { + String name = boundReferenceDefinition.getName(); + String host = boundReferenceDefinition.getBinding().getHost(); + String port = boundReferenceDefinition.getBinding().getPort(); + String svcName = boundReferenceDefinition.getBinding().getServiceName(); + // Class interfaze = boundReferenceDefinition.getServiceContract().getInterfaceClass(); + + return new RMIReference(name, parent, wireService, rmiHost, host, port, svcName, + boundReferenceDefinition.getServiceContract().getInterfaceClass()); + + } + +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBindingLoader.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBindingLoader.java new file mode 100755 index 0000000000..bd9b165b6d --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIBindingLoader.java @@ -0,0 +1,64 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.osoa.sca.annotations.Scope; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.loader.LoaderUtil; + +/** + * Loader for handling elements. + * + * @version $Rev$ $Date$ + */ +@Scope("MODULE") +public class RMIBindingLoader extends LoaderExtension { + public static final QName BINDING_RMI = new QName( + "http://incubator.apache.org/tuscany/xmlns/binding/rmi/1.0-incubator-M2", "binding.rmi"); + + public RMIBindingLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return BINDING_RMI; + } + + public RMIBinding load(CompositeComponent parent, + XMLStreamReader reader, + DeploymentContext deploymentContext) throws XMLStreamException, LoaderException { + String host = reader.getAttributeValue(null, "host"); + String port = reader.getAttributeValue(null, "port"); + String svcName = reader.getAttributeValue(null, "serviceName"); + LoaderUtil.skipToEndElement(reader); + RMIBinding binding = new RMIBinding(); + binding.setHost(host); + binding.setPort(port); + binding.setServiceName(svcName); + return binding; + } +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIInvoker.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIInvoker.java new file mode 100644 index 0000000000..afd1992d20 --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIInvoker.java @@ -0,0 +1,66 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.rmi.Remote; + +import org.apache.tuscany.host.rmi.RMIHost; +import org.apache.tuscany.host.rmi.RMIHostException; +import org.apache.tuscany.spi.extension.TargetInvokerExtension; + +/** + * Invoke an RMI reference. + * + * @version $Rev$ $Date$ + */ +public class RMIInvoker extends TargetInvokerExtension { + private Method remoteMethod; + private String host; + private String port; + private String svcName; + private RMIHost rmiHost; + private Remote proxy; + + public RMIInvoker(RMIHost rmiHost, String host, String port, String svcName, Method remoteMethod) { + // assert remoteMethod.isAccessible(); + this.remoteMethod = remoteMethod; + this.host = host; + this.port = port; + this.svcName = svcName; + this.rmiHost = rmiHost; + } + + public Object invokeTarget(Object payload) throws InvocationTargetException { + try { + if (proxy == null) { + proxy = rmiHost.findService(host, port, svcName); + // proxy = Naming.lookup(serviceURI); + } + return remoteMethod.invoke(proxy, (Object[]) payload); + } catch (RMIHostException e) { + // the method we are passed must be accessible + throw new AssertionError(e); + } catch (IllegalAccessException e) { + // the method we are passed must be accessible + throw new AssertionError(e); + } + + } + +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIReference.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIReference.java new file mode 100644 index 0000000000..5e810892d9 --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIReference.java @@ -0,0 +1,87 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +import static org.apache.tuscany.spi.idl.java.JavaIDLUtils.findMethod; + +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ReferenceExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.model.ServiceContract; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.WireService; + +import org.apache.tuscany.host.rmi.RMIHost; + +/** + * @version $Rev$ $Date$ + */ +public class RMIReference extends ReferenceExtension { + private final String host; + + private final String port; + + private final String svcName; + + private RMIHost rmiHost; + + public RMIReference(String name, + CompositeComponent parent, + WireService wireService, + RMIHost rmiHost, + String host, + String port, + String svcName, + Class service) { + super(name, service, parent, wireService); + this.host = host; + this.port = port; + this.svcName = svcName; + this.rmiHost = rmiHost; + } + + public TargetInvoker createTargetInvoker(ServiceContract contract, Operation operation) { + try { + /*Remote proxy = getProxy(); + Method remoteMethod = proxy.getClass().getMethod(operation.getName(), + (Class[]) operation.getParameterTypes()); + return new RMIInvoker(proxy, remoteMethod); + */ + Method method = findMethod(operation, contract.getInterfaceClass().getMethods()); + Method remoteMethod = + getInterface().getMethod(operation.getName(), (Class[]) method.getParameterTypes()); + return new RMIInvoker(rmiHost, host, port, svcName, remoteMethod); + } catch (NoSuchMethodException e) { + throw new NoRemoteMethodException(operation.toString(), e); + } + } + + /*protected Remote getProxy() { + try { + // todo do we need to cache this result? + return Naming.lookup(uri); + } catch (NotBoundException e) { + throw new NoRemoteServiceException(uri); + } catch (MalformedURLException e) { + throw new NoRemoteServiceException(uri); + } catch (RemoteException e) { + throw new NoRemoteServiceException(uri); + } + }*/ +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIRuntimeException.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIRuntimeException.java new file mode 100644 index 0000000000..c36dec4531 --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIRuntimeException.java @@ -0,0 +1,39 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +import org.apache.tuscany.api.TuscanyRuntimeException; + +/** + * @version $Rev$ $Date$ + */ +public abstract class RMIRuntimeException extends TuscanyRuntimeException { + protected RMIRuntimeException() { + } + + protected RMIRuntimeException(String message) { + super(message); + } + + protected RMIRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + protected RMIRuntimeException(Throwable cause) { + super(cause); + } +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIService.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIService.java new file mode 100644 index 0000000000..0de833801f --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RMIService.java @@ -0,0 +1,160 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +import java.lang.reflect.Method; +import java.rmi.Remote; +import java.rmi.server.UnicastRemoteObject; + +import net.sf.cglib.asm.ClassWriter; +import net.sf.cglib.asm.Constants; +import net.sf.cglib.asm.Type; +import net.sf.cglib.proxy.Enhancer; + +import org.apache.tuscany.host.rmi.RMIHost; +import org.apache.tuscany.host.rmi.RMIHostException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.extension.ServiceExtension; +import org.apache.tuscany.spi.wire.WireService; + +/** + * @version $Rev$ $Date$ + */ +public class RMIService extends ServiceExtension { + + public static final String URI_PREFIX = "//localhost"; + public static final String SLASH = "/"; + public static final String COLON = ":"; + //private final String host; + private final String port; + private final String serviceName; + private RMIHost rmiHost; + + // need this member to morph the service interface to extend from Remote if it does not + // the base class's member variable interfaze is to be maintained to enable the connection + // of the service outbound to the component's inbound wire which requires that the service + // and the component match in their service contracts. + private Class serviceInterface; + + public RMIService(String name, + CompositeComponent parent, + WireService wireService, + RMIHost rHost, + String host, + String port, + String svcName, + Class service) { + super(name, service, parent, wireService); + + this.serviceInterface = service; + this.rmiHost = rHost; + //this.host = host; + this.port = port; + this.serviceName = svcName; + } + + public void start() { + super.start(); + Remote rmiProxy = createRmiService(); + + try { + // startRMIRegistry(); + rmiHost.registerService(serviceName, + getPort(port), + rmiProxy); + // bindRmiService(uri,rmiProxy); + } catch (RMIHostException e) { + throw new NoRemoteServiceException(e); + } + } + + public void stop() { + try { + rmiHost.unregisterService(serviceName, getPort(port)); + } catch (RMIHostException e) { + throw new NoRemoteServiceException(e.getMessage()); + } + super.stop(); + } + + protected Remote createRmiService() { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(UnicastRemoteObject.class); + enhancer.setCallback(new RemoteMethodHandler(getHandler(), interfaze)); + + if (!Remote.class.isAssignableFrom(serviceInterface)) { + RMIServiceClassLoader classloader = + new RMIServiceClassLoader(getClass().getClassLoader()); + final byte[] byteCode = generateRemoteInterface(serviceInterface); + serviceInterface = classloader.defineClass(byteCode); + enhancer.setClassLoader(classloader); + } + enhancer.setInterfaces(new Class[]{serviceInterface}); + return (Remote) enhancer.create(); + } + + protected int getPort(String port) { + int portNumber = RMIHost.RMI_DEFAULT_PORT; + if (port != null && port.length() > 0) { + portNumber = Integer.decode(port); + } + + return portNumber; + } + + // if the interface of the component whose services 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 tbe the responsibility + // of the caller of this method, since it requires a classloader to be created to define and load + // this interface. + protected byte[] generateRemoteInterface(Class serviceInterface) { + String interfazeName = serviceInterface.getCanonicalName(); + ClassWriter cw = new ClassWriter(false); + + String simpleName = serviceInterface.getSimpleName(); + cw.visit(Constants.V1_5, Constants.ACC_PUBLIC + Constants.ACC_ABSTRACT + Constants.ACC_INTERFACE, + interfazeName.replace('.', '/'), "java/lang/Object", new String[]{"java/rmi/Remote"}, simpleName + ".java"); + + 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(), + new String[]{"java/rmi/RemoteException"}, null); + } + cw.visitEnd(); + return cw.toByteArray(); + } + + private class RMIServiceClassLoader extends ClassLoader { + public RMIServiceClassLoader(ClassLoader parent) { + super(parent); + } + public Class defineClass(byte[] byteArray) { + return defineClass(null, byteArray, 0, byteArray.length); + } + } +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RemoteMethodHandler.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RemoteMethodHandler.java new file mode 100644 index 0000000000..b27c87b510 --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/RemoteMethodHandler.java @@ -0,0 +1,47 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi; + +import java.lang.reflect.Method; + +import org.apache.tuscany.spi.wire.WireInvocationHandler; + +import net.sf.cglib.proxy.MethodInterceptor; +import net.sf.cglib.proxy.MethodProxy; + +public class RemoteMethodHandler implements MethodInterceptor { + public static final String FINALIZE_METHOD = "finalize"; + + private WireInvocationHandler wireHandler; + + private Class compSvcIntf; + + public RemoteMethodHandler(WireInvocationHandler handler, Class intf) { + this.wireHandler = handler; + compSvcIntf = intf; + } + + public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { + // since incoming method signatures have 'remotemethod invocation' it will not match with the + // wired component's method signatures. Hence need to pull in the corresponding method from the + // component's service contract interface to make this invocation. + return wireHandler.invoke(compSvcIntf.getMethod(method.getName(), + (Class[]) method.getParameterTypes()), + args); + } + +} diff --git a/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/host/RMIHostImpl.java b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/host/RMIHostImpl.java new file mode 100644 index 0000000000..9355cf642e --- /dev/null +++ b/branches/sca-java-M2/sca/services/bindings/binding.rmi/src/main/java/org/apache/tuscany/binding/rmi/host/RMIHostImpl.java @@ -0,0 +1,136 @@ +/** + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tuscany.binding.rmi.host; + +import java.rmi.AlreadyBoundException; +import java.rmi.NotBoundException; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.host.rmi.RMIHost; +import org.apache.tuscany.host.rmi.RMIHostException; +import org.apache.tuscany.host.rmi.RMIHostRuntimeException; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Scope; + +/** + * This class provides an implementation for the RMI Host SPIs + */ +@Scope("MODULE") +public class RMIHostImpl implements RMIHost { + + // map of RMI registries started and running + private Map rmiRegistries; + + public RMIHostImpl() { + rmiRegistries = new ConcurrentHashMap(); + /* + * if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } + */ + } + + @Init(eager = true) + public void init() { + } + + public void registerService(String serviceName, int port, Remote serviceObject) throws RMIHostException, + RMIHostRuntimeException { + Registry registry; + try { + registry = rmiRegistries.get(Integer.toString(port)); + if (registry == null) { + registry = LocateRegistry.createRegistry(port); + rmiRegistries.put(Integer.toString(port), + registry); + } + registry.bind(serviceName, + serviceObject); + } catch (AlreadyBoundException e) { + throw new RMIHostException(e.getMessage()); + } catch (RemoteException e) { + RMIHostRuntimeException rmiExec = new RMIHostRuntimeException(e.getMessage()); + rmiExec.setStackTrace(e.getStackTrace()); + throw rmiExec; + } + + } + + public void registerService(String serviceName, Remote serviceObject) throws RMIHostException, + RMIHostRuntimeException { + registerService(serviceName, + RMI_DEFAULT_PORT, + serviceObject); + } + + public void unregisterService(String serviceName, int port) throws RMIHostException, + RMIHostRuntimeException { + Registry registry; + + try { + registry = rmiRegistries.get(Integer.toString(port)); + if (registry == null) { + registry = LocateRegistry.createRegistry(port); + rmiRegistries.put(Integer.toString(port), + registry); + } + registry.unbind(serviceName); + } catch (RemoteException e) { + RMIHostRuntimeException rmiExec = new RMIHostRuntimeException(e.getMessage()); + rmiExec.setStackTrace(e.getStackTrace()); + throw rmiExec; + } catch (NotBoundException e) { + throw new RMIHostException(e.getMessage()); + } + } + + public void unregisterService(String serviceName) throws RMIHostException, + RMIHostRuntimeException { + unregisterService(serviceName, + RMI_DEFAULT_PORT); + + } + + public Remote findService(String host, String port, String svcName) throws RMIHostException, + RMIHostRuntimeException { + Registry registry; + Remote remoteService = null; + host = (host == null || host.length() <= 0) ? "localhost" : host; + int portNumber = (port == null || port.length() <= 0) ? RMI_DEFAULT_PORT : Integer + .decode(port); + + try { + registry = LocateRegistry.getRegistry(host, + portNumber); + + if (registry != null) { + remoteService = registry.lookup(svcName); + } + } catch (RemoteException e) { + RMIHostRuntimeException rmiExec = new RMIHostRuntimeException(e.getMessage()); + rmiExec.setStackTrace(e.getStackTrace()); + throw rmiExec; + } catch (NotBoundException e) { + throw new RMIHostException(e.getMessage()); + } + return remoteService; + } + +} -- cgit v1.2.3