View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.output;
18  
19  import java.io.IOException;
20  import java.io.Serializable;
21  import java.io.Writer;
22  import java.util.UUID;
23  
24  import org.apache.commons.io.TaggedIOException;
25  
26  /**
27   * A writer decorator that tags potential exceptions so that the
28   * reader that caused the exception can easily be identified. This is
29   * done by using the {@link TaggedIOException} class to wrap all thrown
30   * {@link IOException}s. See below for an example of using this class.
31   * <pre>
32   * TaggedReader reader = new TaggedReader(...);
33   * try {
34   *     // Processing that may throw an IOException either from this reader
35   *     // or from some other IO activity like temporary files, etc.
36   *     writeToWriter(writer);
37   * } catch (IOException e) {
38   *     if (writer.isCauseOf(e)) {
39   *         // The exception was caused by this writer.
40   *         // Use e.getCause() to get the original exception.
41   *     } else {
42   *         // The exception was caused by something else.
43   *     }
44   * }
45   * </pre>
46   * <p>
47   * Alternatively, the {@link #throwIfCauseOf(Exception)} method can be
48   * used to let higher levels of code handle the exception caused by this
49   * writer while other processing errors are being taken care of at this
50   * lower level.
51   * </p>
52   * <pre>
53   * TaggedWriter writer = new TaggedWriter(...);
54   * try {
55   *     writeToWriter(writer);
56   * } catch (IOException e) {
57   *     writer.throwIfCauseOf(e);
58   *     // ... or process the exception that was caused by something else
59   * }
60   * </pre>
61   *
62   * @see TaggedIOException
63   * @since 2.0
64   */
65  public class TaggedWriter extends ProxyWriter {
66  
67      /**
68       * The unique tag associated with exceptions from writer.
69       */
70      private final Serializable tag = UUID.randomUUID();
71  
72      /**
73       * Constructs a tagging decorator for the given writer.
74       *
75       * @param proxy writer to be decorated
76       */
77      public TaggedWriter(final Writer proxy) {
78          super(proxy);
79      }
80  
81      /**
82       * Tags any IOExceptions thrown, wrapping and re-throwing.
83       *
84       * @param e The IOException thrown
85       * @throws IOException if an I/O error occurs.
86       */
87      @Override
88      protected void handleIOException(final IOException e) throws IOException {
89          throw new TaggedIOException(e, tag);
90      }
91  
92      /**
93       * Tests if the given exception was caused by this writer.
94       *
95       * @param exception an exception
96       * @return {@code true} if the exception was thrown by this writer,
97       *         {@code false} otherwise
98       */
99      public boolean isCauseOf(final Exception exception) {
100         return TaggedIOException.isTaggedWith(exception, tag);
101     }
102 
103     /**
104      * Re-throws the original exception thrown by this writer. This method
105      * first checks whether the given exception is a {@link TaggedIOException}
106      * wrapper created by this decorator, and then unwraps and throws the
107      * original wrapped exception. Returns normally if the exception was
108      * not thrown by this writer.
109      *
110      * @param exception an exception
111      * @throws IOException original exception, if any, thrown by this writer
112      */
113     public void throwIfCauseOf(final Exception exception) throws IOException {
114         TaggedIOException.throwCauseIfTaggedWith(exception, tag);
115     }
116 
117 }