Example usage for java.net ConnectException initCause

List of usage examples for java.net ConnectException initCause

Introduction

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

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:com.github.ignition.support.http.IgnitedHttpRequestBase.java

@Override
public IgnitedHttpResponse send() throws ConnectException {

    IgnitedHttpRequestRetryHandler retryHandler = new IgnitedHttpRequestRetryHandler(maxRetries);

    // tell HttpClient to user our own retry handler
    httpClient.setHttpRequestRetryHandler(retryHandler);

    HttpContext context = new BasicHttpContext();

    // Grab a coffee now and lean back, I'm not good at explaining stuff. This code realizes
    // a second retry layer on top of HttpClient. Rationale: HttpClient.execute sometimes craps
    // out even *before* the HttpRequestRetryHandler set above is called, e.g. on a
    // "Network unreachable" SocketException, which can happen when failing over from Wi-Fi to
    // 3G or vice versa. Hence, we catch these exceptions, feed it through the same retry
    // decision method *again*, and align the execution count along the way.
    boolean retry = true;
    IOException cause = null;// w  ww  . j  a v a 2s . co  m
    while (retry) {
        try {
            return httpClient.execute(request, this, context);
        } catch (IOException e) {
            cause = e;
            retry = retryRequest(retryHandler, cause, context);
        } catch (NullPointerException e) {
            // there's a bug in HttpClient 4.0.x that on some occasions causes
            // DefaultRequestExecutor to throw an NPE, see
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryRequest(retryHandler, cause, context);
        } finally {
            // if timeout was changed with this request using withTimeout(), reset it
            if (timeoutChanged) {
                ignitedHttp.setConnectionTimeout(oldConnTimeout);
                ignitedHttp.setSocketTimeout(oldSocketTimeout);
            }
        }
    }

    // no retries left, crap out with exception
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:net.tsz.afinal.http.AjaxRequestHandler.java

private void makeRequestWithRetries(HttpUriRequest request) throws ConnectException {

    boolean retry = true;
    IOException cause = null;/*  w  ww. j a v a  2 s .  co  m*/
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (retry) {
        try {
            if (!isCancelled()) {
                HttpResponse response = client.execute(request, context);
                if (!isCancelled()) {
                    handleResponse(response);
                }
            }
            return;
        } catch (UnknownHostException e) {
            publishProgress(Update_failure, e, "can't resolve host");
            return;
        } catch (IOException e) {
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (NullPointerException e) {
            // HttpClient 4.0.x ?bug
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        }
    }

    //??
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:cn.edu.szjm.support.http.IgnitedHttpRequestBase.java

public Object send() throws ConnectException {

    IgnitedHttpRequestRetryHandler retryHandler = new IgnitedHttpRequestRetryHandler(maxRetries);

    // tell HttpClient to user our own retry handler
    httpClient.setHttpRequestRetryHandler(retryHandler);

    HttpContext context = new BasicHttpContext();

    // Grab a coffee now and lean back, I'm not good at explaining stuff. This code realizes
    // a second retry layer on top of HttpClient. Rationale: HttpClient.execute sometimes craps
    // out even *before* the HttpRequestRetryHandler set above is called, e.g. on a
    // "Network unreachable" SocketException, which can happen when failing over from Wi-Fi to
    // 3G or vice versa. Hence, we catch these exceptions, feed it through the same retry
    // decision method *again*, and align the execution count along the way.
    boolean retry = true;
    IOException cause = null;/*from w w w  . j av  a2 s  .  c  o  m*/
    while (retry) {
        try {
            if (wrapResponse) {
                return httpClient.execute(request, this, context);
            } else {
                return httpClient.execute(request, context);
            }
        } catch (IOException e) {
            cause = e;
            retry = retryRequest(retryHandler, cause, context);
        } catch (NullPointerException e) {
            // there's a bug in HttpClient 4.0.x that on some occasions causes
            // DefaultRequestExecutor to throw an NPE, see
            // http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryRequest(retryHandler, cause, context);
        } finally {
            // if timeout was changed with this request using withTimeout(), reset it
            if (timeoutChanged) {
                ignitedHttp.setConnectionTimeout(oldConnTimeout);
                ignitedHttp.setSocketTimeout(oldSocketTimeout);
            }
        }
    }

    // no retries left, crap out with exception
    ConnectException ex = new ConnectException();
    ex.initCause(cause);
    throw ex;
}

From source file:com.hellblazer.process.impl.JavaProcessImpl.java

@Override
public MBeanServerConnection getLocalMBeanServerConnection(String connectionName)
        throws ConnectException, NoLocalJmxConnectionException {
    JMXConnector connector = getLocalJmxConnector(connectionName);

    try {/*from w  ww .ja  va 2 s.  c  o m*/
        return connector.getMBeanServerConnection();
    } catch (IOException e) {
        ConnectException cex = new ConnectException("Cannot establish local JMX connection to: " + this);
        cex.initCause(e);
        throw cex;
    }
}

From source file:com.hellblazer.process.impl.JavaProcessImpl.java

@Override
public MBeanServerConnection getLocalMBeanServerConnection(String connectionName, Subject delegationSubject)
        throws ConnectException, NoLocalJmxConnectionException {
    JMXConnector connector = getLocalJmxConnector(connectionName);

    try {//www . j  a v a 2 s.  co  m
        return connector.getMBeanServerConnection(delegationSubject);
    } catch (IOException e) {
        ConnectException cex = new ConnectException("Cannot establish local JMX connection to: " + this);
        cex.initCause(e);
        throw cex;
    }
}

From source file:com.hellblazer.process.impl.JavaProcessImpl.java

/**
 * @throws ConnectException//from  www  .  j a  v a2  s.c o m
 */
@Override
public JMXConnector getLocalJmxConnector(String connectorName)
        throws ConnectException, NoLocalJmxConnectionException {
    if (jmxc != null) {
        return jmxc;
    }

    if (!process.isActive()) {
        throw new ConnectException("Cannot establish local JMX connection as process is not active: " + this);
    }

    String address;
    try {
        VirtualMachine vm = VirtualMachine.attach("" + process.getPid());
        Properties props = vm.getSystemProperties();
        address = props.getProperty(connectorName);

        if (address == null) {
            throw new ConnectException(
                    "Unable to find address for remote JMX connection with name = " + connectorName);
        }
    } catch (IOException e) {
        ConnectException cex = new ConnectException("Cannot obtain local JMX connector address of: " + this);
        cex.initCause(e);
        throw cex;
    } catch (AttachNotSupportedException e) {
        throw new RuntimeException(e);
    }

    JMXServiceURL jmxUrl;
    try {
        jmxUrl = new JMXServiceURL(address);
    } catch (MalformedURLException e) {
        ConnectException cex = new ConnectException("Invalid local JMX URL for " + this + " : " + address);
        cex.initCause(e);
        throw cex;
    }

    try {
        jmxc = JMXConnectorFactory.connect(jmxUrl);
    } catch (java.rmi.ConnectException e) {
        if (e.getMessage().startsWith("Connection refused")) {
            throw new NoLocalJmxConnectionException("Local JMX connector address does not exist for: " + this);
        }
        ConnectException cex = new ConnectException("Underlying RMI communications exception");
        cex.initCause(e);
        throw cex;
    } catch (IOException e) {
        ConnectException cex = new ConnectException("Cannot establish local JMX connection to: " + this);
        cex.initCause(e);
        throw cex;
    }

    try {
        jmxc.connect();
    } catch (IOException e) {
        ConnectException cex = new ConnectException("Cannot establish local JMX connection to: " + this);
        cex.initCause(e);
        throw cex;
    }

    return jmxc;
}

From source file:org.apache.camel.component.netty.NettyProducer.java

protected Channel openChannel(ChannelFuture channelFuture) throws Exception {
    // blocking for channel to be done
    if (LOG.isTraceEnabled()) {
        LOG.trace("Waiting for operation to complete {} for {} millis", channelFuture,
                configuration.getConnectTimeout());
    }//  w ww.j  a va2  s  . c  o  m
    // here we need to wait it in other thread
    final CountDownLatch channelLatch = new CountDownLatch(1);
    channelFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture cf) throws Exception {
            channelLatch.countDown();
        }
    });

    try {
        channelLatch.await(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        throw new CamelException(
                "Interrupted while waiting for " + "connection to " + configuration.getAddress());
    }

    if (!channelFuture.isDone() || !channelFuture.isSuccess()) {
        ConnectException cause = new ConnectException("Cannot connect to " + configuration.getAddress());
        if (channelFuture.getCause() != null) {
            cause.initCause(channelFuture.getCause());
        }
        throw cause;
    }
    Channel answer = channelFuture.getChannel();
    // to keep track of all channels in use
    ALL_CHANNELS.add(answer);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating connector to address: {}", configuration.getAddress());
    }
    return answer;
}

From source file:org.apache.camel.component.netty4.NettyProducer.java

protected Channel openChannel(ChannelFuture channelFuture) throws Exception {
    // blocking for channel to be done
    if (LOG.isTraceEnabled()) {
        LOG.trace("Waiting for operation to complete {} for {} millis", channelFuture,
                configuration.getConnectTimeout());
    }// ww  w .  ja  v a2 s .  c o m
    // here we need to wait it in other thread
    final CountDownLatch channelLatch = new CountDownLatch(1);
    channelFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture cf) throws Exception {
            channelLatch.countDown();
        }
    });

    try {
        channelLatch.await(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        throw new CamelException(
                "Interrupted while waiting for " + "connection to " + configuration.getAddress());
    }

    if (!channelFuture.isDone() || !channelFuture.isSuccess()) {
        ConnectException cause = new ConnectException("Cannot connect to " + configuration.getAddress());
        if (channelFuture.cause() != null) {
            cause.initCause(channelFuture.cause());
        }
        throw cause;
    }
    Channel answer = channelFuture.channel();
    // to keep track of all channels in use
    allChannels.add(answer);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating connector to address: {}", configuration.getAddress());
    }
    return answer;
}