org.zodiac.template.velocity.spring.ui.SpringResourceLoader Maven / Gradle / Ivy
package org.zodiac.template.velocity.spring.ui;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.util.ExtProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.commons.util.lang.Strings;
public class SpringResourceLoader extends ResourceLoader {
public static final String NAME = "spring";
public static final String SPRING_RESOURCE_LOADER = "spring.resource.loader";
public static final String RESOURCE_LOADER_SPRING_CLASS = "resource.loader.spring.class";
public static final String RESOURCE_LOADER_SPRING_CACHE = "resource.loader.spring.cache";
public static final String RESOURCE_LOADER_SPRING_PATH = "resource.loader.spring.path";
protected final Logger logger = LoggerFactory.getLogger(getClass());
private org.springframework.core.io.ResourceLoader resourceLoader;
private String[] resourceLoaderPaths;
public SpringResourceLoader() {
super();
}
@Override
public boolean isSourceModified(Resource templateResource) {
org.springframework.core.io.Resource resource = getResource(templateResource.getName(), null);
/*1. 假如资源没找到,可能是被删除了,那么认为modified==true,模板将会在重新装载时报错。*/
if (resource == null || !resource.exists()) {
return true;
}
long lastModified;
try {
lastModified = resource.lastModified();
} catch (IOException e) {
lastModified = 0;
}
/*2. 假如资源找到了,但是不支持lastModified功能,则认为modified==false,模板不会重新装载。*/
if (lastModified <= 0L) {
return false;
}
/*3. 资源找到,并支持lastModified功能,则比较lastModified。*/
return lastModified != templateResource.getLastModified();
}
@Override
public long getLastModified(Resource templateResource) {
org.springframework.core.io.Resource resource = getResource(templateResource.getName(), null);
if (resource != null && resource.exists()) {
try {
return resource.lastModified();
} catch (IOException e) {
}
}
return 0;
}
@Override
public void init(ExtProperties configuration) {
this.resourceLoader =
(org.springframework.core.io.ResourceLoader)this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER);
String resourceLoaderPath = (String)this.rsvc.getApplicationAttribute(RESOURCE_LOADER_SPRING_PATH);
if (this.resourceLoader == null) {
throw new IllegalArgumentException(
"'resourceLoader' application attribute must be present for SpringResourceLoader");
}
if (resourceLoaderPath == null) {
throw new IllegalArgumentException(
"'resourceLoaderPath' application attribute must be present for SpringResourceLoader");
}
this.resourceLoaderPaths = Strings.commaDelimitedListToStringArray(resourceLoaderPath);
for (int i = 0; i < this.resourceLoaderPaths.length; i++) {
String path = this.resourceLoaderPaths[i];
if (!path.endsWith("/")) {
this.resourceLoaderPaths[i] = path + "/";
}
}
if (logger.isInfoEnabled()) {
logger.info("SpringResourceLoader for Velocity: using resource loader [{}] and resource loader paths {} .",
this.resourceLoader, Arrays.asList(this.resourceLoaderPaths));
}
}
@Override
public Reader getResourceReader(String source, String encoding) throws ResourceNotFoundException {
if (logger.isDebugEnabled()) {
logger.debug("Looking for Velocity resource with name [{}] .", source);
}
for (String resourceLoaderPath : this.resourceLoaderPaths) {
org.springframework.core.io.Resource resource = getResource(resourceLoaderPath + source, encoding);
this.resourceLoader.getResource(resourceLoaderPath + source);
try {
return new InputStreamReader(resource.getInputStream());
} catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find Velocity resource: {} .", resource);
}
}
}
throw new ResourceNotFoundException(
String.format("Could not find resource [%s] in Spring resource loader path.", source));
}
protected org.springframework.core.io.Resource getResource(String source, String encoding) {
return this.resourceLoader.getResource(source);
}
}