| 1 |
package org.globus.util; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.net.MalformedURLException; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.io.FileInputStream; |
| 7 |
import java.io.FileNotFoundException; |
| 8 |
import java.io.IOException; |
| 9 |
import java.net.URL; |
| 10 |
import java.net.URI; |
| 11 |
|
| 12 |
import org.apache.commons.io.IOExceptionWithCause; |
| 13 |
|
| 14 |
/** |
| 15 |
* This class provides a way of managing file system resources |
| 16 |
* in a manner similar to the spring framework's Resource class. |
| 17 |
* |
| 18 |
* 3/2/2012 |
| 19 |
*/ |
| 20 |
|
| 21 |
public class GlobusResource { |
| 22 |
|
| 23 |
//A file instance of the specified resourcePath |
| 24 |
private File resourceFile = null; |
| 25 |
//The path to the file |
| 26 |
private String resourcePath = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* Creates a new instance of GlobusResource referencing the |
| 30 |
* specified resourcePath. |
| 31 |
* |
| 32 |
* @param resourcePath The path to the specified resource in the style |
| 33 |
* /directory/directory/fileName.ext |
| 34 |
*/ |
| 35 |
public GlobusResource(String resourcePath) { |
| 36 |
this.resourcePath = resourcePath; |
| 37 |
this.resourceFile = new File(resourcePath); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @return A string representation of the resource's URI |
| 42 |
*/ |
| 43 |
public String toURI() { |
| 44 |
return resourceFile.toURI().toASCIIString(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return A string representation of the resource's URL |
| 49 |
* @throws MalformedURLException |
| 50 |
*/ |
| 51 |
public String toURL() throws MalformedURLException { |
| 52 |
String fileURLPathString = null; |
| 53 |
fileURLPathString = resourceFile.toURI().toURL().toString(); |
| 54 |
return fileURLPathString; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return The resource's URI(java.Net.URI) |
| 59 |
* @throws IOException |
| 60 |
*/ |
| 61 |
public URI getURI() throws IOException { |
| 62 |
return resourceFile.toURI(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @return The resource's URL(java.Net.URL) |
| 67 |
* @throws MalformedURLException |
| 68 |
*/ |
| 69 |
public URL getURL() throws MalformedURLException { |
| 70 |
return resourceFile.toURI().toURL(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @return A new java.io.File object referencing the resource's resourcePath |
| 75 |
* @throws IOException |
| 76 |
*/ |
| 77 |
public File getFile() throws IOException { |
| 78 |
File duplicateFile = new File(this.resourceFile.getAbsolutePath()); |
| 79 |
return duplicateFile; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @return True if the resource exists, and false if the resource does not exist |
| 84 |
*/ |
| 85 |
public boolean exists() { |
| 86 |
return this.resourceFile.exists(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @return The time the resource was last modified |
| 91 |
* @throws IOException |
| 92 |
*/ |
| 93 |
public long lastModified() throws IOException { |
| 94 |
return this.resourceFile.lastModified(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @return True if the resource is readable, false if the resource is not readable |
| 99 |
*/ |
| 100 |
public boolean isReadable() { |
| 101 |
return this.resourceFile.canRead(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* @return A new InputStream(java.io.InputStream) of the resource |
| 106 |
* @throws FileNotFoundException |
| 107 |
* @throws IOException |
| 108 |
*/ |
| 109 |
public InputStream getInputStream() throws FileNotFoundException, IOException { |
| 110 |
InputStream fileInputStream = new FileInputStream(this.getFile()); |
| 111 |
return fileInputStream; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @return The name of the resource in the style fileName.ext |
| 116 |
*/ |
| 117 |
public String getFilename() { |
| 118 |
return this.resourcePath.substring(resourcePath.lastIndexOf("/") + 1, resourcePath.length()); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @return A string representing resourcePath and URI of the resource |
| 123 |
*/ |
| 124 |
@Override |
| 125 |
public String toString() { |
| 126 |
return String.format("resourcePath: %s\nURI: %s\n", this.resourcePath, this.toURI()); |
| 127 |
} |
| 128 |
} |