KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > IOUtils


1 /*
2  * $Id: IOUtils.java 3937 2006-11-20 16:04:25Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.security.AccessController JavaDoc;
19 import java.security.PrivilegedAction JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.mule.config.i18n.Message;
24 import org.mule.config.i18n.Messages;
25
26 // @ThreadSafe
27
/**
28  * Mule input/output utilities.
29  */

30 public class IOUtils extends org.apache.commons.io.IOUtils
31 {
32     /** Logger. */
33     private static final Log logger = LogFactory.getLog(IOUtils.class);
34
35     /**
36      * Attempts to load a resource from the file system, from a URL, or from the
37      * classpath, in that order.
38      *
39      * @param resourceName The name of the resource to load
40      * @param callingClass The Class object of the calling object
41      * @return the requested resource as a string
42      * @throws java.io.IOException IO error
43      */

44     public static String JavaDoc getResourceAsString(final String JavaDoc resourceName, final Class JavaDoc callingClass)
45         throws IOException JavaDoc
46     {
47         InputStream JavaDoc is = getResourceAsStream(resourceName, callingClass);
48         if (is != null)
49         {
50             return toString(is);
51         }
52         else
53         {
54             throw new IOException JavaDoc("Unable to load resource " + resourceName);
55         }
56     }
57
58     /**
59      * Attempts to load a resource from the file system, from a URL, or from the
60      * classpath, in that order.
61      *
62      * @param resourceName The name of the resource to load
63      * @param callingClass The Class object of the calling object
64      * @return an InputStream to the resource or null if resource not found
65      * @throws java.io.IOException IO error
66      */

67     public static InputStream JavaDoc getResourceAsStream(final String JavaDoc resourceName, final Class JavaDoc callingClass)
68         throws IOException JavaDoc
69     {
70         return getResourceAsStream(resourceName, callingClass, true, true);
71     }
72
73     /**
74      * Attempts to load a resource from the file system, from a URL, or from the
75      * classpath, in that order.
76      *
77      * @param resourceName The name of the resource to load
78      * @param callingClass The Class object of the calling object
79      * @param tryAsFile - try to load the resource from the local file system
80      * @param tryAsUrl - try to load the resource as a URL
81      * @return an InputStream to the resource or null if resource not found
82      * @throws java.io.IOException IO error
83      */

84     public static InputStream JavaDoc getResourceAsStream(final String JavaDoc resourceName,
85                                                   final Class JavaDoc callingClass,
86                                                   boolean tryAsFile,
87                                                   boolean tryAsUrl) throws IOException JavaDoc
88     {
89
90         URL JavaDoc url = getResourceAsUrl(resourceName, callingClass, tryAsFile);
91
92         // Try to load the resource itself as a URL.
93
if ((url == null) && (tryAsUrl))
94         {
95             try
96             {
97                 url = new URL JavaDoc(resourceName);
98             }
99             catch (MalformedURLException JavaDoc e)
100             {
101                 logger.debug("Unable to load resource as a URL: " + resourceName);
102             }
103         }
104
105         if (url == null)
106         {
107             return null;
108         }
109         else
110         {
111             return url.openStream();
112         }
113     }
114
115     /**
116      * Attempts to load a resource from the file system or from the classpath, in
117      * that order.
118      *
119      * @param resourceName The name of the resource to load
120      * @param callingClass The Class object of the calling object
121      * @return an URL to the resource or null if resource not found
122      */

123     public static URL JavaDoc getResourceAsUrl(final String JavaDoc resourceName, final Class JavaDoc callingClass)
124     {
125         return getResourceAsUrl(resourceName, callingClass, true);
126     }
127
128     /**
129      * Attempts to load a resource from the file system or from the classpath, in
130      * that order.
131      *
132      * @param resourceName The name of the resource to load
133      * @param callingClass The Class object of the calling object
134      * @param tryAsFile - try to load the resource from the local file system
135      * @return an URL to the resource or null if resource not found
136      */

137     public static URL JavaDoc getResourceAsUrl(final String JavaDoc resourceName, final Class JavaDoc callingClass, boolean tryAsFile)
138     {
139         if (resourceName == null)
140         {
141             throw new IllegalArgumentException JavaDoc(new Message(Messages.X_IS_NULL, "Resource name").getMessage());
142         }
143         URL JavaDoc url = null;
144
145         // Try to load the resource from the file system.
146
if (tryAsFile)
147         {
148             try
149             {
150                 File JavaDoc file = FileUtils.newFile(resourceName);
151                 if (file.exists())
152                 {
153                     url = file.getAbsoluteFile().toURL();
154                 }
155                 else
156                 {
157                     logger.debug("Unable to load resource from the file system: " + file.getAbsolutePath());
158                 }
159             }
160             catch (Exception JavaDoc e)
161             {
162                 logger.debug("Unable to load resource from the file system: " + e.getMessage());
163             }
164         }
165
166         // Try to load the resource from the classpath.
167
if (url == null)
168         {
169             try
170             {
171                 url = (URL JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
172                 {
173                     public Object JavaDoc run()
174                     {
175                         return ClassUtils.getResource(resourceName, callingClass);
176                     }
177                 });
178                 if (url == null)
179                 {
180                     logger.debug("Unable to load resource from the classpath");
181                 }
182             }
183             catch (Exception JavaDoc e)
184             {
185                 logger.debug("Unable to load resource from the classpath: " + e.getMessage());
186             }
187         }
188
189         return url;
190     }
191 }
192
Popular Tags