eu.lindenbaum.maven.util
Class NetworkUtils

java.lang.Object
  extended by eu.lindenbaum.maven.util.NetworkUtils

public final class NetworkUtils
extends Object

Containing network related utilities for IP resolution.

Since:
2.1.0
Author:
Tobias Schlager , Timo Koepke

Constructor Summary
NetworkUtils()
           
 
Method Summary
static Collection<InetAddress> filterInetAddresses(Collection<InetAddress> in, boolean ipv4only)
          Filters the elements of the given Collection of InetAddress objects for IPv4 or IPv6 addresses.
static Collection<InetAddress> getInetAddresses()
          Returns the InetAddresses as returned by the network interfaces of this host.
static String getIPv4Address()
          Returns a valid IPv4 address for this host.
static Collection<InetAddress> sortInetAddresses(Collection<InetAddress> in)
           Orders the elements of the given Collection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetworkUtils

public NetworkUtils()
Method Detail

getIPv4Address

public static String getIPv4Address()
                             throws org.apache.maven.plugin.MojoExecutionException
Returns a valid IPv4 address for this host. Only addresses of currently up network interfaces are considered. The returned address might be a loopback address. A loopback address will only be returned if no other address could be resolved.

Returns:
a non-null string containing one network address of this host
Throws:
org.apache.maven.plugin.MojoExecutionException - in case no up network interfaces could be detected

getInetAddresses

public static Collection<InetAddress> getInetAddresses()
                                                throws org.apache.maven.plugin.MojoExecutionException
Returns the InetAddresses as returned by the network interfaces of this host. Addresses of network interfaces currently not up are excluded.

Returns:
a non-null collection of network addresses.
Throws:
org.apache.maven.plugin.MojoExecutionException

filterInetAddresses

public static Collection<InetAddress> filterInetAddresses(Collection<InetAddress> in,
                                                          boolean ipv4only)
Filters the elements of the given Collection of InetAddress objects for IPv4 or IPv6 addresses. The input collection will not be modified, instead a new Collection will be returned. Multicast and wildcard addresses will be filtered out.

Parameters:
in - to filter for IPv4/IPv6 addresses
ipv4only - if true the List will only contain IPv4 addresses, otherwise only IPv6 addresses will be contained
Returns:
a filtered Collection

sortInetAddresses

public static Collection<InetAddress> sortInetAddresses(Collection<InetAddress> in)

Orders the elements of the given Collection. The input Collection will not be modified, instead a new Collection is returned. Output order is as followed:

  1. public network IP addresses
  2. private network IP addresses (192.x.x.x preferred)
  3. loopback addresses (e.g. 127.0.0.1)

Parameters:
in - Collection of addresses to reorder
Returns:
a sorted Collection


Copyright © 2012 Lindenbaum GmbH. All Rights Reserved.