1616cd24aaebc4c6b7512c7493195b629194ebcd
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / ExceptionMapper.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.util.concurrent;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import java.util.concurrent.CancellationException;
14 import java.util.concurrent.ExecutionException;
15
16 /**
17  * Utility exception mapper which translates an Exception to a specified type of
18  * Exception.
19  *
20  * <p>
21  * This mapper is intended to be used with
22  * {@link com.google.common.util.concurrent.Futures#makeChecked(com.google.common.util.concurrent.ListenableFuture, Function)}
23  * <ul>
24  * <li>if exception is the specified type or one of its subclasses, it returns
25  * original exception.
26  * <li>if exception is {@link ExecutionException} and the cause is of the
27  * specified type, it returns the cause
28  * <li>otherwise returns an instance of the specified exception type with
29  * original exception as the cause.
30  * </ul>
31  *
32  * @author Thomas Pantelis
33  *
34  * @param <X>
35  *            the exception type
36  */
37 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
38
39     private final Class<X> exceptionType;
40     private final String opName;
41
42     /**
43      * Constructor.
44      *
45      * @param opName the String prefix for exception messages.
46      * @param exceptionType the exception type to which to translate.
47      */
48     public ExceptionMapper(final String opName, final Class<X> exceptionType) {
49         this.exceptionType = Preconditions.checkNotNull(exceptionType);
50         this.opName = Preconditions.checkNotNull(opName);
51     }
52
53     /**
54      * Return the exception class produced by this instance.
55      *
56      * @return Exception class.
57      */
58     protected final Class<X> getExceptionType() {
59         return exceptionType;
60     }
61
62     /**
63      * Invoked to create a new exception instance of the specified type.
64      *
65      * @param message the message for the new exception.
66      * @param cause the cause for the new exception.
67      *
68      * @return an instance of the exception type.
69      */
70     protected abstract X newWithCause(String message, Throwable cause);
71
72     @SuppressWarnings("unchecked")
73     @Override
74     public X apply(final Exception e) {
75
76         // If exception is of the specified type,return it.
77         if (exceptionType.isAssignableFrom( e.getClass())) {
78             return (X) e;
79         }
80
81         // If exception is ExecutionException whose cause is of the specified
82         // type, return the cause.
83         if (e instanceof ExecutionException && e.getCause() != null) {
84             if (exceptionType.isAssignableFrom( e.getCause().getClass())) {
85                 return (X) e.getCause();
86             } else {
87                 return newWithCause(opName + " execution failed", e.getCause());
88             }
89         }
90
91         // Otherwise return an instance of the specified type with the original
92         // cause.
93
94         if (e instanceof InterruptedException) {
95             return newWithCause( opName + " was interupted.", e);
96         }
97
98         if (e instanceof CancellationException ) {
99             return newWithCause( opName + " was cancelled.", e);
100         }
101
102         // We really shouldn't get here but need to cover it anyway for completeness.
103         return newWithCause(opName + " encountered an unexpected failure", e);
104     }
105 }