2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.util.concurrent;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
14 import java.util.concurrent.CancellationException;
15 import java.util.concurrent.ExecutionException;
18 * Utility exception mapper which translates an Exception to a specified type of Exception.
20 * This mapper is intended to be used with {@link com.google.common.util.concurrent.Futures#makeChecked(com.google.common.util.concurrent.ListenableFuture, Function)}
22 * <li>if exception is the specified type or one of its subclasses, it returns original exception.
23 * <li>if exception is {@link ExecutionException} and the cause is of the specified type, it returns the cause
24 * <li>otherwise returns an instance of the specified exception type with original exception as the cause.
27 * @author Thomas Pantelis
29 * @param <X> the exception type
31 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
32 private final Class<X> exceptionType;
33 private final String opName;
38 * @param opName the String prefix for exception messages.
39 * @param exceptionType the exception type to which to translate.
41 public ExceptionMapper(final String opName, final Class<X> exceptionType) {
42 this.exceptionType = Preconditions.checkNotNull(exceptionType);
43 this.opName = Preconditions.checkNotNull(opName);
47 * Return the exception class produced by this instance.
49 * @return Exception class.
51 protected final Class<X> getExceptionType() {
56 * Invoked to create a new exception instance of the specified type.
58 * @param message the message for the new exception.
59 * @param cause the cause for the new exception.
61 * @return an instance of the exception type.
63 protected abstract X newWithCause(String message, Throwable cause);
65 @SuppressWarnings("unchecked")
67 public X apply(final Exception e) {
69 // If exception is of the specified type,return it.
70 if (exceptionType.isAssignableFrom( e.getClass())) {
74 // If exception is ExecutionException whose cause is of the specified
75 // type, return the cause.
76 if (e instanceof ExecutionException && e.getCause() != null) {
77 if (exceptionType.isAssignableFrom( e.getCause().getClass())) {
78 return (X) e.getCause();
80 return newWithCause(opName + " execution failed", e.getCause());
84 // Otherwise return an instance of the specified type with the original
87 if (e instanceof InterruptedException) {
88 return newWithCause( opName + " was interupted.", e);
91 if (e instanceof CancellationException ) {
92 return newWithCause( opName + " was cancelled.", e);
95 // We really shouldn't get here but need to cover it anyway for completeness.
96 return newWithCause(opName + " encountered an unexpected failure", e);