001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.core.io;
018:
019: import java.net.MalformedURLException;
020: import java.net.URL;
021:
022: import org.springframework.util.Assert;
023: import org.springframework.util.ClassUtils;
024:
025: /**
026: * Default implementation of the {@link ResourceLoader} interface.
027: * Used by {@link ResourceEditor}, and serves as base class for
028: * {@link org.springframework.context.support.AbstractApplicationContext}.
029: * Can also be used standalone.
030: *
031: * <p>Will return a {@link UrlResource} if the location value is a URL,
032: * and a {@link ClassPathResource} if it is a non-URL path or a
033: * "classpath:" pseudo-URL.
034: *
035: * @author Juergen Hoeller
036: * @since 10.03.2004
037: * @see FileSystemResourceLoader
038: * @see org.springframework.context.support.ClassPathXmlApplicationContext
039: */
040: public class DefaultResourceLoader implements ResourceLoader {
041:
042: private ClassLoader classLoader;
043:
044: /**
045: * Create a new DefaultResourceLoader.
046: * <p>ClassLoader access will happen using the thread context class loader
047: * at the time of this ResourceLoader's initialization.
048: * @see java.lang.Thread#getContextClassLoader()
049: */
050: public DefaultResourceLoader() {
051: this .classLoader = ClassUtils.getDefaultClassLoader();
052: }
053:
054: /**
055: * Create a new DefaultResourceLoader.
056: * @param classLoader the ClassLoader to load class path resources with, or <code>null</code>
057: * for using the thread context class loader at the time of actual resource access
058: */
059: public DefaultResourceLoader(ClassLoader classLoader) {
060: this .classLoader = classLoader;
061: }
062:
063: /**
064: * Specify the ClassLoader to load class path resources with, or <code>null</code>
065: * for using the thread context class loader at the time of actual resource access.
066: * <p>The default is that ClassLoader access will happen using the thread context
067: * class loader at the time of this ResourceLoader's initialization.
068: */
069: public void setClassLoader(ClassLoader classLoader) {
070: this .classLoader = classLoader;
071: }
072:
073: /**
074: * Return the ClassLoader to load class path resources with,
075: * or <code>null</code> if using the thread context class loader on actual access
076: * (applying to the thread that constructs the ClassPathResource object).
077: * <p>Will get passed to ClassPathResource's constructor for all
078: * ClassPathResource objects created by this resource loader.
079: * @see ClassPathResource
080: */
081: public ClassLoader getClassLoader() {
082: return this .classLoader;
083: }
084:
085: public Resource getResource(String location) {
086: Assert.notNull(location, "Location must not be null");
087: if (location.startsWith(CLASSPATH_URL_PREFIX)) {
088: return new ClassPathResource(location
089: .substring(CLASSPATH_URL_PREFIX.length()),
090: getClassLoader());
091: } else {
092: try {
093: // Try to parse the location as a URL...
094: URL url = new URL(location);
095: return new UrlResource(url);
096: } catch (MalformedURLException ex) {
097: // No URL -> resolve as resource path.
098: return getResourceByPath(location);
099: }
100: }
101: }
102:
103: /**
104: * Return a Resource handle for the resource at the given path.
105: * <p>The default implementation supports class path locations. This should
106: * be appropriate for standalone implementations but can be overridden,
107: * e.g. for implementations targeted at a Servlet container.
108: * @param path the path to the resource
109: * @return the corresponding Resource handle
110: * @see ClassPathResource
111: * @see org.springframework.context.support.FileSystemXmlApplicationContext#getResourceByPath
112: * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
113: */
114: protected Resource getResourceByPath(String path) {
115: return new ClassPathContextResource(path, getClassLoader());
116: }
117:
118: /**
119: * ClassPathResource that explicitly expresses a context-relative path
120: * through implementing the ContextResource interface.
121: */
122: private static class ClassPathContextResource extends
123: ClassPathResource implements ContextResource {
124:
125: public ClassPathContextResource(String path,
126: ClassLoader classLoader) {
127: super (path, classLoader);
128: }
129:
130: public String getPathWithinContext() {
131: return getPath();
132: }
133: }
134:
135: }
|