wangting
2024-05-14 7b9edd238b007402a6027bb32cf612640cb54186
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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<String,String> elementMap = new HashMap<String,String>();
    
    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<String, String> list = (HashMap<String, String>) util.getAllLifyCycleFileName();
//      System.out.println(list.size());
//  }
 
//  private List<String> getAllLifyCycleFileName() {
//      List<String> nameList = new ArrayList<String>();
//      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<JarEntry> 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<String, String> 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<Entry<Object, Object>> propSet = confProperties.entrySet();
                for (Entry<Object, Object> ent : propSet) {
                    if (ent.getKey() != null && ent.getValue() != null)
                        elementMap.put(ent.getKey().toString(), ent.getValue().toString());
                }
            }
            
//          List<String> 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<String, String> getElementMap() {
        return elementMap;
    }
 
    public void setElementMap(HashMap<String, String> elementMap) {
        this.elementMap = elementMap;
    }
}