Clover coverage report -
Coverage timestamp: Sat Jul 7 2007 16:41:13 CEST
file stats: LOC: 184   Methods: 6
NCLOC: 116   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RmiUtil.java 83.3% 63.5% 100% 68%
coverage coverage
 1    /**
 2    * JTRunner is free software; you can redistribute it and/or modify it under the
 3    * terms of the GNU General Public License as published by the Free Software
 4    * Foundation; either version 2, or (at your option) any later version.
 5    */
 6   
 7    package jtr.remote.utils;
 8   
 9    import java.net.MalformedURLException;
 10    import java.rmi.AlreadyBoundException;
 11    import java.rmi.Naming;
 12    import java.rmi.NotBoundException;
 13    import java.rmi.RMISecurityManager;
 14    import java.rmi.Remote;
 15    import java.rmi.RemoteException;
 16    import java.rmi.registry.LocateRegistry;
 17    import java.rmi.registry.Registry;
 18    import java.util.concurrent.atomic.AtomicBoolean;
 19    import jtr.enterprise.LocatorException;
 20    import jtr.remote.test.NodeInfo;
 21   
 22    import static jtr.test.SystemProperties.*;
 23    import org.apache.log4j.Logger;
 24   
 25    /**
 26    * This class provides utility methods for managing the registry service and for
 27    * registering new services to it.
 28    *
 29    * @author frusso
 30    * @version 4.0
 31    * @since 4.0
 32    */
 33    public class RmiUtil {
 34   
 35    /**
 36    * Launches the registry on the given host and on the given port.<br>
 37    * <b>Note:</b> the registry can only be started on the localhost.
 38    * Should a different host be specified, this method will try to connect
 39    * to the remote registry.
 40    *
 41    * @param host The host name the registry is running on
 42    * @param port The port the registry is attached to
 43    * @return The lauched/discovered registry
 44    * @throws RmiUtilException Thrown if unable to lauch the registry
 45    */
 46  3 public static final Registry launchRegistry(String host, int port) {
 47  3 Registry registry = null;
 48   
 49  3 setSecurityManager();
 50   
 51  3 try {
 52  3 logger.debug("Creating registry on localhost, on port "+port+"...");
 53  3 registry = LocateRegistry.createRegistry(port);
 54  2 logger.debug("... done");
 55    } catch(RemoteException re1) {
 56    // it has not been possible to locate the registry
 57    // maybe there is already a registry started locally on that port
 58  1 logger.error(re1);
 59  1 logger.warn("Failed, maybe there is already a running instance, trying to locate it...");
 60  1 try {
 61  1 registry = LocateRegistry.getRegistry(port);
 62  1 logger.debug("... located on port "+port);
 63    } catch(RemoteException re2) {
 64  0 try {
 65  0 logger.debug("Trying to locate it on host "+host+", port "+port+"...");
 66  0 registry = LocateRegistry.getRegistry(host,port);
 67  0 logger.debug("... done");
 68    } catch(RemoteException re3) {
 69  0 String msg = "Unable to locate/create any registry with host "+host+" and port "+port;
 70  0 logger.fatal(msg,re3);
 71  0 throw new RmiUtilException(msg,re3);
 72    }
 73    }
 74    }
 75   
 76  3 return registry;
 77    }
 78   
 79    /**
 80    * This method looks-up/registers a service on the registry. Firstly it tries
 81    * to lookup the service; should it fail, it tries to bind it.
 82    *
 83    * @param r The registry
 84    * @param service The service definition
 85    * @param serviceName The name under which the service must be registered
 86    * @return Remote The service instance, or <code>null</code> in case of error
 87    */
 88  3 public static final Remote registerService(Registry r, Class<? extends Remote> service, final String serviceName) {
 89  3 Remote srvc = null;
 90  3 try {
 91  3 logger.debug("Trying to look up an already registered "+serviceName+"...");
 92  3 srvc = r.lookup(serviceName);
 93  0 logger.debug("... done");
 94    } catch (RemoteException ex) {
 95  0 logger.fatal("Unable to contact the registry, aborting "+serviceName+" registration",ex);
 96    } catch (NotBoundException ex) {
 97  3 logger.error(ex);
 98  3 logger.warn("Unable to find any "+serviceName+", trying to bind a new one...");
 99  3 try {
 100  3 srvc = service.newInstance();
 101  3 r.bind(serviceName,srvc);
 102  3 logger.info("Service "+serviceName+" successfully registered");
 103    } catch (RemoteException ex1) {
 104  0 logger.fatal("Unable to bind the "+serviceName+". Aborting.",ex1);
 105    } catch (AlreadyBoundException ex1) {
 106  0 logger.fatal("Unable to bind the "+serviceName+". Aboring.",ex1);
 107    } catch (InstantiationException ie) {
 108  0 logger.fatal("Unable to bind the "+serviceName+". Aboring.",ie);
 109    } catch (IllegalAccessException ie) {
 110  0 logger.fatal("Unable to bind the "+serviceName+". Aboring.",ie);
 111    }
 112    }
 113  3 return srvc;
 114    }
 115   
 116    /**
 117    * Utility method for performing server look-up.
 118    * @param jtrNode
 119    * @return The remote reference to the looked-up object
 120    * @throws jtr.enterprise.LocatorException
 121    */
 122  8 public static Remote lookupServer(NodeInfo jtrNode) throws LocatorException {
 123  8 Remote srvc = null;
 124  8 String url = jtrNode.toURL();
 125  8 try {
 126  8 srvc = Naming.lookup(url);
 127    } catch (MalformedURLException ex) {
 128  0 String msg = "Unable to look-up JTR-node "+jtrNode;
 129  0 logger.fatal(msg,ex);
 130  0 throw new LocatorException(msg,ex);
 131    } catch (RemoteException ex) {
 132  0 String msg = "Unable to look-up JTR-node "+jtrNode;
 133  0 logger.fatal(msg,ex);
 134  0 throw new LocatorException(msg,ex);
 135    } catch (NotBoundException ex) {
 136  0 String msg = "Unable to look-up JTR-node "+jtrNode;
 137  0 logger.fatal(msg,ex);
 138  0 throw new LocatorException(msg,ex);
 139    }
 140  8 return srvc;
 141    }
 142   
 143    /**
 144    * This method logs the content of the registry.
 145    *
 146    * @param r The registry
 147    */
 148  3 public static final void logRegistryContent(Registry r) {
 149  3 String entries[];
 150  3 try {
 151  3 entries = r.list();
 152  3 for(int i=0; i<entries.length; i++) {
 153  4 System.out.println("Registry entry #"+i+": "+entries[i]);
 154    }
 155    } catch (RemoteException ex) {
 156  0 ex.printStackTrace();
 157    }
 158    }
 159   
 160    /**
 161    * This method sets all the properties required by the JTR distributed-runtime
 162    * to work propertly.<br>
 163    * In more detail this methods looks for the <code>java.rmi.server.hostname</code>
 164    * and, if not defined, it is set to be equal to the value of the
 165    * <code>jtr.remote.test.host</code> property.
 166    */
 167  2 public synchronized static final void setSystemProperties() {
 168  2 String remStubsHost = System.getProperty(REMOTE_STUBS_HOST);
 169  2 if(remStubsHost==null) {
 170  2 remStubsHost = System.getProperty(RMI_REGISTRY_HOST, DEF_RMI_REGISTRY_HOST);
 171  2 System.setProperty(REMOTE_STUBS_HOST, remStubsHost);
 172    }
 173    }
 174   
 175  3 private synchronized static void setSecurityManager() {
 176  3 if(!secured.get()) {
 177  2 System.setSecurityManager(new RMISecurityManager());
 178  2 secured.compareAndSet(false,true);
 179    }
 180    }
 181   
 182    private static AtomicBoolean secured = new AtomicBoolean(false);
 183    private static Logger logger = Logger.getLogger(RmiUtil.class);
 184    }