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");
        } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值