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.hadoop.HadoopNameNodeClusterActionHandler.java

@Override
protected void afterConfigure(ClusterActionEvent event) throws IOException {
    ClusterSpec clusterSpec = event.getClusterSpec();
    Cluster cluster = event.getCluster();

    // TODO: wait for TTs to come up (done in test for the moment)

    LOG.info("Completed configuration of {}", clusterSpec.getClusterName());
    Instance instance = cluster.getInstanceMatching(role(ROLE));
    InetAddress namenodePublicAddress = instance.getPublicAddress();
    InetAddress jobtrackerPublicAddress = namenodePublicAddress;

    LOG.info("Namenode web UI available at http://{}:{}", namenodePublicAddress.getHostName(),
            NAMENODE_WEB_UI_PORT);// w  ww  .  j  av a2s . c o  m
    LOG.info("Jobtracker web UI available at http://{}:{}", jobtrackerPublicAddress.getHostName(),
            JOBTRACKER_WEB_UI_PORT);
    Properties config = createClientSideProperties(clusterSpec, namenodePublicAddress, jobtrackerPublicAddress);
    createClientSideHadoopSiteFile(clusterSpec, config);
    createProxyScript(clusterSpec, cluster);
    event.setCluster(new Cluster(cluster.getInstances(), config));
}

From source file:at.ac.tuwien.infosys.Balancer.java

@RequestMapping(value = "/ip", method = RequestMethod.GET)
public String getIp() {
    InetAddress ip;
    String hostname;/* ww  w.  j av a  2s . c  o m*/
    try {
        ip = InetAddress.getLocalHost();
        hostname = ip.getHostName();
        return "{\"ip\":\"" + ip + "\", \"hostname\":\"" + hostname + "\"}";
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return "Error: " + e.getMessage();
    }
}

From source file:com.sshtools.j2ssh.authentication.HostbasedAuthenticationClient.java

/**
 *
 *
 * @param authentication//from  w  w  w.  j ava  2s  . c o m
 * @param serviceToStart
 *
 * @throws IOException
 * @throws TerminatedStateException
 * @throws AuthenticationProtocolException
 */
public void authenticate(AuthenticationProtocolClient authentication, String serviceToStart)
        throws IOException, TerminatedStateException {
    if ((getUsername() == null) || (key == null)) {
        throw new AuthenticationProtocolException("You must supply a username and a key");
    }

    ByteArrayWriter baw = new ByteArrayWriter();

    log.info("Generating data to sign");

    SshPublicKey pub = key.getPublicKey();
    InetAddress addr = InetAddress.getLocalHost();
    String hostname = addr.getHostName();

    log.info("Preparing hostbased authentication request for " + hostname);

    // Now prepare and send the message
    baw.writeString(pub.getAlgorithmName());
    baw.writeBinaryString(pub.getEncoded());
    baw.writeString(hostname);

    if (clientUser != null) {
        baw.writeString(clientUser);
    } else {
        baw.writeString(getUsername());
    }

    // Create the signature data
    ByteArrayWriter data = new ByteArrayWriter();
    data.writeBinaryString(authentication.getSessionIdentifier());
    data.write(SshMsgUserAuthRequest.SSH_MSG_USERAUTH_REQUEST);
    data.writeString(getUsername());
    data.writeString(serviceToStart);
    data.writeString(getMethodName());
    data.writeString(pub.getAlgorithmName());
    data.writeBinaryString(pub.getEncoded());
    data.writeString(hostname);

    if (clientUser != null) {
        data.writeString(clientUser);
    } else {
        data.writeString(getUsername());
    }

    // Generate the signature
    baw.writeBinaryString(key.generateSignature(data.toByteArray()));

    SshMsgUserAuthRequest msg = new SshMsgUserAuthRequest(getUsername(), serviceToStart, getMethodName(),
            baw.toByteArray());

    authentication.sendMessage(msg);
}

From source file:com.nridge.connector.ws.con_ws.restlet.RestletApplication.java

