From 93a53e1cda23ae3a186cd8956ebb0b1908673a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Mon, 21 Dec 2015 23:24:28 +0200 Subject: [PATCH] Fix UUIDGenerator.getHardwareAddress on recent Java versions Enable related tests on all Java versions and drop < 1.7 support code. --- .../activemq/artemis/utils/UUIDGenerator.java | 32 ++++++------------- .../tests/unit/util/UUIDGeneratorTest.java | 12 ++----- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUIDGenerator.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUIDGenerator.java index e9878d1f60..116a6482ba 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUIDGenerator.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUIDGenerator.java @@ -16,7 +16,6 @@ */ package org.apache.activemq.artemis.utils; -import java.lang.reflect.Method; import java.net.NetworkInterface; import java.net.SocketException; import java.security.SecureRandom; @@ -134,21 +133,13 @@ public final class UUIDGenerator { * @return A byte array containing the hardware address. */ public static byte[] getHardwareAddress() { - Method getHardwareAddressMethod; - Method isUpMethod; - Method isLoopbackMethod; - Method isVirtualMethod; try { - getHardwareAddressMethod = NetworkInterface.class.getMethod("getHardwareAddress"); - isUpMethod = NetworkInterface.class.getMethod("isUp"); - isLoopbackMethod = NetworkInterface.class.getMethod("isLoopback"); - isVirtualMethod = NetworkInterface.class.getMethod("isVirtual"); // check if we have enough security permissions to create and shutdown an executor - ExecutorService executor = Executors.newFixedThreadPool(0); + ExecutorService executor = Executors.newFixedThreadPool(1); executor.shutdownNow(); } catch (Throwable t) { - // not on Java 6 or not enough security permission + // not enough security permission return null; } @@ -159,7 +150,7 @@ public final class UUIDGenerator { return null; } - byte[] address = findFirstMatchingHardwareAddress(ifaces, getHardwareAddressMethod, isUpMethod, isLoopbackMethod, isVirtualMethod); + byte[] address = findFirstMatchingHardwareAddress(ifaces); if (address != null) { if (ActiveMQUtilLogger.LOGGER.isDebugEnabled()) { ActiveMQUtilLogger.LOGGER.debug("using hardware address " + UUIDGenerator.asString(address)); @@ -269,11 +260,7 @@ public final class UUIDGenerator { } } - private static byte[] findFirstMatchingHardwareAddress(List ifaces, - final Method getHardwareAddressMethod, - final Method isUpMethod, - final Method isLoopbackMethod, - final Method isVirtualMethod) { + private static byte[] findFirstMatchingHardwareAddress(List ifaces) { ExecutorService executor = Executors.newFixedThreadPool(ifaces.size()); Collection> tasks = new ArrayList<>(ifaces.size()); @@ -281,18 +268,17 @@ public final class UUIDGenerator { tasks.add(new Callable() { @Override public byte[] call() throws Exception { - boolean up = (Boolean) isUpMethod.invoke(networkInterface); - boolean loopback = (Boolean) isLoopbackMethod.invoke(networkInterface); - boolean virtual = (Boolean) isVirtualMethod.invoke(networkInterface); + boolean up = networkInterface.isUp(); + boolean loopback = networkInterface.isLoopback(); + boolean virtual = networkInterface.isVirtual(); if (loopback || virtual || !up) { throw new Exception("not suitable interface"); } - Object res = getHardwareAddressMethod.invoke(networkInterface); - if (res != null && res instanceof byte[]) { + byte[] address = networkInterface.getHardwareAddress(); + if (address != null) { - byte[] address = (byte[]) res; byte[] paddedAddress = UUIDGenerator.getZeroPaddedSixBytes(address); if (UUIDGenerator.isBlackList(address)) { diff --git a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UUIDGeneratorTest.java b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UUIDGeneratorTest.java index 48282e32de..46a906506f 100644 --- a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UUIDGeneratorTest.java +++ b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UUIDGeneratorTest.java @@ -34,15 +34,9 @@ public class UUIDGeneratorTest extends ActiveMQTestBase { @Test public void testGetHardwareAddress() throws Exception { - String javaVersion = System.getProperty("java.vm.version"); - if (javaVersion.startsWith("1.5")) { - Assert.assertNull(UUIDGenerator.getHardwareAddress()); - } - else if (javaVersion.startsWith("1.6")) { - byte[] bytes = UUIDGenerator.getHardwareAddress(); - Assert.assertNotNull(bytes); - Assert.assertTrue(bytes.length == 6); - } + byte[] bytes = UUIDGenerator.getHardwareAddress(); + Assert.assertNotNull(bytes); + Assert.assertTrue(bytes.length == 6); } @Test