1 19 20 package org.apache.cayenne.util; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 40 public class ResourceLocator { 41 42 private static Log logObj = LogFactory.getLog(ResourceLocator.class); 43 44 protected boolean skipAbsolutePath; 46 protected boolean skipClasspath; 47 protected boolean skipCurrentDirectory; 48 protected boolean skipHomeDirectory; 49 50 protected List additionalClassPaths; 52 protected List additionalFilesystemPaths; 53 54 protected ClassLoader classLoader; 56 57 62 public static InputStream findResourceInClasspath(String name) { 63 try { 64 URL url = findURLInClasspath(name); 65 if (url != null) { 66 logObj.debug("resource found in classpath: " + url); 67 return url.openStream(); 68 } 69 else { 70 logObj.debug("resource not found in classpath: " + name); 71 return null; 72 } 73 } 74 catch (IOException ioex) { 75 return null; 76 } 77 } 78 79 85 public static InputStream findResourceInFileSystem(String name) { 86 try { 87 File file = findFileInFileSystem(name); 88 if (file != null) { 89 logObj.debug("resource found in file system: " + file); 90 return new FileInputStream (file); 91 } 92 else { 93 logObj.debug("resource not found in file system: " + name); 94 return null; 95 } 96 } 97 catch (IOException ioex) { 98 return null; 99 } 100 } 101 102 111 public static File findFileInFileSystem(String name) { 112 File file = findFileInHomeDirectory(name); 113 114 if (file == null) { 115 file = findFileInCurrentDirectory(name); 116 } 117 118 if (file != null) { 119 logObj.debug("file found in file system: " + file); 120 } 121 else { 122 logObj.debug("file not found in file system: " + name); 123 } 124 125 return file; 126 } 127 128 134 public static File findFileInHomeDirectory(String name) { 135 String homeDirPath = System.getProperty("user.home") + File.separator + name; 137 138 try { 139 140 File file = new File (homeDirPath); 141 if (file.exists() && file.canRead()) { 142 logObj.debug("file found in home directory: " + file); 143 } 144 else { 145 file = null; 146 logObj.debug("file not found in home directory: " + name); 147 } 148 149 return file; 150 } 151 catch (SecurityException se) { 152 logObj.debug("permission denied reading file: " + homeDirPath, se); 153 return null; 154 } 155 } 156 157 163 public static File findFileInCurrentDirectory(String name) { 164 String currentDirPath = System.getProperty("user.dir") + File.separator + name; 166 167 try { 168 169 File file = new File (currentDirPath); 170 171 if (file.exists() && file.canRead()) { 172 logObj.debug("file found in current directory: " + file); 173 } 174 else { 175 logObj.debug("file not found in current directory: " + name); 176 file = null; 177 } 178 179 return file; 180 } 181 catch (SecurityException se) { 182 logObj.debug("permission denied reading file: " + currentDirPath, se); 183 return null; 184 } 185 } 186 187 190 public static URL findURLInClasspath(String name) { 191 ClassLoader classLoader = ResourceLocator.class.getClassLoader(); 192 if (classLoader == null) { 193 classLoader = ClassLoader.getSystemClassLoader(); 194 } 195 return findURLInClassLoader(name, classLoader); 196 } 197 198 201 public static URL findURLInClassLoader(String name, ClassLoader loader) { 202 URL url = loader.getResource(name); 203 204 if (url != null) { 205 logObj.debug("URL found with classloader: " + url); 206 } 207 else { 208 logObj.debug("URL not found with classloader: " + name); 209 } 210 211 return url; 212 } 213 214 218 public static String classBaseUrl(Class aClass) { 219 String pathToClass = aClass.getName().replace('.', '/') + ".class"; 220 ClassLoader classLoader = aClass.getClassLoader(); 221 222 if (classLoader == null) { 223 classLoader = ClassLoader.getSystemClassLoader(); 224 } 225 226 URL selfUrl = classLoader.getResource(pathToClass); 227 228 if (selfUrl == null) { 229 return null; 230 } 231 232 String urlString = selfUrl.toExternalForm(); 233 return urlString.substring(0, urlString.length() - pathToClass.length()); 234 } 235 236 240 public ResourceLocator() { 241 this.additionalClassPaths = new ArrayList (); 242 this.additionalFilesystemPaths = new ArrayList (); 243 } 244 245 250 public InputStream findResourceStream(String name) { 251 URL url = findResource(name); 252 if (url == null) { 253 return null; 254 } 255 256 try { 257 return url.openStream(); 258 } 259 catch (IOException ioex) { 260 logObj.debug("Error reading URL, ignoring", ioex); 261 return null; 262 } 263 } 264 265 270 public URL findResource(String name) { 271 if (!willSkipAbsolutePath()) { 272 File f = new File (name); 273 if (f.isAbsolute() && f.exists()) { 274 logObj.debug("File found at absolute path: " + name); 275 try { 276 return f.toURL(); 277 } 278 catch (MalformedURLException ex) { 279 logObj.debug("Malformed url, ignoring.", ex); 281 } 282 } 283 else { 284 logObj.debug("No file at absolute path: " + name); 285 } 286 } 287 288 if (!willSkipHomeDirectory()) { 289 File f = findFileInHomeDirectory(name); 290 if (f != null) { 291 292 try { 293 return f.toURL(); 294 } 295 catch (MalformedURLException ex) { 296 logObj.debug("Malformed url, ignoring", ex); 298 } 299 } 300 } 301 302 if (!willSkipCurrentDirectory()) { 303 File f = findFileInCurrentDirectory(name); 304 if (f != null) { 305 306 try { 307 return f.toURL(); 308 } 309 catch (MalformedURLException ex) { 310 logObj.debug("Malformed url, ignoring", ex); 312 } 313 } 314 } 315 316 if (!additionalFilesystemPaths.isEmpty()) { 317 logObj.debug("searching additional paths: " + this.additionalFilesystemPaths); 318 Iterator pi = this.additionalFilesystemPaths.iterator(); 319 while (pi.hasNext()) { 320 File f = new File ((String ) pi.next(), name); 321 logObj.debug("searching for: " + f.getAbsolutePath()); 322 if (f.exists()) { 323 try { 324 return f.toURL(); 325 } 326 catch (MalformedURLException ex) { 327 logObj.debug("Malformed URL, ignoring.", ex); 329 } 330 } 331 } 332 } 333 334 if (!willSkipClasspath()) { 335 336 if (!this.additionalClassPaths.isEmpty()) { 338 logObj.debug("searching additional classpaths: " 339 + this.additionalClassPaths); 340 Iterator cpi = this.additionalClassPaths.iterator(); 341 while (cpi.hasNext()) { 342 String fullName = cpi.next() + "/" + name; 343 logObj.debug("searching for: " + fullName); 344 URL url = findURLInClassLoader(fullName, getClassLoader()); 345 if (url != null) { 346 return url; 347 } 348 } 349 } 350 351 URL url = findURLInClassLoader(name, getClassLoader()); 352 if (url != null) { 353 return url; 354 } 355 } 356 357 return null; 358 } 359 360 366 public URL findDirectoryResource(String name) { 367 URL url = findResource(name); 368 if (url == null) { 369 return null; 370 } 371 372 try { 373 String urlSt = url.toExternalForm(); 374 return (urlSt.endsWith("/")) ? url : new URL (urlSt + "/"); 375 } 376 catch (MalformedURLException ex) { 377 logObj.debug("Malformed URL, ignoring.", ex); 379 return null; 380 } 381 } 382 383 386 public boolean willSkipHomeDirectory() { 387 return skipHomeDirectory; 388 } 389 390 393 public void setSkipHomeDirectory(boolean skipHomeDir) { 394 this.skipHomeDirectory = skipHomeDir; 395 } 396 397 400 public boolean willSkipCurrentDirectory() { 401 return skipCurrentDirectory; 402 } 403 404 407 public void setSkipCurrentDirectory(boolean skipCurDir) { 408 this.skipCurrentDirectory = skipCurDir; 409 } 410 411 414 public boolean willSkipClasspath() { 415 return skipClasspath; 416 } 417 418 421 public void setSkipClasspath(boolean skipClasspath) { 422 this.skipClasspath = skipClasspath; 423 } 424 425 428 public ClassLoader getClassLoader() { 429 ClassLoader loader = this.classLoader; 430 431 if (loader == null) { 432 loader = Thread.currentThread().getContextClassLoader(); 433 } 434 435 if (loader == null) { 436 loader = getClass().getClassLoader(); 437 } 438 439 if (loader == null) { 440 loader = ClassLoader.getSystemClassLoader(); 441 } 442 443 return loader; 444 } 445 446 450 public void setClassLoader(ClassLoader classLoader) { 451 this.classLoader = classLoader; 452 } 453 454 457 public boolean willSkipAbsolutePath() { 458 return skipAbsolutePath; 459 } 460 461 464 public void setSkipAbsolutePath(boolean skipAbsPath) { 465 this.skipAbsolutePath = skipAbsPath; 466 } 467 468 472 public void addClassPath(String customPath) { 473 this.additionalClassPaths.add(customPath); 474 } 475 476 482 public void addFilesystemPath(String path) { 483 if (path != null) { 484 this.additionalFilesystemPaths.add(path); 485 } 486 else { 487 throw new IllegalArgumentException ("Path must not be null."); 488 } 489 } 490 491 498 public void addFilesystemPath(File path) { 499 if (path != null && path.isDirectory()) { 500 this.addFilesystemPath(path.getPath()); 501 } 502 else { 503 throw new IllegalArgumentException ("Path '" + path + "' is not a directory."); 504 } 505 } 506 } 507 | Popular Tags |