1 16 17 package org.springframework.core.io; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.URL ; 24 25 import org.springframework.util.Assert; 26 import org.springframework.util.ClassUtils; 27 import org.springframework.util.ObjectUtils; 28 import org.springframework.util.ResourceUtils; 29 import org.springframework.util.StringUtils; 30 31 44 public class ClassPathResource extends AbstractResource { 45 46 private final String path; 47 48 private ClassLoader classLoader; 49 50 private Class clazz; 51 52 53 63 public ClassPathResource(String path) { 64 this(path, (ClassLoader ) null); 65 } 66 67 76 public ClassPathResource(String path, ClassLoader classLoader) { 77 Assert.notNull(path, "Path must not be null"); 78 if (path.startsWith("/")) { 79 path = path.substring(1); 80 } 81 this.path = StringUtils.cleanPath(path); 82 this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); 83 } 84 85 93 public ClassPathResource(String path, Class clazz) { 94 Assert.notNull(path, "Path must not be null"); 95 this.path = StringUtils.cleanPath(path); 96 this.clazz = clazz; 97 } 98 99 106 protected ClassPathResource(String path, ClassLoader classLoader, Class clazz) { 107 this.path = path; 108 this.classLoader = classLoader; 109 this.clazz = clazz; 110 } 111 112 113 116 public final String getPath() { 117 return this.path; 118 } 119 120 121 126 public InputStream getInputStream() throws IOException { 127 InputStream is = null; 128 if (this.clazz != null) { 129 is = this.clazz.getResourceAsStream(this.path); 130 } 131 else { 132 is = this.classLoader.getResourceAsStream(this.path); 133 } 134 if (is == null) { 135 throw new FileNotFoundException ( 136 getDescription() + " cannot be opened because it does not exist"); 137 } 138 return is; 139 } 140 141 146 public URL getURL() throws IOException { 147 URL url = null; 148 if (this.clazz != null) { 149 url = this.clazz.getResource(this.path); 150 } 151 else { 152 url = this.classLoader.getResource(this.path); 153 } 154 if (url == null) { 155 throw new FileNotFoundException ( 156 getDescription() + " cannot be resolved to URL because it does not exist"); 157 } 158 return url; 159 } 160 161 166 public File getFile() throws IOException { 167 return ResourceUtils.getFile(getURL(), getDescription()); 168 } 169 170 175 public Resource createRelative(String relativePath) { 176 String pathToUse = StringUtils.applyRelativePath(this.path, relativePath); 177 return new ClassPathResource(pathToUse, this.classLoader, this.clazz); 178 } 179 180 185 public String getFilename() { 186 return StringUtils.getFilename(this.path); 187 } 188 189 192 public String getDescription() { 193 return "class path resource [" + this.path + "]"; 194 } 195 196 197 200 public boolean equals(Object obj) { 201 if (obj == this) { 202 return true; 203 } 204 if (obj instanceof ClassPathResource) { 205 ClassPathResource otherRes = (ClassPathResource) obj; 206 return (this.path.equals(otherRes.path) && 207 ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) && 208 ObjectUtils.nullSafeEquals(this.clazz, otherRes.clazz)); 209 } 210 return false; 211 } 212 213 217 public int hashCode() { 218 return this.path.hashCode(); 219 } 220 221 } 222 | Popular Tags |