aboutsummaryrefslogtreecommitdiffstats
path: root/guava/src/com/google/common/io/Closer.java
blob: 49141c27e37d863595121e0060dcbc71b7b8db92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2012 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.io;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.logging.Level;

/**
 * A {@link Closeable} that collects {@code Closeable} resources and closes them all when it is
 * {@linkplain #close closed}. This is intended to approximately emulate the behavior of Java 7's
 * <a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html">
 * try-with-resources</a> statement in JDK6-compatible code. Running on Java 7, code using this
 * should be approximately equivalent in behavior to the same code written with try-with-resources.
 * Running on Java 6, exceptions that cannot be thrown must be logged rather than being added to the
 * thrown exception as a suppressed exception.
 *
 * <p>This class is intended to to be used in the following pattern:
 *
 * <pre>{@code
 * Closer closer = Closer.create();
 * try {
 *   InputStream in = closer.register(openInputStream());
 *   OutputStream out = closer.register(openOutputStream());
 *   // do stuff
 * } catch (Throwable e) {
 *   // ensure that any checked exception types other than IOException that could be thrown are
 *   // provided here, e.g. throw closer.rethrow(e, CheckedException.class);
 *   throw closer.rethrow(e);
 * } finally {
 *   closer.close();
 * }
 * }</pre>
 *
 * <p>Note that this try-catch-finally block is not equivalent to a try-catch-finally block using
 * try-with-resources. To get the equivalent of that, you must wrap the above code in <i>another</i>
 * try block in order to catch any exception that may be thrown (including from the call to
 * {@code close()}).
 *
 * <p>This pattern ensures the following:
 * <ul>
 *   <li>Each {@code Closeable} resource that is successfully registered will be closed later.</li>
 *   <li>If a {@code Throwable} is thrown in the try block, no exceptions that occur when attempting
 *   to close resources will be thrown from the finally block. The throwable from the try block will
 *   be thrown.</li>
 *   <li>If no exceptions or errors were thrown in the try block, the <i>first</i> exception thrown
 *   by an attempt to close a resource will be thrown.</li>
 *   <li>Any exception caught when attempting to close a resource that is <i>not</i> thrown
 *   (because another exception is already being thrown) is <i>suppressed</i>.</li>
 * </ul>
 *
 * An exception that is suppressed is not thrown. The method of suppression used depends on the
 * version of Java the code is running on:
 *
 * <ul>
 *   <li><b>Java 7+:</b> Exceptions are suppressed by adding them to the exception that <i>will</i>
 *   be thrown using {@code Throwable.addSuppressed(Throwable)}.</li>
 *   <li><b>Java 6:</b> Exceptions are suppressed by logging them instead.</li>
 * </ul>
 *
 * @author Colin Decker
 * @since 14.0
 */
// Coffee's for {@link Closer closers} only.
@Beta
public final class Closer implements Closeable {

  /**
   * The suppressor implementation to use for the current Java version.
   */
  private static final Suppressor SUPPRESSOR = SuppressingSuppressor.isAvailable()
      ? SuppressingSuppressor.INSTANCE
      : LoggingSuppressor.INSTANCE;

  /**
   * Creates a new {@link Closer}.
   */
  public static Closer create() {
    return new Closer(SUPPRESSOR);
  }

  @VisibleForTesting final Suppressor suppressor;

  // only need space for 2 elements in most cases, so try to use the smallest array possible
  private final Deque<Closeable> stack = new ArrayDeque<Closeable>(4);
  private Throwable thrown;

  @VisibleForTesting Closer(Suppressor suppressor) {
    this.suppressor = checkNotNull(suppressor); // checkNotNull to satisfy null tests
  }

  /**
   * Registers the given {@code closeable} to be closed when this {@code Closer} is
   * {@linkplain #close closed}.
   *
   * @return the given {@code closeable}
   */
  // close. this word no longer has any meaning to me.
  public <C extends Closeable> C register(C closeable) {
    stack.push(closeable);
    return closeable;
  }

  /**
   * Stores the given throwable and rethrows it. It will be rethrown as is if it is an
   * {@code IOException}, {@code RuntimeException} or {@code Error}. Otherwise, it will be rethrown
   * wrapped in a {@code RuntimeException}. <b>Note:</b> Be sure to declare all of the checked
   * exception types your try block can throw when calling an overload of this method so as to avoid
   * losing the original exception type.
   *
   * <p>This method always throws, and as such should be called as
   * {@code throw closer.rethrow(e);} to ensure the compiler knows that it will throw.
   *
   * @return this method does not return; it always throws
   * @throws IOException when the given throwable is an IOException
   */
  public RuntimeException rethrow(Throwable e) throws IOException {
    thrown = e;
    Throwables.propagateIfPossible(e, IOException.class);
    throw Throwables.propagate(e);
  }

