Subversion Repositories DevTools

Rev

Rev 6914 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * A collection of utility methods used by all components
 * Should all be public static
 */
package com.erggroup.buildtool.utilities;

import java.io.File;

public class utilities {
    
    private static final String fs = System.getProperty( "file.separator" );
    
    private utilities() {
        throw new IllegalStateException("utilities class");
      }
    
    /** Create a File object based on the provided path
     *  <br>Will attempt to flush OS file and directory caches so to overcome
     *  issues seen on NFS file systems. The caches will have been freshened
     *  <p>Only really needed when testing for the existence of files that may be
     *  on file systems that are NFS mounted.
     *  <p> Don't use too often as it may cause excessive network overhead.
     * 
     * @param path
     * @return A File object, suitable for isFile and isDirectory testing
     */
    public static File freshFile( String path)
    {
        // Walk up the path forcing a directory read at every step
        File target = new File(path);
        while (target != null )
        {
            @SuppressWarnings("unused")
            File[] contents = target.listFiles();
            
            target = target.getParentFile();
        }
        
        // Create a File object
        return new File(path);
    }
    
    /** Test for existence of a file taking into account directory caching
     *  Assume that if that the file being tested may have been recently added
     *  
     *   @param path
     *   @return True if the file exists
     */
    public static boolean freshFileExists( String path )
    {
        File target = new File(path);
        
        // See if the file exists using a quick check
        if (target.exists()) {
            return true;
        }
        
        // File does exist after a quick check
        // Attempt to flush the file caches and test again
        //
        // Walk up the path forcing a directory read at every step
        while (target != null )
        {
            @SuppressWarnings("unused")
            File[] contents = target.listFiles();
            
            target = target.getParentFile();
        }
        
        // Try again
        target = new File(path);
        return target.exists();
    }

    /** Create a pathname from an array of strings
     *  @param  elements - One or more strings to concatenate. Null items will be ignored
     *  @return - A string. Will not be null, may be empty.
     */
    public static String catDir(String ... elements)
    {
        StringBuilder sbStr = new StringBuilder();
        for (int i = 0, il = elements.length; i < il; i++) {
            if ( elements[i] != null ) {
                if (sbStr.length() > 0)
                    sbStr.append(fs);
                sbStr.append(elements[i]);
            }
        }

        return sbStr.toString();        
    }
    
}