This closes #281

This commit is contained in:
Clebert Suconic 2015-12-21 21:47:16 -05:00
commit 8be8f16e57
6 changed files with 21 additions and 97 deletions

View File

@ -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)) {

View File

@ -386,6 +386,7 @@ public interface ActiveMQClientLogger extends BasicLogger {
@Message(id = 214017, value = "Caught unexpected Throwable", format = Message.Format.MESSAGE_FORMAT)
void caughtunexpectedThrowable(@Cause Throwable t);
@Deprecated
@LogMessage(level = Logger.Level.ERROR)
@Message(id = 214018, value = "Failed to invoke getTextContent() on node {0}", format = Message.Format.MESSAGE_FORMAT)
void errorOnXMLTransform(@Cause Throwable t, Node n);

View File

@ -26,7 +26,6 @@ import javax.xml.validation.Validator;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
@ -145,16 +144,9 @@ public final class XMLUtil {
return sb.toString();
}
private static final Object[] EMPTY_ARRAY = new Object[0];
/**
* This metod is here because Node.getTextContent() is not available in JDK 1.4 and I would like
* to have an uniform access to this functionality.
* <p>
* Note: if the content is another element or set of elements, it returns a string representation
* of the hierarchy.
* <p>
* TODO implementation of this method is a hack. Implement it properly.
*/
public static String getTextContent(final Node n) {
if (n.hasChildNodes()) {
@ -173,55 +165,7 @@ public final class XMLUtil {
}
}
Method[] methods = Node.class.getMethods();
for (Method getTextContext : methods) {
if ("getTextContent".equals(getTextContext.getName())) {
try {
return (String) getTextContext.invoke(n, XMLUtil.EMPTY_ARRAY);
}
catch (Exception e) {
ActiveMQClientLogger.LOGGER.errorOnXMLTransform(e, n);
return null;
}
}
}
String textContent = null;
if (n.hasChildNodes()) {
NodeList nl = n.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node c = nl.item(i);
if (c.getNodeType() == Node.TEXT_NODE) {
textContent = n.getNodeValue();
if (textContent == null) {
// TODO This is a hack. Get rid of it and implement this properly
String s = c.toString();
int idx = s.indexOf("#text:");
if (idx != -1) {
textContent = s.substring(idx + 6).trim();
if (textContent.endsWith("]")) {
textContent = textContent.substring(0, textContent.length() - 1);
}
}
}
if (textContent == null) {
break;
}
}
}
// TODO This is a hack. Get rid of it and implement this properly
String s = n.toString();
int i = s.indexOf('>');
int i2 = s.indexOf("</");
if (i != -1 && i2 != -1) {
textContent = s.substring(i + 1, i2);
}
}
return textContent;
return n.getTextContent();
}
public static void assertEquivalent(final Node node, final Node node2) {

View File

@ -10,9 +10,9 @@ org.eclipse.jdt.core.codeComplete.staticFieldSuffixes=
org.eclipse.jdt.core.codeComplete.staticFinalFieldPrefixes=
org.eclipse.jdt.core.codeComplete.staticFinalFieldSuffixes=
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=optimize out
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
@ -20,7 +20,7 @@ org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.processAnnotations=enabled
org.eclipse.jdt.core.compiler.source=1.6
org.eclipse.jdt.core.compiler.source=1.7
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=18
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=16

View File

@ -99,6 +99,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!--
note for idea users:
@ -783,8 +786,6 @@
<!-- Enable Google's Error-Prone https://github.com/google/error-prone -->
<configuration>
<showWarnings>true</showWarnings>
<source>1.7</source>
<target>1.7</target>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<compilerId>${javac-compiler-id}</compilerId>
<compilerArgs>
@ -892,12 +893,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.0.1</version>
<version>3.6</version>
<configuration>
<linkXRef>true</linkXRef>
<sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>1.5</targetJdk>
</configuration>
</plugin>
<plugin>

View File

@ -34,16 +34,10 @@ 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);
}
}
@Test
public void testZeroPaddedBytes() throws Exception {