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:de.codecentric.boot.admin.config.AdminClientProperties.java

public String getManagementUrl() {
    if (managementUrl != null) {
        return managementUrl;
    }//from www.  j av a 2s. com

    if ((managementPort == null || managementPort.equals(serverPort)) && getServiceUrl() != null) {
        return append(append(getServiceUrl(), server.getServletPrefix()), management.getContextPath());
    }

    if (managementPort == null) {
        throw new IllegalStateException("serviceUrl must be set when deployed to servlet-container");
    }

    if (preferIp) {
        InetAddress address = management.getAddress();
        if (address == null) {
            address = getHostAddress();
        }
        return append(append(createLocalUri(address.getHostAddress(), managementPort), server.getContextPath()),
                management.getContextPath());

    }
    return append(createLocalUri(getHostAddress().getCanonicalHostName(), managementPort),
            management.getContextPath());
}

From source file:brooklyn.location.basic.WinRmMachineLocation.java

@Nullable
@Override/*from www.j ava2s  . co  m*/
public String getHostname() {
    InetAddress address = getAddress();
    return (address != null) ? address.getHostAddress() : null;
}

From source file:ch.cyberduck.core.socket.NetworkInterfaceAwareSocketFactory.java

/**
 * @param network Network interface index
 * @throws UnknownHostException/*from ww  w .j  a  v a2 s.c  o m*/
 */
private Inet6Address getByAddressForInterface(final NetworkInterface network, final InetAddress address)
        throws UnknownHostException {
    // Append network interface. Workaround for issue #8802
    return Inet6Address.getByAddress(address.getHostAddress(),
            IPAddressUtil.textToNumericFormatV6(address.getHostAddress()), network.getIndex());
}

From source file:com.clustercontrol.port.protocol.ReachAddressIMAP.java

/**
 * IMAP????????// www . ja  v  a  2s.  co  m
 * 
 * @param addressText
 * @return IMAP
 */
