volicity 模板类,java操作配置文件

import java.io.StringWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;


public class VMRenderUtils {

    static public final String vmfileName = "email.vm";
    static private final Properties  p      = new Properties();
    static {
        p.put("file.resource.loader.class",
              "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(p);
    }

    static public String render(String flag, String emailSubject, String emailHost, 
            String emailFrom, String emailPassword, Map<String, HashSet<String>> moduleInCharge) {
        Template template = Velocity.getTemplate(vmfileName);
        
        StringBuilder moduleInChargeString = new StringBuilder();
        
        Set<String> key = moduleInCharge.keySet();
        for (Iterator<String> it = key.iterator(); it.hasNext();) {
            String s = (String) it.next();
            moduleInChargeString.append(s+"="+HashSetToString(moduleInCharge.get(s))+"\n");
        }
       
        Context context = buildContext(flag, emailSubject, emailHost, 
                 emailFrom,  emailPassword,  moduleInChargeString.toString());
        StringWriter sw = new StringWriter();
        template.merge(context, sw);
        return sw.toString();
    }

    static Context buildContext(String flag, String emailSubject, String emailHost, 
            String emailFrom, String emailPassword, String moduleInCharge) {
        Context context = new VelocityContext();
        context.put("flag", flag);
        context.put("emailSubject", emailSubject);
        context.put("emailHost", emailHost);
        context.put("emailFrom", emailFrom);
        context.put("emailPassword", emailPassword);
        
        context.put("moduleInCharge", moduleInCharge);

        return context;
    }
    
    private static String HashSetToString(HashSet<String> hashSet) {
        String result = "";
        if(hashSet == null) {
            return result;
        }
        
        for(String at:hashSet) {
            result += (at+",");
        }
        if(!isBlank(result)) {
            result = result.substring(0,result.length()-1);
        }
        return result;
    }
    private static boolean isBlank(String value) {
        if(value == null || "".equals(value)) {
            return true;
        }
        return false;
    }
}

email.vm文件

SUPER_ADMIN=huahuiyang@gmail.com

#whether to send email
LOL_EMAIL=${flag}

#email subject
EMAIL_SUBJECT=${emailSubject}
EMAIL_HOST=${emailHost}
EMAIL_FROM=${emailFrom}
EMAIL_PASSWORD=${emailPassword}

#send mail to....
${moduleInCharge}

 

java如何调用:

URL filePath = Thread.currentThread().getContextClassLoader().getResource("email.properties");
        File file = new File(filePath.toString().substring(5));
        FileWriter fw;
        try {
            fw = new FileWriter(file);
            fw.write(configureFile);
            fw.close();
        } catch (IOException e) {
            logger.warn("fail to update configure file email.properties");
        } 

 

posted @ 2013-07-29 20:08  yanghuahui  阅读(1283)  评论(0编辑  收藏  举报