On an enterprise machine it is not uncommon to have multiple network interface cards. When used internally in UUID, it is okay to ignore that scenario, and it's also more convenient to return localhost rather than null if there is no network card at all.
If we make this logic public, we cannot make the same assumptions any more. Shouldn't we return some sort of iterator or collection of MacAddresses? Or call it getFirstMacAddress? The Javadoc says null is returned if there's no NIC, but I don't see that in the code. (Do you anticipate using this method for something other than UUID? It might be better to leave it private...) Just my 2c. Remko Sent from my iPhone > On 2014/09/19, at 9:05, [email protected] wrote: > > Repository: logging-log4j2 > Updated Branches: > refs/heads/master 6b80804dc -> 2a754e12b > > > Refactor MAC address getter to own method. > > > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/2a754e12 > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/2a754e12 > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/2a754e12 > > Branch: refs/heads/master > Commit: 2a754e12b28df03dab38d5e66db251094a9834a9 > Parents: 6b80804 > Author: Matt Sicker <[email protected]> > Authored: Thu Sep 18 19:04:56 2014 -0500 > Committer: Matt Sicker <[email protected]> > Committed: Thu Sep 18 19:04:56 2014 -0500 > > ---------------------------------------------------------------------- > .../logging/log4j/core/util/NetUtils.java | 40 ++++++++++++++++++++ > .../logging/log4j/core/util/UuidUtil.java | 40 +------------------- > 2 files changed, 41 insertions(+), 39 deletions(-) > ---------------------------------------------------------------------- > > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2a754e12/log4j-core/src/main/java/org/apache/logging/log4j/core/util/NetUtils.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/NetUtils.java > b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/NetUtils.java > index 2609fef..46e16ce 100644 > --- > a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/NetUtils.java > +++ > b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/NetUtils.java > @@ -70,4 +70,44 @@ public final class NetUtils { > return "UNKNOWN_LOCALHOST"; > } > } > + > + /** > + * Returns the local network interface's MAC address if possible. The > local network interface is defined here as > + * the {@link NetworkInterface} that is both up and not a loopback > interface. > + * > + * @return the MAC address of the local network interface or {@code > null} if no MAC address could be determined. > + * @since 2.1 > + */ > + public static byte[] getLocalMacAddress() { > + byte[] mac = null; > + try { > + final InetAddress localHost = InetAddress.getLocalHost(); > + try { > + final NetworkInterface localInterface = > NetworkInterface.getByInetAddress(localHost); > + if (isUpAndNotLoopback(localInterface)) { > + mac = localInterface.getHardwareAddress(); > + } > + if (mac == null) { > + final Enumeration<NetworkInterface> networkInterfaces = > NetworkInterface.getNetworkInterfaces(); > + while (networkInterfaces.hasMoreElements() && mac == > null) { > + final NetworkInterface nic = > networkInterfaces.nextElement(); > + if (isUpAndNotLoopback(nic)) { > + mac = nic.getHardwareAddress(); > + } > + } > + } > + } catch (final SocketException e) { > + LOGGER.catching(e); > + } > + if (mac == null || mac.length == 0) { > + mac = localHost.getAddress(); > + } > + } catch (final UnknownHostException ignored) { > + } > + return mac; > + } > + > + private static boolean isUpAndNotLoopback(final NetworkInterface ni) > throws SocketException { > + return ni != null && !ni.isLoopback() && ni.isUp(); > + } > } > > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2a754e12/log4j-core/src/main/java/org/apache/logging/log4j/core/util/UuidUtil.java > ---------------------------------------------------------------------- > diff --git > a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/UuidUtil.java > b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/UuidUtil.java > index 34b021f..9bed752 100644 > --- > a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/UuidUtil.java > +++ > b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/UuidUtil.java > @@ -16,13 +16,8 @@ > */ > package org.apache.logging.log4j.core.util; > > -import java.lang.reflect.Method; > -import java.net.InetAddress; > -import java.net.NetworkInterface; > -import java.net.UnknownHostException; > import java.nio.ByteBuffer; > import java.security.SecureRandom; > -import java.util.Enumeration; > import java.util.Random; > import java.util.UUID; > import java.util.concurrent.atomic.AtomicInteger; > @@ -65,40 +60,7 @@ public final class UuidUtil { > private static final int HUNDRED_NANOS_PER_MILLI = 10000; > > static { > - byte[] mac = null; > - try { > - final InetAddress address = InetAddress.getLocalHost(); > - try { > - NetworkInterface ni = > NetworkInterface.getByInetAddress(address); > - if (ni != null && !ni.isLoopback() && ni.isUp()) { > - final Method method = > ni.getClass().getMethod("getHardwareAddress"); > - if (method != null) { > - mac = (byte[]) method.invoke(ni); > - } > - } > - > - if (mac == null) { > - final Enumeration<NetworkInterface> enumeration = > NetworkInterface.getNetworkInterfaces(); > - while (enumeration.hasMoreElements() && mac == null) { > - ni = enumeration.nextElement(); > - if (ni != null && ni.isUp() && !ni.isLoopback()) { > - final Method method = > ni.getClass().getMethod("getHardwareAddress"); > - if (method != null) { > - mac = (byte[]) method.invoke(ni); > - } > - } > - } > - } > - } catch (final Exception ex) { > - ex.printStackTrace(); > - // Ignore exception > - } > - if (mac == null || mac.length == 0) { > - mac = address.getAddress(); > - } > - } catch (final UnknownHostException e) { > - // Ignore exception > - } > + byte[] mac = NetUtils.getLocalMacAddress(); > final Random randomGenerator = new SecureRandom(); > if (mac == null || mac.length == 0) { > mac = new byte[6]; > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
