| 6914 |
dpurdie |
1 |
/**
|
|
|
2 |
* A collection of utility methods used by all components
|
|
|
3 |
* Should all be public static
|
|
|
4 |
*/
|
|
|
5 |
package com.erggroup.buildtool.utilities;
|
|
|
6 |
|
|
|
7 |
import java.io.File;
|
|
|
8 |
|
|
|
9 |
public class utilities {
|
|
|
10 |
|
|
|
11 |
private static final String fs = System.getProperty( "file.separator" );
|
|
|
12 |
|
|
|
13 |
private utilities() {
|
|
|
14 |
throw new IllegalStateException("utilities class");
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
/** Create a File object based on the provided path
|
|
|
18 |
* <br>Will attempt to flush OS file and directory caches so to overcome
|
|
|
19 |
* issues seen on NFS file systems. The caches will have been freshened
|
|
|
20 |
* <p>Only really needed when testing for the existence of files that may be
|
|
|
21 |
* on file systems that are NFS mounted.
|
|
|
22 |
* <p> Don't use too often as it may cause excessive network overhead.
|
|
|
23 |
*
|
|
|
24 |
* @param path
|
|
|
25 |
* @return A File object, suitable for isFile and isDirectory testing
|
|
|
26 |
*/
|
|
|
27 |
public static File freshFile( String path)
|
|
|
28 |
{
|
|
|
29 |
// Walk up the path forcing a directory read at every step
|
|
|
30 |
File target = new File(path);
|
|
|
31 |
while (target != null )
|
|
|
32 |
{
|
|
|
33 |
@SuppressWarnings("unused")
|
|
|
34 |
File[] contents = target.listFiles();
|
|
|
35 |
|
|
|
36 |
target = target.getParentFile();
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
// Create a File object
|
|
|
40 |
return new File(path);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/** Test for existence of a file taking into account directory caching
|
|
|
44 |
* Assume that if that the file being tested may have been recently added
|
|
|
45 |
*
|
|
|
46 |
* @param path
|
|
|
47 |
* @return True if the file exists
|
|
|
48 |
*/
|
|
|
49 |
public static boolean freshFileExists( String path )
|
|
|
50 |
{
|
|
|
51 |
File target = new File(path);
|
|
|
52 |
|
|
|
53 |
// See if the file exists using a quick check
|
|
|
54 |
if (target.exists()) {
|
|
|
55 |
return true;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// File does exist after a quick check
|
|
|
59 |
// Attempt to flush the file caches and test again
|
|
|
60 |
//
|
|
|
61 |
// Walk up the path forcing a directory read at every step
|
|
|
62 |
while (target != null )
|
|
|
63 |
{
|
|
|
64 |
@SuppressWarnings("unused")
|
|
|
65 |
File[] contents = target.listFiles();
|
|
|
66 |
|
|
|
67 |
target = target.getParentFile();
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
// Try again
|
|
|
71 |
target = new File(path);
|
|
|
72 |
return target.exists();
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/** Create a pathname from an array of strings
|
|
|
76 |
* @param elements - One or more strings to concatenate. Null items will be ignored
|
|
|
77 |
* @return - A string. Will not be null, may be empty.
|
|
|
78 |
*/
|
|
|
79 |
public static String catDir(String ... elements)
|
|
|
80 |
{
|
|
|
81 |
StringBuilder sbStr = new StringBuilder();
|
|
|
82 |
for (int i = 0, il = elements.length; i < il; i++) {
|
|
|
83 |
if ( elements[i] != null ) {
|
|
|
84 |
if (sbStr.length() > 0)
|
|
|
85 |
sbStr.append(fs);
|
|
|
86 |
sbStr.append(elements[i]);
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
return sbStr.toString();
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
}
|