604ca3692c6f41b3cc74f941a1f16e201ca591a2
[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
14 import java.util.concurrent.CancellationException;
15 import java.util.concurrent.ExecutionException;
16
17 /**
18  * Utility exception mapper which translates an Exception to a specified type of Exception.
19  *
20  * This mapper is intended to be used with {@link com.google.common.util.concurrent.Futures#makeChecked(com.google.common.util.concurrent.ListenableFuture, Function)}
21  * <ul>
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.
25  * </ul>
26  *
27  * @author Thomas Pantelis
28  *
29  * @param <X> the exception type
30  */
31 public abstract class ExceptionMapper<X extends Exception> implements Function<Exception, X> {
32     private final Class<X> exceptionType;
33     private final String opName;
34
35     /**
36      * Constructor.
37      *
38      * @param opName the String prefix for exception messages.
39      * @param exceptionType the exception type to which to translate.
40      */
41     public ExceptionMapper(final String opName, final Class<X> exceptionType) {
42         this.exceptionType = Preconditions.checkNotNull(exceptionType);
43         this.opName = Preconditions.checkNotNull(opName);
44     }
45
46     /**
47      * Return the exception class produced by this instance.
48      *
49      * @return Exception class.
50      */
51     protected final Class<X> getExceptionType() {
52         return exceptionType;
53     }
54
55     /**
56      * Invoked to create a new exception instance of the specified type.
57      *
58      * @param message the message for the new exception.
59      * @param cause the cause for the new exception.
60      *
61      * @return an instance of the exception type.
62      */
63     protected abstract X newWithCause(String message, Throwable cause);
64
65     @SuppressWarnings("unchecked")
66     @Override
67     public X apply(final Exception e) {
68
69         // If exception is of the specified type,return it.
70         if (exceptionType.isAssignableFrom( e.getClass())) {
71             return (X) e;
72         }
73
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();
79             } else {
80                 return newWithCause(opName + " execution failed", e.getCause());
81             }
82         }
83
84         // Otherwise return an instance of the specified type with the original
85         // cause.
86
87         if (e instanceof InterruptedException) {
88             return newWithCause( opName + " was interupted.", e);
89         }
90
91         if (e instanceof CancellationException ) {
92             return newWithCause( opName + " was cancelled.", e);
93         }
94
95         // We really shouldn't get here but need to cover it anyway for completeness.
96         return newWithCause(opName + " encountered an unexpected failure", e);
97     }
98 }