View Javadoc
1   package org.kuali.ole.config;
2   
3   import org.w3c.dom.Document;
4   import org.w3c.dom.Element;
5   import org.w3c.dom.Node;
6   import org.apache.log4j.Logger;
7   import org.w3c.dom.NodeList;
8   
9   import javax.xml.parsers.DocumentBuilder;
10  import javax.xml.parsers.DocumentBuilderFactory;
11  import java.io.*;
12  
13  import org.apache.commons.lang.StringUtils;
14  
15  import java.util.Properties;
16  
17  /**
18   * Created by chenchulakshmig on 3/4/15.
19   */
20  public class OLEReportDBConfig {
21  
22      private static final Logger LOG = Logger.getLogger(OLEReportDBConfig.class);
23      Properties prop;
24  
25      public Properties getProp() {
26          if (prop == null) {
27              prop = new Properties();
28              try {
29                  String environment = System.getProperty("environment");
30                  LOG.info(" Environment : " + environment);
31                  String path = System.getProperty("user.home") + File.separator + "kuali" + File.separator + "main" + File.separator + (environment == null ? "local" : environment);
32                  LOG.info(" Path for common-config.xml file  : " + path);
33                  File file = new File(path + File.separator + "common-config.xml");
34                  if (file.exists() && file.isFile()) {
35                      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
36                      DocumentBuilder dBuilder = null;
37                      dBuilder = dbFactory.newDocumentBuilder();
38                      Document doc = dBuilder.parse(file);
39                      NodeList nList = doc.getElementsByTagName("param");
40                      for (int i = 0; i < nList.getLength(); i++) {
41                          Node nNode = nList.item(i);
42                          if (nNode.getNodeType() == Node.ELEMENT_NODE) {
43                              Element element = (Element) nNode;
44                              if (element != null) {
45                                  String key = element.getAttribute("name");
46                                  if (element.getFirstChild() != null) {
47                                      String value = element.getFirstChild().getNodeValue();
48                                      prop.setProperty(key, value);
49                                  }
50                              }
51                          }
52                      }
53                      setAdditionalPropeties();
54                  }
55              } catch (Exception ex) {
56                  ex.printStackTrace();
57              }
58          }
59          return prop;
60      }
61  
62      public void setProp(Properties prop) {
63          this.prop = prop;
64      }
65  
66      private void setAdditionalPropeties() {
67          String dbaVendor = prop.getProperty("db.vendor");
68          if (StringUtils.isNotBlank(dbaVendor) && dbaVendor.equalsIgnoreCase("oracle")) {
69              prop.setProperty("report.dba.url", prop.getProperty("oracle.dba.url"));
70              prop.setProperty("report.dba.username", prop.getProperty("jdbc.username"));
71              prop.setProperty("report.dba.password", prop.getProperty("jdbc.username"));
72              prop.setProperty("report.dba.driver", "oracle.jdbc.driver.OracleDriver");
73          } else if (StringUtils.isNotBlank(dbaVendor) && dbaVendor.equalsIgnoreCase("mysql")) {
74              prop.setProperty("report.dba.url", prop.getProperty("mysql.dba.url") + File.separator + prop.getProperty("jdbc.username"));
75              prop.setProperty("report.dba.username", prop.getProperty("mysql.dba.username"));
76              prop.setProperty("report.dba.password", prop.getProperty("mysql.dba.password"));
77              prop.setProperty("report.dba.driver", "com.mysql.jdbc.Driver");
78          }
79      }
80  
81      public String getPropertyByKey(String key) {
82          return getProp().getProperty(key);
83      }
84  }