Example usage for java.net InetAddress getHostAddress

List of usage examples for java.net InetAddress getHostAddress

Introduction

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

Prototype

public String getHostAddress() 

Source Link

Document

Returns the IP address string in textual presentation.

Usage

From source file:com.adaptris.sftp.SftpClient.java

/**
 * Constructor assuming the default SSH port.
 *
 * @param addr the remote ssh host.//from  www  .  java 2s  .co  m
 */
public SftpClient(InetAddress addr) throws SftpException {
    this(addr.getHostAddress(), DEFAULT_SSH_PORT, DEFAULT_TIMEOUT, null, null);
}

From source file:jp.primecloud.auto.common.component.DnsStrategy.java

protected String getHostAddress(String fqdn) {
    try {/*from  w  ww  .  j  a  v a 2 s.com*/
        InetAddress address = InetAddress.getByName(fqdn);
        return address.getHostAddress();
    } catch (UnknownHostException e) {
        return null;
    }
}

From source file:ibp.plugin.nsd.NSDHelper.java

private JSONObject NsdServiceInfoToJSON(NsdServiceInfo info) {
    String name = info.getServiceName();
    String type = info.getServiceType();
    InetAddress host = info.getHost();
    int port = info.getPort();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", name);
    map.put("type", type);
    map.put("address", (host == null) ? "null" : host.getHostAddress());
    map.put("port", (host == null) ? 0 : port);
    JSONObject jsonObj = new JSONObject(map);
    return jsonObj;
}

From source file:alluxio.yarn.ApplicationMaster.java

/**
 * Submits requests for containers until the master and all workers are launched.
 *
 * @throws Exception if an error occurs while requesting or launching containers
 *///from www. ja  v a  2s. co m
public void requestAndLaunchContainers() throws Exception {

    if (masterExists()) {
        InetAddress address = InetAddress.getByName(mMasterAddress);
        mMasterContainerNetAddress = address.getHostAddress();
        LOG.info("Found master already running on " + mMasterAddress);
    } else {
        LOG.info("Configuring master container request.");
        Resource masterResource = Records.newRecord(Resource.class);
        masterResource.setMemory(mMasterMemInMB);
        masterResource.setVirtualCores(mMasterCpu);
        mContainerAllocator = new ContainerAllocator("master", 1, 1, masterResource, mYarnClient, mRMClient,
                mMasterAddress);
        List<Container> masterContainers = mContainerAllocator.allocateContainers();
        launchMasterContainer(Iterables.getOnlyElement(masterContainers));
    }

    Resource workerResource = Records.newRecord(Resource.class);
    workerResource.setMemory(mWorkerMemInMB + mRamdiskMemInMB);
    workerResource.setVirtualCores(mWorkerCpu);
    mContainerAllocator = new ContainerAllocator("worker", mNumWorkers, mMaxWorkersPerHost, workerResource,
            mYarnClient, mRMClient);
    List<Container> workerContainers = mContainerAllocator.allocateContainers();
    for (Container container : workerContainers) {
        launchWorkerContainer(container);
    }
    LOG.info("Master and workers are launched");
}

From source file:org.mobisocial.corral.ContentCorral.java

public static String getLocalIpAddress() {
    try {//from   ww w. java2  s  .  c o m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    // not ready for IPv6, apparently.
                    if (!inetAddress.getHostAddress().contains(":")) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
    } catch (SocketException ex) {

    }
    return null;
}

From source file:com.digitalpebble.stormcrawler.bolt.SimpleFetcherBolt.java

private String getPolitenessKey(URL u) {
    String key;// ww w.  j  ava2 s.  c o  m
    if (QUEUE_MODE_IP.equalsIgnoreCase(queueMode)) {
        try {
            final InetAddress addr = InetAddress.getByName(u.getHost());
            key = addr.getHostAddress();
        } catch (final UnknownHostException e) {
            // unable to resolve it, so don't fall back to host name
            LOG.warn("Unable to resolve: {}, skipping.", u.getHost());
            return null;
        }
    } else if (QUEUE_MODE_DOMAIN.equalsIgnoreCase(queueMode)) {
        key = PaidLevelDomain.getPLD(u.getHost());
        if (key == null) {
            LOG.warn("Unknown domain for url: {}, using hostname as key", u.toExternalForm());
            key = u.getHost();
        }
    } else {
        key = u.getHost();
        if (key == null) {
            LOG.warn("Unknown host for url: {}, using URL string as key", u.toExternalForm());
            key = u.toExternalForm();
        }
    }
    return key.toLowerCase(Locale.ROOT);
}

