All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.template.velocity.impl.AbstractResourceLoader Maven / Gradle / Ivy

package org.zodiac.template.velocity.impl;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

/**
 * Velocity ResourceLoader的抽象实现。
 *
 */
public abstract class AbstractResourceLoader extends ResourceLoader {

    protected Logger log = LoggerFactory.getLogger(getClass());

    protected AbstractResourceLoader() {
        super();
    }

    @Override
    public final Reader getResourceReader(String source, String encoding) throws ResourceNotFoundException {
        Resource resource = getResource(source, encoding);
        Exception exception = null;

        if (resource != null && resource.exists()) {
            try {
                return new InputStreamReader(resource.getInputStream());
            } catch (IOException e) {
                exception = e;
            }
        }
        throw new ResourceNotFoundException(getLogID() + " Error: could not find template: " + source, exception);
    }

    @Override
    public final boolean isSourceModified(org.apache.velocity.runtime.resource.Resource templateResource) {
        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 final long getLastModified(org.apache.velocity.runtime.resource.Resource templateResource) {
        Resource resource = getResource(templateResource.getName(), null);
        if (resource != null && resource.exists()) {
            try {
                return resource.lastModified();
            } catch (IOException e) {
            }
        }
        return 0;
    }

    protected final String normalizeTemplateName(String templateName) {
        if (StrUtil.isEmpty(templateName)) {
            throw new ResourceNotFoundException("Need to specify a template name!");
        }
        if (templateName.startsWith("/")) {
            templateName = templateName.substring(1);
        }
        return templateName;
    }

    protected abstract Resource getResource(String source, String encoding);

    @Override
    public boolean resourceExists(String resourceName) {
        Resource resource = getResource(resourceName, null);
        return resource != null && resource.exists();
    }

    protected abstract String getLogID();

    protected abstract String getDesc();

    protected abstract String getLoaderName();

    @Override
    public String toString() {
        return getLogID() + "[" + getDesc() + "]";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy