View Javadoc
1 package com.bonevich.util; 2 3 import java.util.jar.JarFile; 4 import java.io.File; 5 import java.io.InputStream; 6 import java.io.IOException; 7 import java.net.URL; 8 9 /*** 10 * Utility functions for retrieving and dealing with resource files 11 * and streams (i.e. classpath-relative files and URLs) given a path 12 * to a resource file. 13 * @author Jeffrey Bonevich <bonevich@telocity.com> 14 * @version $Id: ResourceUtilities.html,v 1.1 2009/03/07 17:55:36 jbonevic Exp $ 15 */ 16 public class ResourceUtilities 17 { 18 /*** 19 * Make constructor private - this is a utility class not to be instantiated! 20 */ 21 private ResourceUtilities() {} 22 23 /*** 24 * Retrieve the <code>URL</code> for a resource from the System classloader 25 * given the file path of the resource. 26 * @param path java.lang.String 27 */ 28 public static URL getResourceAsURL(String path) 29 { 30 return getResourceAsURL(path, null); 31 } 32 33 /*** 34 * Retrieve the URL for a resource from the same classloader of 35 * <code>obj</code> given the file path of the resource. If no 36 * such resource exists, find it relative to the System classloader. 37 * @param path java.lang.String 38 * @param obj java.lang.Object 39 */ 40 public static URL getResourceAsURL(String path, Object obj) 41 { 42 if (obj != null) 43 { 44 ClassLoader cl = obj.getClass().getClassLoader(); 45 if (cl != null) { 46 return cl.getResource(path); 47 } 48 } 49 return ClassLoader.getSystemResource(path); 50 } 51 52 /*** 53 * Returns the current last modification date of the resource file. 54 * @param java.net.URL url The URL of the resource file 55 * @return long The last modification date of the resource file in 56 * milliseconds since the epoch (UNIX time). If the resource file 57 * is not a local file system file or an entry in a local file system 58 * jar file, this method always returns 0. 59 */ 60 public static long lastModified(URL url) { 61 return lastModified(url,null); 62 } 63 64 /*** 65 * Returns the current last modification date of the resource file. If 66 * the resource is inside a jar file, the jar entry is examined for this 67 * information. 68 * @param java.net.URL url The URL of the resource file 69 * @param java.lang.String filepath The path to the resource file, 70 * independent of the URL/protocol used to access it. 71 * @return long The last modification date of the resource file in 72 * milliseconds since the epoch (UNIX time). If the resource file 73 * is not a local file system file or an entry in a local file system 74 * jar file, this method always returns 0. 75 */ 76 public static long lastModified(URL url, String filePath) 77 { 78 long lastMod = 0; 79 String protocol = url.getProtocol(); 80 // visual-age produces a protocol of valoader for files (I don't know about jars!) 81 if (protocol.equals("file") || protocol.equals("valoader")) 82 { 83 File file = new File(url.getFile()); 84 lastMod = file.lastModified(); 85 //System.err.println("file: " + url.getFile() + " last mod date = " + new Date(lastMod)); 86 } 87 else if (filePath!=null && protocol.equals("jar")) 88 { 89 String fileName = url.getFile(); 90 if (fileName.indexOf("file:") >= 0) // if this is a local file system jar 91 { 92 // FIXME: This is being *very* sneaky 93 // Jar file URLs are of the form 'file:path-to-jar!path-to-resource-inside-jar' 94 // so we are taking a chance here that this won't change over time! 95 int bangIdx = fileName.indexOf("!"); 96 try { 97 // extract the jar file name 98 JarFile jar = new JarFile( fileName.substring(5,bangIdx) ); 99 lastMod = jar.getEntry(filePath).getTime(); 100 } catch (java.lang.Exception e) { 101 lastMod = 0; 102 } 103 } 104 } 105 return lastMod; 106 } 107 108 /*** 109 * Retrieve the <code>InputStream</code> for a resource from the System classloader 110 * given the file path of the resource. 111 * @param path java.lang.String 112 */ 113 public static InputStream getResourceStream(String path) throws IOException 114 { 115 return getResourceAsURL(path).openStream(); 116 } 117 118 /*** 119 * Retrieve the <code>InputStream</code> for a resource from the same 120 * classloader of <code>obj</code> given the file path of the resource. 121 * If no such resource exists, find it relative to the System classloader. 122 * @param path java.lang.String 123 * @param obj java.lang.Object 124 */ 125 public static InputStream getResourceStream(String path, Object obj) throws IOException 126 { 127 return getResourceAsURL(path, obj).openStream(); 128 } 129 }

This page was automatically generated by Maven