/**
 * Returns a Restlet instance used to identify inbound requests for the
 * web service endpoints./*from   ww  w .jav  a2 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 = "restlet.host_names";
    String hostNames = mAppMgr.getString(propertyName);
    if (StringUtils.isEmpty(hostNames)) {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();

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

    RestletFilter restletFilter = new RestletFilter(mAppMgr, restletContext);
    propertyName = "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:weinre.server.ServerSettings.java

public String[] getBoundHosts() {
    if (getBoundHostValue() != null) {
        return new String[] { getBoundHost() };
    }/*ww w .  j  a  v  a 2s  . c  o  m*/

    ArrayList<String> hosts = new ArrayList<String>();
    List<NetworkInterface> networkInterfaces;
    try {
        networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
    } catch (SocketException e) {
        return new String[] { "localhost" };
    }

    for (NetworkInterface networkInterface : networkInterfaces) {
        List<InetAddress> inetAddresses = Collections.list(networkInterface.getInetAddresses());

        for (InetAddress inetAddress : inetAddresses) {
            hosts.add(inetAddress.getHostName());
        }
    }

    return hosts.toArray(new String[] {});
}

From source file:ws.argo.BrowserWeb.BrowserWebController.java

private Properties getPropeGeneratorProps() throws UnknownHostException {

    if (pgProps != null)
        return pgProps;

    InputStream in = getPropertiesFileInputStream();

    pgProps = new Properties();

    if (in != null) {
        try {//from   w ww.j a va2  s.com
            pgProps.load(in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            // Should log the props file issue
            setDefaultProbeGeneratorProperties(pgProps);
        }

    }

    if (pgProps.getProperty("listenerIPAddress", "").equals("")) {
        // If the listenerIPAddress is blank, then try to get the ip address of the interface

        String hostIPAddr = null;

        try {
            NetworkInterface n = NetworkInterface
                    .getByName(pgProps.getProperty("listenerInterfaceName", "en0"));

            if (!n.isLoopback()) {

                Enumeration ee = n.getInetAddresses();
                while (ee.hasMoreElements()) {
                    InetAddress i = (InetAddress) ee.nextElement();
                    hostIPAddr = i.getHostName();
                    System.out.println(hostIPAddr);
                }
            }
        } catch (SocketException e) {
            System.out.println("A socket exception occurred.");
        }

        if (hostIPAddr == null) {
            hostIPAddr = InetAddress.getLocalHost().getHostName();
            System.out.println("Defaulting to local address: " + hostIPAddr);
        }
        pgProps.put("listenerIPAddress", hostIPAddr);
    }

    return pgProps;
}

From source file:ch.cyberduck.core.proxy.ProxySocketFactory.java

@Override
public Socket createSocket(final InetAddress inetAddress, final int port, final InetAddress localHost,
        final int localPort) throws IOException {
    try {/*  ww w. ja  v  a  2  s .c  o m*/
        final Socket socket = this.factory(inetAddress.getHostName()).createSocket(inetAddress, port, localHost,
                localPort);
        configurator.configure(socket);
        return socket;
    } catch (IllegalArgumentException e) {
        throw this.failure(inetAddress.getHostName(), e);
    }
}

From source file:org.springframework.data.hadoop.util.net.DefaultHostInfoDiscovery.java

@Override
public HostInfo getHostInfo() {
    List<NetworkInterface> interfaces;
    try {//w  ww  . j av  a 2 s .  c om
        interfaces = getAllAvailableInterfaces();
    } catch (SocketException e) {
        return null;
    }

    // pre filter candidates
    interfaces = filterInterfaces(interfaces);

    // sort to prepare getting first match
    interfaces = sortInterfaces(interfaces);

    for (NetworkInterface nic : interfaces) {
        List<InetAddress> addresses = new ArrayList<InetAddress>();
        for (InterfaceAddress interfaceAddress : nic.getInterfaceAddresses()) {
            addresses.add(interfaceAddress.getAddress());
        }
        addresses = filterAddresses(addresses);
        if (!addresses.isEmpty()) {
            InetAddress address = addresses.get(0);
            return new HostInfo(address.getHostAddress(), address.getHostName());
        }
    }
    return null;
}

From source file:org.apache.synapse.SynapseServerInfoImpl.java

private void initServerHostAndIP() {
    try {// w w w . j a  v  a2s  .  c o m
        InetAddress addr = InetAddress.getLocalHost();
        if (addr != null) {
            ipAddress = addr.getHostAddress();
            hostName = addr.getHostName();
            if (hostName == null) {
                hostName = ipAddress;
            }
        }
    } catch (UnknownHostException e) {
        log.warn("Unable to get the hostName or IP address of the server", e);
    }
}

From source file:org.infoglue.calendar.actions.ViewApplicationStateAction.java

public String getServerName() {
    String serverName = "Unknown";

    try {/*  w ww.  ja va 2 s  .co m*/
        InetAddress localhost = InetAddress.getLocalHost();
        serverName = localhost.getHostName();
    } catch (Exception e) {

    }

    return serverName;
}