add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / util / IOUtils.java
1 package org.apache.lucene.util;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.Closeable;
21 import java.io.IOException;
22 import java.lang.reflect.Method;
23
24 /** This class emulates the new Java 7 "Try-With-Resources" statement.
25  * Remove once Lucene is on Java 7.
26  * @lucene.internal */
27 public final class IOUtils {
28
29   private IOUtils() {} // no instance
30
31   /**
32    * <p>Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions. Some of the <tt>Closeable</tt>s
33    * may be null, they are ignored. After everything is closed, method either throws <tt>priorException</tt>,
34    * if one is supplied, or the first of suppressed exceptions, or completes normally.</p>
35    * <p>Sample usage:<br/>
36    * <pre>
37    * Closeable resource1 = null, resource2 = null, resource3 = null;
38    * ExpectedException priorE = null;
39    * try {
40    *   resource1 = ...; resource2 = ...; resource3 = ...; // Acquisition may throw ExpectedException
41    *   ..do..stuff.. // May throw ExpectedException
42    * } catch (ExpectedException e) {
43    *   priorE = e;
44    * } finally {
45    *   closeSafely(priorE, resource1, resource2, resource3);
46    * }
47    * </pre>
48    * </p>
49    * @param priorException  <tt>null</tt> or an exception that will be rethrown after method completion
50    * @param objects         objects to call <tt>close()</tt> on
51    */
52   public static <E extends Exception> void closeWhileHandlingException(E priorException, Closeable... objects) throws E, IOException {
53     Throwable th = null;
54
55     for (Closeable object : objects) {
56       try {
57         if (object != null) {
58           object.close();
59         }
60       } catch (Throwable t) {
61         addSuppressed((priorException == null) ? th : priorException, t);
62         if (th == null) {
63           th = t;
64         }
65       }
66     }
67
68     if (priorException != null) {
69       throw priorException;
70     } else if (th != null) {
71       if (th instanceof IOException) throw (IOException) th;
72       if (th instanceof RuntimeException) throw (RuntimeException) th;
73       if (th instanceof Error) throw (Error) th;
74       throw new RuntimeException(th);
75     }
76   }
77
78   /** @see #closeWhileHandlingException(Exception, Closeable...) */
79   public static <E extends Exception> void closeWhileHandlingException(E priorException, Iterable<Closeable> objects) throws E, IOException {
80     Throwable th = null;
81
82     for (Closeable object : objects) {
83       try {
84         if (object != null) {
85           object.close();
86         }
87       } catch (Throwable t) {
88         addSuppressed((priorException == null) ? th : priorException, t);
89         if (th == null) {
90           th = t;
91         }
92       }
93     }
94
95     if (priorException != null) {
96       throw priorException;
97     } else if (th != null) {
98       if (th instanceof IOException) throw (IOException) th;
99       if (th instanceof RuntimeException) throw (RuntimeException) th;
100       if (th instanceof Error) throw (Error) th;
101       throw new RuntimeException(th);
102     }
103   }
104
105   /**
106    * Closes all given <tt>Closeable</tt>s.  Some of the
107    * <tt>Closeable</tt>s may be null; they are
108    * ignored.  After everything is closed, the method either
109    * throws the first exception it hit while closing, or
110    * completes normally if there were no exceptions.
111    * 
112    * @param objects
113    *          objects to call <tt>close()</tt> on
114    */
115   public static void close(Closeable... objects) throws IOException {
116     Throwable th = null;
117
118     for (Closeable object : objects) {
119       try {
120         if (object != null) {
121           object.close();
122         }
123       } catch (Throwable t) {
124         addSuppressed(th, t);
125         if (th == null) {
126           th = t;
127         }
128       }
129     }
130
131     if (th != null) {
132       if (th instanceof IOException) throw (IOException) th;
133       if (th instanceof RuntimeException) throw (RuntimeException) th;
134       if (th instanceof Error) throw (Error) th;
135       throw new RuntimeException(th);
136     }
137   }
138   
139   /**
140    * @see #close(Closeable...)
141    */
142   public static void close(Iterable<? extends Closeable> objects) throws IOException {
143     Throwable th = null;
144
145     for (Closeable object : objects) {
146       try {
147         if (object != null) {
148           object.close();
149         }
150       } catch (Throwable t) {
151         addSuppressed(th, t);
152         if (th == null) {
153           th = t;
154         }
155       }
156     }
157
158     if (th != null) {
159       if (th instanceof IOException) throw (IOException) th;
160       if (th instanceof RuntimeException) throw (RuntimeException) th;
161       if (th instanceof Error) throw (Error) th;
162       throw new RuntimeException(th);
163     }
164   }
165
166   /**
167    * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.
168    * Some of the <tt>Closeable</tt>s may be null, they are ignored.
169    * 
170    * @param objects
171    *          objects to call <tt>close()</tt> on
172    */
173   public static void closeWhileHandlingException(Closeable... objects) throws IOException {
174     for (Closeable object : objects) {
175       try {
176         if (object != null) {
177           object.close();
178         }
179       } catch (Throwable t) {
180       }
181     }
182   }
183   
184   /**
185    * @see #closeWhileHandlingException(Closeable...)
186    */
187   public static void closeWhileHandlingException(Iterable<? extends Closeable> objects) throws IOException {
188     for (Closeable object : objects) {
189       try {
190         if (object != null) {
191           object.close();
192         }
193       } catch (Throwable t) {
194       }
195     }
196   }
197   
198   /** This reflected {@link Method} is {@code null} before Java 7 */
199   private static final Method SUPPRESS_METHOD;
200   static {
201     Method m;
202     try {
203       m = Throwable.class.getMethod("addSuppressed", Throwable.class);
204     } catch (Exception e) {
205       m = null;
206     }
207     SUPPRESS_METHOD = m;
208   }
209
210   /** adds a Throwable to the list of suppressed Exceptions of the first Throwable (if Java 7 is detected)
211    * @param exception this exception should get the suppressed one added
212    * @param suppressed the suppressed exception
213    */
214   private static final void addSuppressed(Throwable exception, Throwable suppressed) {
215     if (SUPPRESS_METHOD != null && exception != null && suppressed != null) {
216       try {
217         SUPPRESS_METHOD.invoke(exception, suppressed);
218       } catch (Exception e) {
219         // ignore any exceptions caused by invoking (e.g. security constraints)
220       }
221     }
222   }
223
224 }