001: /*
002: * $Id: IOUtils.java 11338 2008-03-12 23:10:14Z aperepel $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.util;
012:
013: import org.mule.config.i18n.CoreMessages;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.net.MalformedURLException;
019: import java.net.URL;
020: import java.security.AccessController;
021: import java.security.PrivilegedAction;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: // @ThreadSafe
027: /**
028: * Mule input/output utilities.
029: */
030: public class IOUtils extends org.apache.commons.io.IOUtils {
031: /** Logger. */
032: private static final Log logger = LogFactory.getLog(IOUtils.class);
033:
034: /**
035: * Attempts to load a resource from the file system, from a URL, or from the
036: * classpath, in that order.
037: *
038: * @param resourceName The name of the resource to load
039: * @param callingClass The Class object of the calling object
040: * @return the requested resource as a string
041: * @throws java.io.IOException IO error
042: */
043: public static String getResourceAsString(final String resourceName,
044: final Class callingClass) throws IOException {
045: InputStream is = getResourceAsStream(resourceName, callingClass);
046: if (is != null) {
047: return toString(is);
048: } else {
049: throw new IOException("Unable to load resource "
050: + resourceName);
051: }
052: }
053:
054: /**
055: * Attempts to load a resource from the file system, from a URL, or from the
056: * classpath, in that order.
057: *
058: * @param resourceName The name of the resource to load
059: * @param callingClass The Class object of the calling object
060: * @return an InputStream to the resource or null if resource not found
061: * @throws java.io.IOException IO error
062: */
063: public static InputStream getResourceAsStream(
064: final String resourceName, final Class callingClass)
065: throws IOException {
066: return getResourceAsStream(parseResourceName(resourceName),
067: callingClass, true, true);
068: }
069:
070: /**
071: * Attempts to load a resource from the file system, from a URL, or from the
072: * classpath, in that order.
073: *
074: * @param resourceName The name of the resource to load
075: * @param callingClass The Class object of the calling object
076: * @param tryAsFile - try to load the resource from the local file system
077: * @param tryAsUrl - try to load the resource as a URL
078: * @return an InputStream to the resource or null if resource not found
079: * @throws java.io.IOException IO error
080: */
081: public static InputStream getResourceAsStream(
082: final String resourceName, final Class callingClass,
083: boolean tryAsFile, boolean tryAsUrl) throws IOException {
084:
085: URL url = getResourceAsUrl(resourceName, callingClass,
086: tryAsFile, tryAsUrl);
087:
088: if (url == null) {
089: return null;
090: } else {
091: return url.openStream();
092: }
093: }
094:
095: /**
096: * Attempts to load a resource from the file system or from the classpath, in
097: * that order.
098: *
099: * @param resourceName The name of the resource to load
100: * @param callingClass The Class object of the calling object
101: * @return an URL to the resource or null if resource not found
102: */
103: public static URL getResourceAsUrl(final String resourceName,
104: final Class callingClass) {
105: return getResourceAsUrl(resourceName, callingClass, true, true);
106: }
107:
108: /**
109: * Attempts to load a resource from the file system or from the classpath, in
110: * that order.
111: *
112: * @param resourceName The name of the resource to load
113: * @param callingClass The Class object of the calling object
114: * @param tryAsFile - try to load the resource from the local file system
115: * @param tryAsUrl - try to load the resource as a Url string
116: * @return an URL to the resource or null if resource not found
117: */
118: public static URL getResourceAsUrl(final String resourceName,
119: final Class callingClass, boolean tryAsFile,
120: boolean tryAsUrl) {
121: if (resourceName == null) {
122: throw new IllegalArgumentException(CoreMessages
123: .objectIsNull("Resource name").getMessage());
124: }
125: URL url = null;
126:
127: // Try to load the resource from the file system.
128: if (tryAsFile) {
129: try {
130: File file = FileUtils.newFile(resourceName);
131: if (file.exists()) {
132: url = file.getAbsoluteFile().toURL();
133: } else {
134: logger
135: .debug("Unable to load resource from the file system: "
136: + file.getAbsolutePath());
137: }
138: } catch (Exception e) {
139: logger
140: .debug("Unable to load resource from the file system: "
141: + e.getMessage());
142: }
143: }
144:
145: // Try to load the resource from the classpath.
146: if (url == null) {
147: try {
148: url = (URL) AccessController
149: .doPrivileged(new PrivilegedAction() {
150: public Object run() {
151: return ClassUtils.getResource(
152: resourceName, callingClass);
153: }
154: });
155: if (url == null) {
156: logger.debug("Unable to load resource "
157: + resourceName + " from the classpath");
158: }
159: } catch (Exception e) {
160: logger.debug("Unable to load resource " + resourceName
161: + " from the classpath: " + e.getMessage());
162: }
163: }
164:
165: if (url == null) {
166: try {
167: url = new URL(resourceName);
168: } catch (MalformedURLException e) {
169: //ignore
170: }
171: }
172: return url;
173: }
174:
175: /**
176: * This method checks whether the name of the resource needs to be parsed. If it
177: * is, it parses the name and tries to get the variable from the Environmental
178: * Variables configured on the system.
179: *
180: * @param src
181: * @return
182: */
183: private static String parseResourceName(String src) {
184: String var;
185: String[] split;
186: String ps = File.separator;
187:
188: if (src.indexOf('$') > -1) {
189: split = src.split("}");
190: } else {
191: return src;
192: }
193:
194: var = split[0].substring(2);
195: var = SystemUtils.getenv(var);
196: if (split.length > 1) {
197: if (var == null) {
198: var = System.getProperty(split[0].substring(2));
199: if (var == null) {
200: return split[1].substring(1);
201: } else {
202: return var + ps + split[1].substring(1);
203: }
204: } else {
205: return var + ps + split[1].substring(1);
206: }
207: } else {
208: if (var == null) {
209: return "";
210: } else {
211: return var;
212: }
213: }
214: }
215:
216: /**
217: * This method wraps {@link org.apache.commons.io.IOUtils}' <code>toString(InputStream)</code>
218: * method but catches any {@link IOException} and wraps it into a {@link RuntimeException}.
219: */
220: public static String toString(InputStream input) {
221: try {
222: return org.apache.commons.io.IOUtils.toString(input);
223: } catch (IOException iox) {
224: throw new RuntimeException(iox);
225: }
226: }
227:
228: /**
229: * This method wraps {@link org.apache.commons.io.IOUtils}' <code>toByteArray(InputStream)</code>
230: * method but catches any {@link IOException} and wraps it into a {@link RuntimeException}.
231: */
232: public static byte[] toByteArray(InputStream input) {
233: try {
234: return org.apache.commons.io.IOUtils.toByteArray(input);
235: } catch (IOException iox) {
236: throw new RuntimeException(iox);
237: }
238: }
239: }
|