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.ui.velocity;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import org.apache.commons.collections.ExtendedProperties;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.apache.velocity.exception.ResourceNotFoundException;
  23. import org.apache.velocity.runtime.resource.Resource;
  24. import org.apache.velocity.runtime.resource.loader.ResourceLoader;
  25. /**
  26. * Velocity ResourceLoader adapter that loads via a Spring ResourceLoader.
  27. * Used by VelocityEngineFactory for any resource loader path that cannot
  28. * be resolved to a java.io.File.
  29. *
  30. * <p>Note that this loader does not allow for modification detection:
  31. * Use Velocity's default FileResourceLoader for java.io.File resources.
  32. *
  33. * <p>Expects "spring.resource.loader" and "spring.resource.loader.path"
  34. * application attributes in the Velocity runtime: the former of type
  35. * org.springframework.core.io.ResourceLoader, the latter a String.
  36. *
  37. * @author Juergen Hoeller
  38. * @since 14.03.2004
  39. * @see VelocityEngineFactory#setResourceLoaderPath
  40. * @see org.springframework.core.io.ResourceLoader
  41. * @see org.apache.velocity.runtime.resource.loader.FileResourceLoader
  42. */
  43. public class SpringResourceLoader extends ResourceLoader {
  44. protected final Log logger = LogFactory.getLog(getClass());
  45. public static final String NAME = "spring";
  46. public static final String SPRING_RESOURCE_LOADER_CLASS = "spring.resource.loader.class";
  47. public static final String SPRING_RESOURCE_LOADER = "spring.resource.loader";
  48. public static final String SPRING_RESOURCE_LOADER_PATH = "spring.resource.loader.path";
  49. private org.springframework.core.io.ResourceLoader resourceLoader;
  50. private String resourceLoaderPath;
  51. public void init(ExtendedProperties configuration) {
  52. this.resourceLoader = (org.springframework.core.io.ResourceLoader)
  53. this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER);
  54. this.resourceLoaderPath = (String) this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER_PATH);
  55. if (this.resourceLoader == null) {
  56. throw new IllegalArgumentException("'resourceLoader' application attribute must be present for SpringResourceLoader");
  57. }
  58. if (this.resourceLoaderPath == null) {
  59. throw new IllegalArgumentException("'resourceLoaderPath' application attribute must be present for SpringResourceLoader");
  60. }
  61. if (!this.resourceLoaderPath.endsWith("/")) {
  62. this.resourceLoaderPath += "/";
  63. }
  64. logger.info("SpringResourceLoader for Velocity: using resource loader [" + this.resourceLoader +
  65. "] and resource loader path [" + this.resourceLoaderPath + "]");
  66. }
  67. public InputStream getResourceStream(String source) throws ResourceNotFoundException {
  68. try {
  69. return this.resourceLoader.getResource(this.resourceLoaderPath + source).getInputStream();
  70. }
  71. catch (IOException ex) {
  72. if (logger.isInfoEnabled()) {
  73. logger.info("Could not find Velocity resource [" + this.resourceLoaderPath + source + "]");
  74. }
  75. throw new ResourceNotFoundException(ex.getMessage());
  76. }
  77. }
  78. public boolean isSourceModified(Resource resource) {
  79. return false;
  80. }
  81. public long getLastModified(Resource resource) {
  82. return 0;
  83. }
  84. }