@Override
protected boolean isRunning(String addressText) {

    m_message = "";
    m_messageOrg = "";
    m_response = -1;

    boolean isReachable = false;

    try {
        long start = 0; // 
        long end = 0; // 
        boolean retry = true; // ????(true:??false:???)

        StringBuffer bufferOrg = new StringBuffer(); // 
        String result = "";

        InetAddress address = InetAddress.getByName(addressText);

        bufferOrg.append("Monitoring the IMAP Service of " + address.getHostName() + "["
                + address.getHostAddress() + "]:" + m_portNo + ".\n\n");

        IMAPClient client = new IMAPClient();

        for (int i = 0; i < m_sentCount && retry; i++) {
            try {
                bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: ");
                client.setDefaultTimeout(m_timeout);

                start = HinemosTime.currentTimeMillis();
                client.connect(address, m_portNo);
                end = HinemosTime.currentTimeMillis();

                m_response = end - start;

                result = client.getReplyString();

                if (m_response > 0) {
                    if (m_response < m_timeout) {
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    } else {
                        m_response = m_timeout;
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    }
                } else {
                    result = result + ("\n" + "Response Time < 1ms");
                }

                retry = false;
                isReachable = true;

            } catch (SocketException e) {
                result = (e.getMessage() + "[SocketException]");
                retry = true;
                isReachable = false;
            } catch (IOException e) {
                result = (e.getMessage() + "[IOException]");
                retry = true;
                isReachable = false;
            } finally {
                bufferOrg.append(result + "\n");
                if (client.isConnected()) {
                    try {
                        client.disconnect();
                    } catch (IOException e) {
                        m_log.warn("isRunning(): " + "socket disconnect failed: " + e.getMessage(), e);
                    }
                }
            }

            if (i < m_sentCount - 1 && retry) {
                try {
                    Thread.sleep(m_sentInterval);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }

        m_message = result + "(IMAP/" + m_portNo + ")";
        m_messageOrg = bufferOrg.toString();
        return isReachable;
    } catch (UnknownHostException e) {
        m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage()
                + e.getMessage());

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage()
                + ")";

        return false;
    }
}

From source file:com.clustercontrol.port.protocol.ReachAddressPOP3.java

/**
 * POP3????????//from   w  w w  .j  ava  2  s.  com
 * 
 * @param addressText
 * @return POP3
 */
@Override
protected boolean isRunning(String addressText) {

    m_message = "";
    m_messageOrg = "";
    m_response = -1;

    boolean isReachable = false;

    try {
        long start = 0; // 
        long end = 0; // 
        boolean retry = true; // ????(true:??false:???)

        StringBuffer bufferOrg = new StringBuffer(); // 
        String result = "";

        InetAddress address = InetAddress.getByName(addressText);

        bufferOrg.append("Monitoring the POP3 Service of " + address.getHostName() + "["
                + address.getHostAddress() + "]:" + m_portNo + ".\n\n");

        POP3Client client = new POP3Client();

        for (int i = 0; i < m_sentCount && retry; i++) {
            try {
                bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: ");
                client.setDefaultTimeout(m_timeout);

                start = HinemosTime.currentTimeMillis();
                client.connect(address, m_portNo);
                end = HinemosTime.currentTimeMillis();

                m_response = end - start;

                result = client.getReplyString();

                if (m_response > 0) {
                    if (m_response < m_timeout) {
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    } else {
                        m_response = m_timeout;
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    }
                } else {
                    result = result + ("\n" + "Response Time < 1ms");
                }

                retry = false;
                isReachable = true;

            } catch (SocketException e) {
                result = (e.getMessage() + "[SocketException]");
                retry = true;
                isReachable = false;
            } catch (IOException e) {
                result = (e.getMessage() + "[IOException]");
                retry = true;
                isReachable = false;
            } finally {
                bufferOrg.append(result + "\n");
                if (client.isConnected()) {
                    try {
                        client.disconnect();
                    } catch (IOException e) {
                        m_log.warn("isRunning(): " + "socket disconnect failed: " + e.getMessage(), e);
                    }
                }
            }

            if (i < m_sentCount - 1 && retry) {
                try {
                    Thread.sleep(m_sentInterval);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }

        m_message = result + "(POP3/" + m_portNo + ")";
        m_messageOrg = bufferOrg.toString();
        return isReachable;
    } catch (UnknownHostException e) {
        m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage()
                + e.getMessage());

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage()
                + ")";

        return false;
    }
}

From source file:com.clustercontrol.port.protocol.ReachAddressTCP.java

/**
 * ????????????//w w w .  java 2 s. co m
 * 
 * @param addressText
 * @return PORT
 */
@Override
protected boolean isRunning(String addressText) {

    m_message = "";
    m_messageOrg = "";
    m_response = -1;

    boolean isReachable = false;

    try {
        long start = 0; // 
        long end = 0; // 

        StringBuffer bufferOrg = new StringBuffer(); // 
        String result = "";

        // Reachability ?? ICMP ??
        boolean retry = true;

        InetAddress address = InetAddress.getByName(addressText);

        bufferOrg.append("Monitoring the port of " + address.getHostName() + "[" + address.getHostAddress()
                + "]:" + m_portNo + ".\n\n");

        // 
        Socket socket = null;

        for (int i = 0; i < m_sentCount && retry; i++) {
            try {
                // ?
                socket = new Socket();
                InetSocketAddress isa = new InetSocketAddress(address, m_portNo);

                bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: ");
                start = HinemosTime.currentTimeMillis();
                socket.connect(isa, m_timeout);
                end = HinemosTime.currentTimeMillis();

                m_response = end - start;
                if (m_response > 0) {
                    if (m_response < m_timeout) {
                        result = ("Response Time = " + m_response + "ms");
                    } else {
                        m_response = m_timeout;
                        result = ("Response Time = " + m_response + "ms");
                    }
                } else {
                    result = ("Response Time < 1ms");
                }
                retry = false;
                isReachable = true;
            } catch (BindException e) {
                result = (e.getMessage() + "[BindException]");
                retry = true;
                isReachable = false;
            } catch (ConnectException e) {
                result = (e.getMessage() + "[ConnectException]");
                retry = false;
                isReachable = false;
            } catch (NoRouteToHostException e) {
                result = (e.getMessage() + "[NoRouteToHostException]");
                retry = true;
                isReachable = false;
            } catch (PortUnreachableException e) {
                result = (e.getMessage() + "[PortUnreachableException]");
                retry = true;
                isReachable = false;
            } catch (IOException e) {
                result = (e.getMessage() + "[IOException]");
                retry = true;
                isReachable = false;
            } finally {
                bufferOrg.append(result + "\n");
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        m_log.warn("isRunning(): " + "socket close failed: " + e.getMessage(), e);
                    }
                }
            }

            if (i < m_sentCount - 1 && retry) {
                try {
                    Thread.sleep(m_sentInterval);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }

        m_message = result + "(TCP/" + m_portNo + ")";
        m_messageOrg = bufferOrg.toString();
        return isReachable;
    } catch (UnknownHostException e) {
        m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage()
                + e.getMessage());

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage()
                + ")";

        return false;
    }
}

From source file:com.datatorrent.stram.security.StramWSFilter.java

@SuppressWarnings("ReturnOfCollectionOrArrayField")
protected Set<String> getProxyAddresses() throws ServletException {
    long now = System.currentTimeMillis();
    synchronized (this) {
        if (proxyAddresses == null || (lastUpdate + updateInterval) >= now) {
            try {
                proxyAddresses = new HashSet<String>();
                for (InetAddress add : InetAddress.getAllByName(proxyHost)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("proxy address is: " + add.getHostAddress());
                    }//from   ww w.j av  a  2s  . c o  m
                    proxyAddresses.add(add.getHostAddress());
                }
                lastUpdate = now;
            } catch (UnknownHostException e) {
                throw new ServletException("Could not locate " + proxyHost, e);
            }
        }
        return proxyAddresses;
    }
}

