diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-11 23:24:58 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-11 23:24:58 +0000 |
commit | dd46d9fdec04702b415b6d6bc9a85f9c797042ad (patch) | |
tree | 7bf66635630284065b5d34d26c0bdde112d4feff /java/sca | |
parent | e0cfd4c221c019b42d46ee36b8f19246f4846aa3 (diff) |
Use a customized socket factory for registry to control timeout
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@803322 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca')
2 files changed, 70 insertions, 8 deletions
diff --git a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java index 99a6a5fab7..f7d2184e24 100644 --- a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java +++ b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/DefaultRMIHost.java @@ -27,11 +27,13 @@ import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; +import java.rmi.server.RMISocketFactory; import java.rmi.server.UnicastRemoteObject; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; + /** * Default implementation of a RMI host. * @@ -39,16 +41,18 @@ import java.util.logging.Logger; */ public class DefaultRMIHost implements RMIHost { private final static Logger logger = Logger.getLogger(DefaultRMIHost.class.getName()); + private static final int CONNECTION_TIMEOUT = 3000; // 3 seconds + // Map of RMI registries started and running private Map<String, Registry> rmiRegistries; + + private RMISocketFactory socketFactory; public DefaultRMIHost() { rmiRegistries = new ConcurrentHashMap<String, Registry>(); - /* - * if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } - */ + this.socketFactory = new RMISocketFactoryImpl(CONNECTION_TIMEOUT); } - + public void registerService(String uri, Remote serviceObject) throws RMIHostException, RMIHostRuntimeException { RMIURI rmiURI = new RMIURI(uri); @@ -57,10 +61,11 @@ public class DefaultRMIHost implements RMIHost { registry = rmiRegistries.get(Integer.toString(rmiURI.port)); if (registry == null) { try { - registry = LocateRegistry.getRegistry(rmiURI.port); + registry = LocateRegistry.getRegistry(null, rmiURI.port, socketFactory); + // FIXME: It takes about 15 seconds to time out registry.lookup(rmiURI.serviceName); } catch (RemoteException e) { - registry = LocateRegistry.createRegistry(rmiURI.port); + registry = LocateRegistry.createRegistry(rmiURI.port, socketFactory, socketFactory); } catch (NotBoundException e) { // Ignore } @@ -84,7 +89,7 @@ public class DefaultRMIHost implements RMIHost { try { Registry registry = rmiRegistries.get(Integer.toString(rmiURI.port)); if (registry == null) { - registry = LocateRegistry.getRegistry(rmiURI.port); + registry = LocateRegistry.getRegistry(null, rmiURI.port, socketFactory); rmiRegistries.put(Integer.toString(rmiURI.port), registry); } registry.unbind(rmiURI.serviceName); @@ -105,7 +110,7 @@ public class DefaultRMIHost implements RMIHost { try { // Requires permission java.net.SocketPermission "host:port", "connect,accept,resolve" // in security policy. - Registry registry = LocateRegistry.getRegistry(rmiURI.host, rmiURI.port); + Registry registry = LocateRegistry.getRegistry(rmiURI.host, rmiURI.port, socketFactory); if (registry != null) { remoteService = registry.lookup(rmiURI.serviceName); diff --git a/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMISocketFactoryImpl.java b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMISocketFactoryImpl.java new file mode 100644 index 0000000000..763e5c1315 --- /dev/null +++ b/java/sca/modules/host-rmi/src/main/java/org/apache/tuscany/sca/host/rmi/RMISocketFactoryImpl.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.sca.host.rmi; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.rmi.server.RMISocketFactory; + +import javax.net.ServerSocketFactory; +import javax.net.SocketFactory; + +class RMISocketFactoryImpl extends RMISocketFactory { + private int clientTimeout; + private SocketFactory factory; + private ServerSocketFactory serverSocketFactory; + + /** + * @param timeout + */ + public RMISocketFactoryImpl(int clientTimeout) { + super(); + this.clientTimeout = clientTimeout; + this.factory = SocketFactory.getDefault(); + this.serverSocketFactory = ServerSocketFactory.getDefault(); + } + + public Socket createSocket(String host, int port) throws IOException { + Socket socket = factory.createSocket(host, port); + socket.setSoTimeout(clientTimeout); + return socket; + } + + @Override + public ServerSocket createServerSocket(int port) throws IOException { + ServerSocket socket = serverSocketFactory.createServerSocket(port); + return socket; + } + +} |