1 /*
2 Copyright (C) 2000 - 2007 Grid Systems, S.A.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17 package com.gridsystems.innergrid.api;
18
19 import java.io.Closeable;
20 import java.io.IOException;
21
22 import javax.activation.DataHandler;
23
24 import org.apache.axis.attachments.ManagedMemoryDataSource;
25
26 /**
27 * Utility class for input-output operations.
28 *
29 * @author dsanchez
30 * @author Rodrigo Ruiz
31 */
32 public final class IOUtils {
33 /**
34 * Private constructor.
35 *
36 */
37 private IOUtils() { }
38
39 /**
40 * Deletes the temporal file associated to the specified attachment, if it
41 * has one.
42 * <p>
43 * The current implementation only supports ManagedMemoryDataSources.
44 *
45 * @param dh The data handler to process
46 */
47 public static void delete(DataHandler dh) {
48 if (dh == null) {
49 return;
50 }
51 if (dh.getDataSource() instanceof ManagedMemoryDataSource) {
52 ManagedMemoryDataSource ds = (ManagedMemoryDataSource)dh.getDataSource();
53 ds.delete();
54 }
55 }
56
57 /**
58 * Closes a Closeable instance without throwing exceptions.
59 *
60 * @param c The instance to close
61 */
62 public static void close(Closeable c) {
63 try {
64 if (c != null) {
65 c.close();
66 }
67 } catch (IOException e) { }
68 }
69
70 /**
71 * Closes an instance of an object that does not implement the Closeable
72 * interface, without throwing exceptions.
73 *
74 * @param o The object to close
75 */
76 public static void close(Object o) {
77 try {
78 o.getClass().getMethod("close").invoke(o);
79 } catch (Exception e) { }
80 }
81 }