diff --git a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/util/IdGenerator.java b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/util/IdGenerator.java index bdad73b25d..60971b1f4c 100644 --- a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/util/IdGenerator.java +++ b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/util/IdGenerator.java @@ -41,47 +41,36 @@ public class IdGenerator { static { String stub = ""; - boolean canAccessSystemProps = true; + + int idGeneratorPort = 0; + ServerSocket ss = null; try { - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - sm.checkPropertiesAccess(); + idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0")); + LOG.trace("Using port {}", idGeneratorPort); + hostName = getLocalHostName(); + ss = new ServerSocket(idGeneratorPort); + stub = "-" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "-"; + Thread.sleep(100); + } catch (Exception e) { + if (LOG.isTraceEnabled()) { + LOG.trace("could not generate unique stub by using DNS and binding to local port", e); + } else { + LOG.warn("could not generate unique stub by using DNS and binding to local port: {} {}", e.getClass().getCanonicalName(), e.getMessage()); } - } catch (SecurityException se) { - canAccessSystemProps = false; - } - if (canAccessSystemProps) { - int idGeneratorPort = 0; - ServerSocket ss = null; - try { - idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0")); - LOG.trace("Using port {}", idGeneratorPort); - hostName = getLocalHostName(); - ss = new ServerSocket(idGeneratorPort); - stub = "-" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "-"; - Thread.sleep(100); - } catch (Exception e) { - if (LOG.isTraceEnabled()) { - LOG.trace("could not generate unique stub by using DNS and binding to local port", e); - } else { - LOG.warn("could not generate unique stub by using DNS and binding to local port: {} {}", e.getClass().getCanonicalName(), e.getMessage()); - } - - // Restore interrupted state so higher level code can deal with it. - if (e instanceof InterruptedException) { - Thread.currentThread().interrupt(); - } - } finally { - if (ss != null) { - try { - ss.close(); - } catch (IOException ioe) { - if (LOG.isTraceEnabled()) { - LOG.trace("Closing the server socket failed", ioe); - } else { - LOG.warn("Closing the server socket failed" + " due " + ioe.getMessage()); - } + // Restore interrupted state so higher level code can deal with it. + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } + } finally { + if (ss != null) { + try { + ss.close(); + } catch (IOException ioe) { + if (LOG.isTraceEnabled()) { + LOG.trace("Closing the server socket failed", ioe); + } else { + LOG.warn("Closing the server socket failed" + " due " + ioe.getMessage()); } } } diff --git a/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java b/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java index 09a5658035..1404536da4 100644 --- a/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java +++ b/activemq-client/src/main/java/org/apache/activemq/util/IdGenerator.java @@ -41,63 +41,52 @@ public class IdGenerator { static { String stub = ""; - boolean canAccessSystemProps = true; + + hostName = System.getProperty(PROPERTY_IDGENERATOR_HOSTNAME); + int localPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_LOCALPORT, "0")); + + int idGeneratorPort = 0; + ServerSocket ss = null; try { - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - sm.checkPropertiesAccess(); + if( hostName==null ) { + hostName = InetAddressUtil.getLocalHostName(); + } + if( localPort==0 ) { + idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0")); + LOG.trace("Using port {}", idGeneratorPort); + ss = new ServerSocket(idGeneratorPort); + localPort = ss.getLocalPort(); + stub = "-" + localPort + "-" + System.currentTimeMillis() + "-"; + Thread.sleep(100); + } else { + stub = "-" + localPort + "-" + System.currentTimeMillis() + "-"; + } + } catch (Exception e) { + if (LOG.isTraceEnabled()) { + LOG.trace("could not generate unique stub by using DNS and binding to local port", e); + } else { + LOG.warn("could not generate unique stub by using DNS and binding to local port: {} {}", e.getClass().getCanonicalName(), e.getMessage()); } - } catch (SecurityException se) { - canAccessSystemProps = false; - } - if (canAccessSystemProps) { - - hostName = System.getProperty(PROPERTY_IDGENERATOR_HOSTNAME); - int localPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_LOCALPORT, "0")); - - int idGeneratorPort = 0; - ServerSocket ss = null; - try { - if( hostName==null ) { - hostName = InetAddressUtil.getLocalHostName(); - } - if( localPort==0 ) { - idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0")); - LOG.trace("Using port {}", idGeneratorPort); - ss = new ServerSocket(idGeneratorPort); - localPort = ss.getLocalPort(); - stub = "-" + localPort + "-" + System.currentTimeMillis() + "-"; - Thread.sleep(100); - } else { - stub = "-" + localPort + "-" + System.currentTimeMillis() + "-"; - } - } catch (Exception e) { - if (LOG.isTraceEnabled()) { - LOG.trace("could not generate unique stub by using DNS and binding to local port", e); - } else { - LOG.warn("could not generate unique stub by using DNS and binding to local port: {} {}", e.getClass().getCanonicalName(), e.getMessage()); - } - - // Restore interrupted state so higher level code can deal with it. - if (e instanceof InterruptedException) { - Thread.currentThread().interrupt(); - } - } finally { - if (ss != null) { - try { - // TODO: replace the following line with IOHelper.close(ss) when Java 6 support is dropped - ss.close(); - } catch (IOException ioe) { - if (LOG.isTraceEnabled()) { - LOG.trace("Closing the server socket failed", ioe); - } else { - LOG.warn("Closing the server socket failed" + " due " + ioe.getMessage()); - } + // Restore interrupted state so higher level code can deal with it. + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } + } finally { + if (ss != null) { + try { + // TODO: replace the following line with IOHelper.close(ss) when Java 6 support is dropped + ss.close(); + } catch (IOException ioe) { + if (LOG.isTraceEnabled()) { + LOG.trace("Closing the server socket failed", ioe); + } else { + LOG.warn("Closing the server socket failed" + " due " + ioe.getMessage()); } } } } + // fallback if (hostName == null) { hostName = "localhost";