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.apache.whirr.service.hbase.HBaseMasterClusterActionHandler.java

private void createProxyScript(ClusterSpec clusterSpec, Cluster cluster) {
    File configDir = getConfigDir(clusterSpec);
    File hbaseProxyFile = new File(configDir, "hbase-proxy.sh");
    try {//w  w w.jav  a2 s  .c o m
        HadoopProxy proxy = new HadoopProxy(clusterSpec, cluster);
        InetAddress master = HBaseCluster.getMasterPublicAddress(cluster);
        String script = String.format("echo 'Running proxy to HBase cluster at %s. " + "Use Ctrl-c to quit.'\n",
                master.getHostName()) + Joiner.on(" ").join(proxy.getProxyCommand());
        Files.write(script, hbaseProxyFile, Charsets.UTF_8);
        hbaseProxyFile.setExecutable(true);
        LOG.info("Wrote HBase proxy script {}", hbaseProxyFile);
    } catch (IOException e) {
        LOG.error("Problem writing HBase proxy script {}", hbaseProxyFile, e);
    }
}

From source file:weinre.server.ServerSettings.java

@SuppressWarnings("unused")
private String getSuperNiceHostName() {
    String hostName = getBoundHost();

    // get the host address used
    InetAddress inetAddress;/*  w w  w. j av  a2 s  .co m*/
    try {
        inetAddress = InetAddress.getByName(hostName);
    } catch (UnknownHostException e) {
        Main.warn("Unable to get host address of " + hostName);
        return null;
    }

    // if it's "any local address", deal with that
    if (inetAddress.isAnyLocalAddress()) {
        try {
            InetAddress oneAddress = InetAddress.getLocalHost();
            return oneAddress.getHostName();
        } catch (UnknownHostException e) {
            Main.warn("Unable to get any local host address");
            return null;
        }
    }

    return inetAddress.getHostName();
}

From source file:com.gomoob.embedded.command.GetConfigurationCommand.java

private JSONObject createNet(IMongoContext mongoContext) throws CommandException {

    String bindIp = mongoContext.getMongodConfig().net().getBindIp();
    int port = mongoContext.getMongodConfig().net().getPort();
    boolean ipv6 = mongoContext.getMongodConfig().net().isIpv6();

    JSONObject serverAddress = new JSONObject();

    try {//from   w  ww  . java2  s  . c  o m
        InetAddress inetAddress = mongoContext.getMongodConfig().net().getServerAddress();
        serverAddress.put("canonicalHostName", inetAddress.getCanonicalHostName());
        serverAddress.put("hostAddress", inetAddress.getHostAddress());
        serverAddress.put("hostName", inetAddress.getHostName());
    } catch (UnknownHostException uhex) {
        throw new CommandException(uhex);
    }

    JSONObject net = new JSONObject();
    net.put("bindIp", bindIp);
    net.put("port", port);
    net.put("ipv6", ipv6);
    net.put("serverAddress", serverAddress);

    return net;

}

From source file:com.cscao.apps.ntplib.NTPClient.java

/**
 * Process <code>TimeInfo</code> object and print its details and return the offset.
 *
 * @param info <code>TimeInfo</code> object.
 *//*from  w  w w . j a va  2  s  .  c o m*/
