Example usage for java.net ConnectException getClass

List of usage examples for java.net ConnectException getClass

Introduction

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

Prototype

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

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.apache.sling.launchpad.LaunchpadReadyRule.java

private void runCheck(CloseableHttpClient client, Check check) throws Exception {

    String lastFailure = null;/*  www. j  a va  2  s. co m*/
    HttpGet get = new HttpGet(check.getUrl());

    for (int i = 0; i < TRIES; i++) {
        try (CloseableHttpResponse response = client.execute(get)) {

            if (response.getStatusLine().getStatusCode() != 200) {
                lastFailure = "Status code is " + response.getStatusLine();
                Thread.sleep(WAIT_BETWEEN_TRIES_MILLIS);
                continue;
            }

            lastFailure = check.runCheck(response);
            if (lastFailure == null) {
                return;
            }
        } catch (ConnectException e) {
            lastFailure = e.getClass().getName() + " : " + e.getMessage();
        }

        Thread.sleep(WAIT_BETWEEN_TRIES_MILLIS);
    }

    throw new RuntimeException(String.format("Launchpad not ready. Failed check for URL %s with message '%s'",
            check.getUrl(), lastFailure));
}

From source file:eu.fusepool.tests.BaseTest.java

@Before
public void waitForServerReady() throws Exception {
    log.debug("> before {}#waitForServerReady()", getClass().getSimpleName());
    if (serverReady) {
        log.debug(" ... server already marked as ready!");
        return;/*from  w w  w .  j a v  a 2s.c o m*/
    }

    // Timeout for readiness test
    final String sec = System.getProperty(SERVER_READY_TIMEOUT_PROP);
    final int timeoutSec = sec == null ? 60 : Integer.valueOf(sec);
    log.info("Will wait up to " + timeoutSec + " seconds for server to become ready");
    final long endTime = System.currentTimeMillis() + timeoutSec * 1000L;

    // Get the list of paths to test and expected content regexps
    final List<String> testPaths = new ArrayList<String>();
    final TreeSet<Object> propertyNames = new TreeSet<Object>();
    propertyNames.addAll(System.getProperties().keySet());
    for (Object o : propertyNames) {
        final String key = (String) o;
        if (key.startsWith(SERVER_READY_PROP_PREFIX)) {
            testPaths.add(System.getProperty(key));
        }
    }

    // Consider the server ready if it responds to a GET on each of 
    // our configured request paths with a 200 result and content
    // that matches the regexp supplied with the path
    long sleepTime = 100;
    readyLoop: while (!serverReady && System.currentTimeMillis() < endTime) {
        // Wait a bit between checks, to let the server come up
        Thread.sleep(sleepTime);
        sleepTime = Math.min(5000L, sleepTime * 2);

        // A test path is in the form path:substring or just path, in which case
        // we don't check that the content contains the substring 
        log.debug(" - check serverReady Paths");
        for (String p : testPaths) {
            log.debug("    > path: {}", p);
            final String[] s = p.split(":");
            final String path = s[0];
            final String substring = (s.length > 0 ? s[1] : null);
            final String url = serverBaseUrl + path;
            log.debug("    > url: {}", url);
            log.debug("    > content: {}", substring);
            final HttpGet get = new HttpGet(url);
            //authenticate as admin with password admin
            get.setHeader("Authorization", "Basic YWRtaW46YWRtaW4=");
            for (int i = 2; i + 1 < s.length; i = i + 2) {
                log.debug("    > header: {}:{}", s[i], s[i + 1]);
                if (s[i] != null && !s[i].isEmpty() && s[i + 1] != null && !s[i + 1].isEmpty()) {
                    get.setHeader(s[i], s[i + 1]);
                }
            }
            HttpEntity entity = null;
            try {
                log.debug("    > execute: {}", get);
                HttpResponse response = httpClient.execute(get);
                log.debug("    > response: {}", response);
                entity = response.getEntity();
                final int status = response.getStatusLine().getStatusCode();
                if (status != 200) {
                    log.info("Got {} at {} - will retry", status, url);
                    continue readyLoop;
                } else {
                    log.debug("Got {} at {} - will retry", status, url);
                }

                if (substring != null) {
                    if (entity == null) {
                        log.info("No entity returned for {} - will retry", url);
                        continue readyLoop;
                    }
                    final String content = EntityUtils.toString(entity);
                    final boolean checkAbsence = substring.startsWith("!");
                    final String notPresentString = substring.substring(1);
                    if ((!checkAbsence && content.contains(substring))
                            || (checkAbsence && content.contains(notPresentString))) {
                        log.debug("Returned content for {}  contains {} - ready", url, substring);
                    } else {
                        log.info("Returned content for {}  does not contain " + "{} - will retry", url,
                                substring);
                        continue readyLoop;

                    }
                }
            } catch (ConnectException e) {
                log.info("Got {} at {} - will retry", e.getClass().getSimpleName(), url);
                continue readyLoop;
            } finally {
                if (entity != null) {
                    entity.consumeContent();
                }
            }
        }
        log.info("Got expected content for all configured requests, server is ready");
        //Some additional wait time, as not everything can be tested with the paths
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        serverReady = true;
    }

    if (!serverReady) {
        throw new Exception("Server not ready after " + timeoutSec + " seconds");
    }
}