1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.MuleRuntimeException; |
15 | |
import org.mule.config.i18n.MessageFactory; |
16 | |
|
17 | |
import java.io.BufferedOutputStream; |
18 | |
import java.io.BufferedWriter; |
19 | |
import java.io.File; |
20 | |
import java.io.FileOutputStream; |
21 | |
import java.io.FileWriter; |
22 | |
import java.io.IOException; |
23 | |
import java.io.InputStream; |
24 | |
import java.io.OutputStream; |
25 | |
import java.io.UnsupportedEncodingException; |
26 | |
import java.net.JarURLConnection; |
27 | |
import java.net.URI; |
28 | |
import java.net.URL; |
29 | |
import java.net.URLConnection; |
30 | |
import java.net.URLDecoder; |
31 | |
import java.util.Enumeration; |
32 | |
import java.util.jar.JarEntry; |
33 | |
import java.util.jar.JarFile; |
34 | |
import java.util.zip.ZipEntry; |
35 | |
import java.util.zip.ZipFile; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class FileUtils extends org.apache.commons.io.FileUtils |
43 | |
{ |
44 | |
public static synchronized void copyStreamToFile(InputStream input, File destination) throws IOException |
45 | |
{ |
46 | 0 | if (destination.exists() && !destination.canWrite()) |
47 | |
{ |
48 | 0 | throw new IOException("Destination file does not exist or is not writeable"); |
49 | |
} |
50 | |
|
51 | |
try |
52 | |
{ |
53 | 0 | FileOutputStream output = new FileOutputStream(destination); |
54 | |
try |
55 | |
{ |
56 | 0 | IOUtils.copy(input, output); |
57 | |
} |
58 | |
finally |
59 | |
{ |
60 | 0 | IOUtils.closeQuietly(output); |
61 | 0 | } |
62 | |
} |
63 | |
finally |
64 | |
{ |
65 | 0 | IOUtils.closeQuietly(input); |
66 | 0 | } |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
public static File createFile(String filename) throws IOException |
71 | |
{ |
72 | 0 | File file = FileUtils.newFile(filename); |
73 | 0 | if (!file.canWrite()) |
74 | |
{ |
75 | 0 | String dirName = file.getPath(); |
76 | 0 | int i = dirName.lastIndexOf(File.separator); |
77 | 0 | if (i > -1) |
78 | |
{ |
79 | 0 | dirName = dirName.substring(0, i); |
80 | 0 | File dir = FileUtils.newFile(dirName); |
81 | 0 | dir.mkdirs(); |
82 | |
} |
83 | 0 | file.createNewFile(); |
84 | |
} |
85 | 0 | return file; |
86 | |
} |
87 | |
|
88 | |
|
89 | |
public static String prepareWinFilename(String filename) |
90 | |
{ |
91 | 0 | filename = filename.replaceAll("<", "("); |
92 | 0 | filename = filename.replaceAll(">", ")"); |
93 | 0 | filename = filename.replaceAll("[/\\*?|:;\\]\\[\"]", "-"); |
94 | 0 | return filename; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
public static File openDirectory(String directory) throws IOException |
99 | |
{ |
100 | 0 | File dir = FileUtils.newFile(directory); |
101 | 0 | if (!dir.exists()) |
102 | |
{ |
103 | 0 | dir.mkdirs(); |
104 | |
} |
105 | 0 | if (!dir.isDirectory() || !dir.canRead()) |
106 | |
{ |
107 | 0 | throw new IOException("Path: " + directory + " exists but isn't a directory"); |
108 | |
} |
109 | 0 | return dir; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public static File stringToFile(String filename, String data) throws IOException |
121 | |
{ |
122 | 0 | return stringToFile(filename, data, false); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
public static synchronized File stringToFile(String filename, String data, boolean append) |
127 | |
throws IOException |
128 | |
{ |
129 | 0 | return stringToFile(filename, data, append, false); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
public static synchronized File stringToFile(String filename, String data, boolean append, boolean newLine) |
134 | |
throws IOException |
135 | |
{ |
136 | 0 | File f = createFile(filename); |
137 | 0 | BufferedWriter writer = null; |
138 | |
try |
139 | |
{ |
140 | 0 | writer = new BufferedWriter(new FileWriter(f, append)); |
141 | 0 | writer.write(data); |
142 | 0 | if (newLine) |
143 | |
{ |
144 | 0 | writer.newLine(); |
145 | |
} |
146 | |
} |
147 | |
finally |
148 | |
{ |
149 | 0 | if (writer != null) |
150 | |
{ |
151 | 0 | writer.close(); |
152 | |
} |
153 | |
} |
154 | 0 | return f; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
public static String getResourcePath(String resourceName, Class callingClass) throws IOException |
159 | |
{ |
160 | 0 | return getResourcePath(resourceName, callingClass, MuleManager.getConfiguration().getEncoding()); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
public static String getResourcePath(String resourceName, Class callingClass, String encoding) |
165 | |
throws IOException |
166 | |
{ |
167 | 0 | if (resourceName == null) |
168 | |
{ |
169 | |
|
170 | 0 | return null; |
171 | |
} |
172 | |
|
173 | 0 | URL url = IOUtils.getResourceAsUrl(resourceName, callingClass); |
174 | 0 | if (url == null) |
175 | |
{ |
176 | |
|
177 | 0 | return null; |
178 | |
} |
179 | 0 | return normalizeFilePath(url, encoding); |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public static String normalizeFilePath(URL url, String encoding) throws UnsupportedEncodingException |
192 | |
{ |
193 | 0 | String resource = URLDecoder.decode(url.toExternalForm(), encoding); |
194 | 0 | if (resource != null) |
195 | |
{ |
196 | 0 | if (resource.startsWith("file:/")) |
197 | |
{ |
198 | 0 | resource = resource.substring(6); |
199 | |
} |
200 | 0 | if (!resource.startsWith(File.separator)) |
201 | |
{ |
202 | 0 | resource = File.separator + resource; |
203 | |
} |
204 | |
} |
205 | 0 | return resource; |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
public static boolean deleteTree(File dir) |
215 | |
{ |
216 | 0 | return deleteTree(dir, null); |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public static boolean deleteTree(File dir, final String[] topLevelDirsToIgnore) |
230 | |
{ |
231 | 0 | if (dir == null || !dir.exists()) |
232 | |
{ |
233 | 0 | return true; |
234 | |
} |
235 | 0 | File[] files = dir.listFiles(); |
236 | 0 | if (files != null) |
237 | |
{ |
238 | 0 | for (int i = 0; i < files.length; i++) |
239 | |
{ |
240 | |
OUTER: |
241 | 0 | if (files[i].isDirectory()) |
242 | |
{ |
243 | 0 | if (topLevelDirsToIgnore != null) |
244 | |
{ |
245 | 0 | for (int j = 0; j < topLevelDirsToIgnore.length; j++) |
246 | |
{ |
247 | 0 | String ignored = topLevelDirsToIgnore[j]; |
248 | 0 | if (ignored.equals(FilenameUtils.getBaseName(files[i].getName()))) |
249 | |
{ |
250 | 0 | break OUTER; |
251 | |
} |
252 | |
} |
253 | |
} |
254 | 0 | if (!deleteTree(files[i])) |
255 | |
{ |
256 | 0 | return false; |
257 | |
} |
258 | |
} |
259 | |
else |
260 | |
{ |
261 | 0 | if (!files[i].delete()) |
262 | |
{ |
263 | 0 | return false; |
264 | |
} |
265 | |
} |
266 | |
} |
267 | |
} |
268 | 0 | return dir.delete(); |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public static void unzip(File archive, File directory) throws IOException |
275 | |
{ |
276 | 0 | ZipFile zip = null; |
277 | |
|
278 | 0 | if (directory.exists()) |
279 | |
{ |
280 | 0 | if (!directory.isDirectory()) |
281 | |
{ |
282 | 0 | throw new IOException("Directory is not a directory: " + directory); |
283 | |
} |
284 | |
} |
285 | |
else |
286 | |
{ |
287 | 0 | if (!directory.mkdirs()) |
288 | |
{ |
289 | 0 | throw new IOException("Could not create directory: " + directory); |
290 | |
} |
291 | |
} |
292 | |
try |
293 | |
{ |
294 | 0 | zip = new ZipFile(archive); |
295 | 0 | for (Enumeration entries = zip.entries(); entries.hasMoreElements();) |
296 | |
{ |
297 | 0 | ZipEntry entry = (ZipEntry) entries.nextElement(); |
298 | 0 | File f = FileUtils.newFile(directory, entry.getName()); |
299 | 0 | if (entry.isDirectory()) |
300 | |
{ |
301 | 0 | if (!f.mkdirs()) |
302 | |
{ |
303 | 0 | throw new IOException("Could not create directory: " + f); |
304 | |
} |
305 | |
} |
306 | |
else |
307 | |
{ |
308 | 0 | InputStream is = zip.getInputStream(entry); |
309 | 0 | OutputStream os = new BufferedOutputStream(new FileOutputStream(f)); |
310 | 0 | IOUtils.copy(is, os); |
311 | 0 | IOUtils.closeQuietly(is); |
312 | 0 | IOUtils.closeQuietly(os); |
313 | |
} |
314 | |
} |
315 | |
} |
316 | |
finally |
317 | |
{ |
318 | 0 | if (zip != null) |
319 | |
{ |
320 | 0 | zip.close(); |
321 | |
} |
322 | |
} |
323 | 0 | } |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
public static File newFile(String pathName) |
336 | |
{ |
337 | |
try |
338 | |
{ |
339 | 0 | return new File(pathName).getCanonicalFile(); |
340 | |
} |
341 | 0 | catch (IOException e) |
342 | |
{ |
343 | 0 | throw new MuleRuntimeException( |
344 | |
MessageFactory.createStaticMessage("Unable to create a canonical file for " + pathName), |
345 | |
e); |
346 | |
} |
347 | |
} |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
public static File newFile(URI uri) |
360 | |
{ |
361 | |
try |
362 | |
{ |
363 | 0 | return new File(uri).getCanonicalFile(); |
364 | |
} |
365 | 0 | catch (IOException e) |
366 | |
{ |
367 | 0 | throw new MuleRuntimeException( |
368 | |
MessageFactory.createStaticMessage("Unable to create a canonical file for " + uri), |
369 | |
e); |
370 | |
} |
371 | |
} |
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
public static File newFile(File parent, String child) |
384 | |
{ |
385 | |
try |
386 | |
{ |
387 | 0 | return new File(parent, child).getCanonicalFile(); |
388 | |
} |
389 | 0 | catch (IOException e) |
390 | |
{ |
391 | 0 | throw new MuleRuntimeException( |
392 | |
MessageFactory.createStaticMessage("Unable to create a canonical file for parent: " |
393 | |
+ parent + " and child: " + child), |
394 | |
e); |
395 | |
} |
396 | |
} |
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
public static File newFile(String parent, String child) |
409 | |
{ |
410 | |
try |
411 | |
{ |
412 | 0 | return new File(parent, child).getCanonicalFile(); |
413 | |
} |
414 | 0 | catch (IOException e) |
415 | |
{ |
416 | 0 | throw new MuleRuntimeException( |
417 | |
MessageFactory.createStaticMessage("Unable to create a canonical file for parent: " |
418 | |
+ parent + " and child: " + child), |
419 | |
e); |
420 | |
} |
421 | |
} |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
public static void extractResources(String resourceName, Class callingClass, File outputDir, boolean keepParentDirectory) throws IOException |
434 | |
{ |
435 | 0 | URL url = callingClass.getClassLoader().getResource(resourceName); |
436 | 0 | URLConnection connection = url.openConnection(); |
437 | 0 | if (connection instanceof JarURLConnection) |
438 | |
{ |
439 | 0 | extractJarResources((JarURLConnection) connection, outputDir, keepParentDirectory); |
440 | |
} |
441 | |
else |
442 | |
{ |
443 | 0 | extractFileResources(normalizeFilePath(url, MuleManager.getConfiguration().getEncoding()), outputDir, resourceName, keepParentDirectory); |
444 | |
} |
445 | 0 | } |
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
private static void extractFileResources(String path, File outputDir, String resourceName, boolean keepParentDirectory) throws IOException |
457 | |
{ |
458 | 0 | File file = FileUtils.newFile(path); |
459 | 0 | if (!file.exists()) |
460 | |
{ |
461 | 0 | throw new IOException("The resource by path " + path + " "); |
462 | |
} |
463 | 0 | if (file.isDirectory()) |
464 | |
{ |
465 | 0 | if (keepParentDirectory) |
466 | |
{ |
467 | 0 | outputDir = FileUtils.newFile(outputDir.getPath() + File.separator + resourceName); |
468 | 0 | if (!outputDir.exists()) |
469 | |
{ |
470 | 0 | outputDir.mkdirs(); |
471 | |
} |
472 | |
} |
473 | |
else |
474 | |
{ |
475 | 0 | outputDir = FileUtils.newFile(outputDir.getPath()); |
476 | |
} |
477 | 0 | copyDirectory(file, outputDir); |
478 | |
} |
479 | |
else |
480 | |
{ |
481 | |
|
482 | 0 | if (keepParentDirectory) |
483 | |
{ |
484 | 0 | outputDir = FileUtils.newFile(outputDir.getPath() + File.separator + resourceName); |
485 | |
} |
486 | |
else |
487 | |
{ |
488 | 0 | outputDir = FileUtils.newFile(outputDir.getPath() + File.separator + file.getName()); |
489 | |
} |
490 | 0 | copyFile(file, outputDir); |
491 | |
} |
492 | 0 | } |
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
private static void extractJarResources(JarURLConnection connection, File outputDir, boolean keepParentDirectory) throws IOException |
503 | |
{ |
504 | 0 | JarFile jarFile = connection.getJarFile(); |
505 | 0 | JarEntry jarResource = connection.getJarEntry(); |
506 | 0 | Enumeration entries = jarFile.entries(); |
507 | 0 | InputStream inputStream = null; |
508 | 0 | OutputStream outputStream = null; |
509 | 0 | int jarResourceNameLenght = jarResource.getName().length(); |
510 | 0 | for (; entries.hasMoreElements();) |
511 | |
{ |
512 | 0 | JarEntry entry = (JarEntry) entries.nextElement(); |
513 | 0 | if (entry.getName().startsWith(jarResource.getName())) |
514 | |
{ |
515 | |
|
516 | 0 | String path = outputDir.getPath() + File.separator + entry.getName(); |
517 | |
|
518 | |
|
519 | 0 | if (!keepParentDirectory) |
520 | |
{ |
521 | 0 | if (entry.isDirectory()) |
522 | |
{ |
523 | 0 | if (entry.getName().equals(jarResource.getName())) |
524 | |
{ |
525 | 0 | continue; |
526 | |
} |
527 | 0 | path = outputDir.getPath() + File.separator + entry.getName().substring(jarResourceNameLenght, entry.getName().length()); |
528 | |
} |
529 | |
else |
530 | |
{ |
531 | 0 | if (entry.getName().length() > jarResourceNameLenght) |
532 | |
{ |
533 | 0 | path = outputDir.getPath() + File.separator + entry.getName().substring(jarResourceNameLenght, entry.getName().length()); |
534 | |
} |
535 | |
else |
536 | |
{ |
537 | 0 | path = outputDir.getPath() + File.separator + entry.getName().substring(entry.getName().lastIndexOf("/"), entry.getName().length()); |
538 | |
} |
539 | |
} |
540 | |
} |
541 | |
|
542 | 0 | File file = FileUtils.newFile(path); |
543 | 0 | if (!file.getParentFile().exists()) |
544 | |
{ |
545 | 0 | if (!file.getParentFile().mkdirs()) |
546 | |
{ |
547 | 0 | throw new IOException("Could not create directory: " + file.getParentFile()); |
548 | |
} |
549 | |
} |
550 | 0 | if (entry.isDirectory()) |
551 | |
{ |
552 | 0 | if (!file.exists() && !file.mkdirs()) |
553 | |
{ |
554 | 0 | throw new IOException("Could not create directory: " + file); |
555 | |
} |
556 | |
|
557 | |
} |
558 | |
else |
559 | |
{ |
560 | |
try |
561 | |
{ |
562 | 0 | inputStream = jarFile.getInputStream(entry); |
563 | 0 | outputStream = new BufferedOutputStream(new FileOutputStream(file)); |
564 | 0 | IOUtils.copy(inputStream, outputStream); |
565 | |
} |
566 | |
finally |
567 | |
{ |
568 | 0 | IOUtils.closeQuietly(inputStream); |
569 | 0 | IOUtils.closeQuietly(outputStream); |
570 | 0 | } |
571 | |
} |
572 | |
|
573 | |
} |
574 | |
} |
575 | 0 | } |
576 | |
|
577 | |
|
578 | |
} |