tfischer 2005/07/08 10:53:34
Modified: src/java/org/apache/torque/dsfactory DataSourceFactory.java
AbstractDataSourceFactory.java
src/java/org/apache/torque/adapter DB.java
src/java/org/apache/torque TorqueInstance.java Torque.java
xdocs changes.xml
Log:
Updated runtime configuration process to do stricter checking, and removed the automatic
configuration of the DataSourceFactory with the name "default".
See xdocs/changes.xml for details. Solves issue TRQS308.
Revision Changes Path
1.6 +12 -1 db-torque/src/java/org/apache/torque/dsfactory/DataSourceFactory.java
Index: DataSourceFactory.java
===================================================================
RCS file: /home/cvs/db-torque/src/java/org/apache/torque/dsfactory/DataSourceFactory.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- DataSourceFactory.java 2 Jul 2005 07:22:33 -0000 1.5
+++ DataSourceFactory.java 8 Jul 2005 17:53:34 -0000 1.6
@@ -24,10 +24,21 @@
* A factory that returns a DataSource.
*
* @author <a href="mailto:jmcnally@apache.org">John McNally</a>
+ * @author <a href="mailto:fischer@seitenbau.de">Thomas Fischer</a>
* @version $Id$
*/
public interface DataSourceFactory
{
+ /**
+ * Key for the configuration which contains DataSourceFactories
+ */
+ public static final String DSFACTORY_KEY = "dsfactory";
+
+ /**
+ * Key for the configuration which contains the fully qualified name
+ * of the factory implementation class
+ */
+ public static final String FACTORY_KEY = "factory";
/**
* @return the <code>DataSource</code> configured by the factory.
1.15 +14 -8 db-torque/src/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java
Index: AbstractDataSourceFactory.java
===================================================================
RCS file: /home/cvs/db-torque/src/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- AbstractDataSourceFactory.java 31 Jan 2005 19:43:56 -0000 1.14
+++ AbstractDataSourceFactory.java 8 Jul 2005 17:53:34 -0000 1.15
@@ -56,14 +56,20 @@
/** "schema" Key for the configuration */
public static final String SCHEMA_KEY = "schema";
- /** "default.pool" Key for the configuration */
- public static final String DEFAULT_POOL_KEY = "defaults.pool";
+ /** "defaults" Key for the configuration */
+ public static final String DEFAULTS_KEY = "defaults";
- /** "default.connection" Key for the configuration */
- public static final String DEFAULT_CONNECTION_KEY = "defaults.connection";
-
- /** "default schema name" for the configuration */
- public static final String DEFAULT_SCHEMA_KEY = "defaults.schema";
+ /** "defaults.pool" Key for the configuration */
+ public static final String DEFAULT_POOL_KEY
+ = DEFAULTS_KEY + "." + POOL_KEY;
+
+ /** "defaults.connection" Key for the configuration */
+ public static final String DEFAULT_CONNECTION_KEY
+ = DEFAULTS_KEY + "." + CONNECTION_KEY;
+
+ /** default schema name for the configuration */
+ public static final String DEFAULT_SCHEMA_KEY
+ = DEFAULTS_KEY + "." + SCHEMA_KEY;
/** The log */
1.35 +6 -1 db-torque/src/java/org/apache/torque/adapter/DB.java
Index: DB.java
===================================================================
RCS file: /home/cvs/db-torque/src/java/org/apache/torque/adapter/DB.java,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- DB.java 31 Jan 2005 19:43:55 -0000 1.34
+++ DB.java 8 Jul 2005 17:53:34 -0000 1.35
@@ -73,6 +73,11 @@
/** <code><pre>SELECT ... WHERE ... AND ROW_NUMBER() OVER() < <limit></pre></code>
*/
public static final int LIMIT_STYLE_DB2 = 5;
+ /**
+ * Key for the configuration which contains database adapters
+ */
+ public static final String ADAPTER_KEY = "adapter";
+
/**
* Empty constructor.
*/
1.13 +134 -110 db-torque/src/java/org/apache/torque/TorqueInstance.java
Index: TorqueInstance.java
===================================================================
RCS file: /home/cvs/db-torque/src/java/org/apache/torque/TorqueInstance.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- TorqueInstance.java 2 Jul 2005 07:22:33 -0000 1.12
+++ TorqueInstance.java 8 Jul 2005 17:53:34 -0000 1.13
@@ -28,13 +28,12 @@
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
-
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-
import org.apache.torque.adapter.DB;
import org.apache.torque.adapter.DBFactory;
+import org.apache.torque.dsfactory.AbstractDataSourceFactory;
import org.apache.torque.dsfactory.DataSourceFactory;
import org.apache.torque.manager.AbstractBaseManager;
import org.apache.torque.map.DatabaseMap;
@@ -64,11 +63,8 @@
/** Logging */
private static Log log = LogFactory.getLog(TorqueInstance.class);
- /** A constant for <code>default</code>. */
- private static final String DEFAULT_NAME = "default";
-
/** The db name that is specified as the default in the property file */
- private String defaultDBName;
+ private String defaultDBName = null;
/** The global cache of database maps */
private Map dbMaps;
@@ -136,17 +132,22 @@
// properties that are contained in the configuration. First
// look for properties that are in the "torque" namespace.
- Configuration subConf = conf.subset("torque");
-
- if (subConf != null && !subConf.isEmpty())
+ Configuration subConf = conf.subset(Torque.TORQUE_KEY);
+ if (subConf == null || subConf.isEmpty())
{
- setConfiguration(subConf);
+ String error = ("Invalid configuration. No keys starting with "
+ + Torque.TORQUE_KEY
+ + " found in configuration");
+ log.error(error);
+ throw new TorqueException(error);
}
+ setConfiguration(subConf);
- dbMaps = new HashMap();
+ initDefaultDbName(conf);
initAdapters(conf);
initDataSourceFactories(conf);
+ dbMaps = new HashMap();
for (Iterator i = mapBuilders.iterator(); i.hasNext();)
{
//this will add any maps in this builder to the proper database map
@@ -160,10 +161,41 @@
isInit = true;
}
+
+
+ /**
+ * initializes the name of the default database
+ * @param conf the configuration representing the torque section
+ * of the properties file
+ * @throws TorqueException if the appropriate key is not set
+ */
+ private final void initDefaultDbName(Configuration conf)
+ throws TorqueException
+ {
+ // Determine default database name.
+ defaultDBName =
+ conf.getString(
+ Torque.DATABASE_KEY
+ + "."
+ + Torque.DEFAULT_KEY);
+ if (defaultDBName == null)
+ {
+ String error = "Invalid configuration: Key "
+ + Torque.TORQUE_KEY
+ + "."
+ + Torque.DATABASE_KEY
+ + "."
+ + Torque.DEFAULT_KEY
+ + " not set";
+ log.error(error);
+ throw new TorqueException(error);
+ }
+ }
/**
*
- * @param conf the Configuration representing the properties file
+ * @param conf the Configuration representing the torque section of the
+ * properties file
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
@@ -172,45 +204,58 @@
{
log.debug("initAdapters(" + conf + ")");
adapterMap = new HashMap();
- Configuration c = conf.subset("database");
+ Configuration c = conf.subset(Torque.DATABASE_KEY);
if (c == null || c.isEmpty())
{
- log.warn("No Database definitions found!");
+ String error = "Invalid configuration : "
+ + "No keys starting with "
+ + Torque.TORQUE_KEY
+ + "."
+ + Torque.DATABASE_KEY
+ + " found in configuration";
+ log.error(error);
+ throw new TorqueException(error);
}
- else
+
+ try
{
- boolean foundAdapters = false;
-
- try
+ for (Iterator it = c.getKeys(); it.hasNext(); )
{
- for (Iterator it = c.getKeys(); it.hasNext(); )
+ String key = (String) it.next();
+ if (key.endsWith(DB.ADAPTER_KEY))
{
- String key = (String) it.next();
- if (key.endsWith("adapter"))
- {
- String adapter = c.getString(key);
- String handle = key.substring(0, key.indexOf('.'));
- DB db = DBFactory.create(adapter);
- // register the adapter for this name
- adapterMap.put(handle, db);
- log.debug("Adding " + adapter + " -> " + handle + " as Adapter");
- foundAdapters = true;
- }
+ String adapter = c.getString(key);
+ String handle = key.substring(0, key.indexOf('.'));
+ DB db = DBFactory.create(adapter);
+ // register the adapter for this name
+ adapterMap.put(handle, db);
+ log.debug("Adding " + adapter + " -> " + handle + " as Adapter");
}
- if (!foundAdapters)
- {
- log.warn("Databases defined but no adapter "
- + "configurations found!");
- }
- }
- catch (Exception e)
- {
- log.error("Error reading configuration seeking database "
- + "adapters", e);
- throw new TorqueException(e);
}
}
+ catch (Exception e)
+ {
+ log.error("Error reading configuration seeking database "
+ + "adapters", e);
+ throw new TorqueException(e);
+ }
+
+ if (adapterMap.get(Torque.getDefaultDB()) == null)
+ {
+ String error = "Invalid configuration : "
+ + "No adapter definition found for default DB "
+ + "An adapter must be defined under "
+ + Torque.TORQUE_KEY
+ + "."
+ + Torque.DATABASE_KEY
+ + "."
+ + Torque.getDefaultDB()
+ + "."
+ + DB.ADAPTER_KEY;
+ log.error(error);
+ throw new TorqueException(error);
+ }
}
/**
@@ -224,63 +269,59 @@
{
log.debug("initDataSourceFactories(" + conf + ")");
dsFactoryMap = new HashMap();
- Configuration c = conf.subset("dsfactory");
- if (c != null)
+
+ Configuration c = conf.subset(DataSourceFactory.DSFACTORY_KEY);
+ if (c == null || c.isEmpty())
{
- boolean foundFactories = false;
+ String error = "Invalid configuration: "
+ + "No keys starting with "
+ + Torque.TORQUE_KEY
+ + "."
+ + DataSourceFactory.DSFACTORY_KEY
+ + " found in configuration";
+ log.error(error);
+ throw new TorqueException(error);
+ }
- try
+ try
+ {
+ for (Iterator it = c.getKeys(); it.hasNext();)
{
- for (Iterator it = c.getKeys(); it.hasNext();)
- {
- String key = (String) it.next();
- if (key.endsWith("factory"))
- {
- String classname = c.getString(key);
- String handle = key.substring(0, key.indexOf('.'));
- log.debug("handle: " + handle
- + " DataSourceFactory: " + classname);
- Class dsfClass = Class.forName(classname);
- DataSourceFactory dsf =
- (DataSourceFactory) dsfClass.newInstance();
- dsf.initialize(c.subset(handle));
- dsFactoryMap.put(handle, dsf);
- foundFactories = true;
- }
- }
- if (!foundFactories)
+ String key = (String) it.next();
+ if (key.endsWith(DataSourceFactory.FACTORY_KEY))
{
- log.warn("Data Sources configured but no factories found!");
+ String classname = c.getString(key);
+ String handle = key.substring(0, key.indexOf('.'));
+ log.debug("handle: " + handle
+ + " DataSourceFactory: " + classname);
+ Class dsfClass = Class.forName(classname);
+ DataSourceFactory dsf =
+ (DataSourceFactory) dsfClass.newInstance();
+ dsf.initialize(c.subset(handle));
+ dsFactoryMap.put(handle, dsf);
}
}
- catch (Exception e)
- {
- log.error("Error reading adapter configuration", e);
- throw new TorqueException(e);
- }
+ }
+ catch (Exception e)
+ {
+ log.error("Error reading adapter configuration", e);
+ throw new TorqueException(e);
}
- // As there might be a default database configured
- // to map "default" onto an existing datasource, we
- // must check, whether there _is_ really an entry for
- // the "default" in the dsFactoryMap or not. If it is
- // not, then add a dummy entry for the "default"
- //
- // Without this, you can't actually access the "default"
- // data-source, even if you have an entry like
- //
- // database.default = bookstore
- //
- // in your Torque.properties
- //
- String defaultDB = getDefaultDB();
-
- if (dsFactoryMap.get(DEFAULT_NAME) == null
- && !defaultDB.equals(DEFAULT_NAME))
- {
- log.debug("Adding a dummy entry for "
- + DEFAULT_NAME + ", mapped onto " + defaultDB);
- dsFactoryMap.put(DEFAULT_NAME, dsFactoryMap.get(defaultDB));
+ if (dsFactoryMap.get(Torque.getDefaultDB()) == null)
+ {
+ String error = "Invalid configuration : "
+ + "No DataSourceFactory definition for default DB found. "
+ + "A DataSourceFactory must be defined under the key"
+ + Torque.TORQUE_KEY
+ + "."
+ + DataSourceFactory.DSFACTORY_KEY
+ + "."
+ + Torque.getDefaultDB()
+ + "."
+ + DataSourceFactory.FACTORY_KEY;
+ log.error(error);
+ throw new TorqueException(error);
}
}
@@ -531,12 +572,7 @@
= (DataSourceFactory) dsFactoryMap.get(dsfKey);
try
{
- // Database "default" is just a reference to another database.
- // We must not close it explicitly.
- if (!DEFAULT_NAME.equals(dsfKey))
- {
- dsf.close();
- }
+ dsf.close();
it.remove();
}
catch (TorqueException e)
@@ -824,22 +860,10 @@
/**
* Returns the name of the default database.
*
- * @return name of the default DB
+ * @return name of the default DB, or null if Torque is not initialized yet
*/
public String getDefaultDB()
{
- if (conf == null)
- {
- return DEFAULT_NAME;
- }
- else if (defaultDBName == null)
- {
- // Determine default database name.
- defaultDBName =
- conf.getString(Torque.DATABASE_DEFAULT,
- DEFAULT_NAME).trim();
- }
-
return defaultDBName;
}
1.96 +21 -3 db-torque/src/java/org/apache/torque/Torque.java
Index: Torque.java
===================================================================
RCS file: /home/cvs/db-torque/src/java/org/apache/torque/Torque.java,v
retrieving revision 1.95
retrieving revision 1.96
diff -u -r1.95 -r1.96
--- Torque.java 2 Jul 2005 07:22:33 -0000 1.95
+++ Torque.java 8 Jul 2005 17:53:34 -0000 1.96
@@ -41,9 +41,27 @@
public abstract class Torque
{
/**
+ * The prefix for all configuration keys used by Torque
+ */
+ public static final String TORQUE_KEY = "torque";
+
+ /**
+ * the prefix for configuring the database adapters and the default database
+ */
+ public static final String DATABASE_KEY = "database";
+
+ /**
+ * The key used to configure the name of the default database
+ */
+ public static final String DEFAULT_KEY = "default";
+
+ /**
* Name of property that specifies the default map builder and map.
+ * @deprecated is not used any more. Use DATABASE_KEY and
+ * DEFAULT_KEY instead
*/
- public static final String DATABASE_DEFAULT = "database.default";
+ public static final String DATABASE_DEFAULT
+ = DATABASE_KEY + "." + DEFAULT_KEY;
/**
* A prefix for <code>Manager</code> properties in the configuration.
@@ -288,7 +306,7 @@
/**
* Returns the name of the default database.
*
- * @return name of the default DB
+ * @return name of the default DB, or null if Torque is not initialized yet
*/
public static String getDefaultDB()
{
1.170 +19 -0 db-torque/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/db-torque/xdocs/changes.xml,v
retrieving revision 1.169
retrieving revision 1.170
diff -u -r1.169 -r1.170
--- changes.xml 2 Jul 2005 15:22:32 -0000 1.169
+++ changes.xml 8 Jul 2005 17:53:34 -0000 1.170
@@ -28,6 +28,25 @@
<body>
<release version="3.2-dev" date="in CVS">
+ <action type="update" dev="tfischer" issue="TRQS308">
+ Changed runtime configuration checking. It is now required having set
+ the following properties:
+ <ul>
+ <li>torque.database.default</li>
+ <li>torque.database.<defaultDbName>.adapter</li>
+ <li>torque.dsfactory.<defaultDbName>.factory</li>
+ </ul>
+ This was done in order to detect misconfiguration more easily.
+ </action>
+ <action type="update" dev="tfischer">
+ The runtime does not configure the DataSourveFactory with the name "default"
+ any more automatically. If you need a DataSourceFactory with that name,
+ you have to configure it manually from now on.
+ </action>
+ <action type="update" dev="tfischer">
+ If Torque is not yet initialized, it does now return null as name of the
+ default database (not "default" as it was before).
+ </action>
<action type="update" dev="tfischer" issue="TRQS252">
The asColumns in a Criteria are now added to the SQL select clause
in the same order as they were added to a Criteria.
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org
|