From source file:com.hg.development.apps.messagenotifier_v1.Utils.Utility.java

/**
 *
 * @return adresse IP v4 actuelle du tlphone.
 * @throws SocketException/*from   w  w  w .  j  a va2  s  . c  om*/
 */
private String getLocalIpAddress() throws SocketException {
    String ipv4 = null;
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

        if (interfaces.size() > 0) {
            for (NetworkInterface ni : interfaces) {
                List<InetAddress> ialist = Collections.list(ni.getInetAddresses());

                if (ialist.size() > 0) {
                    for (InetAddress address : ialist) {
                        if (!address.isLoopbackAddress()
                                && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress()))
                            ;
                    }
                }

            }
        }

    } catch (SocketException ex) {
        throw ex;
    } finally {
        return ipv4;
    }
}

From source file:com.clustercontrol.port.protocol.ReachAddressIMAPS.java

/**
 * IMAPS????????//from w w w . j  a v  a 2  s.  c o m
 * 
 * @param addressText
 * @return IMAPS
 */
@Override
protected boolean isRunning(String addressText) {

    m_message = "";
    m_messageOrg = "";
    m_response = -1;

    boolean isReachable = false;

    try {
        long start = 0; // 
        long end = 0; // 
        boolean retry = true; // ????(true:??false:???)

        StringBuffer bufferOrg = new StringBuffer(); // 
        String result = "";

        InetAddress address = InetAddress.getByName(addressText);

        bufferOrg.append("Monitoring the IMAPS Service of " + address.getHostName() + "["
                + address.getHostAddress() + "]:" + m_portNo + ".\n\n");

        IMAPSClient client = new IMAPSClient(true);

        for (int i = 0; i < m_sentCount && retry; i++) {
            try {
                bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: ");
                client.setDefaultTimeout(m_timeout);

                start = HinemosTime.currentTimeMillis();
                client.connect(address, m_portNo);
                end = HinemosTime.currentTimeMillis();

                m_response = end - start;

                result = client.getReplyString();

                if (m_response > 0) {
                    if (m_response < m_timeout) {
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    } else {
                        m_response = m_timeout;
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    }
                } else {
                    result = result + ("\n" + "Response Time < 1ms");
                }

                retry = false;
                isReachable = true;

            } catch (SocketException e) {
                result = (e.getMessage() + "[SocketException]");
                retry = true;
                isReachable = false;
            } catch (IOException e) {
                result = (e.getMessage() + "[IOException]");
                retry = true;
                isReachable = false;
            } finally {
                bufferOrg.append(result + "\n");
                if (client.isConnected()) {
                    try {
                        client.disconnect();
                    } catch (IOException e) {
                        m_log.warn("isRunning(): " + "socket disconnect failed: " + e.getMessage(), e);
                    }
                }
            }

            if (i < m_sentCount - 1 && retry) {
                try {
                    Thread.sleep(m_sentInterval);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }

        m_message = result + "(IMAPS/" + m_portNo + ")";
        m_messageOrg = bufferOrg.toString();
        return isReachable;
    } catch (UnknownHostException e) {
        m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage()
                + e.getMessage());

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage()
                + ")";

        return false;
    }
}

From source file:com.clustercontrol.port.protocol.ReachAddressPOP3S.java

/**
 * POP3S????????/* w w  w . j av  a 2  s .co m*/
 * 
 * @param addressText
 * @return POP3S
 */
@Override
protected boolean isRunning(String addressText) {

    m_message = "";
    m_messageOrg = "";
    m_response = -1;

    boolean isReachable = false;

    try {
        long start = 0; // 
        long end = 0; // 
        boolean retry = true; // ????(true:??false:???)

        StringBuffer bufferOrg = new StringBuffer(); // 
        String result = "";

        InetAddress address = InetAddress.getByName(addressText);

        bufferOrg.append("Monitoring the POP3S Service of " + address.getHostName() + "["
                + address.getHostAddress() + "]:" + m_portNo + ".\n\n");

        POP3SClient client = new POP3SClient(true);

        for (int i = 0; i < m_sentCount && retry; i++) {
            try {
                bufferOrg.append(HinemosTime.getDateString() + " Tried to Connect: ");
                client.setDefaultTimeout(m_timeout);

                start = HinemosTime.currentTimeMillis();
                client.connect(address, m_portNo);
                end = HinemosTime.currentTimeMillis();

                m_response = end - start;

                result = client.getReplyString();

                if (m_response > 0) {
                    if (m_response < m_timeout) {
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    } else {
                        m_response = m_timeout;
                        result = result + ("\n" + "Response Time = " + m_response + "ms");
                    }
                } else {
                    result = result + ("\n" + "Response Time < 1ms");
                }

                retry = false;
                isReachable = true;

            } catch (SocketException e) {
                result = (e.getMessage() + "[SocketException]");
                retry = true;
                isReachable = false;
            } catch (IOException e) {
                result = (e.getMessage() + "[IOException]");
                retry = true;
                isReachable = false;
            } finally {
                bufferOrg.append(result + "\n");
                if (client.isConnected()) {
                    try {
                        client.disconnect();
                    } catch (IOException e) {
                        m_log.warn("isRunning(): " + "socket disconnect failed: " + e.getMessage(), e);
                    }
                }
            }

            if (i < m_sentCount - 1 && retry) {
                try {
                    Thread.sleep(m_sentInterval);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }

        m_message = result + "(POP3S/" + m_portNo + ")";
        m_messageOrg = bufferOrg.toString();
        return isReachable;
    } catch (UnknownHostException e) {
        m_log.debug("isRunning(): " + MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage()
                + e.getMessage());

        m_message = MessageConstant.MESSAGE_FAIL_TO_EXECUTE_TO_CONNECT.getMessage() + " (" + e.getMessage()
                + ")";

        return false;
    }
}