package com.vci.server.omd.lifecycle; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; public class LifeCycleServerUtil { //private static String developerFileName = "properties/lce_events.properties"; private static String developerFileName = "properties/lifeCycleEvents.properties"; private static Properties confProperties = null; private static LifeCycleServerUtil instance = null; private HashMap elementMap = new HashMap(); private LifeCycleServerUtil() { } public static LifeCycleServerUtil getInstance() { if (instance == null) { instance = new LifeCycleServerUtil(); } return instance; } // public static void main(String[] args) { // LifeCycleServerUtil util = new LifeCycleServerUtil(); // HashMap list = (HashMap) util.getAllLifyCycleFileName(); // System.out.println(list.size()); // } // private List getAllLifyCycleFileName() { // List nameList = new ArrayList(); // try { // URLClassLoader loader = (URLClassLoader)this.getClass().getClassLoader(); // URL[] urls = loader.getURLs(); // for(URL url : urls){ //add by caill 2016.3.21收集src-properties目录下所有properties文件中的键值对 // if(url.toURI().toString().endsWith(".jar")){ // JarFile jar = new JarFile(new File(url.toURI())); // // Enumeration entrys = jar.entries(); // while(entrys.hasMoreElements()){ // JarEntry entry = entrys.nextElement(); // ZipEntry zip = (ZipEntry)entry; // if(zip.getName().contains("properties/lce_event") // && zip.getName().endsWith(".properties")){ // //nameList.add(zip.getName()); // // Properties props = new Properties(); // props.load(this.getClass().getClassLoader().getResourceAsStream(zip.getName())); // nameList.add(props.toString().trim()); // } // } // } // } // //add by caill start 2016.3.24收集与src目录平级的properties目录下的lce_events.properties文件中的键值对 // File file2 = new File(developerFileName); // if (file2.exists()) { // FileInputStream confPropertiesStream = new FileInputStream(developerFileName); // confProperties = new Properties(); // confProperties.load(confPropertiesStream); // nameList.add(confProperties.toString().trim()); // } // //add by caill end // } catch (Exception e) { // e.printStackTrace(); // } // // return nameList; // } //add by caill start 2016.3.10将propertity文件中的key和value放入map中 public Map getAllLifeEventConf() { if (elementMap == null || elementMap.keySet().size() == 0) { File file = new File(developerFileName); try { if (file.exists()) { FileInputStream stream = new FileInputStream(developerFileName); confProperties = new Properties(); confProperties.load(stream); } } catch (Exception e) { e.printStackTrace(); } if (confProperties != null) { Set> propSet = confProperties.entrySet(); for (Entry ent : propSet) { if (ent.getKey() != null && ent.getValue() != null) elementMap.put(ent.getKey().toString(), ent.getValue().toString()); } } // List nameList = getAllLifyCycleFileName(); // for(int i = 0;i < nameList.size(); i++){ // String str = nameList.get(i).substring(1, nameList.get(i).length()-1); // String[] strArray = str.split(","); // for(int j = 0; j < strArray.length; j++){ // int index = strArray[j].indexOf("="); // String key = strArray[j].substring(0, index).trim(); // String value = strArray[j].substring(index+1, strArray[j].length()).trim(); // elementMap.put(key, value); // } // } } return elementMap; } //add by caill end public HashMap getElementMap() { return elementMap; } public void setElementMap(HashMap elementMap) { this.elementMap = elementMap; } }