zoukankan      html  css  js  c++  java
  • 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))+"
    ");
            }
           
            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");
            } 
  • 相关阅读:
    2015北京网络赛 Couple Trees 倍增算法
    POJ 1330 Nearest Common Ancestors 倍增算法的LCA
    2015北京网络赛 G Boxes BFS+打表
    Codeforces Round#320 Div2 解题报告
    HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
    codeforces #329 div 2 B. Anton and Lines(几何)
    codeforces #329 div 2 A. 2Char (暴力)
    hdu 1394 Minimum Inversion Number (树状数组 逆序对)
    hdu 1754 I Hate It (线段树)
    codeforces 589 G
  • 原文地址:https://www.cnblogs.com/yanghuahui/p/3223822.html
Copyright © 2011-2022 走看看