diff options
Diffstat (limited to 'branches/sca-equinox/modules/host-rmi/src/main/java/org')
3 files changed, 62 insertions, 87 deletions
diff --git a/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java b/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java index 8985c508fc..359ef488d4 100644 --- a/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java +++ b/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java @@ -19,6 +19,7 @@ package org.apache.tuscany.sca.host.rmi; +import java.net.URI; import java.rmi.AlreadyBoundException; import java.rmi.NotBoundException; import java.rmi.Remote; @@ -35,7 +36,7 @@ import java.util.concurrent.ConcurrentHashMap; */ public class DefaultRMIHost implements RMIHost { - //map of RMI registries started and running + // Map of RMI registries started and running private Map<String, Registry> rmiRegistries; public DefaultRMIHost() { @@ -45,21 +46,22 @@ public class DefaultRMIHost implements RMIHost { */ } - public void registerService(String serviceName, int port, Remote serviceObject) throws RMIHostException, - RMIHostRuntimeException { + public void registerService(String uri, Remote serviceObject) throws RMIHostException, RMIHostRuntimeException { + RMIURI rmiURI = new RMIURI(uri); + Registry registry; try { - registry = rmiRegistries.get(Integer.toString(port)); + registry = rmiRegistries.get(Integer.toString(rmiURI.port)); if (registry == null) { try { - registry = LocateRegistry.getRegistry(port); + registry = LocateRegistry.getRegistry(rmiURI.port); registry.list(); } catch (RemoteException e) { - registry = LocateRegistry.createRegistry(port); + registry = LocateRegistry.createRegistry(rmiURI.port); } - rmiRegistries.put(Integer.toString(port), registry); + rmiRegistries.put(Integer.toString(rmiURI.port), registry); } - registry.bind(serviceName, serviceObject); + registry.bind(rmiURI.serviceName, serviceObject); } catch (AlreadyBoundException e) { throw new RMIHostException(e); } catch (RemoteException e) { @@ -70,21 +72,16 @@ public class DefaultRMIHost implements RMIHost { } - 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; + public void unregisterService(String uri) throws RMIHostException, RMIHostRuntimeException { + RMIURI rmiURI = new RMIURI(uri); try { - registry = rmiRegistries.get(Integer.toString(port)); + Registry registry = rmiRegistries.get(Integer.toString(rmiURI.port)); if (registry == null) { - registry = LocateRegistry.getRegistry(port); - rmiRegistries.put(Integer.toString(port), registry); + registry = LocateRegistry.getRegistry(rmiURI.port); + rmiRegistries.put(Integer.toString(rmiURI.port), registry); } - registry.unbind(serviceName); + registry.unbind(rmiURI.serviceName); } catch (RemoteException e) { RMIHostRuntimeException rmiExec = new RMIHostRuntimeException(e.getMessage()); rmiExec.setStackTrace(e.getStackTrace()); @@ -94,25 +91,17 @@ public class DefaultRMIHost implements RMIHost { } } - 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; + public Remote findService(String uri) throws RMIHostException, RMIHostRuntimeException { + RMIURI rmiURI = new RMIURI(uri); + 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 { // Requires permission java.net.SocketPermission "host:port", "connect,accept,resolve" // in security policy. - registry = LocateRegistry.getRegistry(host, portNumber); + Registry registry = LocateRegistry.getRegistry(rmiURI.host, rmiURI.port); if (registry != null) { - remoteService = registry.lookup(svcName); + remoteService = registry.lookup(rmiURI.serviceName); } } catch (RemoteException e) { RMIHostRuntimeException rmiExec = new RMIHostRuntimeException(e.getMessage()); @@ -124,4 +113,33 @@ public class DefaultRMIHost implements RMIHost { return remoteService; } + /** + * A representation of an RMI URI. + * + * rmi://[host][:port][/[object]] + * rmi:[/][object] + */ + private static class RMIURI { + private String host; + private int port; + private String serviceName; + + private RMIURI(String uriStr) { + URI uri = URI.create(uriStr); + host = uri.getHost(); + if (host == null) { + host = "localhost"; + } + port = uri.getPort(); + if (port <= 0) { + port = RMI_DEFAULT_PORT; + } + String path = uri.getPath(); + if (path != null && path.charAt(0) == '/') { + path = path.substring(1); + } + serviceName = path; + } + } + } diff --git a/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/ExtensibleRMIHost.java b/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/ExtensibleRMIHost.java index 32fe6c2deb..29b4e195c7 100644 --- a/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/ExtensibleRMIHost.java +++ b/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/ExtensibleRMIHost.java @@ -35,39 +35,25 @@ public class ExtensibleRMIHost implements RMIHost { this.rmiHosts = rmiHosts; } - public void registerService(String serviceName, int port, Remote serviceObject) throws RMIHostException, RMIHostRuntimeException { + public void registerService(String uri, Remote serviceObject) throws RMIHostException, RMIHostRuntimeException { if (rmiHosts.getRMIHosts().isEmpty()) { throw new RMIHostException("No RMI host available"); } - rmiHosts.getRMIHosts().get(0).registerService(serviceName, port, serviceObject); + rmiHosts.getRMIHosts().get(0).registerService(uri, serviceObject); } - public Remote findService(String host, String port, String svcName) throws RMIHostException, RMIHostRuntimeException { + public void unregisterService(String uri) throws RMIHostException, RMIHostRuntimeException { if (rmiHosts.getRMIHosts().isEmpty()) { throw new RMIHostException("No RMI host available"); } - return rmiHosts.getRMIHosts().get(0).findService(host, port, svcName); + rmiHosts.getRMIHosts().get(0).unregisterService(uri); } - public void registerService(String serviceName, Remote serviceObject) throws RMIHostException, RMIHostRuntimeException { + public Remote findService(String uri) throws RMIHostException, RMIHostRuntimeException { if (rmiHosts.getRMIHosts().isEmpty()) { throw new RMIHostException("No RMI host available"); } - rmiHosts.getRMIHosts().get(0).registerService(serviceName, serviceObject); + return rmiHosts.getRMIHosts().get(0).findService(uri); } - public void unregisterService(String serviceName) throws RMIHostException, RMIHostRuntimeException { - if (rmiHosts.getRMIHosts().isEmpty()) { - throw new RMIHostException("No RMI host available"); - } - rmiHosts.getRMIHosts().get(0).unregisterService(serviceName); - } - - public void unregisterService(String serviceName, int port) throws RMIHostException, RMIHostRuntimeException { - if (rmiHosts.getRMIHosts().isEmpty()) { - throw new RMIHostException("No RMI host available"); - } - rmiHosts.getRMIHosts().get(0).unregisterService(serviceName, port); - } - } diff --git a/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMIHost.java b/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMIHost.java index 59377de22a..b95b91f003 100644 --- a/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMIHost.java +++ b/branches/sca-equinox/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMIHost.java @@ -32,59 +32,30 @@ public interface RMIHost { /** * Register an RMI service with the given name and port * - * @param serviceName against which the server is to be registered - * @param port the port against which the server is to be registered + * @param uri the URI against which the server is to be registered * @param serviceObject the server object to be registered * @throws RMIHostException * @throws RMIHostRuntimeException */ - void registerService(String serviceName, int port, Remote serviceObject) throws RMIHostException, - RMIHostRuntimeException; - - /** - * Register an RMI service with the given name and default port (1099) - * - * @param serviceName serviceName against which the server is to be registered - * @param serviceObject the server object to be registered - * @throws RMIHostException - * @throws RMIHostRuntimeException - */ - void registerService(String serviceName, Remote serviceObject) throws RMIHostException, - RMIHostRuntimeException; + void registerService(String uri, Remote serviceObject) throws RMIHostException, RMIHostRuntimeException; /** * Unregister a service registered under the given service name and port number * - * @param serviceName serviceName against which the server is to be registered - * @param port the port against which the server is to be registered + * @param uri the URI of the server * @throws RMIHostException * @throws RMIHostRuntimeException */ - void unregisterService(String serviceName, int port) throws RMIHostException, - RMIHostRuntimeException; - - /** - * Unregister a service registered under the given service name and default port number (1099) - * - * @param serviceName the name of the service that has to be unregistered - * @throws RMIHostException - * @throws RMIHostRuntimeException - */ - void unregisterService(String serviceName) throws RMIHostException, - RMIHostRuntimeException; - + void unregisterService(String uri) throws RMIHostException, RMIHostRuntimeException; /** * find a remote service hosted on the given host, port and service name * - * @param host the name of the host on which the RMI service to be unregistered is running - * @param port the port against which the server is to be unregistered is running - * @param svcName serviceName against which the server is to be unregistered is running + * @param uri the URI of the service * @return the RMI server object * @throws RMIHostException * @throws RMIHostRuntimeException */ - Remote findService(String host, String port, String svcName) throws RMIHostException, - RMIHostRuntimeException; + Remote findService(String uri) throws RMIHostException, RMIHostRuntimeException; } |