| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| VerboseCallable |
|
| 2.3333333333333335;2.333 | ||||
| VerboseCallable$1 |
|
| 2.3333333333333335;2.333 |
| 1 | /** | |
| 2 | * Copyright (c) 2012-2015, jcabi.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.jcabi.log; | |
| 31 | ||
| 32 | import java.util.concurrent.Callable; | |
| 33 | import java.util.concurrent.TimeUnit; | |
| 34 | import lombok.EqualsAndHashCode; | |
| 35 | import lombok.ToString; | |
| 36 | ||
| 37 | /** | |
| 38 | * Wrapper of {@link Callable}, that logs all uncaught runtime exceptions. | |
| 39 | * | |
| 40 | * <p>You can use it with scheduled executor, for example: | |
| 41 | * | |
| 42 | * <pre> Executors.newFixedThreadPool(1).submit( | |
| 43 | * new VerboseCallable(callable, true) | |
| 44 | * );</pre> | |
| 45 | * | |
| 46 | * <p>Now, every runtime exception that is not caught inside your | |
| 47 | * {@link Callable} will be reported to log (using {@link Logger}). | |
| 48 | * Two-arguments constructor can be used when you need to instruct the class | |
| 49 | * about what to do with the exception: either swallow it or escalate. | |
| 50 | * Sometimes it's very important to swallow exceptions. Otherwise an entire | |
| 51 | * thread may get stuck (like in the example above). | |
| 52 | * | |
| 53 | * <p>This class is thread-safe. | |
| 54 | * | |
| 55 | * @author Yegor Bugayenko (yegor@teamed.io) | |
| 56 | * @version $Id: d2cb7c735555e40a04b9a6ae9621e754a8c40e05 $ | |
| 57 | * @since 0.16 | |
| 58 | * @see VerboseThreads | |
| 59 | * @link <a href="http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html">Java theory and practice: Dealing with InterruptedException</a> | |
| 60 | */ | |
| 61 | 0 | @ToString |
| 62 | 0 | @EqualsAndHashCode(of = { "origin", "rethrow", "verbose" }) |
| 63 | @SuppressWarnings("PMD.DoNotUseThreads") | |
| 64 | public final class VerboseCallable<T> implements Callable<T> { | |
| 65 | ||
| 66 | /** | |
| 67 | * Original runnable. | |
| 68 | */ | |
| 69 | private final transient Callable<T> origin; | |
| 70 | ||
| 71 | /** | |
| 72 | * Rethrow exceptions (TRUE) or swallow them? | |
| 73 | */ | |
| 74 | private final transient boolean rethrow; | |
| 75 | ||
| 76 | /** | |
| 77 | * Shall we report a full stacktrace? | |
| 78 | */ | |
| 79 | private final transient boolean verbose; | |
| 80 | ||
| 81 | /** | |
| 82 | * Default constructor, doesn't swallow exceptions. | |
| 83 | * @param callable Callable to wrap | |
| 84 | */ | |
| 85 | public VerboseCallable(final Callable<T> callable) { | |
| 86 | 0 | this(callable, false); |
| 87 | 0 | } |
| 88 | ||
| 89 | /** | |
| 90 | * Default constructor, doesn't swallow exceptions. | |
| 91 | * @param callable Callable to wrap | |
| 92 | * @param swallow Shall we swallow exceptions | |
| 93 | * ({@code TRUE}) or re-throw | |
| 94 | * ({@code FALSE})? Exception swallowing means that {@link #call()} | |
| 95 | * will never throw any exceptions (in any case all exceptions are logged | |
| 96 | * using {@link Logger}. | |
| 97 | */ | |
| 98 | public VerboseCallable(final Callable<T> callable, final boolean swallow) { | |
| 99 | 0 | this(callable, swallow, true); |
| 100 | 0 | } |
| 101 | ||
| 102 | /** | |
| 103 | * Default constructor. | |
| 104 | * @param runnable Runnable to wrap | |
| 105 | * @param swallow Shall we swallow exceptions | |
| 106 | * ({@code TRUE}) or re-throw | |
| 107 | * ({@code FALSE})? Exception swallowing means that {@link #call()} | |
| 108 | * will never throw any exceptions (in any case all exceptions are logged | |
| 109 | * using {@link Logger}. | |
| 110 | * @param vrbs Shall we report the entire | |
| 111 | * stacktrace of the exception | |
| 112 | * ({@code TRUE}) or just its message in one line ({@code FALSE}) | |
| 113 | */ | |
| 114 | @SuppressWarnings("PMD.AvoidCatchingGenericException") | |
| 115 | public VerboseCallable(final Runnable runnable, | |
| 116 | final boolean swallow, final boolean vrbs) { | |
| 117 | 0 | this( |
| 118 | 0 | new Callable<T>() { |
| 119 | @Override | |
| 120 | public T call() throws Exception { | |
| 121 | 0 | runnable.run(); |
| 122 | 0 | return null; |
| 123 | } | |
| 124 | @Override | |
| 125 | public String toString() { | |
| 126 | 0 | return runnable.toString(); |
| 127 | } | |
| 128 | }, | |
| 129 | swallow, | |
| 130 | vrbs | |
| 131 | ); | |
| 132 | 0 | } |
| 133 | ||
| 134 | /** | |
| 135 | * Default constructor, with configurable behavior for exceptions. | |
| 136 | * @param runnable Runnable to wrap | |
| 137 | * @param swallow Shall we swallow exceptions | |
| 138 | * ({@code TRUE}) or re-throw | |
| 139 | * ({@code FALSE})? Exception swallowing means that {@link #call()} | |
| 140 | * will never throw any exceptions (in any case all exceptions are logged | |
| 141 | * using {@link Logger}. | |
| 142 | */ | |
| 143 | public VerboseCallable(final Runnable runnable, final boolean swallow) { | |
| 144 | 0 | this(runnable, swallow, true); |
| 145 | 0 | } |
| 146 | ||
| 147 | /** | |
| 148 | * Default constructor, with fully configurable behavior. | |
| 149 | * @param callable Runnable to wrap | |
| 150 | * @param swallow Shall we swallow exceptions | |
| 151 | * ({@code TRUE}) or re-throw | |
| 152 | * ({@code FALSE})? Exception swallowing means that {@link #call()} | |
| 153 | * will never throw any exceptions (in any case all exceptions are logged | |
| 154 | * using {@link Logger}. | |
| 155 | * @param vrbs Shall we report the entire | |
| 156 | * stacktrace of the exception | |
| 157 | * ({@code TRUE}) or just its message in one line ({@code FALSE}) | |
| 158 | */ | |
| 159 | @SuppressWarnings("PMD.BooleanInversion") | |
| 160 | public VerboseCallable(final Callable<T> callable, | |
| 161 | 0 | final boolean swallow, final boolean vrbs) { |
| 162 | 0 | this.origin = callable; |
| 163 | 0 | this.rethrow = !swallow; |
| 164 | 0 | this.verbose = vrbs; |
| 165 | 0 | } |
| 166 | ||
| 167 | /** | |
| 168 | * {@inheritDoc} | |
| 169 | * | |
| 170 | * <p>We catch {@link RuntimeException} and {@link Error} here. All other | |
| 171 | * types of exceptions are "checked exceptions" and won't be thrown out | |
| 172 | * of {@code Callable@call()} method. | |
| 173 | */ | |
| 174 | @Override | |
| 175 | @SuppressWarnings("PMD.AvoidCatchingGenericException") | |
| 176 | public T call() throws Exception { | |
| 177 | 0 | T result = null; |
| 178 | try { | |
| 179 | 0 | result = this.origin.call(); |
| 180 | // @checkstyle IllegalCatch (1 line) | |
| 181 | 0 | } catch (final RuntimeException ex) { |
| 182 | 0 | if (this.rethrow) { |
| 183 | 0 | Logger.warn( |
| 184 | this, "escalated runtime exception: %s", this.tail(ex) | |
| 185 | ); | |
| 186 | 0 | throw ex; |
| 187 | } | |
| 188 | 0 | Logger.warn(this, "swallowed runtime exception: %s", this.tail(ex)); |
| 189 | // @checkstyle IllegalCatch (1 line) | |
| 190 | 0 | } catch (final Exception ex) { |
| 191 | 0 | if (this.rethrow) { |
| 192 | 0 | Logger.warn(this, "escalated exception: %s", this.tail(ex)); |
| 193 | 0 | throw ex; |
| 194 | } | |
| 195 | 0 | Logger.warn(this, "swallowed exception: %s", this.tail(ex)); |
| 196 | // @checkstyle IllegalCatch (1 line) | |
| 197 | 0 | } catch (final Error error) { |
| 198 | 0 | if (this.rethrow) { |
| 199 | 0 | Logger.error(this, "escalated error: %s", this.tail(error)); |
| 200 | 0 | throw error; |
| 201 | } | |
| 202 | 0 | Logger.error(this, "swallowed error: %s", this.tail(error)); |
| 203 | 0 | } |
| 204 | try { | |
| 205 | 0 | TimeUnit.MICROSECONDS.sleep(1L); |
| 206 | 0 | } catch (final InterruptedException ex) { |
| 207 | 0 | Thread.currentThread().interrupt(); |
| 208 | 0 | throw new IllegalStateException(ex); |
| 209 | 0 | } |
| 210 | 0 | return result; |
| 211 | } | |
| 212 | ||
| 213 | /** | |
| 214 | * Make a tail of the error/warning message, using the exception thrown. | |
| 215 | * @param throwable The exception/error caught | |
| 216 | * @return The message to show in logs | |
| 217 | */ | |
| 218 | private String tail(final Throwable throwable) { | |
| 219 | final String tail; | |
| 220 | 0 | if (this.verbose) { |
| 221 | 0 | tail = Logger.format("%[exception]s", throwable); |
| 222 | } else { | |
| 223 | 0 | tail = Logger.format( |
| 224 | "%[type]s('%s')", | |
| 225 | throwable, | |
| 226 | throwable.getMessage() | |
| 227 | ); | |
| 228 | } | |
| 229 | 0 | return tail; |
| 230 | } | |
| 231 | ||
| 232 | } |