⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 templateloader.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
字号:
package com.cloudwebsoft.framework.template;

import java.io.File;
import java.io.IOException;
import cn.js.fan.cache.jcs.RMCache;
import com.cloudwebsoft.framework.util.LogUtil;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class TemplateLoader {
    HttpServletRequest request;

    String fileName = null;

    volatile long lastModifiedTime = 0;

    final String cacheGroup = "cws.framework.TemplateLoader";

    /**
     * the parsed result template
     */
    ITemplate template = null;

    /**
     * the template file path
     * @return String
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * load the template. record the file timestamp.
     *
     * @param fileName String
     * @throws IOException
     */
    public TemplateLoader(HttpServletRequest request, String fileName) throws IOException {
        this.request = request;
        this.fileName = fileName;

        load();
    }

    /**
     * each time a template is required,
     * the template loader check the file timestamp,
     * if the file timestamp is changed, the template loader reload the template.
     * if not return the current template
     *
     * @throws IOException
     * @return ITemplate
     */
    public ITemplate getTemplate() {
        return template;
    }

    /**
     *
     * @return
     */
    public boolean isFileModified() {
        long theFiletime = new File(fileName).lastModified();
        return lastModifiedTime != theFiletime;
    }

    public void loadFromFile() throws IOException {
        Parser parser = new Parser(request);
        // LogUtil.getLog(getClass()).info(getClass().getName() + " fileName=" + fileName);
        template = parser.parse(fileName);
        lastModifiedTime = new File(fileName).lastModified();
        // LogUtil.getLog(getClass()).info("template=" + template + " lastModifiedTime=" + lastModifiedTime);
    }

    /**
     *
     * @throws IOException
     */
    public void load() throws IOException {
        LogUtil.getLog(getClass()).info(getClass().getName() + " isFileModified=" + isFileModified());

        if (isFileModified()) {
            loadFromFile();
            if (template!=null) {
                try {
                    RMCache.getInstance().putInGroup(fileName, cacheGroup,
                            template);
                } catch (Exception e) {
                    LogUtil.getLog(getClass()).error("load:" + e.getMessage());
                }
            }
        } else {
            try {
                template = (ITemplate) RMCache.getInstance().getFromGroup(
                        fileName,
                        cacheGroup);
            } catch (Exception e) {
                LogUtil.getLog(getClass()).error("load:" + e.getMessage());
            }
            if (template == null) {
                loadFromFile();
                if (template != null) {
                    try {
                        RMCache.getInstance().putInGroup(fileName, cacheGroup,
                                template);
                    } catch (Exception e) {
                        LogUtil.getLog(getClass()).error("load:" + e.getMessage());
                    }
                }
            }
        }
    }

    public String toString() {
        return template.toString();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -