KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > DefaultResourceLoader


1 /*
2  * Copyright 2002-2007 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 JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import org.springframework.util.Assert;
23 import org.springframework.util.ClassUtils;
24
25 /**
26  * Default implementation of the {@link ResourceLoader} interface.
27  * Used by {@link ResourceEditor}, and serves as base class for
28  * {@link org.springframework.context.support.AbstractApplicationContext}.
29  * Can also be used standalone.
30  *
31  * <p>Will return a {@link UrlResource} if the location value is a URL,
32  * and a {@link ClassPathResource} if it is a non-URL path or a
33  * "classpath:" pseudo-URL.
34  *
35  * @author Juergen Hoeller
36  * @since 10.03.2004
37  * @see FileSystemResourceLoader
38  * @see org.springframework.context.support.ClassPathXmlApplicationContext
39  */

40 public class DefaultResourceLoader implements ResourceLoader {
41
42     private ClassLoader JavaDoc classLoader;
43
44
45     /**
46      * Create a new DefaultResourceLoader.
47      * <p>ClassLoader access will happen using the thread context class loader
48      * at the time of this ResourceLoader's initialization.
49      * @see java.lang.Thread#getContextClassLoader()
50      */

51     public DefaultResourceLoader() {
52         this.classLoader = ClassUtils.getDefaultClassLoader();
53     }
54
55     /**
56      * Create a new DefaultResourceLoader.
57      * @param classLoader the ClassLoader to load class path resources with, or <code>null</code>
58      * for using the thread context class loader at the time of actual resource access
59      */

60     public DefaultResourceLoader(ClassLoader JavaDoc classLoader) {
61         this.classLoader = classLoader;
62     }
63
64
65     /**
66      * Specify the ClassLoader to load class path resources with, or <code>null</code>
67      * for using the thread context class loader at the time of actual resource access.
68      * <p>The default is that ClassLoader access will happen using the thread context
69      * class loader at the time of this ResourceLoader's initialization.
70      */

71     public void setClassLoader(ClassLoader JavaDoc classLoader) {
72         this.classLoader = classLoader;
73     }
74
75     /**
76      * Return the ClassLoader to load class path resources with,
77      * or <code>null</code> if using the thread context class loader on actual access
78      * (applying to the thread that constructs the ClassPathResource object).
79      * <p>Will get passed to ClassPathResource's constructor for all
80      * ClassPathResource objects created by this resource loader.
81      * @see ClassPathResource
82      */

83     public ClassLoader JavaDoc getClassLoader() {
84         return this.classLoader;
85     }
86
87
88     public Resource getResource(String JavaDoc location) {
89         Assert.notNull(location, "Location must not be null");
90         if (location.startsWith(CLASSPATH_URL_PREFIX)) {
91             return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
92         }
93         else {
94             try {
95                 // Try to parse the location as a URL...
96
URL JavaDoc url = new URL JavaDoc(location);
97                 return new UrlResource(url);
98             }
99             catch (MalformedURLException JavaDoc ex) {
100                 // No URL -> resolve as resource path.
101
return getResourceByPath(location);
102             }
103         }
104     }
105
106     /**
107      * Return a Resource handle for the resource at the given path.
108      * <p>The default implementation supports class path locations. This should
109      * be appropriate for standalone implementations but can be overridden,
110      * e.g. for implementations targeted at a Servlet container.
111      * @param path path to the resource
112      * @return Resource handle
113      * @see ClassPathResource
114      * @see org.springframework.context.support.FileSystemXmlApplicationContext#getResourceByPath
115      * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
116      */

117     protected Resource getResourceByPath(String JavaDoc path) {
118         return new ClassPathResource(path, getClassLoader());
119     }
120
121 }
122
Popular Tags