Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2282174
  • 博文数量: 321
  • 博客积分: 3440
  • 博客等级: 中校
  • 技术积分: 2992
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-24 09:08
个人简介

我就在这里

文章分类

全部博文(321)

文章存档

2015年(9)

2014年(84)

2013年(101)

2012年(25)

2011年(29)

2010年(21)

2009年(6)

2008年(23)

2007年(23)

分类: Java

2011-05-04 09:51:09

package org.stephencat.listener;
import java.io.IOException;
import java.io.*;
import java.util.*;
import javax.servlet.*;
/**
 * 自动监听任务
 * @author stephen
 *
 */
public class PropertiesTask extends TimerTask {
    private ServletContext context = null;
    /**
     * 配置文件的最后更新时间
     */
    private long lastModified = 0;
    /**
     * 构造一个自动更新任务
     * @param context
     */
    public PropertiesTask(ServletContext context){
        this.context = context;
        System.out.println("A task instance is created now."); // 任务在整个 application 周期内只创建一次。
    }
    /**
     * 每次执行任务时显示一个随机数。
     */
    public void todoTestRandom(){
        System.out.println("Task running");
        context.setAttribute("random", String.valueOf(Math.random()));
        System.out.println((String)context.getAttribute("random"));
    }
    /**
     * 监听配置文件是否被更新。
     */
    public void todoTestFileStatus(){
        System.out.println("Getting file status");
        System.out.println(this.isFileUpdated("WEB-INF/platforms/test.properties"));
    }
    /**
     * 监听配置文件是否被更新,自动更新文件中的配置项存储到 application 变量中。
     */
    public void todo(){
        String filename = "WEB-INF/platforms/test.properties";
        if(this.isFileUpdated(filename)){
            System.out.println("Getting properties");
            try{
                this.loadProperties("test", filename);
            }catch(IOException ioe){
                System.err.println(ioe.getMessage());
            }
        }
        System.out.println("Test value is: " + this.getTestProperty("name"));
    }
    public void run() {
        todoTestRandom();
        todo();
        //todo();
    }
    /**
     * 判断物理文件是否已被更新
     * @param filename 物理文件名
     * @return 是 true 否 false
     */
    private boolean isFileUpdated(String filename){
        File file = new File(context.getRealPath(filename));
        if(file.isFile()){
            long lastUpdateTime = file.lastModified();
            if(lastUpdateTime > this.lastModified){
                System.out.println("The properties file was modified.");
                this.lastModified = lastUpdateTime;
                return true;
            }else{
                System.out.println("The properties file was not modified.");
                return false;
            }
        }else{
            System.out.println("The path does not point to a file.");
            return false;
        }
    }
    /**
     * 获取配置文件
     * @param key
     * @param filename
     * @return
     */
    public void loadProperties(String key, String filename) throws IOException{
        Properties prop = new Properties();
        InputStream stream = context.getResourceAsStream(filename);
        prop.load(stream);
        if(stream!=null){
            stream.close();
        }
        context.setAttribute(key, prop);
    }
    /**
     * 从 application 取配置项的值
     * @param key 配置项的键名
     * @return 配置项的值
     */
    public String getTestProperty(String key){
        Properties prop = (Properties)context.getAttribute("test");
        if(prop==null){
            return null;
        }else{
            return (String)prop.get(key);
        }
    }
}
阅读(1339) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~