Example usage for java.net InetAddress getLocalHost

List of usage examples for java.net InetAddress getLocalHost

Introduction

In this page you can find the example usage for java.net InetAddress getLocalHost.

Prototype

public static InetAddress getLocalHost() throws UnknownHostException 

Source Link

Document

Returns the address of the local host.

Usage

From source file:com.baidu.rigel.biplatform.cache.util.MacAddressUtil.java

/** 
 * ??mac???mac?/*from   w  w  w .j  av a2s  . c  om*/
 * @param ia
 * @throws SocketException
 * @throws UnknownHostException 
 */
public static String getMacAddress(InetAddress ia) throws SocketException, UnknownHostException {
    if (ia == null) {
        ia = InetAddress.getLocalHost();
    }
    //????
    byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
    StringBuffer sb = new StringBuffer("");
    for (int i = 0; i < mac.length; i++) {
        if (i != 0) {
            sb.append("-");
        }
        //?
        int temp = mac[i] & 0xff;
        String str = Integer.toHexString(temp).toUpperCase();
        if (str.length() == 1) {
            sb.append("0" + str);
        } else {
            sb.append(str);
        }
    }
    return sb.toString();
}

From source file:com.nokia.dempsy.mpcluster.zookeeper.ZookeeperTestServer.java

public static int findNextPort() throws IOException {
    // find an unused ehpemeral port
    InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    ServerSocket serverSocket = new ServerSocket();
    serverSocket.setReuseAddress(true); // this allows the server port to be bound to even if it's in TIME_WAIT
    serverSocket.bind(inetSocketAddress);
    port = serverSocket.getLocalPort();// w  w w .j a va  2s  .  c o m
    serverSocket.close();
    return port;
}

From source file:com.janrain.utils.BackplaneSystemProps.java

public static String getMachineName() {
    try {/*  w  w  w  .  ja  v a 2s  .c  o  m*/
        return "backplane/" + InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        logger.warn("get localhost call failed");
    }
    return "n/a";
}

From source file:com.netflix.genie.agent.AgentMetadataImpl.java

private static String getAgentHostNameOrFallback() {
    try {/*from  w  w  w . j  av  a  2  s . c  om*/
        final String hostName = InetAddress.getLocalHost().getHostName();
        if (!StringUtils.isBlank(hostName)) {
            return hostName;
        }
    } catch (final UnknownHostException e) {
        log.warn("Failed to retrieve local host name", e);
    }
    return FALLBACK_STRING;
}

From source file:Uuid32Generator.java

public String generate() {
    StringBuilder strRetVal = new StringBuilder();
    String strTemp;/*from  w  w w. ja  v a 2s .c  o  m*/
    try {
        // IPAddress segment
        InetAddress addr = InetAddress.getLocalHost();
        byte[] ipaddr = addr.getAddress();
        for (byte anIpaddr : ipaddr) {
            Byte b = new Byte(anIpaddr);
            strTemp = Integer.toHexString(b.intValue() & 0x000000ff);
            strRetVal.append(ZEROS.substring(0, 2 - strTemp.length()));
            strRetVal.append(strTemp);
        }
        strRetVal.append(':');

        // CurrentTimeMillis() segment
        strTemp = Long.toHexString(System.currentTimeMillis());
        strRetVal.append(ZEROS.substring(0, 12 - strTemp.length()));
        strRetVal.append(strTemp).append(':');

        // random segment
        SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
        strTemp = Integer.toHexString(prng.nextInt());
        while (strTemp.length() < 8) {
            strTemp = '0' + strTemp;
        }
        strRetVal.append(strTemp.substring(4)).append(':');

        // IdentityHash() segment
        strTemp = Long.toHexString(System.identityHashCode(this));
        strRetVal.append(ZEROS.substring(0, 8 - strTemp.length()));
        strRetVal.append(strTemp);
    } catch (UnknownHostException uhex) {
        throw new RuntimeException("Unknown host.", uhex);
    } catch (NoSuchAlgorithmException nsaex) {
        throw new RuntimeException("Algorithm 'SHA1PRNG' is unavailiable.", nsaex);
    }
    return strRetVal.toString().toUpperCase();
}

From source file:com.medsphere.ovid.common.uuid.KeyGenerator.java

public KeyGenerator() {
    try {//from   w  ww .j a  v  a2s  .  co  m
        InetAddress inet = InetAddress.getLocalHost();
        byte[] bytes = inet.getAddress();
        String inetAddressInHex = new String(Hex.encodeHex(bytes));
        String hashCodeForThis = new String(Hex.encodeHex(intToByteArray(System.identityHashCode(this))));
        middle = inetAddressInHex + hashCodeForThis;
        seeder = new SecureRandom();
        seeder.nextInt();

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        seeder = new SecureRandom();
        seeder.nextInt();
        middle = new Integer(this.hashCode()).toString();
    }

}

From source file:com.google.gdt.eclipse.designer.hosted.tdt.GWTEnvironmentUtils.java

private static String getHostName() {
    String hostName = "";
    try {//w  w  w.j  a  v  a 2  s .c  om
        hostName = InetAddress.getLocalHost().getHostName();
        String[] names = StringUtils.split(hostName, '.');
        for (String name : names) {
            if (StringUtils.isNumeric(name)) {
                // getHostName() returned in a IP-address form
                return hostName;
            }
        }
        hostName = names[0];
    } catch (UnknownHostException e) {
    }
    return hostName;
}

From source file:com.mb.framework.util.property.HostnameAwareList.java

/**
 * default constructor/*from  ww  w.  j  a  va 2  s.  c  om*/
 * 
 * @throws UnknownHostException
 */
public HostnameAwareList() throws UnknownHostException {
    hostname = InetAddress.getLocalHost().getHostName().toUpperCase();
    logger.debug("Resolved hostname: " + hostname);

    if (StringUtils.isBlank(hostname)) {
        throw new UnknownHostException("Error resolving hostname, hostname is null or empty: " + hostname);
    }
}

From source file:com.mastertheboss.IPAddressController.java

@RequestMapping(value = "/ip", method = RequestMethod.GET)
public IPAddress ipaddress() throws Exception {
    return new IPAddress(++counter, InetAddress.getLocalHost().getHostAddress());
}

From source file:UuidGenerator.java

/**
 * Initializes the factory./*from   w  ww .j av  a 2s.c  om*/
 *
 * @param obj
 */
private synchronized static void initialize(final Object obj) {
    try {
        InetAddress inet = InetAddress.getLocalHost();
        byte[] bytes = inet.getAddress();
        String hexInetAddress = hexFormat(getInt(bytes), 8);
        String thisHashCode = hexFormat(System.identityHashCode(obj), 8);
        s_midValue = hexInetAddress + thisHashCode;
        s_seeder = new SecureRandom();
        s_seeder.nextInt();
    } catch (java.net.UnknownHostException e) {
        throw new Error("can not initialize the UuidGenerator generator");
    }
    s_initialized = true;
}