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");
            } 
  • 相关阅读:
    vagrant up报错 Warning: Authentication failure. Retrying...解决方案
    node读写Excel操作
    批量转换word为pdf
    分享7个shell脚本实例--shell脚本练习必备
    shell脚本实例,通向shell脚本大师的必经之路
    前端优化DNS预解析
    如何选择开源协议
    深入理解document.referrer的用法
    使用 WebRTC 构建简单的前端视频通讯
    深入理解WebRTC
  • 原文地址:https://www.cnblogs.com/yanghuahui/p/3223822.html
Copyright © 2011-2022 走看看