Example usage for java.net InetAddress getHostName

List of usage examples for java.net InetAddress getHostName

Introduction

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

Prototype

public String getHostName() 

Source Link

Document

Gets the host name for this IP address.

Usage

From source file:org.hyperic.plugin.vrealize.automation.VRAUtils.java

public static String getApplicationServicePathFromJson(String applicationServicesPath) {
    // TODO: Need to replaced by regular expressions.
    log.debug("XML Content : " + applicationServicesPath);
    int index = applicationServicesPath.indexOf("com.vmware.darwin.appd.csp");
    String temp = applicationServicesPath.substring(index);
    index = temp.indexOf("statusEndPointUrl");
    temp = temp.substring(index);//w w w  .  j av  a  2 s .c om
    index = temp.indexOf(":/");
    temp = temp.substring(index + 3);
    index = temp.indexOf(":");
    temp = temp.substring(0, index);
    try {
        log.debug("host name or ip = " + temp);
        InetAddress addr = InetAddress.getByName(temp);
        log.debug("Application services hostname = " + addr.getHostName());
        return addr.getHostName();
    } catch (UnknownHostException e) {
        return "Unable to Resolve application services IP to hostname";
    }

}

From source file:org.jkcsoft.java.util.JavaHelper.java

public static InetAddress getUsefulInetAddr() throws UnknownHostException {
    InetAddress returnInetAddr = InetAddress.getLocalHost();
    int usefulCount = 0;
    try {/*from  w w w.  j  av  a  2s. c o m*/
        Enumeration niEnum = NetworkInterface.getNetworkInterfaces();
        while (niEnum.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) niEnum.nextElement();
            Enumeration ieEnum = ni.getInetAddresses();
            while (ieEnum.hasMoreElements()) {
                InetAddress inetAddr = InetAddress.getLocalHost();
                inetAddr = (InetAddress) ieEnum.nextElement();
                log.debug("NIC [" + ni.getDisplayName() + "]" + " Addr hn=[" + inetAddr.getHostName() + "]"
                        + " chn=[" + inetAddr.getCanonicalHostName() + "]" + " ha=[" + inetAddr.getHostAddress()
                        + "]");
                // hack to skip addresses often provided by Linux...
                if (!"127.0.0.1".equals(inetAddr.getHostAddress())
                        && inetAddr.getHostAddress().indexOf(':') == -1) {
                    if (usefulCount == 0)
                        returnInetAddr = inetAddr;
                    usefulCount++;
                } else {
                    // 
                }
            }
        }
    } catch (SocketException e) {
        log.error("getHostName", e);
    }

    if (usefulCount == 0) {
        log.warn("Only the loopback InetAddress could be found");
    }
    if (usefulCount > 1) {
        log.warn("More than one non-loopback InetAddrss was found; using the first one found.");
    }

    log.debug("Returning inet addr [" + returnInetAddr.toString() + "]");
    return returnInetAddr;
}

From source file:cn.bc.web.util.WebUtils.java

/**
 * ?Server?:[0]-IP?,[1]-??,[2]-url/*from   ww w.j a  va  2  s.c o  m*/
 * 
 * @return
 */
public static String[] getServer(HttpServletRequest request) {
    String[] server = new String[] { null, null, null };
    try {
        InetAddress localhost = InetAddress.getLocalHost();
        server[0] = localhost.getHostAddress();
        server[1] = localhost.getHostName();
    } catch (UnknownHostException e) {
        server[0] = "unknown";
        server[1] = "unknown";
    }
    server[2] = request != null ? request.getRequestURL().toString() : "unknown";
    return server;
}

From source file:ffx.Main.java

/**
 * Determine the host name, process ID, and FFX base directory.
 *///from w w w .  j a  va  2  s . com
private static void environment() {
    try {
        InetAddress addr = InetAddress.getLocalHost();
        hostName = addr.getHostName();
    } catch (UnknownHostException e) {
        // Do nothing.
    }

    String procString = System.getProperty("app.pid");
    if (procString != null) {
        procID = Integer.parseInt(procString);
    } else {
        procID = 0;
    }

    String dirString = System.getProperty("basedir");
    if (dirString != null) {
        ffxDirectory = new File(dirString);
    } else {
        ffxDirectory = new File(".");
    }

    try {
        logger.fine(String.format(" Force Field X directory is %s", ffxDirectory.getCanonicalPath()));
    } catch (Exception e) {
        // Do Nothing.
    }

}

From source file:gridool.util.GridUtils.java

@Nonnull
public static String getGridEndpoint(@Nonnull GridNode node) {
    InetAddress addr = node.getPhysicalAdress();
    String hostname = addr.getHostName(); //NetUtils.getHostNameWithoutDomain(addr);
    return "//" + hostname + ":1099/gridool/grid-01"; // TODO non default endpoint.
}

From source file:gridool.util.GridUtils.java

/**
 * @return hostname(255.255.255.255):77777
 *//*from   w  w  w .j  a  v a  2  s .com*/
public static String toHostNameAddrPort(@Nonnull final GridNode node) {
    InetAddress addr = node.getPhysicalAdress();
    String hostname = addr.getHostName();
    String ipAddr = addr.getHostAddress();
    int port = node.getPort();
    return hostname + '(' + ipAddr + "):" + port;
}

From source file:org.apache.hama.zookeeper.QuorumPeer.java

