Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 239   Methods: 13
NCLOC: 142   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ResourceLocator.java 40,6% 55,7% 69,2% 53%
coverage coverage
 1    /*
 2    * ResourceLocator.java
 3    *
 4    * Created on 21. Januar 2003, 21:33
 5    */
 6   
 7    package org.jconfig.utils;
 8    import java.io.BufferedInputStream;
 9    import java.io.File;
 10    import java.io.FileInputStream;
 11    import java.io.IOException;
 12    import java.io.InputStream;
 13    import java.net.URL;
 14    import java.net.URLDecoder;
 15    import java.util.StringTokenizer;
 16    import java.util.Vector;
 17    /**
 18    * Adopted from <a href="http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index1.html?page=3">Servlet Best Practices, Part 1</a>
 19    *
 20    * A class to locate resources, retrieve their contents, and determine their
 21    * last modified time. To find the resource the class searches the CLASSPATH
 22    * first, then Resource.class.getResource("/" + name). If the Resource finds
 23    * a "file:" URL, the file path will be treated as a file. Otherwise, the
 24    * path is treated as a URL and has limited last modified info.
 25    *
 26    * @author Andreas Mecky <andreas.mecky@jzonic.org>
 27    * @author Terry Dye <terry.dye@jzonic.org>
 28    * @author Steve Braun
 29    */
 30    public class ResourceLocator {
 31   
 32    private String name;
 33    private File file;
 34    private URL url;
 35   
 36  125 public ResourceLocator(String name) throws IOException {
 37  125 this.name = name;
 38  125 SecurityException exception = null;
 39   
 40  125 try {
 41    // Search using the CLASSPATH. If found, "file" is set and the call
 42    // returns true. A SecurityException might bubble up.
 43  125 if (tryClasspath(name)) {
 44  120 return;
 45    }
 46    } catch (SecurityException e) {
 47  0 exception = e; // Save for later.
 48    }
 49   
 50  5 try {
 51    // Search using the classloader getResource( ). If found as a file,
 52    // "file" is set; if found as a URL, "url" is set.
 53  5 if (tryLoader(name)) {
 54  0 return;
 55    }
 56    } catch (SecurityException e) {
 57  0 exception = e; // Save for later.
 58    }
 59   
 60    // If you get here, something went wrong. Report the exception.
 61  5 String msg = "";
 62  5 if (exception != null) {
 63  0 msg = ": " + exception;
 64    }
 65   
 66  5 throw new IOException(
 67    "Resource '"
 68    + name
 69    + "' could not be found in "
 70    + "the CLASSPATH ("
 71    + System.getProperty("java.class.path")
 72    + "), nor could it be located by the classloader responsible for the "
 73    + "web application (WEB-INF/classes)"
 74    + msg);
 75    }
 76   
 77    /**
 78    * Method findResource.
 79    * @param fileName
 80    * @return InputStream
 81    */
 82  2 public InputStream findResource(String fileName) {
 83  2 return getClass().getClassLoader().getResourceAsStream(fileName);
 84    }
 85   
 86    /**
 87    * Returns the resource name, as passed to the constructor
 88    */
 89  1 public String getName() {
 90  1 return name;
 91    }
 92   
 93    /**
 94    * Returns an input stream to read the resource contents
 95    */
 96  116 public InputStream getInputStream() throws IOException {
 97  116 if (file != null) {
 98  116 return new BufferedInputStream(new FileInputStream(file));
 99  0 } else if (url != null) {
 100  0 return new BufferedInputStream(url.openStream());
 101    }
 102  0 return null;
 103    }
 104   
 105    /**
 106    * Returns when the resource was last modified. If the resource
 107    * was found using a URL, this method will work only if the URL
 108    * connection supports last modified information. If there's no
 109    * support, Long.MAX_VALUE is returned. Perhaps this should return
 110    * -1, but you should return MAX_VALUE on the assumption that if
 111    * you can't determine the time, it's maximally new.
 112    */
 113  0 public long lastModified() {
 114  0 if (file != null) {
 115  0 return file.lastModified();
 116  0 } else if (url != null) {
 117  0 try {
 118  0 return url.openConnection().getLastModified(); // Hail Mary
 119    } catch (IOException e) {
 120  0 return Long.MAX_VALUE;
 121    }
 122    }
 123  0 return 0; // can't happen
 124    }
 125   
 126    /**
 127    * Returns the directory containing the resource, or null if the
 128    * resource isn't directly available on the filesystem.
 129    * This value can be used to locate the configuration file on disk,
 130    * or to write files in the same directory.
 131    */
 132  0 public String getDirectory() {
 133  0 if (file != null) {
 134  0 return file.getParent();
 135  0 } else if (url != null) {
 136  0 return null;
 137    }
 138  0 return null;
 139    }
 140   
 141    // Returns true if found
 142  125 private boolean tryClasspath(String filename) {
 143  125 String classpath = System.getProperty("java.class.path");
 144    //String[] paths = split(classpath, File.separator);
 145    // TODO: test it
 146  125 String[] paths = split(classpath,System.getProperty("path.separator"));
 147  125 file = searchDirectories(paths, filename);
 148  125 return (file != null);
 149    }
 150   
 151  125 private static File searchDirectories(String[] paths, String filename) {
 152  125 SecurityException exception = null;
 153  125 for (int i = 0; i < paths.length; i++) {
 154  400 try {
 155    //path += paths[ i ] + File.separator;
 156    //File file = new File(path, filename);
 157  400 File file = new File(paths[i], filename);
 158    //System.out.println("Lookup " + file.getAbsolutePath()); //ADDED CODE
 159  400 if (file.exists() && !file.isDirectory()) {
 160  120 return file;
 161    }
 162  280 file = new File(paths[i]);
 163    //String[] subPaths = getSubDirectories(file);
 164    //if ( subPaths != null && subPaths.length > 0 ) {
 165    // searchDirectories(subPaths,filename);
 166    // }
 167    } catch (SecurityException e) {
 168    // Security exceptions can usually be ignored, but if all attempts
 169    // to find the file fail, report the (last) security exception.
 170  0 exception = e;
 171    }
 172    }
 173    // Couldn't find any match
 174  5 if (exception != null) {
 175  0 throw exception;
 176    } else {
 177  5 return null;
 178    }
 179    }
 180   
 181    // Splits a String into pieces according to a delimiter.
 182    // Uses JDK 1.1 classes for backward compatibility.
 183    // JDK 1.4 actually has a split( ) method now.
 184  125 private static String[] split(String str, String delim) {
 185    // Use a Vector to hold the split strings.
 186  125 Vector v = new Vector();
 187   
 188    // Use a StringTokenizer to do the splitting.
 189  125 StringTokenizer tokenizer = new StringTokenizer(str, delim);
 190  125 while (tokenizer.hasMoreTokens()) {
 191  1000 v.addElement(tokenizer.nextToken());
 192    }
 193   
 194  125 String[] ret = new String[v.size()];
 195  125 v.copyInto(ret);
 196  125 return ret;
 197    }
 198   
 199    // Returns true if found
 200  5 private boolean tryLoader(String name) {
 201  5 name = "/" + name;
 202  5 URL res = ResourceLocator.class.getResource(name);
 203  5 if (res == null) {
 204  5 return false;
 205    }
 206   
 207    // Try converting from a URL to a File.
 208  0 File resFile = urlToFile(res);
 209  0 if (resFile != null) {
 210  0 file = resFile;
 211    } else {
 212  0 url = res;
 213    }
 214  0 return true;
 215    }
 216   
 217  0 private static File urlToFile(URL res) {
 218    // decode URL
 219    // patch by: Apu Shah ashah@randomwalk.com
 220  0 String externalForm = URLDecoder.decode(res.toExternalForm());
 221  0 if (externalForm.startsWith("file:")) {
 222  0 return new File(externalForm.substring(5));
 223    }
 224  0 return null;
 225    }
 226   
 227  0 public String toString() {
 228  0 return "[Resource: File: " + file + " URL: " + url + "]";
 229    }
 230   
 231    /**
 232    * Returns the file.
 233    * @return File
 234    */
 235  1 public File getFile() {
 236  1 return file;
 237    }
 238   
 239    }