1 56 package org.objectstyle.cayenne.util; 57 58 import java.io.File ; 59 import java.io.FileInputStream ; 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.net.MalformedURLException ; 63 import java.net.URL ; 64 import java.util.ArrayList ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 68 import org.apache.commons.collections.Predicate; 69 import org.apache.log4j.Level; 70 import org.apache.log4j.Logger; 71 import org.apache.log4j.Priority; 72 import org.objectstyle.cayenne.conf.Configuration; 73 74 79 public class ResourceLocator { 80 81 private static Logger logObj; 82 83 static { 87 Predicate p = new Predicate() { 88 89 public boolean evaluate(Object o) { 90 return Configuration.isLoggingConfigured(); 91 } 92 }; 93 94 logObj = new PredicateLogger(ResourceLocator.class, p); 95 } 96 97 98 protected boolean skipAbsolutePath; 99 protected boolean skipClasspath; 100 protected boolean skipCurrentDirectory; 101 protected boolean skipHomeDirectory; 102 103 104 protected List additionalClassPaths; 105 protected List additionalFilesystemPaths; 106 107 108 protected ClassLoader classLoader; 109 110 115 public static InputStream findResourceInClasspath(String name) { 116 try { 117 URL url = findURLInClasspath(name); 118 if (url != null) { 119 logObj.debug("resource found in classpath: " + url); 120 return url.openStream(); 121 } 122 else { 123 logObj.debug("resource not found in classpath: " + name); 124 return null; 125 } 126 } 127 catch (IOException ioex) { 128 return null; 129 } 130 } 131 132 138 public static InputStream findResourceInFileSystem(String name) { 139 try { 140 File file = findFileInFileSystem(name); 141 if (file != null) { 142 logObj.debug("resource found in file system: " + file); 143 return new FileInputStream (file); 144 } 145 else { 146 logObj.debug("resource not found in file system: " + name); 147 return null; 148 } 149 } 150 catch (IOException ioex) { 151 return null; 152 } 153 } 154 155 164 public static File findFileInFileSystem(String name) { 165 File file = findFileInHomeDirectory(name); 166 167 if (file == null) { 168 file = findFileInCurrentDirectory(name); 169 } 170 171 if (file != null) { 172 logObj.debug("file found in file system: " + file); 173 } 174 else { 175 logObj.debug("file not found in file system: " + name); 176 } 177 178 return file; 179 } 180 181 187 public static File findFileInHomeDirectory(String name) { 188 String homeDirPath = System.getProperty("user.home") + File.separator + name; 190 191 try { 192 193 File file = new File (homeDirPath); 194 if (file.exists() && file.canRead()) { 195 logObj.debug("file found in home directory: " + file); 196 } 197 else { 198 file = null; 199 logObj.debug("file not found in home directory: " + name); 200 } 201 202 return file; 203 } 204 catch (SecurityException se) { 205 logObj.debug("permission denied reading file: " + homeDirPath, se); 206 return null; 207 } 208 } 209 210 216 public static File findFileInCurrentDirectory(String name) { 217 String currentDirPath = System.getProperty("user.dir") + File.separator + name; 219 220 try { 221 222 File file = new File (currentDirPath); 223 224 if (file.exists() && file.canRead()) { 225 logObj.debug("file found in current directory: " + file); 226 } 227 else { 228 logObj.debug("file not found in current directory: " + name); 229 file = null; 230 } 231 232 return file; 233 } 234 catch (SecurityException se) { 235 logObj.debug("permission denied reading file: " + currentDirPath, se); 236 return null; 237 } 238 } 239 240 243 public static URL findURLInClasspath(String name) { 244 ClassLoader classLoader = ResourceLocator.class.getClassLoader(); 245 if (classLoader == null) { 246 classLoader = ClassLoader.getSystemClassLoader(); 247 } 248 return findURLInClassLoader(name, classLoader); 249 } 250 251 254 public static URL findURLInClassLoader(String name, ClassLoader loader) { 255 URL url = loader.getResource(name); 256 257 if (url != null) { 258 logObj.debug("URL found with classloader: " + url); 259 } 260 else { 261 logObj.debug("URL not found with classloader: " + name); 262 } 263 264 return url; 265 } 266 267 271 public static String classBaseUrl(Class aClass) { 272 String pathToClass = aClass.getName().replace('.', '/') + ".class"; 273 ClassLoader classLoader = aClass.getClassLoader(); 274 275 if (classLoader == null) { 276 classLoader = ClassLoader.getSystemClassLoader(); 277 } 278 279 URL selfUrl = classLoader.getResource(pathToClass); 280 281 if (selfUrl == null) { 282 return null; 283 } 284 285 String urlString = selfUrl.toExternalForm(); 286 return urlString.substring(0, urlString.length() - pathToClass.length()); 287 } 288 289 293 public ResourceLocator() { 294 super(); 295 this.setClassLoader(this.getClass().getClassLoader()); 296 this.additionalClassPaths = new ArrayList (); 297 this.additionalFilesystemPaths = new ArrayList (); 298 } 299 300 305 public InputStream findResourceStream(String name) { 306 URL url = findResource(name); 307 if (url == null) { 308 return null; 309 } 310 311 try { 312 return url.openStream(); 313 } 314 catch (IOException ioex) { 315 logObj.debug("Error reading URL, ignoring", ioex); 316 return null; 317 } 318 } 319 320 325 public URL findResource(String name) { 326 if (!willSkipAbsolutePath()) { 327 File f = new File (name); 328 if (f.isAbsolute() && f.exists()) { 329 logObj.debug("File found at absolute path: " + name); 330 try { 331 return f.toURL(); 332 } 333 catch (MalformedURLException ex) { 334 logObj.debug("Malformed url, ignoring.", ex); 336 } 337 } 338 else { 339 logObj.debug("No file at absolute path: " + name); 340 } 341 } 342 343 if (!willSkipHomeDirectory()) { 344 File f = findFileInHomeDirectory(name); 345 if (f != null) { 346 347 try { 348 return f.toURL(); 349 } 350 catch (MalformedURLException ex) { 351 logObj.debug("Malformed url, ignoring", ex); 353 } 354 } 355 } 356 357 if (!willSkipCurrentDirectory()) { 358 File f = findFileInCurrentDirectory(name); 359 if (f != null) { 360 361 try { 362 return f.toURL(); 363 } 364 catch (MalformedURLException ex) { 365 logObj.debug("Malformed url, ignoring", ex); 367 } 368 } 369 } 370 371 if (!additionalFilesystemPaths.isEmpty()) { 372 logObj.debug("searching additional paths: " + this.additionalFilesystemPaths); 373 Iterator pi = this.additionalFilesystemPaths.iterator(); 374 while (pi.hasNext()) { 375 File f = new File ((String ) pi.next(), name); 376 logObj.debug("searching for: " + f.getAbsolutePath()); 377 if (f.exists()) { 378 try { 379 return f.toURL(); 380 } 381 catch (MalformedURLException ex) { 382 logObj.debug("Malformed URL, ignoring.", ex); 384 } 385 } 386 } 387 } 388 389 if (!willSkipClasspath()) { 390 391 if (!this.additionalClassPaths.isEmpty()) { 393 logObj.debug("searching additional classpaths: " 394 + this.additionalClassPaths); 395 Iterator cpi = this.additionalClassPaths.iterator(); 396 while (cpi.hasNext()) { 397 String fullName = cpi.next() + "/" + name; 398 logObj.debug("searching for: " + fullName); 399 URL url = findURLInClassLoader(fullName, classLoader); 400 if (url != null) { 401 return url; 402 } 403 } 404 } 405 406 URL url = findURLInClassLoader(name, classLoader); 407 if (url != null) { 408 return url; 409 } 410 } 411 412 return null; 413 } 414 415 421 public URL findDirectoryResource(String name) { 422 URL url = findResource(name); 423 if (url == null) { 424 return null; 425 } 426 427 try { 428 String urlSt = url.toExternalForm(); 429 return (urlSt.endsWith("/")) ? url : new URL (urlSt + "/"); 430 } 431 catch (MalformedURLException ex) { 432 logObj.debug("Malformed URL, ignoring.", ex); 434 return null; 435 } 436 } 437 438 441 public boolean willSkipHomeDirectory() { 442 return skipHomeDirectory; 443 } 444 445 448 public void setSkipHomeDirectory(boolean skipHomeDir) { 449 this.skipHomeDirectory = skipHomeDir; 450 } 451 452 455 public boolean willSkipCurrentDirectory() { 456 return skipCurrentDirectory; 457 } 458 459 462 public void setSkipCurrentDirectory(boolean skipCurDir) { 463 this.skipCurrentDirectory = skipCurDir; 464 } 465 466 469 public boolean willSkipClasspath() { 470 return skipClasspath; 471 } 472 473 476 public void setSkipClasspath(boolean skipClasspath) { 477 this.skipClasspath = skipClasspath; 478 } 479 480 483 public ClassLoader getClassLoader() { 484 return classLoader; 485 } 486 487 491 public void setClassLoader(ClassLoader classLoader) { 492 if (classLoader == null) { 493 classLoader = this.getClass().getClassLoader(); 494 if (classLoader == null) { 495 classLoader = ClassLoader.getSystemClassLoader(); 496 } 497 } 498 499 this.classLoader = classLoader; 500 } 501 502 505 public boolean willSkipAbsolutePath() { 506 return skipAbsolutePath; 507 } 508 509 512 public void setSkipAbsolutePath(boolean skipAbsPath) { 513 this.skipAbsolutePath = skipAbsPath; 514 } 515 516 520 public void addClassPath(String customPath) { 521 this.additionalClassPaths.add(customPath); 522 } 523 524 530 public void addFilesystemPath(String path) { 531 if (path != null) { 532 this.additionalFilesystemPaths.add(path); 533 } 534 else { 535 throw new IllegalArgumentException ("Path must not be null."); 536 } 537 } 538 539 546 public void addFilesystemPath(File path) { 547 if (path != null && path.isDirectory()) { 548 this.addFilesystemPath(path.getPath()); 549 } 550 else { 551 throw new IllegalArgumentException ("Path '" + path + "' is not a directory."); 552 } 553 } 554 555 558 protected static class PredicateLogger extends Logger { 559 560 private Logger _target; 561 private Predicate _predicate; 562 563 private PredicateLogger(String name) { 564 super(name); 565 } 566 567 public PredicateLogger(Class clazz, Predicate condition) { 568 this(clazz.getName(), condition); 569 } 570 571 public PredicateLogger(String name, Predicate condition) { 572 this(name); 573 _target = Logger.getLogger(name); 574 _predicate = condition; 575 } 576 577 public void debug(Object arg0, Throwable arg1) { 578 this.log(Level.DEBUG, arg0, arg1); 579 } 580 581 public void debug(Object arg0) { 582 this.log(Level.DEBUG, arg0); 583 } 584 585 public void info(Object arg0, Throwable arg1) { 586 this.log(Level.INFO, arg0, arg1); 587 } 588 589 public void info(Object arg0) { 590 this.log(Level.INFO, arg0); 591 } 592 593 public void warn(Object arg0, Throwable arg1) { 594 this.log(Level.WARN, arg0, arg1); 595 } 596 597 public void warn(Object arg0) { 598 this.log(Level.WARN, arg0); 599 } 600 601 public void error(Object arg0, Throwable arg1) { 602 this.log(Level.ERROR, arg0, arg1); 603 } 604 605 public void error(Object arg0) { 606 this.log(Level.ERROR, arg0); 607 } 608 609 public void fatal(Object arg0, Throwable arg1) { 610 this.log(Level.FATAL, arg0, arg1); 611 } 612 613 public void fatal(Object arg0) { 614 this.log(Level.FATAL, arg0); 615 } 616 617 public void log(Priority arg0, Object arg1, Throwable arg2) { 618 if (_predicate.evaluate(arg1)) { 619 _target.log(arg0, arg1); 620 } 621 } 622 623 public void log(Priority arg0, Object arg1) { 624 if (_predicate.evaluate(arg1)) { 625 _target.log(arg0, arg1); 626 } 627 } 628 629 } 630 631 } | Popular Tags |