private long processResponse(TimeInfo info) {
    NtpV3Packet message = info.getMessage();
    if (message == null) {
        return 0;
    }

    int stratum = message.getStratum();
    String refType;
    if (stratum <= 0) {
        refType = "(Unspecified or Unavailable)";
    } else if (stratum == 1) {
        refType = "(Primary Reference; e.g., GPS)"; // GPS, radio clock, etc.
    } else {
        refType = "(Secondary Reference; e.g. via NTP or SNTP)";
    }
    // stratum should be 0..15...
    //        System.out.println(" Stratum: " + stratum + " " + refType);
    details += " Stratum: " + stratum + " " + refType + "\n";

    int version = message.getVersion();
    int li = message.getLeapIndicator();
    //        System.out.println(" leap=" + li + ", version="
    //                + version + ", precision=" + message.getPrecision());

    details += " leap=" + li + ", version=" + version + ", precision=" + message.getPrecision() + "\n";
    details += " mode: " + message.getModeName() + " (" + message.getMode() + ")" + "\n";

    //        System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")");
    int poll = message.getPoll();
    // poll value typically btwn MINPOLL (4) and MAXPOLL (14)
    //        System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll))
    //                + " seconds" + " (2 ** " + poll + ")");

    details += " poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll)) + " seconds" + " (2 ** " + poll + ")"
            + "\n";

    double disp = message.getRootDispersionInMillisDouble();

    //        System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble())
    //                + ", rootdispersion(ms): " + numberFormat.format(disp));

    details += " rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble())
            + ", rootdispersion(ms): " + numberFormat.format(disp) + "\n";

    int refId = message.getReferenceId();
    String refAddr = NtpUtils.getHostAddress(refId);
    String refName = null;
    if (refId != 0) {
        if (refAddr.equals("127.127.1.0")) {
            refName = "LOCAL"; // This is the ref address for the Local Clock
        } else if (stratum >= 2) {
            // If reference id has 127.127 prefix then it uses its own reference clock
            // defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5
            // for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution.
            if (!refAddr.startsWith("127.127")) {
                try {
                    InetAddress addr = InetAddress.getByName(refAddr);
                    String name = addr.getHostName();
                    if (name != null && !name.equals(refAddr)) {
                        refName = name;
                    }
                } catch (UnknownHostException e) {
                    // some stratum-2 servers sync to ref clock device but fudge stratum level higher... (e.g. 2)
                    // ref not valid host maybe it's a reference clock name?
                    // otherwise just show the ref IP address.
                    refName = NtpUtils.getReferenceClock(message);
                }
            }
        } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
            refName = NtpUtils.getReferenceClock(message);
            // refname usually have at least 3 characters (e.g. GPS, WWV, LCL, etc.)
        }
        // otherwise give up on naming the beast...
    }
    if (refName != null && refName.length() > 1) {
        refAddr += " (" + refName + ")";
    }
    //        System.out.println(" Reference Identifier:\t" + refAddr);

    details += " Reference Identifier:\t" + refAddr + "\n";

    TimeStamp refNtpTime = message.getReferenceTimeStamp();
    //        System.out.println(" Reference Timestamp:\t" + refNtpTime + "  " + refNtpTime.toDateString());

    details += " Reference Timestamp:\t" + refNtpTime + "  " + refNtpTime.toDateString() + "\n";

    // Originate Time is time request sent by client (t1)
    TimeStamp origNtpTime = message.getOriginateTimeStamp();
    //        System.out.println(" Originate Timestamp:\t" + origNtpTime + "  " + origNtpTime.toDateString());

    details += " Originate Timestamp:\t" + origNtpTime + "  " + origNtpTime.toDateString() + "\n";

    long destTime = info.getReturnTime();
    // Receive Time is time request received by server (t2)
    TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
    //        System.out.println(" Receive Timestamp:\t" + rcvNtpTime + "  " + rcvNtpTime.toDateString());

    details += " Receive Timestamp:\t" + rcvNtpTime + "  " + rcvNtpTime.toDateString() + "\n";

    // Transmit time is time reply sent by server (t3)
    TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
    //        System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + "  " + xmitNtpTime.toDateString());

    details += " Transmit Timestamp:\t" + xmitNtpTime + "  " + xmitNtpTime.toDateString() + "\n";

    // Destination time is time reply received by client (t4)
    TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime);
    //        System.out.println(" Destination Timestamp:\t" + destNtpTime + "  " + destNtpTime.toDateString());

    details += " Destination Timestamp:\t" + destNtpTime + "  " + destNtpTime.toDateString() + "\n";

    info.computeDetails(); // compute offset/delay if not already done
    Long offsetValue = info.getOffset();
    Long delayValue = info.getDelay();
    String delay = (delayValue == null) ? "N/A" : delayValue.toString();
    String offset = (offsetValue == null) ? "N/A" : offsetValue.toString();

    //        System.out.println(" Roundtrip delay(ms)=" + delay
    //                + ", clock offset(ms)=" + offset); // offset in ms
    details += " Roundtrip delay(ms)=" + delay + ", clock offset(ms)=" + offset + "\n";

    // calibrate local clock
    //        return (offsetValue == null) ? -1 : (destNtpTime.getTime() + offsetValue);

    // return offset
    return (offsetValue == null) ? 0 : offsetValue;
}

