Coverage Report - org.mule.util.FileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtils
0%
0/158
0%
0/44
4.45
 
 1  
 /*
 2  
  * $Id: FileUtils.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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 CPAL v1.0
 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 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  
  * <code>FileUtils</code> contains useful methods for dealing with files &
 39  
  * directories.
 40  
  */
 41  
 // @ThreadSafe
 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  
     // TODO Document me!
 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  
     // TODO Document me!
 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  
     // TODO Document me!
 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  
      * Reads the incoming String into a file at at the given destination.
 114  
      *
 115  
      * @param filename name and path of the file to create
 116  
      * @param data     the contents of the file
 117  
      * @return the new file.
 118  
      * @throws IOException If the creating or writing to the file stream fails
 119  
      */
 120  
     public static File stringToFile(String filename, String data) throws IOException
 121  
     {
 122  0
         return stringToFile(filename, data, false);
 123  
     }
 124  
 
 125  
     // TODO Document me!
 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  
     // TODO Document me!
 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  
     // TODO Document me!
 158  
     public static String getResourcePath(String resourceName, Class callingClass) throws IOException
 159  
     {
 160  0
         return getResourcePath(resourceName, callingClass, MuleManager.getConfiguration().getEncoding());
 161  
     }
 162  
 
 163  
     // TODO Document me!
 164  
     public static String getResourcePath(String resourceName, Class callingClass, String encoding)
 165  
             throws IOException
 166  
     {
 167  0
         if (resourceName == null)
 168  
         {
 169  
             // no name
 170  0
             return null;
 171  
         }
 172  
 
 173  0
         URL url = IOUtils.getResourceAsUrl(resourceName, callingClass);
 174  0
         if (url == null)
 175  
         {
 176  
             // not found
 177  0
             return null;
 178  
         }
 179  0
         return normalizeFilePath(url, encoding);
 180  
     }
 181  
 
 182  
     /**
 183  
      * Remove from uri to file prefix file:/
 184  
      * Add if need file separator to begin
 185  
      *
 186  
      * @param url      file uri to resource
 187  
      * @param encoding - Java encoding names
 188  
      * @return normalized file path
 189  
      * @throws UnsupportedEncodingException if encoding is unknown
 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  
      * Delete a file tree recursively.
 211  
      * @param dir dir to wipe out
 212  
      * @return false when the first unsuccessful attempt encountered
 213  
      */
 214  
     public static boolean deleteTree(File dir)
 215  
     {
 216  0
         return deleteTree(dir, null);
 217  
     }
 218  
 
 219  
     /**
 220  
      * Delete a file tree recursively. This method additionally tries to be
 221  
      * gentle with specified top-level dirs. E.g. this is the case when a
 222  
      * transaction manager asynchronously handles the recovery log, and the test
 223  
      * wipes out everything, leaving the transaction manager puzzled.  
 224  
      * @param dir dir to wipe out
 225  
      * @param topLevelDirsToIgnore which top-level directories to ignore,
 226  
      *        if null or empty then ignored
 227  
      * @return false when the first unsuccessful attempt encountered
 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  
      * Unzip the specified archive to the given directory
 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  
      * Workaround for JDK bug <a href="http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557">
 327  
      * 4117557</a>. More in-context information at
 328  
      * <a href="http://mule.mulesource.org/jira/browse/MULE-1112">MULE-1112</a>
 329  
      * <p/>
 330  
      * Factory methods correspond to constructors of the <code>java.io.File class</code>.
 331  
      * No physical file created in this method.
 332  
      *
 333  
      * @see File
 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  
      * Workaround for JDK bug <a href="http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557">
 351  
      * 4117557</a>. More in-context information at
 352  
      * <a href="http://mule.mulesource.org/jira/browse/MULE-1112">MULE-1112</a>
 353  
      * <p/>
 354  
      * Factory methods correspond to constructors of the <code>java.io.File class</code>.
 355  
      * No physical file created in this method.
 356  
      *
 357  
      * @see File
 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  
      * Workaround for JDK bug <a href="http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557">
 375  
      * 4117557</a>. More in-context information at
 376  
      * <a href="http://mule.mulesource.org/jira/browse/MULE-1112">MULE-1112</a>
 377  
      * <p/>
 378  
      * Factory methods correspond to constructors of the <code>java.io.File class</code>.
 379  
      * No physical file created in this method.
 380  
      *
 381  
      * @see File
 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  
      * Workaround for JDK bug <a href="http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557">
 400  
      * 4117557</a>. More in-context information at
 401  
      * <a href="http://mule.mulesource.org/jira/browse/MULE-1112">MULE-1112</a>
 402  
      * <p/>
 403  
      * Factory methods correspond to constructors of the <code>java.io.File class</code>.
 404  
      * No physical file created in this method.
 405  
      *
 406  
      * @see File
 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  
      * Extract the specified resource to the given directory for
 425  
      * remain all directory struct
 426  
      *
 427  
      * @param resourceName        - full resource name
 428  
      * @param callingClass        - classloader for this class is used
 429  
      * @param outputDir           - extract to this directory
 430  
      * @param keepParentDirectory true -  full structure of directories is kept; false - file - removed all directories, directory - started from resource point
 431  
      * @throws IOException if any errors
 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  
      * Extract resources contain in file
 449  
      *
 450  
      * @param path                - path to file
 451  
      * @param outputDir           Directory for unpack recources
 452  
      * @param resourceName
 453  
      * @param keepParentDirectory true -  full structure of directories is kept; false - file - removed all directories, directory - started from resource point
 454  
      * @throws IOException if any error
 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  
      * Extract recources contain if jar (have to in classpath)
 496  
      *
 497  
      * @param connection          JarURLConnection to jar library
 498  
      * @param outputDir           Directory for unpack recources
 499  
      * @param keepParentDirectory true -  full structure of directories is kept; false - file - removed all directories, directory - started from resource point
 500  
      * @throws IOException if any error
 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  
                 //remove directory struct for file and first dir for directory
 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  
 }