From f05950c78665607bb84646d0f0b0f702afbf0e8a Mon Sep 17 00:00:00 2001
From: Jaroslav Bachorik
The rmi port number can be specified with the "rmi.port" system property. - * If not, this test will use 12424
+ * If not, this test will use the first available port * *When called with some argument, the main() will interprete its args to * be Java M&M configuration file names. The filenames are expected to end @@ -69,7 +70,8 @@ import util.TestLogger; *
Debug traces are logged in "sun.management.test"
**/ public class RmiBootstrapTest { - + // the number of consecutive ports to test for availability + private static final int PORT_TEST_LEN = 800; static TestLogger log = new TestLogger("RmiBootstrapTest"); @@ -78,6 +80,7 @@ public class RmiBootstrapTest { * to avoid falling into "port number already in use" problems. **/ static int testPort = 0; + static int basePort = 0; /** * Default values for RMI configuration properties. @@ -624,7 +627,7 @@ public class RmiBootstrapTest { * eventually cleans up by calling ConnectorBootstrap.terminate(). * @return null if the test succeeds, an error message otherwise. **/ - private String testConfiguration(File file,int port) { + private String testConfiguration(File file,int port) throws BindException { final String path; try { @@ -644,7 +647,7 @@ public class RmiBootstrapTest { System.out.println("***"); System.setProperty("com.sun.management.jmxremote.port", - Integer.toString(port)); + Integer.toString(port)); if (path != null) System.setProperty("com.sun.management.config.file", path); else @@ -661,6 +664,11 @@ public class RmiBootstrapTest { try { cs = ConnectorBootstrap.initialize(); } catch (AgentConfigurationError x) { + if (x.getCause() instanceof ExportException) { + if (x.getCause().getCause() instanceof BindException) { + throw (BindException)x.getCause().getCause(); + } + } final String err = "Failed to initialize connector:" + "\n\tcom.sun.management.jmxremote.port=" + port + ((path!=null)?"\n\tcom.sun.management.config.file="+path: @@ -713,7 +721,15 @@ public class RmiBootstrapTest { * @return null if the test succeeds, an error message otherwise. **/ private String testConfigurationKo(File conf,int port) { - final String errStr = testConfiguration(conf,port+testPort++); + String errStr = null; + for (int i = 0; i < PORT_TEST_LEN; i++) { + try { + errStr = testConfiguration(conf,port+testPort++); + break; + } catch (BindException e) { + // port conflict; try another port + } + } if (errStr == null) { return "Configuration " + conf + " should have failed!"; @@ -733,11 +749,21 @@ public class RmiBootstrapTest { **/ private String testConfigurationFile(String fileName) { File file = new File(fileName); - final String portStr = System.getProperty("rmi.port","12424"); - final int port = Integer.parseInt(portStr); + final String portStr = System.getProperty("rmi.port",null); + final int port = portStr != null ? + Integer.parseInt(portStr) : basePort; if (fileName.endsWith("ok.properties")) { - return testConfiguration(file,port+testPort++); + String errStr = null; + for (int i = 0; i < PORT_TEST_LEN; i++) { + try { + errStr = testConfiguration(file,port+testPort++); + return errStr; + } catch (BindException e) { + // port conflict; try another port + } + } + return "Can not locate available port"; } if (fileName.endsWith("ko.properties")) { return testConfigurationKo(file,port+testPort++); @@ -752,8 +778,9 @@ public class RmiBootstrapTest { * @throws RuntimeException if the test fails. **/ public void runko() { - final String portStr = System.getProperty("rmi.port","12424"); - final int port = Integer.parseInt(portStr); + final String portStr = System.getProperty("rmi.port",null); + final int port = portStr != null ? + Integer.parseInt(portStr) : basePort; final File[] conf = findConfigurationFilesKo(); if ((conf == null)||(conf.length == 0)) throw new RuntimeException("No configuration found"); @@ -774,15 +801,23 @@ public class RmiBootstrapTest { * @throws RuntimeException if the test fails. **/ public void runok() { - final String portStr = System.getProperty("rmi.port","12424"); - final int port = Integer.parseInt(portStr); + final String portStr = System.getProperty("rmi.port",null); + final int port = portStr != null ? + Integer.parseInt(portStr) : basePort; final File[] conf = findConfigurationFilesOk(); if ((conf == null)||(conf.length == 0)) throw new RuntimeException("No configuration found"); - String errStr; + String errStr = null; for (int i=0;i