1 16 17 package org.springframework.ui.velocity; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.Arrays ; 22 23 import org.apache.commons.collections.ExtendedProperties; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.velocity.exception.ResourceNotFoundException; 27 import org.apache.velocity.runtime.resource.Resource; 28 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 29 30 import org.springframework.util.StringUtils; 31 32 51 public class SpringResourceLoader extends ResourceLoader { 52 53 public static final String NAME = "spring"; 54 55 public static final String SPRING_RESOURCE_LOADER_CLASS = "spring.resource.loader.class"; 56 57 public static final String SPRING_RESOURCE_LOADER_CACHE = "spring.resource.loader.cache"; 58 59 public static final String SPRING_RESOURCE_LOADER = "spring.resource.loader"; 60 61 public static final String SPRING_RESOURCE_LOADER_PATH = "spring.resource.loader.path"; 62 63 64 protected final Log logger = LogFactory.getLog(getClass()); 65 66 private org.springframework.core.io.ResourceLoader resourceLoader; 67 68 private String [] resourceLoaderPaths; 69 70 71 public void init(ExtendedProperties configuration) { 72 this.resourceLoader = (org.springframework.core.io.ResourceLoader) 73 this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER); 74 String resourceLoaderPath = (String ) this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER_PATH); 75 if (this.resourceLoader == null) { 76 throw new IllegalArgumentException ( 77 "'resourceLoader' application attribute must be present for SpringResourceLoader"); 78 } 79 if (resourceLoaderPath == null) { 80 throw new IllegalArgumentException ( 81 "'resourceLoaderPath' application attribute must be present for SpringResourceLoader"); 82 } 83 this.resourceLoaderPaths = StringUtils.commaDelimitedListToStringArray(resourceLoaderPath); 84 for (int i = 0; i < this.resourceLoaderPaths.length; i++) { 85 String path = this.resourceLoaderPaths[i]; 86 if (!path.endsWith("/")) { 87 this.resourceLoaderPaths[i] = path + "/"; 88 } 89 } 90 if (logger.isInfoEnabled()) { 91 logger.info("SpringResourceLoader for Velocity: using resource loader [" + this.resourceLoader + 92 "] and resource loader paths " + Arrays.asList(this.resourceLoaderPaths)); 93 } 94 } 95 96 public InputStream getResourceStream(String source) throws ResourceNotFoundException { 97 if (logger.isDebugEnabled()) { 98 logger.debug("Looking for Velocity resource with name [" + source + "]"); 99 } 100 for (int i = 0; i < this.resourceLoaderPaths.length; i++) { 101 org.springframework.core.io.Resource resource = 102 this.resourceLoader.getResource(this.resourceLoaderPaths[i] + source); 103 try { 104 return resource.getInputStream(); 105 } 106 catch (IOException ex) { 107 if (logger.isDebugEnabled()) { 108 logger.debug("Could not find Velocity resource: " + resource); 109 } 110 } 111 } 112 throw new ResourceNotFoundException( 113 "Could not find resource [" + source + "] in Spring resource loader path"); 114 } 115 116 public boolean isSourceModified(Resource resource) { 117 return false; 118 } 119 120 public long getLastModified(Resource resource) { 121 return 0; 122 } 123 124 } 125 | Popular Tags |