Example usage for java.net ConnectException getCause

List of usage examples for java.net ConnectException getCause

Introduction

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

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.fusesource.cloudmix.agent.AgentPoller.java

public void agentPoll() {
    try {/* w w  w .ja  v  a  2s .c o m*/
        agent.call();
    } catch (ConnectException e) {
        handleConnectException(e);
    } catch (Exception e) {
        if (e.getCause() instanceof ConnectException) {
            handleConnectException((ConnectException) e.getCause());
        } else {
            LOG.warn("Caught exception while polling Agent: ", e);
        }
    } catch (Error e) {
        LOG.warn("Error in poll", e);
        throw e;
    }
}

From source file:com.github.caldav4j.CalDAVCollection.java

/**
 * Return a list of caldav resources.// w w w  .j  av  a2 s  .c o  m
 * All other methods should use this one
 * 
 * The use of caching changes the behavior of this method.
 * if cache is not enable, returns a list of CalDAVResource parsed from the response
 * if cache is enabled, foreach   HREF returned by server:
 *   -  retrieve the resource using getCaldavReource(client, string), this method checks cache
 *
 * @param httpClient the httpClient which will make the request
 * @param query Query to get the CalDAV resources for
 * @return List of CalDAVResource's
 * @throws CalDAV4JException on error
 */
protected List<CalDAVResource> getCalDAVResources(HttpClient httpClient, CalendarQuery query)
        throws CalDAV4JException {
    boolean usingCache = isCacheEnabled();
    if (usingCache) {
        query.setCalendarDataProp(null);
        log.debug("Using cache, so I am removing calendar data");
    }
    log.trace("Executing query: " + GenerateQuery.printQuery(query));

    HttpCalDAVReportMethod reportMethod = null;

    List<CalDAVResource> list = new ArrayList<>();
    try {
        reportMethod = methodFactory.createCalDAVReportMethod(getCalendarCollectionRoot(), query,
                CalDAVConstants.DEPTH_1);
        HttpResponse httpResponse = httpClient.execute(getDefaultHttpHost(reportMethod.getURI()), reportMethod);

        log.trace("Parsing response.. ");

        MultiStatusResponse[] responses = reportMethod.getResponseBodyAsMultiStatus(httpResponse)
                .getResponses();
        for (MultiStatusResponse response : responses) {
            String etag = CalendarDataProperty.getEtagfromResponse(response);

            if (usingCache) {
                CalDAVResource resource = getCalDAVResource(httpClient, UrlUtils.stripHost(response.getHref()),
                        etag);
                list.add(resource);
                cache.putResource(resource);
            } else {
                if (response != null) {
                    list.add(new CalDAVResource(response));
                }
            }
        }

    } catch (ConnectException connEx) {
        throw new CalDAV4JException("Can't connecto to " + getDefaultHttpHost(reportMethod.getURI()),
                connEx.getCause());
    } catch (Exception he) {
        throw new CalDAV4JException("Problem executing method", he);
    } finally {
        if (reportMethod != null)
            reportMethod.reset();
    }

    return list;
}

From source file:org.apache.hadoop.hbase.catalog.CatalogTracker.java

/**
 * Verify we can connect to <code>hostingServer</code> and that its carrying
 * <code>regionName</code>.// w ww . java  2s .  c o m
 * @param hostingServer Interface to the server hosting <code>regionName</code>
 * @param address The servername that goes with the <code>metaServer</code>
 * Interface.  Used logging.
 * @param regionName The regionname we are interested in.
 * @return True if we were able to verify the region located at other side of
 * the Interface.
 * @throws IOException
 */
// TODO: We should be able to get the ServerName from the AdminProtocol
// rather than have to pass it in.  Its made awkward by the fact that the
// HRI is likely a proxy against remote server so the getServerName needs
// to be fixed to go to a local method or to a cache before we can do this.
private boolean verifyRegionLocation(AdminService.BlockingInterface hostingServer, final ServerName address,
        final byte[] regionName) throws IOException {
    if (hostingServer == null) {
        LOG.info("Passed hostingServer is null");
        return false;
    }
    Throwable t = null;
    try {
        // Try and get regioninfo from the hosting server.
        return ProtobufUtil.getRegionInfo(hostingServer, regionName) != null;
    } catch (ConnectException e) {
        t = e;
    } catch (RetriesExhaustedException e) {
        t = e;
    } catch (RemoteException e) {
        IOException ioe = e.unwrapRemoteException();
        t = ioe;
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause instanceof EOFException) {
            t = cause;
        } else if (cause != null && cause.getMessage() != null
                && cause.getMessage().contains("Connection reset")) {
            t = cause;
        } else {
            t = e;
        }
    }
    LOG.info("Failed verification of " + Bytes.toStringBinary(regionName) + " at address=" + address
            + ", exception=" + t);
    return false;
}

From source file:org.apache.hadoop.hbase.zookeeper.MetaTableLocator.java

/**
 * Verify we can connect to <code>hostingServer</code> and that its carrying
 * <code>regionName</code>.//  w w  w . ja va 2s  .c o m
 * @param hostingServer Interface to the server hosting <code>regionName</code>
 * @param address The servername that goes with the <code>metaServer</code>
 * Interface.  Used logging.
 * @param regionName The regionname we are interested in.
 * @return True if we were able to verify the region located at other side of
 * the Interface.
 * @throws IOException
 */
