# HG changeset patch # User jbachorik # Date 1383141046 -3600 # Node ID 5d1bda6c1fe3e9ddefd10f8f95cb599f982595b3 # Parent bede752d1e3ca34f2d30088244ce2c178db56ed3 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector Summary: Dynamically discover the first available port instead of hard-coding one Reviewed-by: sla, chegar, dfuchs diff -r bede752d1e3c -r 5d1bda6c1fe3 test/sun/management/jmxremote/bootstrap/RmiBootstrapTest.java --- a/test/sun/management/jmxremote/bootstrap/RmiBootstrapTest.java Tue Oct 29 16:59:11 2013 -0700 +++ b/test/sun/management/jmxremote/bootstrap/RmiBootstrapTest.java Wed Oct 30 14:50:46 2013 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,14 +24,15 @@ import java.io.File; import java.io.FileInputStream; -import java.io.InputStream; import java.io.FilenameFilter; import java.io.IOException; +import java.net.BindException; +import java.net.ServerSocket; +import java.rmi.server.ExportException; import java.util.Properties; import java.util.Iterator; import java.util.Set; -import java.util.Arrays; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -54,7 +55,7 @@ * bootstrap & connection test will fail.

* *

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 @@ *

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 @@ * 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 @@ * 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 @@ 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 @@ 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 @@ * @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 @@ **/ 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 @@ * @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 @@ * @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