Modified the RuntimeConfig#getJDBCURL() method. The method now first tries to lookup an JNDI datasource identified by

'java:/comp/env/jdbc/ccm-ds '. Then the method determines the URL from this datasource. Attention: This will only work if the JDBC-URL provided for the datasource is complete (the URL must contain username and password for the connection). 
If the datasource can't be aquired, the value from the runtime.properties files is used.


git-svn-id: https://svn.libreccm.org/ccm/trunk@2611 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2014-04-14 15:48:58 +00:00
parent 09214ccb3d
commit e62f9c1727
1 changed files with 70 additions and 37 deletions

View File

@ -22,11 +22,18 @@ import com.arsdigita.util.jdbc.JDBCURLParameter;
import com.arsdigita.util.parameter.BooleanParameter; import com.arsdigita.util.parameter.BooleanParameter;
import com.arsdigita.util.parameter.IntegerParameter; import com.arsdigita.util.parameter.IntegerParameter;
import com.arsdigita.util.parameter.Parameter; import com.arsdigita.util.parameter.Parameter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**
* A configuration record for configuration of the runtime environment itself. * A configuration record for configuration of the runtime environment itself. (Which database to
* (Which database to use, database user and password, etc) * use, database user and password, etc)
* *
* @author Justin Ross <jross@redhat.com> * @author Justin Ross <jross@redhat.com>
* @version $Id: RuntimeConfig.java 1393 2006-11-28 09:12:32Z sskracic $ * @version $Id: RuntimeConfig.java 1393 2006-11-28 09:12:32Z sskracic $
@ -38,8 +45,7 @@ public final class RuntimeConfig extends AbstractConfig {
private static RuntimeConfig s_config; private static RuntimeConfig s_config;
/** /**
* Returns the singleton configuration record for the runtime * Returns the singleton configuration record for the runtime environment.
* environment.
* *
* @return The <code>RuntimeConfig</code> record; it cannot be null * @return The <code>RuntimeConfig</code> record; it cannot be null
*/ */
@ -74,24 +80,19 @@ public final class RuntimeConfig extends AbstractConfig {
// private! // private!
// private RuntimeConfig() { // private RuntimeConfig() {
m_url = new JDBCURLParameter("waf.runtime.jdbc_url"); m_url = new JDBCURLParameter("waf.runtime.jdbc_url");
m_poolSize = new IntegerParameter m_poolSize = new IntegerParameter("waf.runtime.jdbc_pool_size", Parameter.OPTIONAL,
("waf.runtime.jdbc_pool_size", Parameter.OPTIONAL,
new Integer(10)); new Integer(10));
m_pingInterval = new IntegerParameter m_pingInterval = new IntegerParameter("waf.runtime.jdbc_ping_interval", Parameter.OPTIONAL,
("waf.runtime.jdbc_ping_interval", Parameter.OPTIONAL,
new Integer(30000)); new Integer(30000));
m_queryCacheSize = new IntegerParameter m_queryCacheSize = new IntegerParameter("waf.runtime.query_cache_size", Parameter.OPTIONAL,
("waf.runtime.query_cache_size", Parameter.OPTIONAL,
new Integer(2000)); new Integer(2000));
m_threadTagging = new BooleanParameter m_threadTagging = new BooleanParameter("waf.runtime.thread_tagging",
("waf.runtime.thread_tagging",
Parameter.REQUIRED, Parameter.REQUIRED,
Boolean.TRUE); Boolean.TRUE);
m_resultSetWindowSize = new IntegerParameter m_resultSetWindowSize = new IntegerParameter("waf.runtime.jdbc_resultset_windowsize",
("waf.runtime.jdbc_resultset_windowsize", Parameter.REQUIRED, Parameter.REQUIRED,
new Integer(1)); new Integer(1));
m_runBackgroundTasks = new BooleanParameter m_runBackgroundTasks = new BooleanParameter("waf.runtime.run_background_tasks",
("waf.runtime.run_background_tasks",
Parameter.REQUIRED, Parameter.REQUIRED,
Boolean.TRUE); Boolean.TRUE);
@ -112,16 +113,46 @@ public final class RuntimeConfig extends AbstractConfig {
* @return A <code>String</code> JDBC URL; it cannot be null * @return A <code>String</code> JDBC URL; it cannot be null
*/ */
public final String getJDBCURL() { public final String getJDBCURL() {
//Try to get URL from JNDI
final String url = getJDBCURLfromJNDI();
if (url == null) {
//No JNDI datasource configured, use old behaviour
return (String) get(m_url); return (String) get(m_url);
} else {
//Return URL acquired via JNDI
return url;
}
}
private String getJDBCURLfromJNDI() {
final Connection connection;
try {
final Context initialContext = new InitialContext();
final DataSource dataSource = (DataSource) initialContext.lookup(
"java:/comp/env/jdbc/ccm-ds");
connection = dataSource.getConnection();
final DatabaseMetaData metaData = connection.getMetaData();
final String url = metaData.getURL();
connection.close();
return url;
} catch (NamingException ex) {
s_log.warn("Failed to find JNDI datasource 'java:/comp/env/jdbc/ccm-ds'. "
+ "Falling back to configuration via properties file.");
return null;
} catch (SQLException ex) {
s_log.warn("Failed to to determine JDBC URL from JNDI datasource 'java:/comp/env/jdbc/ccm-ds'. "
+ "Falling back to configuration via properties file.");
return null;
}
} }
/** /**
* Returns the maximum size to be used for the connection pool. * Returns the maximum size to be used for the connection pool.
* *
* @return An integer limit on the number of JDBC connections * @return An integer limit on the number of JDBC connections allowed open at once.
* allowed open at once.
*/ */
public final int getJDBCPoolSize() { public final int getJDBCPoolSize() {
return ((Integer) get(m_poolSize)).intValue(); return ((Integer) get(m_poolSize)).intValue();
} }
@ -144,9 +175,11 @@ public final class RuntimeConfig extends AbstractConfig {
} }
public final boolean isThreadTaggingEnabled() { public final boolean isThreadTaggingEnabled() {
return ((Boolean)get(m_threadTagging)).booleanValue(); return ((Boolean) get(m_threadTagging)).booleanValue();
} }
public final boolean runBackGroundTasks() { public final boolean runBackGroundTasks() {
return ((Boolean)get(m_runBackgroundTasks)).booleanValue(); return ((Boolean) get(m_runBackgroundTasks)).booleanValue();
} }
} }