1 19 20 package org.apache.cayenne.gen; 21 22 import java.io.BufferedInputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.InputStream ; 27 28 import org.apache.velocity.exception.ResourceNotFoundException; 29 import org.apache.velocity.runtime.resource.loader.FileResourceLoader; 30 31 38 public class ClassGeneratorResourceLoader extends FileResourceLoader { 39 40 44 public synchronized InputStream getResourceStream(String name) 45 throws ResourceNotFoundException { 46 47 InputStream stream = loadFromRelativePath(name); 48 if (stream != null) { 49 return stream; 50 } 51 52 stream = loadFromAbsPath(name); 53 if (stream != null) { 54 return stream; 55 } 56 57 stream = loadFromThreadClassLoader(name); 58 if (stream != null) { 59 return stream; 60 } 61 62 stream = loadFromThisClassLoader(name); 63 if (stream != null) { 64 return stream; 65 } 66 67 throw new ResourceNotFoundException("Couldn't find resource '" 68 + name 69 + "'. Searched filesystem path and classpath"); 70 } 71 72 protected InputStream loadFromRelativePath(String name) { 73 try { 74 return super.getResourceStream(name); 75 } 76 catch (ResourceNotFoundException rnfex) { 77 return null; 78 } 79 } 80 81 protected InputStream loadFromAbsPath(String name) { 82 try { 83 File file = new File (name); 84 return (file.canRead()) ? new BufferedInputStream (new FileInputStream (file 85 .getAbsolutePath())) : null; 86 87 } 88 catch (FileNotFoundException fnfe) { 89 return null; 90 } 91 } 92 93 protected InputStream loadFromThreadClassLoader(String name) { 94 return Thread.currentThread().getContextClassLoader().getResourceAsStream(name); 95 } 96 97 protected InputStream loadFromThisClassLoader(String name) { 98 return getClass().getClassLoader().getResourceAsStream(name); 99 } 100 } 101 | Popular Tags |