// TODO: We should be able to get the ServerName from the AdminProtocol
// rather than have to pass it in.  Its made awkward by the fact that the
// HRI is likely a proxy against remote server so the getServerName needs
// to be fixed to go to a local method or to a cache before we can do this.
private boolean verifyRegionLocation(AdminService.BlockingInterface hostingServer, final ServerName address,
        final byte[] regionName) throws IOException {
    if (hostingServer == null) {
        LOG.info("Passed hostingServer is null");
        return false;
    }
    Throwable t;
    try {
        // Try and get regioninfo from the hosting server.
        return ProtobufUtil.getRegionInfo(hostingServer, regionName) != null;
    } catch (ConnectException e) {
        t = e;
    } catch (RetriesExhaustedException e) {
        t = e;
    } catch (RemoteException e) {
        IOException ioe = e.unwrapRemoteException();
        t = ioe;
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause instanceof EOFException) {
            t = cause;
        } else if (cause != null && cause.getMessage() != null
                && cause.getMessage().contains("Connection reset")) {
            t = cause;
        } else {
            t = e;
        }
    }
    LOG.info("Failed verification of " + Bytes.toStringBinary(regionName) + " at address=" + address
            + ", exception=" + t.getMessage());
    return false;
}

From source file:org.codehaus.mojo.cassandra.Utils.java

/**
 * Stops the Cassandra service.//from   www .  j  av  a2s .c  om
 *
 * @param rpcAddress The rpcAddress to connect to in order to see if Cassandra has stopped.
 * @param rpcPort    The rpcPort to connect on to check if Cassandra has stopped.
 * @param stopPort   The port to stop on.
 * @param stopKey    The key to stop with,
 * @param log        The log to write to.
 */
static void stopCassandraServer(String rpcAddress, int rpcPort, String stopAddress, int stopPort,
        String stopKey, Log log) {
    try {
        Socket s = new Socket(InetAddress.getByName(stopAddress), stopPort);
        s.setSoLinger(false, 0);

        OutputStream out = s.getOutputStream();
        out.write((stopKey + "\r\nstop\r\n").getBytes());
        out.flush();
        s.close();
    } catch (ConnectException e) {
        log.info("Cassandra not running!");
        return;
    } catch (Exception e) {
        log.error(e);
        return;
    }
    log.info("Waiting for Cassandra to stop...");
    long maxWaiting = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(30);
    boolean stopped = false;
    while (!stopped && System.currentTimeMillis() < maxWaiting) {
        TTransport tr = new TFramedTransport(new TSocket(rpcAddress, rpcPort));
        try {
            TProtocol proto = new TBinaryProtocol(tr);
            Cassandra.Client client = new Cassandra.Client(proto);
            try {
                tr.open();
            } catch (TTransportException e) {
                if (e.getCause() instanceof ConnectException) {
                    stopped = true;
                    continue;
                }
                log.debug(e.getLocalizedMessage(), e);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    // ignore
                }
            }
        } finally {
            if (tr.isOpen()) {
                tr.close();
            }
        }
    }
    if (stopped) {
        log.info("Cassandra has stopped.");
    } else {
        log.warn("Gave up waiting for Cassandra to stop.");
    }
}

From source file:org.osaf.caldav4j.CalDAVCollection.java

/**
 * return a list of caldav resources. /* ww  w . ja v  a2  s  .c o  m*/
 * All other methods should use this one
 * 
 * The use of caching changes the behavior of this method.
 * if cache is not enable, returns a list of CalDAVResource parsed from the response
 * if cache is enabled, foreach   HREF returned by server:
 *   -  retrieve the resource using getCaldavReource(client, string), this method checks cache
 *   -  
 * @param httpClient
 * @param componentName
 * @param query
 * @return
 * @throws CalDAV4JException
 */
protected List<CalDAVResource> getCalDAVResources(HttpClient httpClient, CalendarQuery query)
        throws CalDAV4JException {
    boolean usingCache = isCacheEnabled();
    if (usingCache) {
        query.setCalendarDataProp(null);
        log.debug("Using cache, so I am removing calendar data");
    }
    log.trace("Executing query: " + GenerateQuery.printQuery(query));

    CalDAVReportMethod reportMethod = methodFactory.createCalDAVReportMethod();
    reportMethod.setPath(getCalendarCollectionRoot());
    reportMethod.setReportRequest(query);
    try {
        httpClient.executeMethod(getHostConfiguration(), reportMethod);
    } catch (ConnectException connEx) {
        // TODO getHostURL is synchronized
        throw new CalDAV4JException("Can't connecto to " + getHostConfiguration().getHostURL(),
                connEx.getCause());
    } catch (Exception he) {
        throw new CalDAV4JException("Problem executing method", he);
    }

    log.trace("Parsing response.. ");
    Enumeration<CalDAVResponse> responseEnum = reportMethod.getResponses();
    List<CalDAVResource> list = new ArrayList<CalDAVResource>();
    while (responseEnum.hasMoreElements()) {
        try {
            CalDAVResponse response = responseEnum.nextElement();
            String etag = response.getETag();

            if (usingCache) {
                CalDAVResource resource = getCalDAVResource(httpClient, stripHost(response.getHref()), etag);
                list.add(resource);
                cache.putResource(resource);
                /* dead code, to be reenabled in case of tombstones
                //  avoid parsing object if not required
                if (isSkipGoogleTombstones()) {
                   list.add(resource);
                   cache.putResource(resource);
                } else if (! isGoogleTombstone(resource.getCalendar())){
                   list.add(resource);
                   cache.putResource(resource);
                }
                */
            } else {
                if (response != null) {
                    list.add(new CalDAVResource(response));
                }
            }
        } catch (Exception e) {
            log.error("Exception while retrieving objects:" + e.getMessage(), e);
        }
    }

    return list;
}