DefaultResourceLoader.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

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