DefaultResourceLoader implements ResourceLoader:
1
/*
2 * Copyright 2002-2005 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 import org.springframework.util.Assert;
22
23 /**
24 * Default implementation of the ResourceLoader interface. Used by ResourceEditor, but also suitable for standalone usage. <p>Will return an UrlResource if the location value is a URL, and a ClassPathResource if it is a non-URL path or a "classpath:" pseudo-URL.
25 * @author Juergen Hoeller
26 * @since 10.03.2004
27 * @see #CLASSPATH_URL_PREFIX
28 * @see ResourceEditor
29 * @see UrlResource
30 * @see ClassPathResource
31 */
32 public class DefaultResourceLoader implements ResourceLoader {
33
34 private final ClassLoader classLoader;
35
36
37 /**
38 * Create a new DefaultResourceLoader.
39 * <p>ClassLoader access will happen via the thread context class loader on actual
40 * access (applying to the thread that does ClassPathResource calls).
41 */
42 public DefaultResourceLoader() {
43 this ( null );
44 }
45
46 /**
47 * Create a new DefaultResourceLoader.
48 * @param classLoader the ClassLoader to load classpath resources with,
49 * or null if using the thread context class loader on actual access
50 * (applying to the thread that does ClassPathResource calls)
51 */
52 public DefaultResourceLoader(ClassLoader classLoader) {
53 this .classLoader = classLoader;
54 }
55
56 /**
57 * Return the ClassLoader to load classpath resources with, or null if using the thread context class loader on actual access (applying to the thread that does ClassPathResource calls). <p>Will get passed to ClassPathResource's constructor for all ClassPathResource objects created by this resource loader.
58 * @see ClassPathResource
59 * @uml.property name="classLoader"
60 */
61 public ClassLoader getClassLoader() {
62 return classLoader;
63 }
64
65
66 public Resource getResource(String location) {
67 Assert.notNull(location, " location is required " );
68 if (location.startsWith(CLASSPATH_URL_PREFIX)) {
69 return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
70 }
71 else {
72 try {
73 // try URL
74 URL url = new URL(location);
75 return new UrlResource(url);
76 }
77 catch (MalformedURLException ex) {
78 // no URL -> resolve resource path
79 return getResourceByPath(location);
80 }
81 }
82 }
83
84 /**
85 * Return a Resource handle for the resource at the given path.
86 * <p>Default implementation supports class path locations. This should
87 * be appropriate for standalone implementations but can be overridden,
88 * e.g. for implementations targeted at a Servlet container.
89 * @param path path to the resource
90 * @return Resource handle
91 * @see ClassPathResource
92 * @see org.springframework.context.support.FileSystemXmlApplicationContext#getResourceByPath
93 * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
94 */
95 protected Resource getResourceByPath(String path) {
96 return new ClassPathResource(path, getClassLoader());
97 }
98
99 }
100
2 * Copyright 2002-2005 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 import org.springframework.util.Assert;
22
23 /**
24 * Default implementation of the ResourceLoader interface. Used by ResourceEditor, but also suitable for standalone usage. <p>Will return an UrlResource if the location value is a URL, and a ClassPathResource if it is a non-URL path or a "classpath:" pseudo-URL.
25 * @author Juergen Hoeller
26 * @since 10.03.2004
27 * @see #CLASSPATH_URL_PREFIX
28 * @see ResourceEditor
29 * @see UrlResource
30 * @see ClassPathResource
31 */
32 public class DefaultResourceLoader implements ResourceLoader {
33
34 private final ClassLoader classLoader;
35
36
37 /**
38 * Create a new DefaultResourceLoader.
39 * <p>ClassLoader access will happen via the thread context class loader on actual
40 * access (applying to the thread that does ClassPathResource calls).
41 */
42 public DefaultResourceLoader() {
43 this ( null );
44 }
45
46 /**
47 * Create a new DefaultResourceLoader.
48 * @param classLoader the ClassLoader to load classpath resources with,
49 * or null if using the thread context class loader on actual access
50 * (applying to the thread that does ClassPathResource calls)
51 */
52 public DefaultResourceLoader(ClassLoader classLoader) {
53 this .classLoader = classLoader;
54 }
55
56 /**
57 * Return the ClassLoader to load classpath resources with, or null if using the thread context class loader on actual access (applying to the thread that does ClassPathResource calls). <p>Will get passed to ClassPathResource's constructor for all ClassPathResource objects created by this resource loader.
58 * @see ClassPathResource
59 * @uml.property name="classLoader"
60 */
61 public ClassLoader getClassLoader() {
62 return classLoader;
63 }
64
65
66 public Resource getResource(String location) {
67 Assert.notNull(location, " location is required " );
68 if (location.startsWith(CLASSPATH_URL_PREFIX)) {
69 return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
70 }
71 else {
72 try {
73 // try URL
74 URL url = new URL(location);
75 return new UrlResource(url);
76 }
77 catch (MalformedURLException ex) {
78 // no URL -> resolve resource path
79 return getResourceByPath(location);
80 }
81 }
82 }
83
84 /**
85 * Return a Resource handle for the resource at the given path.
86 * <p>Default implementation supports class path locations. This should
87 * be appropriate for standalone implementations but can be overridden,
88 * e.g. for implementations targeted at a Servlet container.
89 * @param path path to the resource
90 * @return Resource handle
91 * @see ClassPathResource
92 * @see org.springframework.context.support.FileSystemXmlApplicationContext#getResourceByPath
93 * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
94 */
95 protected Resource getResourceByPath(String path) {
96 return new ClassPathResource(path, getClassLoader());
97 }
98
99 }
100