1 6 7 package org.jzonic.jlo.reader; 8 import java.io.*; 9 import java.net.URL ; 10 import java.util.StringTokenizer ; 11 import java.util.Vector ; 12 24 public class ResourceLocator implements Serializable { 25 26 private String myName; 27 private File myFile; 28 private URL myUrl; 29 30 31 public ResourceLocator() { 32 } 33 34 35 public ResourceLocator(String name) throws IOException { 36 myName = name; 37 SecurityException exception = null; 38 39 try { 40 if (tryClasspath(name)) { 43 return; 44 } 45 } catch (SecurityException e) { 46 exception = e; } 48 49 try { 50 if (tryLoader(name)) { 53 return; 54 } 55 } catch (SecurityException e) { 56 exception = e; } 58 59 String msg = ""; 61 if (exception != null) { 62 msg = ": " + exception; 63 } 64 65 throw new IOException( 66 "Resource <" 67 + name 68 + "> could not be found in " 69 + "the CLASSPATH (" 70 + System.getProperty("java.class.path") 71 + "), nor could it be located by the classloader responsible for the " 72 + "web application (WEB-INF/classes)" 73 + msg); 74 } 75 76 81 public InputStream findResource(String fileName) { 82 if ( fileName == null ) { 83 return null; 84 } 85 return getClass().getClassLoader().getResourceAsStream(fileName); 86 } 87 88 91 public String getName() { 92 return myName; 93 } 94 95 98 public InputStream getInputStream() throws IOException { 99 if (myFile != null) { 100 return new BufferedInputStream(new FileInputStream(myFile)); 101 } else if (myUrl != null) { 102 return new BufferedInputStream(myUrl.openStream()); 103 } 104 return null; 105 } 106 107 115 public long lastModified() { 116 if (myFile != null) { 117 return myFile.lastModified(); 118 } else if (myUrl != null) { 119 try { 120 return myUrl.openConnection().getLastModified(); } catch (IOException e) { 122 return Long.MAX_VALUE; 123 } 124 } 125 return 0; } 127 128 134 public String getDirectory() { 135 if (myFile != null) { 136 return myFile.getParent(); 137 } else if (myUrl != null) { 138 return null; 139 } 140 return null; 141 } 142 143 private boolean tryClasspath(String filename) { 145 if(filename == null) return false; 146 String classpath = System.getProperty("java.class.path"); 147 String [] paths = split(classpath, File.pathSeparator); 148 myFile = searchDirectories(paths, filename); 149 return (myFile != null); 150 } 151 152 private static File searchDirectories(String [] paths, String filename) { 153 SecurityException exception = null; 154 for (int i = 0; i < paths.length; i++) { 155 try { 156 File file = new File(paths[i], filename); 157 if (file.exists() && !file.isDirectory()) { 158 return file; 159 } 160 } catch (SecurityException e) { 161 exception = e; 164 } 165 } 166 if (exception != null) { 168 throw exception; 169 } else { 170 return null; 171 } 172 } 173 174 private static String [] split(String str, String delim) { 178 Vector v = new Vector (); 180 181 StringTokenizer tokenizer = new StringTokenizer (str, delim); 183 while (tokenizer.hasMoreTokens()) { 184 v.addElement(tokenizer.nextToken()); 185 } 186 187 String [] ret = new String [v.size()]; 188 v.copyInto(ret); 189 return ret; 190 } 191 192 private boolean tryLoader(String name) { 194 name = "/" + name; 195 URL res = ResourceLocator.class.getResource(name); 196 if (res == null) { 197 return false; 198 } 199 200 File resFile = urlToFile(res); 202 if (resFile != null) { 203 myFile = resFile; 204 } else { 205 myUrl = res; 206 } 207 return true; 208 } 209 210 private static File urlToFile(URL res) { 211 String externalForm = res.toExternalForm(); 212 if (externalForm.startsWith("file:")) { 213 return new File(externalForm.substring(5)); 214 } 215 return null; 216 } 217 218 public String toString() { 219 return "[Resource: File: " + myFile + " URL: " + myUrl + "]"; 220 } 221 222 } 223 | Popular Tags |