From source file:com.weibo.api.motan.config.AbstractInterfaceConfig.java

protected String getLocalHostAddress(List<URL> registryURLs) {

    String localAddress = null;//from   www. ja v a 2  s .c o m

    Map<String, Integer> regHostPorts = new HashMap<String, Integer>();
    for (URL ru : registryURLs) {
        if (StringUtils.isNotBlank(ru.getHost()) && ru.getPort() > 0) {
            regHostPorts.put(ru.getHost(), ru.getPort());
        }
    }

    InetAddress address = NetUtils.getLocalAddress(regHostPorts);
    if (address != null) {
        localAddress = address.getHostAddress();
    }

    if (NetUtils.isValidLocalHost(localAddress)) {
        return localAddress;
    }
    throw new MotanServiceException("Please config local server hostname with intranet IP first!",
            MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}

From source file:org.apache.mycat.advisor.common.net.http.HttpService.java

/**
 * ? header?//from   w  w  w  .j av  a  2s  . c o  m
 *
 * @param requestUrl
 * @param requestMethod
 * @param WithTokenHeader
 * @param token
 * @return
 */
public static String doHttpRequest(String requestUrl, String requestMethod, Boolean WithTokenHeader,
        String token) {
    String result = null;
    InetAddress ipaddr;
    int responseCode = -1;
    try {
        ipaddr = InetAddress.getLocalHost();
        StringBuffer buffer = new StringBuffer();
        URL url = new URL(requestUrl);
        HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
        httpUrlConn.setDoOutput(true);
        httpUrlConn.setDoInput(true);
        httpUrlConn.setUseCaches(false);

        httpUrlConn.setUseCaches(false);
        httpUrlConn.setRequestProperty("Accept-Charset", DEFAULT_CHARSET);
        httpUrlConn.setRequestProperty("Content-Type", "application/json;charset=" + DEFAULT_CHARSET);
        if (WithTokenHeader) {
            if (token == null) {
                throw new IllegalStateException("Oauth2 token is not set!");
            }
            httpUrlConn.setRequestProperty("Authorization", "OAuth2 " + token);
            httpUrlConn.setRequestProperty("API-RemoteIP", ipaddr.getHostAddress());
        }
        // ?GET/POST
        httpUrlConn.setRequestMethod(requestMethod);

        if ("GET".equalsIgnoreCase(requestMethod))
            httpUrlConn.connect();

        //            // ????
        //            if (null != outputJson) {
        //                OutputStream outputStream = httpUrlConn.getOutputStream();
        //                //??
        //                outputStream.write(outputJson.getBytes(DEFAULT_CHARSET));
        //                outputStream.close();
        //            }

        // ???
        InputStream inputStream = httpUrlConn.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, DEFAULT_CHARSET);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }
        result = buffer.toString();
        bufferedReader.close();
        inputStreamReader.close();
        // ?
        inputStream.close();
        httpUrlConn.disconnect();
    } catch (ConnectException ce) {
        logger.error("server connection timed out.", ce);
    } catch (Exception e) {
        logger.error("http request error:", e);
    } finally {
        return result;
    }
}

From source file:ComputeNode.java

@Override
public Boolean taskTransferRequest(Task task) throws RemoteException {

    Double load = getCurrentLoad();
    myNodeStats.getNoOfTransferRequests().incrementAndGet();

    lg.log(Level.FINER, "currLoad :" + load + " expectedLoad: " + task.getExpectedLoad()
            + " overLoadThreshold :" + overLoadThreshold);

    if (load + task.getExpectedLoad() > overLoadThreshold) {
        return false;
    }/*from   w  w w . j a va  2  s.c o m*/

    try {
        InetAddress ownIP = InetAddress.getLocalHost();
        String localIP = ownIP.getHostAddress();
        task.setNode(new Pair(id, localIP));

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    server.updateTaskTransfer(task);
    Thread t = new TaskExecutor(task);
    t.start();

    // spawn task request...
    return true;
}