From source file:sos.net.SOSSSLSocketFactory.java

public Socket createSocket(InetAddress address, int port, InetAddress clientAddress, int clientPort)
        throws IOException {
    return createSocket(null, address.getHostName(), port, true);
}

From source file:org.apache.whirr.service.hadoop.HadoopNameNodeClusterActionHandler.java

private void createProxyScript(ClusterSpec clusterSpec, Cluster cluster) {
    File configDir = getConfigDir(clusterSpec);
    File hadoopProxyFile = new File(configDir, "hadoop-proxy.sh");
    try {/* www . j  ava2  s.  c o  m*/
        HadoopProxy proxy = new HadoopProxy(clusterSpec, cluster);
        InetAddress namenode = HadoopCluster.getNamenodePublicAddress(cluster);
        String script = String.format(
                "echo 'Running proxy to Hadoop cluster at %s. " + "Use Ctrl-c to quit.'\n",
                namenode.getHostName()) + Joiner.on(" ").join(proxy.getProxyCommand());
        Files.write(script, hadoopProxyFile, Charsets.UTF_8);
        LOG.info("Wrote Hadoop proxy script {}", hadoopProxyFile);
    } catch (IOException e) {
        LOG.error("Problem writing Hadoop proxy script {}", hadoopProxyFile, e);
    }
}

From source file:com.jcraft.weirdx.XRexec.java

public XRexec(String myName, int num) {
    try {//from  www  .j a  va  2s.  c om
        InetAddress local = null;
        if (myName != null && myName.length() > 0) {
            local = InetAddress.getByName(myName);
        } else {
            local = InetAddress.getLocalHost();
        }
        display = local.getHostName() + ":" + num + ".0";
    } catch (Exception e) {
        display = "localhost:" + num + ".0";
        LOG.error(e);
    }

    JFrame jframe = new JFrame();
    Container cpane = jframe.getContentPane();
    cpane.setLayout(new GridLayout(0, 1));

    JPanel jpanel = null;

    jpanel = new JPanel();
    jpanel.setBorder(BorderFactory.createTitledBorder("Host"));
    jpanel.setLayout(new BorderLayout());
    host.setText("");
    host.setMinimumSize(new Dimension(50, 25));
    host.setEditable(true);
    jpanel.add(host, "Center");
    cpane.add(jpanel);

    jpanel = new JPanel();
    jpanel.setBorder(BorderFactory.createTitledBorder("User"));
    jpanel.setLayout(new BorderLayout());
    name.setText("");
    name.setMinimumSize(new Dimension(50, 25));
    name.setEditable(true);
    jpanel.add(name, "Center");
    cpane.add(jpanel);

    jpanel = new JPanel();
    jpanel.setBorder(BorderFactory.createTitledBorder("Password"));
    jpanel.setLayout(new BorderLayout());
    passwd.setMinimumSize(new Dimension(50, 25));
    passwd.setEditable(true);
    jpanel.add(passwd, "Center");
    cpane.add(jpanel);

    jpanel = new JPanel();
    jpanel.setBorder(BorderFactory.createTitledBorder("Command with Absolute Path"));
    jpanel.setLayout(new BorderLayout());
    command.setText("");
    command.setMinimumSize(new Dimension(50, 25));
    command.setEditable(true);
    jpanel.add(command, "Center");
    command.addActionListener(this);
    cpane.add(jpanel);

    jpanel = new JPanel();
    jpanel.add(rexec, "Center");
    rexec.addActionListener(this);
    cpane.add(jpanel);

    jframe.pack();
    jframe.setVisible(true);
}

