Coverage Report - org.naftulin.configmgr.parsers.DbDirectParserImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DbDirectParserImpl
78%
62/80
56%
10/18
0
 
 1  
 package org.naftulin.configmgr.parsers;
 2  
 
 3  
 import java.net.URL;
 4  
 import java.sql.Connection;
 5  
 import java.sql.DriverManager;
 6  
 import java.sql.PreparedStatement;
 7  
 import java.sql.ResultSet;
 8  
 import java.sql.SQLException;
 9  
 import java.util.HashMap;
 10  
 import java.util.Iterator;
 11  
 import java.util.List;
 12  
 import java.util.Map;
 13  
 import java.util.Properties;
 14  
 
 15  
 import org.apache.log4j.Logger;
 16  
 import org.naftulin.configmgr.ConfigurationManagementEntry;
 17  
 import org.naftulin.configmgr.ConfigurationManagementEntryImpl;
 18  
 import org.naftulin.configmgr.ConfigurationManagerException;
 19  
 import org.naftulin.configmgr.ConfigurationType;
 20  
 import org.naftulin.configmgr.content.AbstractRecordImpl;
 21  
 import org.naftulin.configmgr.content.NameValuePairImpl;
 22  
 
 23  
 /*
 24  
  * 
 25  
  */
 26  5
 public class DbDirectParserImpl implements ConfigEntryParser {
 27  5
         private static final Logger log = Logger.getLogger(ConfigEntryParser.class);
 28  
         
 29  
         private static final long serialVersionUID = 1L;
 30  
         private final String driverClass;
 31  
         private final String connectionUrl;
 32  
         private final String sql;
 33  
         private final String user;
 34  
         private final String password;
 35  
         private final String keyColumnName;
 36  
         private final String valueColumnName;
 37  
         private List<NameValuePairImpl> nameValuePairs;
 38  
         
 39  
          
 40  
 
 41  
         public DbDirectParserImpl(final String driverClass, final String connectionUrl, final String sql, 
 42  
                         final String user, final String password, final String keyColumnName, 
 43  
                         final String valueColumnName, final List<NameValuePairImpl> nameValuePairs) {
 44  85
                 super();
 45  85
                 this.driverClass = driverClass;
 46  85
                 this.connectionUrl = connectionUrl;
 47  85
                 this.sql = sql;
 48  85
                 this.user = user;
 49  85
                 this.password = password;
 50  85
                 this.keyColumnName = keyColumnName;
 51  85
                 this.valueColumnName = valueColumnName;
 52  85
                 this.nameValuePairs = nameValuePairs;
 53  85
         }
 54  
 
 55  
         /**
 56  
          * Returns configuratoin management entry that contains a Map with key-value pairs read from the database based on the sql specified. 
 57  
          * The steps are: first database connection is established by loading the database driver and connecting to the database using database URL
 58  
          * and user/ password if provided with any other connection specific properties if provided in configuration description. 
 59  
          * Second sql provided is executed against the database connection and lastly the results are read based on the columns provided in the
 60  
          * configuration.
 61  
          * @param key configuration management key
 62  
          * @param fileUrl file url is null, since database configuration is not file URL based.
 63  
          * @return Configuration management entry.
 64  
          */
 65  
         public ConfigurationManagementEntry getConfigurationManagementEntry(
 66  
                         final String key, final URL fileUrl) throws ConfigurationManagerException {
 67  85
                 ConfigurationManagementEntry entry = null;
 68  
                 // at this point connect to the database, and get the info
 69  85
                 validateParameters(key);
 70  
                 
 71  
                 
 72  85
                 loadJdbcDriverClass(key);
 73  85
                 log.info("Driver class " + driverClass + " is loaded.");
 74  
                 
 75  85
                 final Properties props = new Properties();
 76  85
                 prepareJdbcConnectionProperties(props);
 77  
                 
 78  85
                 Connection c = null;
 79  85
                 PreparedStatement stmt = null;
 80  85
                 ResultSet rs = null;
 81  
                 try {
 82  85
                         c = DriverManager.getConnection(connectionUrl, props);
 83  5
                         log.info("Connected to the database " + connectionUrl + " and sql " + sql);
 84  
                         
 85  5
                         stmt = c.prepareStatement(sql);
 86  5
                         log.debug("Statment perpared " + sql + " for database " + connectionUrl);
 87  
                         
 88  5
                         rs = stmt.executeQuery();
 89  5
                         log.debug("Received result set for sql " + sql);
 90  
                         
 91  5
                         entry = createEntryBasedOnResultSet(key, rs);
 92  5
                         log.info("configuration management entry created for key " + key + " with sql " + sql);
 93  80
                 } catch (SQLException e) {
 94  80
                         log.error("Could not connect to the database using driver class: " + driverClass + " url " + connectionUrl + " sql " + sql + " and other parameters from configuration " + key, e);
 95  80
                         throw new ConfigurationManagerException("Could not connect to the database using driver class: " + driverClass + " url " + connectionUrl + " sql " + sql + " and other parameters from configuration " + key, e);
 96  80
                 } finally {
 97  85
                         DBUtils.closeResultSet(rs);
 98  85
                         DBUtils.closeStatement(stmt);
 99  85
                         DBUtils.closeConnection(c);
 100  80
                 }
 101  
                 
 102  5
                 return entry;
 103  
         }
 104  
 
 105  
         private ConfigurationManagementEntry createEntryBasedOnResultSet(
 106  
                         final String key, final ResultSet rs) throws SQLException {
 107  
                 ConfigurationManagementEntry entry;
 108  5
                 final Map<String, String> configuration = new HashMap<String, String>();
 109  5
                 String mapKey = null;
 110  5
                 String mapValue = null;
 111  20
                 while(rs.next()) {
 112  10
                         mapKey = rs.getString(keyColumnName);
 113  10
                         mapValue = rs.getString(valueColumnName);
 114  10
                         log.debug("getting key " + mapKey + " value " + mapValue + " for sql " + sql);
 115  10
                         configuration.put(mapKey, mapValue);
 116  
                 }
 117  5
                 final String fileName = "driver : " + driverClass + " connection " + connectionUrl + " sql " + sql + " user " + user;
 118  
                 //construct the configuration entry
 119  5
                 entry = new ConfigurationManagementEntryImpl(key, fileName, configuration, this, ConfigurationType.DB_DIRECT);
 120  5
                 return entry;
 121  
         }
 122  
 
 123  
         private void prepareJdbcConnectionProperties(final Properties props) {
 124  85
                 if (user != null) { props.put("user", user); }
 125  85
                 if (password != null) { props.put("password", password); }
 126  
                 
 127  85
                 final Iterator<NameValuePairImpl> it = nameValuePairs.iterator();
 128  170
                 while(it.hasNext()) {
 129  0
                         NameValuePairImpl nvp = it.next();
 130  0
                         props.put(nvp.getName(), nvp.getValue());                        
 131  
                 }
 132  85
         }
 133  
 
 134  
         private void loadJdbcDriverClass(final String key) throws ConfigurationManagerException {
 135  
                 try {
 136  85
                         Class.forName(driverClass);
 137  0
                 } catch(Exception e) {
 138  0
                         log.error("Could not load driver class: " + driverClass + " for configuration " + key, e);
 139  0
                         throw new ConfigurationManagerException("Could not load driver class: " + driverClass + " for configuration " + key, e);
 140  
                 }
 141  85
         }
 142  
 
 143  
         private void validateParameters(final String key) throws ConfigurationManagerException {
 144  85
                 if (key == null) { 
 145  0
                         throw new ConfigurationManagerException("key is null, please provide the key in you master configuraton file for all the db direct cofigurations");
 146  
                 }
 147  85
                 if (driverClass == null) {
 148  0
                         throw new ConfigurationManagerException("driverClass is null, please provide driver class in you master configuraton file for external cofiguraton with key =" + key);
 149  
                 }
 150  85
                 if (sql == null) {
 151  0
                         throw new ConfigurationManagerException("sql is null, please provide sql in you master configuraton file for external cofiguraton with key =" + key);
 152  
                 }
 153  85
                 if (keyColumnName == null) {
 154  0
                         throw new ConfigurationManagerException("keyColumnName is null, please provide key column name in you master configuraton file for external cofiguraton with key =" + key);
 155  
                 }
 156  85
                 if (valueColumnName == null) {
 157  0
                         throw new ConfigurationManagerException("valueColumnName is null, please provide valueColumnName in you master configuraton file for external cofiguraton with key =" + key);
 158  
                 }
 159  85
         }
 160  
 
 161  
 
 162  
         public String getDriverClass() {
 163  0
                 return driverClass;
 164  
         }
 165  
 
 166  
         public String getKeyColumnName() {
 167  0
                 return keyColumnName;
 168  
         }
 169  
 
 170  
 
 171  
         public List<NameValuePairImpl> getNameValuePairs() {
 172  0
                 return nameValuePairs;
 173  
         }
 174  
 
 175  
 
 176  
         public String getPassword() {
 177  0
                 return password;
 178  
         }
 179  
 
 180  
 
 181  
         public String getSql() {
 182  0
                 return sql;
 183  
         }
 184  
 
 185  
 
 186  
         public String getUser() {
 187  0
                 return user;
 188  
         }
 189  
 
 190  
 
 191  
         public String getValueColumnName() {
 192  0
                 return valueColumnName;
 193  
         }
 194  
         
 195  
         public String getConnectionUrl() { 
 196  0
                 return this.connectionUrl;
 197  
         }
 198  
 
 199  
 }