| 1 | |
package org.naftulin.configmgr.parsers; |
| 2 | |
|
| 3 | |
import java.net.URL; |
| 4 | |
import java.sql.Connection; |
| 5 | |
import java.sql.PreparedStatement; |
| 6 | |
import java.sql.ResultSet; |
| 7 | |
import java.sql.SQLException; |
| 8 | |
import java.util.HashMap; |
| 9 | |
import java.util.List; |
| 10 | |
import java.util.Map; |
| 11 | |
import java.util.Properties; |
| 12 | |
|
| 13 | |
import javax.naming.InitialContext; |
| 14 | |
import javax.naming.NamingException; |
| 15 | |
import javax.sql.DataSource; |
| 16 | |
|
| 17 | |
import org.apache.log4j.Logger; |
| 18 | |
import org.naftulin.configmgr.ConfigurationManagementEntry; |
| 19 | |
import org.naftulin.configmgr.ConfigurationManagementEntryImpl; |
| 20 | |
import org.naftulin.configmgr.ConfigurationManagerException; |
| 21 | |
import org.naftulin.configmgr.ConfigurationType; |
| 22 | |
import org.naftulin.configmgr.content.NameValuePairImpl; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | 5 | public class DbJndiParserImpl extends AbstractJndiParser implements ConfigEntryParser { |
| 28 | 5 | private static final Logger log = Logger.getLogger(DbJndiParserImpl.class); |
| 29 | |
|
| 30 | |
private static final long serialVersionUID = 1L; |
| 31 | |
private final String sql; |
| 32 | |
private final String keyColumnName; |
| 33 | |
private final String valueColumnName; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public DbJndiParserImpl(final String jndiName, final String initialContextFactory, final String sql, |
| 38 | |
final String keyColumnName, final String valueColumnName, final List<NameValuePairImpl> nameValuePairs) { |
| 39 | 5 | super(jndiName, initialContextFactory, nameValuePairs); |
| 40 | 5 | this.sql = sql; |
| 41 | 5 | this.keyColumnName = keyColumnName; |
| 42 | 5 | this.valueColumnName = valueColumnName; |
| 43 | 5 | } |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public ConfigurationManagementEntry getConfigurationManagementEntry( |
| 56 | |
final String key, final URL fileUrl) throws ConfigurationManagerException { |
| 57 | 5 | ConfigurationManagementEntry entry = null; |
| 58 | |
|
| 59 | 5 | validateParameters(key); |
| 60 | |
|
| 61 | |
|
| 62 | 5 | final Properties jndiProperties = new Properties(); |
| 63 | 5 | prepareJndiProperties(jndiProperties); |
| 64 | |
|
| 65 | 5 | Connection c = null; |
| 66 | 5 | PreparedStatement stmt = null; |
| 67 | 5 | ResultSet rs = null; |
| 68 | |
try { |
| 69 | 5 | InitialContext context = null; |
| 70 | 5 | context = getJndiContext(key, jndiProperties, context); |
| 71 | 5 | log.info("Initial context created for JNDI name " + jndiName); |
| 72 | |
|
| 73 | 5 | c = getJdbcConnection(key, context); |
| 74 | 5 | log.info("Connected to the database by JNDI name " + jndiName + " and sql " + sql); |
| 75 | |
|
| 76 | 5 | stmt = c.prepareStatement(sql); |
| 77 | 5 | log.debug("Statment perpared " + sql + " for database jndi name " + jndiName); |
| 78 | |
|
| 79 | 5 | rs = stmt.executeQuery(); |
| 80 | 5 | log.debug("Received result set for sql " + sql); |
| 81 | |
|
| 82 | 5 | entry = createEntryBasedOnNameValuePairs(key, rs); |
| 83 | 5 | log.info("configuration management entry created for key " + key + " with sql " + sql); |
| 84 | 0 | } catch (SQLException e) { |
| 85 | 0 | log.error("Could not connect to the database using jndi name: " + jndiName + " sql " + sql + " and other parameters from configuration " + key, e); |
| 86 | 0 | throw new ConfigurationManagerException("Could not connect to the database using jndiName: " + jndiName + " sql " + sql + " and other parameters from configuration " + key, e); |
| 87 | 0 | } finally { |
| 88 | 5 | DBUtils.closeResultSet(rs); |
| 89 | 5 | DBUtils.closeStatement(stmt); |
| 90 | 5 | DBUtils.closeConnection(c); |
| 91 | 0 | } |
| 92 | |
|
| 93 | 5 | return entry; |
| 94 | |
} |
| 95 | |
|
| 96 | |
private ConfigurationManagementEntry createEntryBasedOnNameValuePairs( |
| 97 | |
final String key, final ResultSet rs) throws SQLException { |
| 98 | |
ConfigurationManagementEntry entry; |
| 99 | 5 | final Map<String, String> configuration = new HashMap<String, String>(); |
| 100 | 5 | String mapKey = null; |
| 101 | 5 | String mapValue = null; |
| 102 | 20 | while(rs.next()) { |
| 103 | 10 | mapKey = rs.getString(keyColumnName); |
| 104 | 10 | mapValue = rs.getString(valueColumnName); |
| 105 | 10 | log.debug("getting key " + mapKey + " value " + mapValue + " for sql " + sql); |
| 106 | 10 | configuration.put(mapKey, mapValue); |
| 107 | |
} |
| 108 | 5 | final String fileName = "jndiName : " + jndiName + " sql " + sql; |
| 109 | |
|
| 110 | 5 | entry = new ConfigurationManagementEntryImpl(key, fileName, configuration, this, ConfigurationType.DB_JNDI); |
| 111 | 5 | return entry; |
| 112 | |
} |
| 113 | |
|
| 114 | |
private Connection getJdbcConnection(final String key, final InitialContext context) throws SQLException, ConfigurationManagerException { |
| 115 | 5 | Connection c = null; |
| 116 | |
try { |
| 117 | 5 | final DataSource dc = (DataSource) context.lookup(jndiName); |
| 118 | 5 | c = dc.getConnection(); |
| 119 | 0 | } catch (NamingException e) { |
| 120 | 0 | log.error("Could not lookup jndi name: " + jndiName + " sql " + sql + " and other parameters from configuration " + key, e); |
| 121 | 0 | throw new ConfigurationManagerException("Could not lookup jndi name: " + jndiName + " sql " + sql + " and other parameters from configuration " + key, e); |
| 122 | |
} |
| 123 | 5 | return c; |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
protected void validateParameters(final String key) throws ConfigurationManagerException { |
| 132 | 5 | super.validateParameters(key); |
| 133 | 5 | if (sql == null) { |
| 134 | 0 | throw new ConfigurationManagerException("sql is null, please provide sql in you master configuraton file for external cofiguraton with key =" + key); |
| 135 | |
} |
| 136 | 5 | if (keyColumnName == null) { |
| 137 | 0 | throw new ConfigurationManagerException("keyColumnName is null, please provide key column name in you master configuraton file for external cofiguraton with key =" + key); |
| 138 | |
} |
| 139 | 5 | if (valueColumnName == null) { |
| 140 | 0 | throw new ConfigurationManagerException("valueColumnName is null, please provide valueColumnName in you master configuraton file for external cofiguraton with key =" + key); |
| 141 | |
} |
| 142 | 5 | } |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
} |