From source file:com.nridge.connector.fs.con_fs.restlet.RestletApplication.java

/**
 * Returns a Restlet instance used to identify inbound requests for the
 * web service endpoints.//from  w  ww  .  java2  s  .co m
 *
 * @return Restlet instance.
 */
@Override
public Restlet createInboundRoot() {
    Restlet restletRoot;
    Logger appLogger = mAppMgr.getLogger(this, "createInboundRoot");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    Context restletContext = getContext();
    Router restletRouter = new Router(restletContext);

    String propertyName = Constants.CFG_PROPERTY_PREFIX + ".restlet.host_names";
    String hostNames = mAppMgr.getString(propertyName);
    if (StringUtils.isEmpty(hostNames)) {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();

            routerAttachEndPoints(restletRouter, Constants.HOST_NAME_DEFAULT);
            routerAttachEndPoints(restletRouter, inetAddress.getHostName());
            routerAttachEndPoints(restletRouter, inetAddress.getHostAddress());
            routerAttachEndPoints(restletRouter, inetAddress.getCanonicalHostName());
        } catch (UnknownHostException e) {
            appLogger.error(e.getMessage(), e);
            routerAttachEndPoints(restletRouter, Constants.HOST_NAME_DEFAULT);
        }
    } else {
        if (mAppMgr.isPropertyMultiValue(propertyName)) {
            String[] hostNameList = mAppMgr.getStringArray(propertyName);
            for (String hostName : hostNameList)
                routerAttachEndPoints(restletRouter, hostName);
        } else
            routerAttachEndPoints(restletRouter, hostNames);
    }

    RestletFilter restletFilter = new RestletFilter(mAppMgr, restletContext);
    propertyName = Constants.CFG_PROPERTY_PREFIX + ".restlet.allow_addresses";
    String allowAddresses = mAppMgr.getString(propertyName);
    if (StringUtils.isNotEmpty(allowAddresses)) {
        if (mAppMgr.isPropertyMultiValue(propertyName)) {
            String[] allowAddressList = mAppMgr.getStringArray(propertyName);
            for (String allowAddress : allowAddressList) {
                restletFilter.add(allowAddress);
                appLogger.debug("Filter Allow Address: " + allowAddress);
            }
        } else {
            restletFilter.add(allowAddresses);
            appLogger.debug("Filter Allow Address: " + allowAddresses);
        }
        restletFilter.setNext(restletRouter);
        restletRoot = restletFilter;
    } else
        restletRoot = restletRouter;

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);

    return restletRoot;
}

From source file:org.apache.geode.internal.net.SocketCreator.java

/**
 * returns the host name for the given inet address, using a local cache of names to avoid dns
 * hits and duplicate strings/*from   w  w w  .  j ava 2s  .c  om*/
 */
public static synchronized String getHostName(InetAddress addr) {
    String result = (String) hostNames.get(addr);
    if (result == null) {
        result = addr.getHostName();
        hostNames.put(addr, result);
    }
    return result;
}

From source file:org.fusesource.mop.commands.AbstractContainerBase.java

protected String getHostName() throws Exception {
    String hostname = "localhost";
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface inet = interfaces.nextElement();
        Enumeration<InetAddress> addresses = inet.getInetAddresses();
        if (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (address.isLoopbackAddress()) {
                InetAddress localAddress = InetAddress.getByName(address.getHostName());
                hostname = localAddress.getHostName();
                break;
            }//from www.ja v  a 2  s.  co  m
        }
    }
    return hostname;
}