Example usage for java.net InetAddress getByName

List of usage examples for java.net InetAddress getByName

Introduction

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

Prototype

public static InetAddress getByName(String host) throws UnknownHostException 

Source Link

Document

Determines the IP address of a host, given the host's name.

Usage

From source file:net.fenyo.gnetwatch.GenericTools.java

/**
 * Converts a string to an Inet6Address.
 * @param str source string.//w  w w .  j  a v  a 2s  .  c om
 * @return Inet6Address address representing this string.
 */
static public Inet6Address stringToInet6Address(final String str) throws UnknownHostException {
    try {
        return (Inet6Address) InetAddress.getByName(str);
    } catch (final ClassCastException ex) {
        return null;
    }
}

From source file:hk.mcc.utils.applog2es.Main.java

private static void post2ES(List<AppLog> appLogs) {
    try {// ww  w .  ja va 2s  .  com
        // on startup
        DateTimeFormatter sdf = ISODateTimeFormat.dateTime();
        Settings settings = Settings.settingsBuilder().put("cluster.name", "my-application").build();
        //Add transport addresses and do something with the client...
        Client client = TransportClient.builder().settings(settings).build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        XContentBuilder mapping = jsonBuilder().startObject().startObject("applog").startObject("properties")
                .startObject("Url").field("type", "string").field("index", "not_analyzed").endObject()
                .startObject("Event").field("type", "string").field("index", "not_analyzed").endObject()
                .startObject("ClassName").field("type", "string").field("index", "not_analyzed").endObject()
                .startObject("UserId").field("type", "string").field("index", "not_analyzed").endObject()
                .startObject("Application").field("type", "string").field("index", "not_analyzed").endObject()
                .startObject("ecid").field("type", "string").field("index", "not_analyzed").endObject()
                .endObject().endObject().endObject();

        PutMappingResponse putMappingResponse = client.admin().indices().preparePutMapping("applog")
                .setType("applog").setSource(mapping).execute().actionGet();
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        for (AppLog appLog : appLogs) {
            // either use client#prepare, or use Requests# to directly build index/delete requests
            if (appLog != null) {
                if (StringUtils.contains(appLog.getMessage(), "[CIMS_INFO] Filter Processing time")) {
                    String[] split = StringUtils.split(appLog.getMessage(), ",");
                    int elapsedTime = 0;
                    String url = "";
                    String event = "";
                    for (String token : split) {
                        if (StringUtils.contains(token, "elapsedTime")) {
                            elapsedTime = Integer.parseInt(StringUtils.substringAfter(token, "="));
                        } else if (StringUtils.contains(token, "with URL")) {
                            url = StringUtils.substringAfter(token, "=");
                        } else if (StringUtils.contains(token, "event")) {
                            event = StringUtils.substringAfter(token, "=");
                        }
                    }

                    bulkRequest.add(client.prepareIndex("applog", "applog").setSource(jsonBuilder()
                            .startObject().field("className", appLog.getClassName())
                            .field("logTime", appLog.getLogTime()).field("application", appLog.getApplication())
                            .field("code", appLog.getCode()).field("message", appLog.getMessage())
                            .field("ecid", appLog.getEcid()).field("application", appLog.getApplication())
                            .field("level", appLog.getLevel()).field("server", appLog.getServer())
                            .field("tid", appLog.getTid()).field("userId", appLog.getUserId())
                            .field("urls", url).field("elapsedTime", elapsedTime).field("events", event)
                            .endObject()));
                }
            }
        }
        BulkResponse bulkResponse = bulkRequest.get();
        if (bulkResponse.hasFailures()) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, bulkResponse.buildFailureMessage());
            // process failures by iterating through each bulk response item
        }

        // on shutdown
        client.close();
    } catch (UnknownHostException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:edu.cmu.sei.ams.cloudlet.impl.CloudletUtilities.java

static InetAddress getSafeInetAddress(String name, JSONObject json) {
    String value = getSafeString(name, json);
    if (value == null)
        return null;
    try {//w ww  .j  a  v a 2  s . c om
        return InetAddress.getByName(value);
    } catch (Exception e) {
        return null;
    }
}

From source file:ru.jts_dev.gameserver.service.AuthServerService.java

/**
 * send game server status to auth server
 *///from ww  w .  jav a 2  s .c o m
@PostConstruct
public void register() throws UnknownHostException {
    byte serverId = gameServerConfig.getServerId();
    String host = gameServerConfig.getHost();
    int port = gameServerConfig.getPort();
    jmsTemplate.convertAndSend("gameServersQueue",
            new GameServerInfo(serverId, InetAddress.getByName(host), port));
}

From source file:net.darkmist.clf.LogEntryTest.java

@Override
protected void setUp() throws Exception {
    ip_a = InetAddress.getByName("1.1.1.1");
    ip_a_dup = InetAddress.getByName("1.1.1.1");
    ip_b = InetAddress.getByName("2.2.2.2");

    date_a = mkStaticDate();//from   w  w w  .  j  a  v a2 s .  c om
    date_a_dup = mkStaticDate();
    date_b = new Date();

    a = new LogEntry(ip_a, "a_ident", "a_usr", date_a, "a_method", "a_uri", "a_proto", 1, 2);
    a_dup = new LogEntry(ip_a_dup, "a_ident", "a_usr", date_a_dup, "a_method", "a_uri", "a_proto", 1, 2);
    b = new LogEntry(ip_b, "b_ident", "b_usr", date_b, "b_method", "b_uri", "b_proto", 3, 4);
}

From source file:ca.viaware.dlna.ssdp.SSDPService.java

public SSDPService() {
    ssdpConfig = SettingsManager.getServerConfig().getJSONObject("ssdp");
    httpConfig = SettingsManager.getServerConfig().getJSONObject("http");

    try {/* ww  w .  ja  v  a2  s .  c  om*/
        outSock = new MulticastSocket(ssdpConfig.getInt("port"));
        outSock.setNetworkInterface(
                NetworkInterface.getByInetAddress(InetAddress.getByName(ssdpConfig.getString("host"))));
        Log.info("SSDP: Outbound UDP bound to " + this.outSock.getLocalAddress());
    } catch (IOException e) {
        e.printStackTrace();
    }

    this.baseAnnounce = "";
    this.baseAnnounce += "HOST: 239.255.255.250:1900\r\n";
    this.baseAnnounce += "CACHE-CONTROL: max-age=1800\r\n";
    this.baseAnnounce += "NTS: ssdp:alive\r\n";
    this.baseAnnounce += "SERVER:" + Globals.SERVER + "\r\n";
}

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

/**
 * IMAP????????//from w w w . j ava  2  s. c  o  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????????//  w  w  w  .jav  a 2s.c  o  m
 * 
 * @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.ReachAddressIMAPS.java

/**
 * IMAPS????????/*from   ww  w.  j  av a  2 s  .com*/
 * 
 * @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????????//  ww  w . j av  a 2  s. c o  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;
    }
}