Merge pull request #1128 from hyteio/AMQ-9216

[AMQ-9216] Remove deprecated java.lang.SecurityManager usage from activemq-client IdGenerator
This commit is contained in:
JB Onofré 2024-02-28 16:54:32 +01:00 committed by GitHub
commit 99dced2fa7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 88 deletions

View File

@ -41,47 +41,36 @@ public class IdGenerator {
static { static {
String stub = ""; String stub = "";
boolean canAccessSystemProps = true;
int idGeneratorPort = 0;
ServerSocket ss = null;
try { try {
SecurityManager sm = System.getSecurityManager(); idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0"));
if (sm != null) { LOG.trace("Using port {}", idGeneratorPort);
sm.checkPropertiesAccess(); 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) { // Restore interrupted state so higher level code can deal with it.
int idGeneratorPort = 0; if (e instanceof InterruptedException) {
ServerSocket ss = null; Thread.currentThread().interrupt();
try { }
idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0")); } finally {
LOG.trace("Using port {}", idGeneratorPort); if (ss != null) {
hostName = getLocalHostName(); try {
ss = new ServerSocket(idGeneratorPort); ss.close();
stub = "-" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "-"; } catch (IOException ioe) {
Thread.sleep(100); if (LOG.isTraceEnabled()) {
} catch (Exception e) { LOG.trace("Closing the server socket failed", ioe);
if (LOG.isTraceEnabled()) { } else {
LOG.trace("could not generate unique stub by using DNS and binding to local port", e); LOG.warn("Closing the server socket failed" + " due " + ioe.getMessage());
} 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());
}
} }
} }
} }

View File

@ -41,63 +41,52 @@ public class IdGenerator {
static { static {
String stub = ""; 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 { try {
SecurityManager sm = System.getSecurityManager(); if( hostName==null ) {
if (sm != null) { hostName = InetAddressUtil.getLocalHostName();
sm.checkPropertiesAccess(); }
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) { // Restore interrupted state so higher level code can deal with it.
if (e instanceof InterruptedException) {
hostName = System.getProperty(PROPERTY_IDGENERATOR_HOSTNAME); Thread.currentThread().interrupt();
int localPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_LOCALPORT, "0")); }
} finally {
int idGeneratorPort = 0; if (ss != null) {
ServerSocket ss = null; try {
try { // TODO: replace the following line with IOHelper.close(ss) when Java 6 support is dropped
if( hostName==null ) { ss.close();
hostName = InetAddressUtil.getLocalHostName(); } catch (IOException ioe) {
} if (LOG.isTraceEnabled()) {
if( localPort==0 ) { LOG.trace("Closing the server socket failed", ioe);
idGeneratorPort = Integer.parseInt(System.getProperty(PROPERTY_IDGENERATOR_PORT, "0")); } else {
LOG.trace("Using port {}", idGeneratorPort); LOG.warn("Closing the server socket failed" + " due " + ioe.getMessage());
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());
}
} }
} }
} }
} }
// fallback // fallback
if (hostName == null) { if (hostName == null) {
hostName = "localhost"; hostName = "localhost";