下面列出了org.apache.commons.lang.exception.ExceptionUtils#getRootCause ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//using DCL idiom
//see effective java 2nd ed. pg. 71
Object s = service;
if (s == null) {
synchronized (this) {
s = service;
if (s == null) {
service = s = GlobalResourceLoader.getService(serviceName);
}
}
}
if (s != null) {
try {
return method.invoke(s, args);
} catch (InvocationTargetException e) {
throw ExceptionUtils.getRootCause(e);
}
}
LOG.warn("serviceName: " + serviceName + " was not found");
return null;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
Throwable rootCause = ExceptionUtils.getRootCause(cause);
LOG.error(getErrorMessage(ExceptionUtils.getMessage(rootCause)), rootCause);
if (cause instanceof RecoverableException) {
sendException((RecoverableException) cause);
} else {
/* unrecoverable fatal error*/
sendExceptions(ExceptionUtils.getMessage(rootCause));
if (ctx.channel().isOpen()) {
ctx.close();
}
}
}
@Test
public void testTransactedRollback() throws InterruptedException {
String message = "this message will explode";
assertEquals(0, auditLogDao.getAuditCount(message));
MockEndpoint mockCompleted = getMockEndpoint("mock:out");
mockCompleted.setExpectedMessageCount(0);
try {
template.sendBody("direct:transacted", message);
fail();
} catch (CamelExecutionException cee) {
Throwable rootCause = ExceptionUtils.getRootCause(cee);
assertTrue(rootCause instanceof org.apache.camel.RollbackExchangeException);
assertTrue(rootCause.getMessage().startsWith("Message contained word 'explode'"));
}
assertMockEndpointsSatisfied();
assertEquals(0, auditLogDao.getAuditCount(message)); // the insert was rolled back
}
@Override
public void run() {
try {
final long start = System.currentTimeMillis();
process(graph, appeared);
final long end = System.currentTimeMillis();
final long time = end - start;
REGISTRY.timer(TIMER_LINE).update(time, TimeUnit.MILLISECONDS);
} catch (Throwable e) {
final Throwable rootCause = ExceptionUtils.getRootCause(e);
final String rootCauseMessage = Optional.ofNullable(rootCause.getMessage()).orElse("");
log.error("Error processing line {} {}", e.getMessage(), rootCauseMessage, e);
} finally {
COMPLETED_TASK_COUNT.incrementAndGet();
}
}
public static void main(String[] args) {
if (args.length < 3) {
System.err.println(PoseidonJMXInvoker.class.getSimpleName() + " <host> <port> <operation>");
System.exit(-1);
}
final String CONNECT_STRING = args[0] + ":" + args[1];
final String OPERATION = args[2];
try {
System.out.println("Running " + OPERATION + " over JMX on " + CONNECT_STRING);
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + CONNECT_STRING + "/jmxrmi");
MBeanServerConnection connection = JMXConnectorFactory.connect(url).getMBeanServerConnection();
connection.invoke(ObjectName.getInstance(MBEAN_NAME), OPERATION, null, null);
} catch (Exception e) {
if (!(ExceptionUtils.getRootCause(e) instanceof EOFException && "destroy".equals(OPERATION))) {
e.printStackTrace();
System.exit(-1);
}
}
System.out.println(OPERATION + " successful over JMX on " + CONNECT_STRING);
}
public boolean invoke(InvokeStrategyContext ctx) {
try {
ctx.setResponse(super.call(ctx.getRequest()));
return true;
} catch (TechnicalConnectorException var4) {
Throwable reason = ExceptionUtils.getRootCause(var4);
LOG.error("Cannot send SOAP message. Reason [" + reason + "]", var4);
ctx.setException(new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_WS, ExceptionUtils.getRootCause(var4), new Object[]{ExceptionUtils.getRootCauseMessage(var4)}));
return false;
}
}
private static TechnicalConnectorException translate(Exception e) {
if (e instanceof SOAPException) {
return new RetryNextEndpointException(e);
} else if (e instanceof TechnicalConnectorException) {
return (TechnicalConnectorException)e;
} else {
Throwable reason = ExceptionUtils.getRootCause(e);
LOG.error("Cannot send SOAP message. Reason [" + reason + "]", e);
return new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_WS, reason, new Object[]{"Cannot send SOAP message"});
}
}
public boolean invoke(InvokeStrategyContext ctx) {
try {
ctx.setResponse(super.call(ctx.getRequest()));
return true;
} catch (TechnicalConnectorException var4) {
Throwable reason = ExceptionUtils.getRootCause(var4);
LOG.error("Cannot send SOAP message. Reason [" + reason + "]", var4);
ctx.setException(new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_WS, ExceptionUtils.getRootCause(var4), new Object[]{ExceptionUtils.getRootCauseMessage(var4)}));
return false;
}
}
public GenericResponse invoke(GenericRequest genericRequest) throws TechnicalConnectorException {
try {
return super.call(genericRequest);
} catch (RetryNextEndpointException var4) {
Throwable reason = ExceptionUtils.getRootCause(var4);
LOG.error("Cannot send SOAP message. Reason [" + reason + "]", var4);
throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_WS, var4, new Object[]{reason});
}
}
private static TechnicalConnectorException translate(Exception e) {
if (e instanceof SOAPException) {
return new RetryNextEndpointException(e);
} else {
Throwable reason = ExceptionUtils.getRootCause(e);
LOG.error("Cannot send SOAP message. Reason [" + reason + "]", e);
return new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_WS, e, new Object[]{reason});
}
}
private Throwable getRootCause(Throwable throwable) {
Throwable rootCause = ExceptionUtils.getRootCause(throwable);
if (rootCause != null) {
return rootCause;
} else {
return throwable;
}
}
public boolean invoke(InvokeStrategyContext ctx) {
try {
ctx.setResponse(super.call(ctx.getRequest()));
return true;
} catch (TechnicalConnectorException var4) {
Throwable reason = ExceptionUtils.getRootCause(var4);
LOG.error("Cannot send SOAP message. Reason [" + reason + "]", var4);
ctx.setException(new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_WS, ExceptionUtils.getRootCause(var4), new Object[]{ExceptionUtils.getRootCauseMessage(var4)}));
return false;
}
}
protected boolean isInterrupt(Throwable e) {
if (!running) {
return true;
}
if (ExceptionUtils.getRootCause(e) instanceof InterruptedException) {
return true;
}
return false;
}
public Throwable transform(Throwable throwable) {
// Arquillian sometimes returns InvocationException with nested
// exception and sometimes exception itself
@SuppressWarnings("unchecked")
List<Throwable> throwableList = ExceptionUtils.getThrowableList(throwable);
if (throwableList.isEmpty())
return throwable;
Throwable root = null;
if (throwableList.size() == 1) {
root = throwable;
} else {
root = ExceptionUtils.getRootCause(throwable);
}
if (root instanceof DeploymentException || root instanceof DefinitionException) {
return root;
}
if (isFragmentFound(DEPLOYMENT_EXCEPTION_FRAGMENTS, root)) {
return new DeploymentException(root.getMessage());
}
if (isFragmentFound(DEFINITION_EXCEPTION_FRAGMENTS, root)) {
return new DefinitionException(root.getMessage());
}
return throwable;
}
protected Long deleteByPolicy(final Repository repository,
final CleanupPolicy policy,
final BooleanSupplier cancelledCheck)
{
log.info("Deleting components in repository {} using policy {}", repository.getName(), policy.getName());
DeletionProgress deletionProgress = new DeletionProgress(cleanupRetryLimit);
if (!policy.getCriteria().isEmpty()) {
do {
try {
Iterable<EntityId> componentsToDelete = browseService.browse(policy, repository);
DeletionProgress currentProgress = cleanupMethod.run(repository, componentsToDelete, cancelledCheck);
deletionProgress.update(currentProgress);
}
catch (Exception e) {
deletionProgress.setFailed(true);
if (ExceptionUtils.getRootCause(e) instanceof SearchContextMissingException) {
log.warn("Search scroll timed out, continuing with new scrollId.", log.isDebugEnabled() ? e : null);
deletionProgress.setAttempts(0);
}
else {
log.error("Failed to delete components.", e);
}
}
} while (!deletionProgress.isFinished());
if (deletionProgress.isFailed()) {
log.warn("Deletion attempts exceeded for repository {}", repository.getName());
}
return deletionProgress.getCount();
}
else {
log.info("Policy {} has no criteria and will therefore be ignored (i.e. no components will be deleted)",
policy.getName());
return 0L;
}
}
protected <T> Transform<Throwable, T> handleVersionConflict(
Provider<T> emptyProvider, Runnable reportWriteDroppedByDuplicate
) {
return throwable -> {
if (ExceptionUtils.getRootCause(throwable) instanceof VersionConflictEngineException) {
// Index request rejected, document already exists. That's ok, return success.
reportWriteDroppedByDuplicate.run();
return emptyProvider.get();
}
throw new RuntimeException(throwable);
};
}
public GenericResponse invoke(GenericRequest genericRequest) throws TechnicalConnectorException {
RetryStrategy.RetryContext ctx = new RetryStrategy.RetryContext(this.getCurrentEndpoint(genericRequest));
int alternatives = distributor.getAmountOfAlternatives(ctx.endpoint);
for(int i = 0; i < alternatives; ++i) {
String activeEndpoint = distributor.getActiveEndpoint(ctx.endpoint);
if (!ctx.invokedEndpoints.contains(activeEndpoint)) {
ctx.invokedEndpoints.add(activeEndpoint);
genericRequest.setEndpoint(activeEndpoint);
try {
GenericResponse resp = super.call(genericRequest);
if (ctx.alternativeActivated) {
LOG.debug("Activating status page polling!");
distributor.activatePolling();
}
return resp;
} catch (RetryNextEndpointException var9) {
LOG.error("Unable to invoke endpoint [{}], activating next one.", activeEndpoint, var9);
try {
distributor.activateNextEndPoint(activeEndpoint);
ctx.alternativeActivated = true;
} catch (NoNextEndpointException var8) {
LOG.error("Unable to activate alternative", var8);
}
ctx.lastException = var9;
}
} else {
LOG.debug("Endpoint [{}] already invoked, skipping it.", activeEndpoint);
}
}
if (EndpointDistributor.update()) {
return this.invoke(genericRequest);
} else {
throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_WS, ExceptionUtils.getRootCause(ctx.lastException), new Object[]{ExceptionUtils.getRootCauseMessage(ctx.lastException)});
}
}
public GenericResponse invoke(GenericRequest genericRequest) throws TechnicalConnectorException {
RetryStrategy.RetryContext ctx = new RetryStrategy.RetryContext(this.getCurrentEndpoint(genericRequest));
int alternatives = distributor.getAmountOfAlternatives(ctx.endpoint);
for(int i = 0; i < alternatives; ++i) {
String activeEndpoint = distributor.getActiveEndpoint(ctx.endpoint);
if (!ctx.invokedEndpoints.contains(activeEndpoint)) {
ctx.invokedEndpoints.add(activeEndpoint);
genericRequest.setEndpoint(activeEndpoint);
try {
GenericResponse resp = super.call(genericRequest);
if (ctx.alternativeActivated) {
LOG.debug("Activating status page polling!");
distributor.activatePolling();
}
return resp;
} catch (RetryNextEndpointException var9) {
LOG.error("Unable to invoke endpoint [{}], activating next one.", activeEndpoint, var9);
try {
distributor.activateNextEndPoint(activeEndpoint);
ctx.alternativeActivated = true;
} catch (NoNextEndpointException var8) {
LOG.error("Unable to activate alternative", var8);
}
ctx.lastException = var9;
}
} else {
LOG.debug("Endpoint [{}] already invoked, skipping it.", activeEndpoint);
}
}
if (EndpointDistributor.update()) {
return this.invoke(genericRequest);
} else {
throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_WS, ExceptionUtils.getRootCause(ctx.lastException), new Object[]{ExceptionUtils.getRootCauseMessage(ctx.lastException)});
}
}
/**
* Gets a short message summarising the root cause exception.
* <p>
* The message returned is of the form
* {ClassNameWithoutPackage}: {ThrowableMessage}
*
* @param th the throwable to get a message for, null returns empty string
* @return the message, non-null
* @since Commons Lang 2.2
*/
public static String getRootCauseMessage(Throwable th) {
Throwable root = ExceptionUtils.getRootCause(th);
root = (root == null ? th : root);
return getMessage(root);
}
/**
* Gets a short message summarising the root cause exception.
* <p>
* The message returned is of the form
* {ClassNameWithoutPackage}: {ThrowableMessage}
*
* @param th the throwable to get a message for, null returns empty string
* @return the message, non-null
* @since Commons Lang 2.2
*/
public static String getRootCauseMessage(Throwable th) {
Throwable root = ExceptionUtils.getRootCause(th);
root = (root == null ? th : root);
return getMessage(root);
}