Fix UUIDGenerator.getHardwareAddress on recent Java versions

Enable related tests on all Java versions and drop < 1.7 support code.


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/93a53e1c
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/93a53e1c
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/93a53e1c

Branch: refs/heads/master
Commit: 93a53e1cda23ae3a186cd8956ebb0b1908673a30
Parents: 8fbb9b6
Author: Ville Skyttä <[email protected]>
Authored: Mon Dec 21 23:24:28 2015 +0200
Committer: Clebert Suconic <[email protected]>
Committed: Mon Dec 21 21:47:15 2015 -0500

----------------------------------------------------------------------
 .../activemq/artemis/utils/UUIDGenerator.java   | 32 ++++++--------------
 .../tests/unit/util/UUIDGeneratorTest.java      | 12 ++------
 2 files changed, 12 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/93a53e1c/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/UUIDGenerator.java
----------------------------------------------------------------------
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 e9878d1..116a648 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<NetworkInterface> ifaces,
-                                                          final Method 
getHardwareAddressMethod,
-                                                          final Method 
isUpMethod,
-                                                          final Method 
isLoopbackMethod,
-                                                          final Method 
isVirtualMethod) {
+   private static byte[] 
findFirstMatchingHardwareAddress(List<NetworkInterface> ifaces) {
       ExecutorService executor = Executors.newFixedThreadPool(ifaces.size());
       Collection<Callable<byte[]>> tasks = new ArrayList<>(ifaces.size());
 
@@ -281,18 +268,17 @@ public final class UUIDGenerator {
          tasks.add(new Callable<byte[]>() {
             @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)) {

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/93a53e1c/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/util/UUIDGeneratorTest.java
----------------------------------------------------------------------
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 48282e3..46a9065 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

Reply via email to