Index: src/main/java/org/mortbay/resource/ClassPathResource.java =================================================================== --- src/main/java/org/mortbay/resource/ClassPathResource.java +++ src/main/java/org/mortbay/resource/ClassPathResource.java @@ -0,0 +1,96 @@ +/* + * Copyright 2006 ThoughtWorks, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.mortbay.resource; + +import java.net.URL; + +/** + * Represents a resource file off of the classpath. + * + * @author Patrick Lightbody (plightbo at gmail dot com) + * @author Matthew Purland (m dot purland at gmail dot com) + */ +public class ClassPathResource extends URLResource { + protected String relativeName; + + /** + * Specifies the classpath path containing the resource. + * + * The way it finds a class path resource is in order of the following. + * + *
+ * 1) Relative name that is relative to the location of the class of + * ClassPathResource + *
+ * + *+ * 2) Relative name to the system as a system resource. See this: + * {@link ClassLoader.getSystemResource} + *
+ * + * This resource uses caches by default. + * + * @param relativeName + * Relative name to the classpath resource. + */ + public ClassPathResource(String relativeName) { + this(relativeName, true); + } + + /** + * Specifies the classpath path containing the resource. + * + * @param relativeName + * Relative name to the classpath resource. + * @param useCaches + * Whether to use caching or not. + */ + public ClassPathResource(String relativeName, boolean useCaches) { + super(findRelativeResourceURL(relativeName), null, useCaches); + + this.relativeName = relativeName; + } + + /** + * Find a relative resource and return the URL to that resource. + * + * If the returned resource (through this classloader) returns a bad URL, + * will check the bootstrap classloader through + * ClassLoader.getSystemResource(name). + * + * @param relativeName + * Relative name to the class path resource to find. + * @return Returns a URL to the resource pointed to by relativePath. + */ + protected static URL findRelativeResourceURL(String relativeName) { + URL url = ClassPathResource.class.getResource(relativeName); + + // Try using a system level classpath to find the resource + if (url == null) { + url = ClassLoader.getSystemResource(relativeName); + } + return url; + } + + /** + * {@inheritDoc} + */ + public synchronized void release() { + super.release(); + relativeName = null; + } +} Index: src/test/java/org/mortbay/resource/ResourceTest.java =================================================================== --- src/test/java/org/mortbay/resource/ResourceTest.java (revision 1387) +++ src/test/java/org/mortbay/resource/ResourceTest.java (working copy) @@ -266,7 +266,62 @@ } - + /** + * Test a class path resource for existence. + */ + public void testClassPathResourceClass() { + final String classPathName = "ClassPathResource.class"; + + Resource resource = new ClassPathResource(classPathName); + + // A class path cannot be a directory + assertFalse("Class path cannot be a directory.", resource.isDirectory()); + + // A class path must exist + assertTrue("Class path resource does not exist.", resource.exists()); + } + + /** + * Test a class path resource for directories. + */ + public void testClassPathResourceDirectory() throws Exception { + final String classPathName = "/"; + + Resource resource = new ClassPathResource(classPathName); + + // A class path must be a directory + assertTrue("Class path must be a directory.", resource.isDirectory()); + + assertTrue("Class path returned file must be a directory.", resource.getFile().isDirectory()); + + // A class path must exist + assertTrue("Class path resource does not exist.", resource.exists()); + } + + /** + * Test a class path resource for a file. + */ + public void testClassPathResourceFile() throws Exception { + final String fileName = "fakeRequests.txt"; + final String classPathName = "/" + fileName; + + // Will locate a resource in the class path + Resource resource = new ClassPathResource(classPathName); + + // A class path cannot be a directory + assertFalse("Class path must be a directory.", resource.isDirectory()); + + File file = resource.getFile(); + + assertTrue("File returned from class path should not be null.", file != null); + assertEquals("File name from class path is not equal.", fileName, file.getName()); + assertTrue("File returned from class path should be a file.", file.isFile()); + + // A class path must exist + assertTrue("Class path resource does not exist.", resource.exists()); + } + + }