1 package org.jicengine.io; 2 3 import java.io.InputStream ; 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.FileNotFoundException ; 7 import java.util.StringTokenizer ; 8 9 27 28 public class ClasspathResource extends AbstractResource implements UrlReadable { 29 30 private ClassLoader classLoader; 31 private String resourceName; 32 33 38 public ClasspathResource(String resourceName) 39 { 40 super("classpath://" + resourceName); 41 this.classLoader = getClass().getClassLoader(); 42 this.resourceName = resourceName; 43 } 44 45 public ClasspathResource(ClassLoader classLoader, String resourceName) 46 { 47 super("classpath://" + resourceName); 48 this.classLoader = classLoader; 49 this.resourceName = resourceName; 50 } 51 52 public ClassLoader getClassLoader() 53 { 54 return this.classLoader; 55 } 56 57 public String getResourceName() 58 { 59 return this.resourceName; 60 } 61 62 public java.net.URL getUrl() throws IOException 63 { 64 java.net.URL url = getClassLoader().getResource(getResourceName()); 65 if( url != null ){ 66 return url; 67 } 68 else { 69 throw new FileNotFoundException ("Resource '" + getResourceName() + "' not available in classpath."); 70 } 71 } 72 73 public boolean isAvailable() 74 { 75 if( getClassLoader().getResource(getResourceName()) != null ){ 76 return true; 77 } 78 else { 79 return false; 80 } 81 } 82 83 public InputStream getInputStream() throws java.io.IOException 84 { 85 InputStream stream = getClassLoader().getResourceAsStream(getResourceName()); 86 87 if( stream != null ){ 88 return stream; 89 } 90 else { 91 throw new java.io.IOException ("Resource '" + getResourceName() + "' not available in classpath."); 92 } 93 } 94 95 public Resource getResource(String relativePath) throws java.io.IOException 96 { 97 return new ClasspathResource(PathResolver.getRealPath(getResourceName(), relativePath)); 98 } 99 } 100 | Popular Tags |