private static void writeMyID(Properties properties) throws IOException {
    long myId = -1;

    Configuration conf = new HamaConfiguration();
    String myAddress = DNS.getDefaultHost(conf.get("hama.zookeeper.dns.interface", "default"),
            conf.get("hama.zookeeper.dns.nameserver", "default"));

    List<String> ips = new ArrayList<String>();

    // Add what could be the best (configured) match
    ips.add(myAddress.contains(".") ? myAddress : StringUtils.simpleHostname(myAddress));

    // For all nics get all hostnames and IPs
    Enumeration<?> nics = NetworkInterface.getNetworkInterfaces();
    while (nics.hasMoreElements()) {
        Enumeration<?> rawAdrs = ((NetworkInterface) nics.nextElement()).getInetAddresses();
        while (rawAdrs.hasMoreElements()) {
            InetAddress inet = (InetAddress) rawAdrs.nextElement();
            ips.add(StringUtils.simpleHostname(inet.getHostName()));
            ips.add(inet.getHostAddress());
        }/*from   w w  w.  ja  v  a  2 s .  c  om*/
    }

    for (Entry<Object, Object> entry : properties.entrySet()) {
        String key = entry.getKey().toString().trim();
        String value = entry.getValue().toString().trim();

        if (key.startsWith("server.")) {
            int dot = key.indexOf('.');
            long id = Long.parseLong(key.substring(dot + 1));
            String[] parts = value.split(":");
            String address = parts[0];
            if (addressIsLocalHost(address) || ips.contains(address)) {
                myId = id;
                break;
            }
        }
    }

    if (myId == -1) {
        throw new IOException(
                "Could not find my address: " + myAddress + " in list of ZooKeeper quorum servers");
    }

    String dataDirStr = properties.get("dataDir").toString().trim();
    File dataDir = new File(dataDirStr);
    if (!dataDir.isDirectory()) {
        if (!dataDir.mkdirs()) {
            throw new IOException("Unable to create data dir " + dataDir);
        }
    }

    File myIdFile = new File(dataDir, "myid");
    PrintWriter w = new PrintWriter(myIdFile);
    w.println(myId);
    w.close();
}

From source file:org.rhq.plugins.apache.util.ApacheDeploymentUtil.java

public static void addDefaultVariables(Map<String, String> variables, String prefix) {
    InetAddress localhost = determineLocalhost();
    checkOrAddDefault(variables, "localhost", localhost.getHostName());
    checkOrAddDefault(variables, "localhost.ip", localhost.getHostAddress());
    checkOrAddDefault(variables, "unresolvable.host", "unreachable.host.com");
    checkOrAddDefault(variables, "port1", "11675");
    checkOrAddDefault(variables, "port2", "11676");
    checkOrAddDefault(variables, "port3", "11677");
    checkOrAddDefault(variables, "port4", "11678");

    if (prefix != null && !prefix.trim().isEmpty()) {
        prefix += ".";
    } else {//from  w  ww.j  av  a  2s .  c om
        prefix = "";
    }

    checkOrAddDefault(variables, prefix + "listen1", "${port1}");
    checkOrAddDefault(variables, prefix + "listen2", "${port2}");
    checkOrAddDefault(variables, prefix + "listen3", "${port3}");
    checkOrAddDefault(variables, prefix + "listen4", "${port4}");

    checkOrAddDefault(variables, prefix + "vhost1.servername", "${localhost}:${port1}");
    checkOrAddDefault(variables, prefix + "vhost1.urls", "${" + prefix + "vhost1.servername}");
    checkOrAddDefault(variables, prefix + "vhost1.servername.directive",
            "ServerName ${" + prefix + "vhost1.servername}");

    checkOrAddDefault(variables, prefix + "vhost2.servername", "${localhost}:${port2}");
    checkOrAddDefault(variables, prefix + "vhost2.urls", "${" + prefix + "vhost2.servername}");
    checkOrAddDefault(variables, prefix + "vhost2.servername.directive",
            "ServerName ${" + prefix + "vhost2.servername}");

    checkOrAddDefault(variables, prefix + "vhost3.servername", "${localhost}:${port3}");
    checkOrAddDefault(variables, prefix + "vhost3.urls", "${" + prefix + "vhost3.servername}");
    checkOrAddDefault(variables, prefix + "vhost3.servername.directive",
            "ServerName ${" + prefix + "vhost3.servername}");

    checkOrAddDefault(variables, prefix + "vhost4.servername", "${localhost}:${port4}");
    checkOrAddDefault(variables, prefix + "vhost4.urls", "${" + prefix + "vhost4.servername}");
    checkOrAddDefault(variables, prefix + "vhost4.servername.directive",
            "ServerName ${" + prefix + "vhost4.servername}");
}

From source file:org.cloudifysource.rest.doclet.RESTExamples.java

private static InstanceDescription getInstanceDescription(final String serviceName, final int id) {
    final InstanceDescription instanceDescription = new InstanceDescription();

    final InetAddress localHost = getLocalHost();
    String hostAddress;//from   ww  w . j  av a 2 s .com
    String hostName;
    if (localHost == null) {
        hostAddress = "localhost";
        hostName = "localhost";
    } else {
        hostAddress = localHost.getHostAddress();
        hostName = localHost.getHostName();
    }

    instanceDescription.setHostAddress(hostAddress);
    instanceDescription.setHostName(hostName);
    instanceDescription.setInstanceId(id);
    instanceDescription.setInstanceName(getInstanceName(serviceName, null));
    instanceDescription.setInstanceStatus("RUNNING");

    return instanceDescription;
}

From source file:org.cloudifysource.dsl.utils.IPUtils.java

/**
 * Resolves IP address to host name./*from   w ww. j av a 2  s .com*/
 * @param ip The IP to resolve
 * @return host name
 * @throws UnknownHostException Indicates the IP is not a known host
 */
public static String resolveIpToHostName(final String ip) throws UnknownHostException {

    String hostName = "";
    try {
        InetAddress addr = InetAddress.getByName(ip);
        hostName = addr.getHostName();
    } catch (UnknownHostException e) {
        throw new IllegalStateException("could not resolve host name of ip " + ip);
    }

    return hostName;
}