KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > util > ResourceCloser


1 package net.sf.uitags.util;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.Reader JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 /**
10  * Provides overloaded methods to close resources. The methods have been
11  * designed to never fail, even if the underlying resource's
12  * <code>close()</code> method fails (a message is logged instead).
13  * This is so that one failing <code>close()</code> does not abruptly stop
14  * the cleaning up of other resources in the same <code>finally</code> block.
15  *
16  * @author jonni
17  */

18 public final class ResourceCloser {
19   private static final Log log = LogFactory.getLog(ResourceCloser.class);
20
21   private ResourceCloser() {
22     // Non-instantiable utility class.
23
}
24
25   public static void close(Reader JavaDoc resource) {
26     if (resource != null) {
27       try {
28         resource.close();
29       }
30       catch (Exception JavaDoc e) {
31         if (log.isWarnEnabled()) {
32           log.warn(
33               "Error encountered while attempting to close a Resource.", e);
34         }
35       }
36     }
37   }
38
39   public static void close(InputStream JavaDoc resource) {
40     if (resource != null) {
41       try {
42         resource.close();
43       }
44       catch (Exception JavaDoc e) {
45         if (log.isWarnEnabled()) {
46           log.warn(
47               "Error encountered while attempting to close an InputStream.", e);
48         }
49       }
50     }
51   }
52 }
53
Popular Tags