1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.config.i18n.CoreMessages;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.security.AccessController;
21 import java.security.PrivilegedAction;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26
27
28
29
30 public class IOUtils extends org.apache.commons.io.IOUtils
31 {
32
33 private static final Log logger = LogFactory.getLog(IOUtils.class);
34
35
36
37
38
39
40
41
42
43
44 public static String getResourceAsString(final String resourceName, final Class callingClass)
45 throws IOException
46 {
47 InputStream is = getResourceAsStream(resourceName, callingClass);
48 if (is != null)
49 {
50 return toString(is);
51 }
52 else
53 {
54 throw new IOException("Unable to load resource " + resourceName);
55 }
56 }
57
58
59
60
61
62
63
64
65
66
67 public static InputStream getResourceAsStream(final String resourceName,
68 final Class callingClass) throws IOException
69 {
70 return getResourceAsStream(parseResourceName(resourceName), callingClass, true, true);
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84 public static InputStream getResourceAsStream(final String resourceName,
85 final Class callingClass,
86 boolean tryAsFile,
87 boolean tryAsUrl) throws IOException
88 {
89
90 URL url = getResourceAsUrl(resourceName, callingClass, tryAsFile);
91
92
93 if ((url == null) && (tryAsUrl))
94 {
95 try
96 {
97 url = new URL(resourceName);
98 }
99 catch (MalformedURLException 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
117
118
119
120
121
122
123 public static URL getResourceAsUrl(final String resourceName, final Class callingClass)
124 {
125 return getResourceAsUrl(resourceName, callingClass, true);
126 }
127
128
129
130
131
132
133
134
135
136
137 public static URL getResourceAsUrl(final String resourceName,
138 final Class callingClass,
139 boolean tryAsFile)
140 {
141 if (resourceName == null)
142 {
143 throw new IllegalArgumentException(
144 CoreMessages.objectIsNull("Resource name").getMessage());
145 }
146 URL url = null;
147
148
149 if (tryAsFile)
150 {
151 try
152 {
153 File file = FileUtils.newFile(resourceName);
154 if (file.exists())
155 {
156 url = file.getAbsoluteFile().toURL();
157 }
158 else
159 {
160 logger.debug("Unable to load resource from the file system: "
161 + file.getAbsolutePath());
162 }
163 }
164 catch (Exception e)
165 {
166 logger.debug("Unable to load resource from the file system: " + e.getMessage());
167 }
168 }
169
170
171 if (url == null)
172 {
173 try
174 {
175 url = (URL)AccessController.doPrivileged(new PrivilegedAction()
176 {
177 public Object run()
178 {
179 return ClassUtils.getResource(resourceName, callingClass);
180 }
181 });
182 if (url == null)
183 {
184 logger.debug("Unable to load resource from the classpath");
185 }
186 }
187 catch (Exception e)
188 {
189 logger.debug("Unable to load resource from the classpath: " + e.getMessage());
190 }
191 }
192
193 return url;
194 }
195
196
197
198
199
200
201
202
203
204 private static String parseResourceName(String src)
205 {
206 String var;
207 String[] split;
208 String ps = File.separator;
209
210 if (src.indexOf('$') > -1)
211 {
212 split = src.split("}");
213 }
214 else
215 {
216 return src;
217 }
218
219 var = split[0].substring(2);
220 var = SystemUtils.getenv(var);
221 if (split.length > 1)
222 {
223 if (var == null)
224 {
225 var = System.getProperty(split[0].substring(2));
226 if (var == null)
227 {
228 return split[1].substring(1);
229 }
230 else
231 {
232 return var + ps + split[1].substring(1);
233 }
234 }
235 else
236 {
237 return var + ps + split[1].substring(1);
238 }
239 }
240 else
241 {
242 if (var == null)
243 {
244 return "";
245 }
246 else
247 {
248 return var;
249 }
250 }
251 }
252 }