View Javadoc

1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.core.io;
18  
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  
22  import org.springframework.util.Assert;
23  import org.springframework.util.ClassUtils;
24  import org.springframework.util.StringUtils;
25  
26  /**
27   * Default implementation of the {@link ResourceLoader} interface.
28   * Used by {@link ResourceEditor}, and serves as base class for
29   * {@link org.springframework.context.support.AbstractApplicationContext}.
30   * Can also be used standalone.
31   *
32   * <p>Will return a {@link UrlResource} if the location value is a URL,
33   * and a {@link ClassPathResource} if it is a non-URL path or a
34   * "classpath:" pseudo-URL.
35   *
36   * @author Juergen Hoeller
37   * @since 10.03.2004
38   * @see FileSystemResourceLoader
39   * @see org.springframework.context.support.ClassPathXmlApplicationContext
40   */
41  public class DefaultResourceLoader implements ResourceLoader {
42  
43  	private ClassLoader classLoader;
44  
45  
46  	/**
47  	 * Create a new DefaultResourceLoader.
48  	 * <p>ClassLoader access will happen using the thread context class loader
49  	 * at the time of this ResourceLoader's initialization.
50  	 * @see java.lang.Thread#getContextClassLoader()
51  	 */
52  	public DefaultResourceLoader() {
53  		this.classLoader = ClassUtils.getDefaultClassLoader();
54  	}
55  
56  	/**
57  	 * Create a new DefaultResourceLoader.
58  	 * @param classLoader the ClassLoader to load class path resources with, or <code>null</code>
59  	 * for using the thread context class loader at the time of actual resource access
60  	 */
61  	public DefaultResourceLoader(ClassLoader classLoader) {
62  		this.classLoader = classLoader;
63  	}
64  
65  
66  	/**
67  	 * Specify the ClassLoader to load class path resources with, or <code>null</code>
68  	 * for using the thread context class loader at the time of actual resource access.
69  	 * <p>The default is that ClassLoader access will happen using the thread context
70  	 * class loader at the time of this ResourceLoader's initialization.
71  	 */
72  	public void setClassLoader(ClassLoader classLoader) {
73  		this.classLoader = classLoader;
74  	}
75  
76  	/**
77  	 * Return the ClassLoader to load class path resources with.
78  	 * <p>Will get passed to ClassPathResource's constructor for all
79  	 * ClassPathResource objects created by this resource loader.
80  	 * @see ClassPathResource
81  	 */
82  	public ClassLoader getClassLoader() {
83  		return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader());
84  	}
85  
86  
87  	public Resource getResource(String location) {
88  		Assert.notNull(location, "Location must not be null");
89  		if (location.startsWith(CLASSPATH_URL_PREFIX)) {
90  			return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
91  		}
92  		else {
93  			try {
94  				// Try to parse the location as a URL...
95  				URL url = new URL(location);
96  				return new UrlResource(url);
97  			}
98  			catch (MalformedURLException ex) {
99  				// No URL -> resolve as resource path.
100 				return getResourceByPath(location);
101 			}
102 		}
103 	}
104 
105 	/**
106 	 * Return a Resource handle for the resource at the given path.
107 	 * <p>The default implementation supports class path locations. This should
108 	 * be appropriate for standalone implementations but can be overridden,
109 	 * e.g. for implementations targeted at a Servlet container.
110 	 * @param path the path to the resource
111 	 * @return the corresponding Resource handle
112 	 * @see ClassPathResource
113 	 * @see org.springframework.context.support.FileSystemXmlApplicationContext#getResourceByPath
114 	 * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
115 	 */
116 	protected Resource getResourceByPath(String path) {
117 		return new ClassPathContextResource(path, getClassLoader());
118 	}
119 
120 
121 	/**
122 	 * ClassPathResource that explicitly expresses a context-relative path
123 	 * through implementing the ContextResource interface.
124 	 */
125 	private static class ClassPathContextResource extends ClassPathResource implements ContextResource {
126 
127 		public ClassPathContextResource(String path, ClassLoader classLoader) {
128 			super(path, classLoader);
129 		}
130 
131 		public String getPathWithinContext() {
132 			return getPath();
133 		}
134 
135 		@Override
136 		public Resource createRelative(String relativePath) {
137 			String pathToUse = StringUtils.applyRelativePath(getPath(), relativePath);
138 			return new ClassPathContextResource(pathToUse, getClassLoader());
139 		}
140 	}
141 
142 }