View Javadoc
1   /*
2      Copyright 2010 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue.retry;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import static java.util.Objects.requireNonNull;
22  
23  /**
24   * Provides some standard implementations of various exception actions.
25   *
26   * This class is not instantiable.
27   *
28   */
29  public class ExceptionHandlers {
30    private static final Logger log = LoggerFactory.getLogger(ExceptionHandlers.class);
31  
32    private ExceptionHandlers() {
33      throw new AssertionError("This class is not instantiable.");
34    }
35  
36    /**
37     * Retrieves an {@link io.atlassian.fugue.retry.ExceptionHandler} which will
38     * log exceptions passed in.
39     *
40     * @param logger the Logger to which exceptions will be logged; if it is null,
41     * a default Logger will be used. The default logger is the logger for the
42     * ExceptionHandlers class, but may change in future.
43     * @return an {@link io.atlassian.fugue.retry.ExceptionHandler} which will log
44     * (at WARN level) exceptions passed in
45     */
46    public static ExceptionHandler loggingExceptionHandler(Logger logger) {
47      return new LoggingExceptionHandler(logger == null ? log : logger);
48    }
49  
50    /**
51     * <p>
52     * ignoreExceptionHandler.
53     * </p>
54     *
55     * @return an {@link io.atlassian.fugue.retry.ExceptionHandler} which does
56     * nothing
57     */
58    public static ExceptionHandler ignoreExceptionHandler() {
59      return new IgnoreExceptionHandler();
60    }
61  
62    /**
63     * Chain a series of ExceptionHandlers together to be executed subsequently;
64     * if one throws an exception, subsequent handlers will not be executed.
65     *
66     * @param handlers the chain of {@link ExceptionHandler handlers} to chain
67     * @return an ExceptionHandler composing the supplied handlers
68     */
69    public static ExceptionHandler chain(ExceptionHandler... handlers) {
70      return new CompositeExceptionHandler(handlers);
71    }
72  
73    static Logger logger() {
74      return log;
75    }
76  
77    private static class IgnoreExceptionHandler implements ExceptionHandler {
78      public void handle(RuntimeException a) {/* do nothing */}
79    }
80  
81    static class LoggingExceptionHandler implements ExceptionHandler {
82      private final Logger logger;
83  
84      LoggingExceptionHandler(Logger logger) {
85        this.logger = logger;
86      }
87  
88      @Override public void handle(RuntimeException e) {
89        warn(logger, e);
90      }
91  
92      private void warn(Logger log, Exception e) {
93        log.warn("Exception encountered: ", e);
94      }
95  
96      Logger logger() {
97        return logger;
98      }
99    }
100 
101   private static class CompositeExceptionHandler implements ExceptionHandler {
102     private final ExceptionHandler[] handlers;
103 
104     public CompositeExceptionHandler(ExceptionHandler... handlers) {
105       requireNonNull(handlers);
106       this.handlers = handlers;
107     }
108 
109     @Override public void handle(RuntimeException e) {
110       for (ExceptionHandler handler : handlers) {
111         handler.handle(e);
112       }
113     }
114   }
115 }