View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package com.rc.celeritas.generics;
7   
8   import com.rc.celeritas.exception.CeleritasException;
9   import com.rc.celeritas.util.UtilConstants;
10  import java.io.FileInputStream;
11  import java.io.FileNotFoundException;
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.util.HashMap;
15  import java.util.Iterator;
16  import java.util.Properties;
17  import java.util.ResourceBundle;
18  import org.apache.commons.collections.MapUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.log4j.Logger;
21  
22  /**
23   *
24   * @author rchoudhary
25   */
26  public class PropertyHelper {
27  
28      private static ResourceBundle rb;
29      private static boolean _initialized = false;
30      private static Logger log = Logger.getLogger(PropertyHelper.class);
31  
32      /**
33       *
34       * @param iluRefMap
35       * @return
36       */
37      public static HashMap<String, HashMap<String, String>> generateMap(Properties iluRefMap) {
38          HashMap<String, HashMap<String, String>> iluMap = null;
39          if(MapUtils.isNotEmpty(iluRefMap)){
40              iluMap = new HashMap<String, HashMap<String, String>>();
41              Iterator iter = iluRefMap.keySet().iterator();
42              while(iter.hasNext()){
43                  String key = (String)iter.next();
44                  String luString = MapUtils.getString(iluRefMap, key, "");
45                  HashMap<String, String> lookup = parseLuFields(luString);
46                  iluMap.put(key, lookup);
47              }
48          }
49          return iluMap;
50      }
51  
52      /**
53       *
54       * @return
55       * @throws com.rc.celeritas.exception.CeleritasException
56       */
57      public static boolean init() throws CeleritasException{
58          try{
59              rb = ResourceBundle.getBundle("celeritas");
60          } catch (Exception e){
61              log.error("Error in Initializing Celeritas : " + e.toString());
62              throw new CeleritasException(e);
63          }
64          return _initialized = true;
65      }
66  
67      /**
68       *
69       * @param key
70       * @return
71       */
72      public static String getValue(String key){
73          return rb.getString(key);
74      }
75  
76      /**
77       * 
78       * @param key
79       * @param database
80       * @return
81       */
82      public static String getDBValue(String key, String database){
83          return rb.getString(rb.getString(database) + "." + key);
84      }
85  
86      /**
87       *
88       * @param fileName
89       * @return
90       * @throws com.rc.celeritas.exception.CeleritasException
91       */
92      public static synchronized Properties loadProperty(String fileName) throws CeleritasException{
93          Properties props = new Properties();
94          try{
95              //FileInputStream in = new FileInputStream(fileName);
96              InputStream in = PropertyHelper.class.getClassLoader().getResourceAsStream(fileName);
97              if(StringUtils.endsWithIgnoreCase(fileName, UtilConstants.XML)){
98                  props.loadFromXML(in);
99              } else if (StringUtils.endsWithIgnoreCase(fileName, UtilConstants.PROPERTIES) || StringUtils.endsWithIgnoreCase(fileName, UtilConstants.PROP)){
100                 props.load(in);
101             } else {
102                 throw new CeleritasException("Unknown File Format in file : " + fileName);
103             }
104 
105         }catch (FileNotFoundException fnfe){
106             log.error("Error in reading file : " + fileName  + "\n" + fnfe.toString());
107             fnfe.printStackTrace();
108             throw new CeleritasException(fnfe);
109         }catch (IOException ioe){
110             log.error("Error in loading property for file : " + fileName + "\n" + ioe.toString());
111             throw new CeleritasException(ioe);
112         }
113         return props;
114     }
115 
116     /**
117      *
118      * @param luString
119      * @return
120      */
121     public static synchronized HashMap<String, String> parseLuFields(String luString) {
122         HashMap<String, String> lookup = null;
123         if(StringUtils.isNotEmpty(luString)){
124             lookup = new HashMap<String, String>();
125             String lus[] = luString.split(Constants.ILU_SEPERATOR);
126             for(int i = 0;i < lus.length; i++){
127                 if(StringUtils.isNotEmpty(lus[i])){
128                     String pair[] = lus[i].split(Constants.KEY_VALUE_SEPERATOR);
129                     if(pair.length == 1){
130                         lookup.put(pair[0], pair[0]);
131                     }else if(pair.length > 1){
132                         lookup.put(pair[0], pair[1]);
133                     }
134                 }
135             }
136         }
137         return lookup;
138     }
139 
140 }