Example usage for java.net Inet4Address isSiteLocalAddress

List of usage examples for java.net Inet4Address isSiteLocalAddress

Introduction

In this page you can find the example usage for java.net Inet4Address isSiteLocalAddress.

Prototype

public boolean isSiteLocalAddress() 

Source Link

Document

Utility routine to check if the InetAddress is a site local address.

Usage

From source file:uk.ac.horizon.ubihelper.j2se.Server.java

/**
 * @param args// w w w  . j  a  va2  s . co m
 */
public static void main(String[] args) {
    InetAddress bestAddress = null;
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            if (!ni.isUp() || ni.isVirtual() || ni.isLoopback())
                continue;
            logger.info("Has interface " + ni.getName() + ": " + ni.getDisplayName());
            Enumeration<InetAddress> as = ni.getInetAddresses();
            while (as.hasMoreElements()) {
                InetAddress a = as.nextElement();
                if (a instanceof Inet4Address) {
                    Inet4Address ip = (Inet4Address) a;
                    logger.info("- IPv4 address " + ip.getHostAddress());
                    if (ip.isSiteLocalAddress()) {
                        logger.info("-- site local!");
                        if (bestAddress == null)
                            bestAddress = ip;
                    } else
                        bestAddress = ip;
                }
            }
        }
    } catch (Exception e) {
        logger.severe("Could not list NetworkInterfaces: " + e);
        System.exit(-1);
    }
    if (bestAddress == null) {
        logger.severe("Could not find an IP address to bind to - using localhost");
        try {
            bestAddress = InetAddress.getLocalHost();
        } catch (Exception e) {
            logger.severe("Could not get localhost address: " + e);
            System.exit(-2);
        }
    }
    int port = 0;
    if (args.length == 1) {
        try {
            port = Integer.parseInt(args[0]);
        } catch (NumberFormatException nfe) {
            System.err.println("Usage: <server-port>");
            System.exit(-3);
        }
    }
    Server server = new Server();
    server.init(bestAddress, port);
}

From source file:it.infn.ct.jsaga.adaptor.rocci.job.rOCCIJobControlAdaptor.java

public boolean testIpAddress(byte[] testAddress) {
    Inet4Address inet4Address;
    boolean result = false;

    try {/*from   w w w.  java  2 s .  co  m*/
        inet4Address = (Inet4Address) InetAddress.getByAddress(testAddress);
        result = inet4Address.isSiteLocalAddress();
    } catch (UnknownHostException ex) {
        log.error(ex);
    }

    return result;
}

From source file:it.infn.ct.jsaga.adaptor.jocci.job.jOCCIJobControlAdaptor.java

public boolean testIpAddress(byte[] testAddress) {
    Inet4Address inet4Address;
    boolean result = false;

    try {/*from  ww  w. ja v  a2 s  .  c  o  m*/
        inet4Address = (Inet4Address) InetAddress.getByAddress(testAddress);
        result = inet4Address.isSiteLocalAddress();
    } catch (UnknownHostException ex) {
        java.util.logging.Logger.getLogger(jOCCIJobControlAdaptor.class.getName()).log(Level.SEVERE, null, ex);
    }

    return result;
}

From source file:org.commonjava.maven.galley.cache.infinispan.FastLocalCacheProvider.java

private String getCurrentNodeIp() throws SocketException {

    final Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();

    Inet4Address top = null;
    while (nis.hasMoreElements()) {
        final NetworkInterface ni = nis.nextElement();
        final Enumeration<InetAddress> ips = ni.getInetAddresses();

        while (ips.hasMoreElements()) {
            final InetAddress ip = ips.nextElement();
            if (ip instanceof Inet4Address && !ip.isLinkLocalAddress()) {
                if (top == null) {
                    top = (Inet4Address) ip;
                } else if (!top.isSiteLocalAddress() && ip.isSiteLocalAddress()) {
                    top = (Inet4Address) ip;
                }/*  w  w  w.ja  v a 2s  . c  o m*/
            }
        }
    }

    if (top == null) {
        throw new IllegalStateException("[galley] IP not found.");
    }

    return top.getHostAddress();
}