Example usage for org.apache.commons.lang.exception ExceptionUtils indexOfType

List of usage examples for org.apache.commons.lang.exception ExceptionUtils indexOfType

Introduction

In this page you can find the example usage for org.apache.commons.lang.exception ExceptionUtils indexOfType.

Prototype

public static int indexOfType(Throwable throwable, Class type) 

Source Link

Document

Returns the (zero based) index of the first Throwable that matches the specified class or subclass in the exception chain.

Usage

From source file:com.dianping.lion.util.DBUtils.java

/**
 * warn: spring delegate sqlmapclient?/*from  w  w w  .  j  a  v a 2s  .  c o m*/
 * @param throwable
 * @return
 */
public static boolean isDuplicateKeyError(Throwable throwable) {
    if (throwable == null) {
        return false;
    }
    return ExceptionUtils.indexOfType(throwable, DataIntegrityViolationException.class) != -1
            && ExceptionUtils.getRootCauseMessage(throwable).contains("Duplicate entry");
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopTimer.java

protected void handleTimerException(RuntimeException ex) {
    if (ExceptionUtils.indexOfType(ex, java.net.ConnectException.class) > -1) {
        // If a ConnectException occurred, just log it and ignore
        log.warn("onTimer error: " + ex.getMessage());
    } else {/*from   w w w  .ja  va2s  . c  o  m*/
        // Otherwise throw the exception, but first search for NoUserSessionException in chain,
        // if found - stop the timer
        int reIdx = ExceptionUtils.indexOfType(ex, RemoteException.class);
        if (reIdx > -1) {
            RemoteException re = (RemoteException) ExceptionUtils.getThrowableList(ex).get(reIdx);
            for (RemoteException.Cause cause : re.getCauses()) {
                //noinspection ThrowableResultOfMethodCallIgnored
                if (cause.getThrowable() instanceof NoUserSessionException) {
                    log.warn("NoUserSessionException in timer, timer will be stopped");
                    disposeTimer();
                    break;
                }
            }
        } else if (ExceptionUtils.indexOfThrowable(ex, NoUserSessionException.class) > -1) {
            log.warn("NoUserSessionException in timer, timer will be stopped");
            disposeTimer();
        }

        throw ex;
    }
}

From source file:mitm.djigzo.web.common.RedirectRequestExceptionHandler.java

@SuppressWarnings("unchecked")
@Override//from  ww w  . j  a va  2  s .  com
public void handleRequestException(Throwable t) throws IOException {
    boolean handled = false;

    /*
     * Check for specified exception
     */
    int exceptionIndex = ExceptionUtils.indexOfType(t, exceptionClass);

    if (exceptionIndex != -1) {
        /*
         * Check if the exception is caused by authentication failure.
         */
        T exception = (T) ExceptionUtils.getThrowables(t)[exceptionIndex];

        if (isMatch(exception)) {
            logger.warn("Exception " + exceptionClass + " handled. Redirecting to page: " + redirectToPage);

            Link pageLink = linkFactory.createPageRenderLink(redirectToPage, false, context);

            response.sendRedirect(pageLink.toRedirectURI());

            handled = true;
        }
    }

    if (!handled) {
        /*
         * Pass the exception to the next handler
         */
        ((RequestExceptionHandler) delegate).handleRequestException(t);
    }
}

From source file:com.granita.icloudcalsync.webdav.TlsSniSocketFactoryTest.java

public void testHostnameNotInCertificate() throws IOException {
    try {/*www  . ja  v a2 s  .  c o m*/
        // host with certificate that doesn't match host name
        // use the IP address as host name because IP addresses are usually not in the certificate subject
        final String ipHostname = sampleTlsEndpoint.getAddress().getHostAddress();
        InetSocketAddress host = new InetSocketAddress(ipHostname, 443);
        @Cleanup
        Socket socket = factory.connectSocket(0, null, new HttpHost(ipHostname), host, null, null);
        fail();
    } catch (SSLException e) {
        Log.i(TAG, "Expected exception", e);
        assertFalse(ExceptionUtils.indexOfType(e, SSLException.class) == -1);
    }
}

From source file:com.haulmont.cuba.web.toolkit.ui.CubaTimer.java

protected void handleOnTimerException(RuntimeException e) {
    int reIdx = ExceptionUtils.indexOfType(e, RemoteException.class);
    if (reIdx > -1) {
        RemoteException re = (RemoteException) ExceptionUtils.getThrowableList(e).get(reIdx);
        for (RemoteException.Cause cause : re.getCauses()) {
            //noinspection ThrowableResultOfMethodCallIgnored
            if (cause.getThrowable() instanceof NoUserSessionException) {
                log.warn("NoUserSessionException in timer {}, timer will be stopped", getLoggingTimerId());
                stop();/*ww w  . j a va 2s .  c o m*/
                break;
            }
        }
    } else if (ExceptionUtils.indexOfThrowable(e, NoUserSessionException.class) > -1) {
        log.warn("NoUserSessionException in timer {}, timer will be stopped", getLoggingTimerId());
        stop();
    }

    throw e;
}

From source file:com.granita.icloudcalsync.webdav.TlsSniSocketFactoryTest.java

public void testUntrustedCertificate() throws IOException {
    try {//  w w w. j a va 2s .com
        // host with certificate that is not trusted by default
        InetSocketAddress host = new InetSocketAddress("cacert.org", 443);

        @Cleanup
        Socket socket = factory.connectSocket(0, null, new HttpHost(host.getHostName()), host, null, null);
        fail();
    } catch (SSLHandshakeException e) {
        Log.i(TAG, "Expected exception", e);
        assertFalse(ExceptionUtils.indexOfType(e, CertPathValidatorException.class) == -1);
    }
}

From source file:org.cleverbus.component.externalcall.ExternalCallComponentTest.java

private boolean sendAndVerifyBatch(Message[] messages) throws Exception {
    boolean lockFailureEncountered = false;
    HashMap<Message, Future<String>> replies = new HashMap<Message, Future<String>>();
    // send messages that have no reply, resend messages that have LockFailureException instead of a reply
    // verify results and re-send failures - test has timeout set because this is potentially endless
    Queue<Message> unverifiedMessages = new LinkedList<Message>(Arrays.asList(messages));
    while (!unverifiedMessages.isEmpty()) {
        Message message = unverifiedMessages.poll();
        boolean replyAvailable = replies.containsKey(message);
        if (replyAvailable) {
            Future<String> reply = replies.get(message);
            try {
                reply.get(); // this will throw an exception if it occurred during processing
            } catch (Exception exc) {
                if (ExceptionUtils.indexOfType(exc, LockFailureException.class) != -1) {
                    // expected cause - this test verifies that this scenario happens and is handled properly
                    lockFailureEncountered = true;
                    replyAvailable = false; // mark reply unavailable to resend the original message
                } else {
                    // fail by rethrowing
                    Log.error("Unexpected failure for message {} --", message, exc);
                    throw exc;
                }/*ww  w .j a  v  a2s . co m*/
            }
        }
        if (!replyAvailable) {
            unverifiedMessages.add(message); // mark message as still unverified
            replies.put(message, requestViaExternalCallAsync(message, "mock:test", "concurrentKey",
                    "external call original body"));
        }
    }
    // check the call is now in DB as OK and with the correct LAST msg timestamp
    assertExtCallStateInDB(extCallId, ExternalCallStateEnum.OK, messages[messages.length - 1]);
    return lockFailureEncountered;
}

From source file:org.jumpmind.symmetric.service.impl.AbstractService.java

protected boolean isStreamClosedByClient(Exception ex) {
    if (ExceptionUtils.indexOfType(ex, EOFException.class) >= 0) {
        return true;
    } else {/*from   w ww  .  ja v a2s . c  om*/
        return false;
    }
}

From source file:org.kuali.rice.core.framework.persistence.ojb.DataAccessUtils.java

public static synchronized boolean isOptimisticLockFailure(Throwable exception) {
    if (exception == null) {
        return false;
    }/*  w w  w  .j av  a  2s . c  om*/
    for (final Class<?> exceptionClass : getOptimisticLockExceptionClasses()) {
        if (ExceptionUtils.indexOfType(exception, exceptionClass) >= 0) {
            return true;
        }
    }
    return false;
}

From source file:org.openmrs.module.hl7query.util.ExceptionUtil.java

/**
 * Inspects the cause chain for the given throwable, looking for an exception of the given class
 * (e.g. to find an APIAuthenticationException wrapped in an InvocationTargetException)
 * /* w  w w  .j av a 2  s. c  o m*/
 * @param throwable
 * @param causeClassToLookFor
 * @return whether any exception in the cause chain of throwable is an instance of
 *         causeClassToLookFor
 */
public static boolean hasCause(Throwable throwable, Class<? extends Throwable> causeClassToLookFor) {
    return ExceptionUtils.indexOfType(throwable, causeClassToLookFor) >= 0;
}