  /**
   * Stores the given throwable and rethrows it. It will be rethrown as is if it is an
   * {@code IOException}, {@code RuntimeException}, {@code Error} or a checked exception of the
   * given type. Otherwise, it will be rethrown wrapped in a {@code RuntimeException}. <b>Note:</b>
   * Be sure to declare all of the checked exception types your try block can throw when calling an
   * overload of this method so as to avoid losing the original exception type.
   *
   * <p>This method always throws, and as such should be called as
   * {@code throw closer.rethrow(e, ...);} to ensure the compiler knows that it will throw.
   *
   * @return this method does not return; it always throws
   * @throws IOException when the given throwable is an IOException
   * @throws X when the given throwable is of the declared type X
   */
  public <X extends Exception> RuntimeException rethrow(Throwable e,
      Class<X> declaredType) throws IOException, X {
    thrown = e;
    Throwables.propagateIfPossible(e, IOException.class);
    Throwables.propagateIfPossible(e, declaredType);
    throw Throwables.propagate(e);
  }

  /**
   * Stores the given throwable and rethrows it. It will be rethrown as is if it is an
   * {@code IOException}, {@code RuntimeException}, {@code Error} or a checked exception of either
   * of the given types. Otherwise, it will be rethrown wrapped in a {@code RuntimeException}.
   * <b>Note:</b> Be sure to declare all of the checked exception types your try block can throw
   * when calling an overload of this method so as to avoid losing the original exception type.
   *
   * <p>This method always throws, and as such should be called as
   * {@code throw closer.rethrow(e, ...);} to ensure the compiler knows that it will throw.
   *
   * @return this method does not return; it always throws
   * @throws IOException when the given throwable is an IOException
   * @throws X1 when the given throwable is of the declared type X1
   * @throws X2 when the given throwable is of the declared type X2
   */
  public <X1 extends Exception, X2 extends Exception> RuntimeException rethrow(
      Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, X1, X2 {
    thrown = e;
    Throwables.propagateIfPossible(e, IOException.class);
    Throwables.propagateIfPossible(e, declaredType1, declaredType2);
    throw Throwables.propagate(e);
  }

  /**
   * Closes all {@code Closeable} instances that have been added to this {@code Closer}. If an
   * exception was thrown in the try block and passed to one of the {@code exceptionThrown} methods,
   * any exceptions thrown when attempting to close a closeable will be suppressed. Otherwise, the
   * <i>first</i> exception to be thrown from an attempt to close a closeable will be thrown and any
   * additional exceptions that are thrown after that will be suppressed.
   */
  @Override
  public void close() throws IOException {
    Throwable throwable = thrown;

    // close closeables in LIFO order
    while (!stack.isEmpty()) {
      Closeable closeable = stack.pop();
      try {
        closeable.close();
      } catch (Throwable e) {
        if (throwable == null) {
          throwable = e;
        } else {
          suppressor.suppress(closeable, throwable, e);
        }
      }
    }

    if (thrown == null && throwable != null) {
      Throwables.propagateIfPossible(throwable, IOException.class);
      throw new AssertionError(throwable); // not possible
    }
  }

  /**
   * Suppression strategy interface.
   */
  @VisibleForTesting interface Suppressor {
    /**
     * Suppresses the given exception ({@code suppressed}) which was thrown when attempting to close
     * the given closeable. {@code thrown} is the exception that is actually being thrown from the
     * method. Implementations of this method should not throw under any circumstances.
     */
    void suppress(Closeable closeable, Throwable thrown, Throwable suppressed);
  }

  /**
   * Suppresses exceptions by logging them.
   */
  @VisibleForTesting static final class LoggingSuppressor implements Suppressor {

    static final LoggingSuppressor INSTANCE = new LoggingSuppressor();

    @Override
    public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed) {
      // log to the same place as Closeables
      Closeables.logger.log(Level.WARNING,
          "Suppressing exception thrown when closing " + closeable, suppressed);
    }
  }

  /**
   * Suppresses exceptions by adding them to the exception that will be thrown using JDK7's
   * addSuppressed(Throwable) mechanism.
   */
  @VisibleForTesting static final class SuppressingSuppressor implements Suppressor {

    static final SuppressingSuppressor INSTANCE = new SuppressingSuppressor();

    static boolean isAvailable() {
      return addSuppressed != null;
    }

    static final Method addSuppressed = getAddSuppressed();

    private static Method getAddSuppressed() {
      try {
        return Throwable.class.getMethod("addSuppressed", Throwable.class);
      } catch (Throwable e) {
        return null;
      }
    }

    @Override
    public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed) {
      // ensure no exceptions from addSuppressed
      if (thrown == suppressed) {
        return;
      }
      try {
        addSuppressed.invoke(thrown, suppressed);
      } catch (Throwable e) {
        // if, somehow, IllegalAccessException or another exception is thrown, fall back to logging
        LoggingSuppressor.INSTANCE.suppress(closeable, thrown, suppressed);
      }
    }
  }
}