Example usage for java.net NoRouteToHostException getClass

List of usage examples for java.net NoRouteToHostException getClass

Introduction

In this page you can find the example usage for java.net NoRouteToHostException getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.opennms.netmgt.poller.monitors.HttpMonitor.java

/**
 * {@inheritDoc}/*from w w w . ja v  a2 s  .  c  om*/
 *
 * Poll the specified address for HTTP service availability.
 *
 * During the poll an attempt is made to connect on the specified port(s) (by default TCP
 * ports 80, 8080, 8888). If the connection request is successful, an HTTP 'GET' command is
 * sent to the interface. The response is parsed and a return code extracted and verified.
 * Provided that the interface's response is valid we set the service status to
 * SERVICE_AVAILABLE and return.
 */
@Override
public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) {
    final NetworkInterface<InetAddress> iface = svc.getNetInterface();
    final String nodeLabel = svc.getNodeLabel();

    if (iface.getType() != NetworkInterface.TYPE_INET) {
        throw new NetworkInterfaceNotSupportedException(
                "Unsupported interface type, only TYPE_INET currently supported");
    }

    // Cycle through the port list
    //
    int currentPort = -1;
    final HttpMonitorClient httpClient = new HttpMonitorClient(nodeLabel, iface,
            new TreeMap<String, Object>(parameters));

    for (int portIndex = 0; portIndex < determinePorts(httpClient.getParameters()).length
            && httpClient.getPollStatus() != PollStatus.SERVICE_AVAILABLE; portIndex++) {
        currentPort = determinePorts(httpClient.getParameters())[portIndex];

        httpClient.setTimeoutTracker(new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT));
        LOG.debug("Port = {}, Address = {}, {}", currentPort, (iface.getAddress()),
                httpClient.getTimeoutTracker());

        httpClient.setCurrentPort(currentPort);
        String serviceInfo = new StringBuilder(iface.getAddress().toString()).append(":")
                .append(svc.getSvcName()).append(":").append(currentPort).toString();

        for (httpClient.getTimeoutTracker().reset(); httpClient.getTimeoutTracker().shouldRetry()
                && httpClient.getPollStatus() != PollStatus.SERVICE_AVAILABLE; httpClient.getTimeoutTracker()
                        .nextAttempt()) {

            try {
                httpClient.getTimeoutTracker().startAttempt();
                httpClient.connect();
                LOG.debug("HttpMonitor: connected to host: {} on port: {}", (iface.getAddress()), currentPort);

                httpClient.sendHttpCommand();

                if (httpClient.isEndOfStream()) {
                    continue;
                }

                httpClient.setResponseTime(httpClient.getTimeoutTracker().elapsedTimeInMillis());
                logResponseTimes(httpClient.getResponseTime(), httpClient.getCurrentLine());

                if (httpClient.getPollStatus() == PollStatus.SERVICE_AVAILABLE
                        && StringUtils.isNotBlank(httpClient.getResponseText())) {
                    httpClient.setPollStatus(PollStatus.SERVICE_UNAVAILABLE);
                    httpClient.readLinedMatching();

                    if (httpClient.isEndOfStream()) {
                        continue;
                    }

                    httpClient.read();

                    if (!httpClient.isResponseTextFound()) {
                        String message = "Matching text: [" + httpClient.getResponseText()
                                + "] not found in body of HTTP response for " + serviceInfo;
                        LOG.debug(message);
                        httpClient.setReason("Matching text: [" + httpClient.getResponseText()
                                + "] not found in body of HTTP response");
                    }
                }

            } catch (NoRouteToHostException e) {
                LOG.warn("checkStatus: No route to host exception while polling {}", serviceInfo, e);
                portIndex = determinePorts(httpClient.getParameters()).length; // Will cause outer for(;;) to terminate
                httpClient.setReason("No route to host exception");
            } catch (SocketTimeoutException e) {
                LOG.info("checkStatus: HTTP socket connection for service {} timed out with {}", serviceInfo,
                        httpClient.getTimeoutTracker().toString());
                httpClient.setReason("HTTP connection timeout");
            } catch (InterruptedIOException e) {
                LOG.info(String.format(
                        "checkStatus: HTTP connection for service {} interrupted after {} bytes transferred with {}",
                        serviceInfo, e.bytesTransferred, httpClient.getTimeoutTracker().toString()), e);
                httpClient.setReason(
                        String.format("HTTP connection interrupted, %d bytes transferred", e.bytesTransferred));
            } catch (ConnectException e) {
                LOG.warn("Connection exception for {}", serviceInfo, e);
                httpClient.setReason("HTTP connection exception on port: "
                        + determinePorts(httpClient.getParameters())[portIndex] + ": " + e.getMessage());
            } catch (IOException e) {
                String exceptionClass = e.getClass().getSimpleName();
                LOG.warn("{} while polling {}", exceptionClass, serviceInfo, e);
                httpClient.setReason(
                        "IOException while polling address: " + (iface.getAddress()) + ": " + e.getMessage());
            } catch (Throwable e) {
                String exceptionClass = e.getClass().getSimpleName();
                LOG.warn("Unexpected {} while polling {}", exceptionClass, serviceInfo, e);
                httpClient.setReason("Unexpected exception while polling address: " + (iface.getAddress())
                        + ": " + e.getMessage());
            } finally {
                httpClient.closeConnection();
            }

        } // end for (attempts)
    } // end for (ports)
    return httpClient.determinePollStatusResponse();

}