1 | // Copyright (c) 2006 Dustin Sallings <dustin@spy.net< |
2 | |
3 | package net.spy.memcached.compat; |
4 | |
5 | import java.io.Closeable; |
6 | |
7 | import net.spy.memcached.compat.log.Logger; |
8 | import net.spy.memcached.compat.log.LoggerFactory; |
9 | |
10 | /** |
11 | * CloseUtil exists to provide a safe means to close anything closeable. |
12 | * This prevents exceptions from being thrown from within finally blocks while |
13 | * still providing logging of exceptions that occur during close. Exceptions |
14 | * during the close will be logged using the spy logging infrastructure, but |
15 | * will not be propagated up the stack. |
16 | */ |
17 | public final class CloseUtil { |
18 | |
19 | private static Logger logger=LoggerFactory.getLogger(CloseUtil.class); |
20 | |
21 | /** |
22 | * Close a closeable. |
23 | */ |
24 | public static void close(Closeable closeable) { |
25 | if (closeable != null) { |
26 | try { |
27 | closeable.close(); |
28 | } catch (Exception e) { |
29 | logger.info("Unable to close %s", closeable, e); |
30 | } |
31 | } |
32 | } |
33 | |
34 | } |