Hi,
I plan to hide it for my application to access a "settings"-table or file with two columns: attribute and value.
For that I thought to use apache.commons.
What I did (using a database) until now is getting the connection like this:
try {
Class.forName(plmSettingsDBDriver);
}
catch (ClassNotFoundException cnfe) {
logger.error("Error while reading database-driver-class", cnfe );
}
String url = plmSettingsDBURL;
String username = plmSettingsUser;
String password = mySettingsPassword;
Connection con = DriverManager.getConnection(url, username, password);
and reading the data as usual:
con = getPLMSettingsConnection();
for (int i = 0; i < select.length; i++) {
stmt = con.createStatement();
String query = "SELECT value FROM settings WHERE ATTRIBUTE = '" +
select[i] + "'";
ResultSet rs = stmt.executeQuery(query);
...
}
Now I would like to replace the reading of data with Connections, Statements and ResultSets with something like this:
String value = config.getEntry(select[0], "settings");
while config is of type CCommConf, where I defined the following functions:
private Configuration getConfiguration(String configName) throws Exception {
ConfigurationFactory factory = new ConfigurationFactory();
factory.setConfigurationFileName(CONFIGFILE);
Configuration conf1 = factory.getConfiguration();
Configuration conf2 = null;
if (con != null) {
conf2 = new DatabaseConfiguration((DataSource)con,
"settings", "attribute", "value");
} else {
ConfigurationFactory factory2 = new ConfigurationFactory();
factory2.setConfigurationFileName(SETTINGSFILE);
conf2 = factory.getConfiguration();
}
// Create and initialize the node combiner
NodeCombiner combiner = new UnionCombiner();
// Construct the combined configuration
CombinedConfiguration cc = new CombinedConfiguration(combiner);
cc.addConfiguration((AbstractConfiguration) conf1, "db");
cc.addConfiguration((AbstractConfiguration) conf2, "settings");
return cc.getConfiguration(configName);
}
/**
* Constructor without any code
*
*/
public CCommConf() {
}
/**
* Set´s the connection for the settings-table, if the
* settings-table is in a database and not some other kind
* of storage.
* @param con Connection
*/
public void setConnection(Connection con) {
this.con = con;
}
public String getEntry(String entry, String configName) throws Exception {
String ret = null;
try {
config = getConfiguration(configName);
if (config.getProperty(entry) != null)
ret = config.getProperty(entry).toString();
} catch (ConfigurationException e) {
logger.fatal(e);
throw e;
}
return ret;
}
What I don´t know is now:
- How to get a DataSource (I know, the following case is not the right thing to do it: conf2 = new DatabaseConfiguration((DataSource)con, "settings", "attribute", "value"); ). I read somewhere that I have to use JNDI, but - if it´s really necessary - I don´t know how to do that. But maybe it´s not necessary to use JNDI, there is a simpler way?
Once you create a JNDI name that binds (links) to a DataSource which contains the connection pool, you can use code something like this to point to it:
public static Connection getConnection() {
Connection dbConnection = null;
InitialContext ic;
DataSource dataSource;
try {
ic = new InitialContext();
dataSource = (DataSource) ic.lookup("<THE JNDI NAME>");
if (dataSource != null) {
dbConnection = dataSource.getConnection()
}
} catch (Exception e) {
System.out.println(e.getMe
}
return dbConnection;
}