Author: tfischer
Date: Wed May 9 11:09:40 2012
New Revision: 1336091
URL: http://svn.apache.org/viewvc?rev=1336091&view=rev
Log:
TORQUE-199
configure schema per-database (still allowing per-datasource configuration)
Added:
db/torque/torque4/trunk/torque-runtime/src/test/resources/torque-schematest.properties
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/PerUserPoolDataSourceFactory.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/SharedPoolDataSourceFactory.java
db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java?rev=1336091&r1=1336090&r2=1336091&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
Wed May 9 11:09:40 2012
@@ -61,6 +61,9 @@ public class Database
*/
private DataSourceFactory dataSourceFactory;
+ /** The Schema name of this database, may be null if not set. */
+ private String schema = null;
+
/**
* A special table used to generate primary keys for the other tables.
*/
@@ -259,28 +262,23 @@ public class Database
/**
* Returns the database schema for this Database.
+ *
* @return the database schema for this database, or null if no schema
* has been set.
*/
public String getSchema()
{
- DataSourceFactory dsf = getDataSourceFactory();
- if (dsf == null)
- {
- return null;
- }
- return dsf.getSchema();
+ return schema;
}
/**
* Sets the schema for this database.
+ *
* @param schema the name of the database schema to set, or null to remove
* the current schema.
- * @throws NullPointerException if no DatasourceFactory exists for this
- * database.
*/
public void setSchema(String schema)
{
- getDataSourceFactory().setSchema(schema);
+ this.schema = schema;
}
}
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java?rev=1336091&r1=1336090&r2=1336091&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java
Wed May 9 11:09:40 2012
@@ -60,13 +60,15 @@ public final class Torque
*/
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_KEY + "." + DEFAULT_KEY;
+ /** "schema" Key for the configuration */
+ public static final String SCHEMA_KEY = "schema";
+
+ /** "defaults" Key for the configuration */
+ public static final String DEFAULTS_KEY = "defaults";
+
+ /** default schema name for the configuration */
+ public static final String DEFAULT_SCHEMA_KEY
+ = DEFAULTS_KEY + "." + SCHEMA_KEY;
/**
* A prefix for <code>Manager</code> properties in the configuration.
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java?rev=1336091&r1=1336090&r2=1336091&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
Wed May 9 11:09:40 2012
@@ -78,14 +78,14 @@ public class TorqueInstance
* and other operations where the databaase map needs to stay
* in a defined state must be synchronized to this map.
*/
- private Map<String, Database> databases
+ private final Map<String, Database> databases
= Collections.synchronizedMap(new HashMap<String, Database>());
/** A repository of Manager instances. */
private Map<String, AbstractBaseManager> managers;
/** A repository of idBroker instances. */
- private Set<IDBroker> idBrokers = new HashSet<IDBroker>();
+ private final Set<IDBroker> idBrokers = new HashSet<IDBroker>();
/** Torque-specific configuration. */
private Configuration conf;
@@ -143,6 +143,7 @@ public class TorqueInstance
initTransactionManager(conf);
initDefaultDbName(conf);
initDataSourceFactories(conf);
+ initSchemata(conf);
initAdapters(conf);
// As there might be a default database configured
@@ -236,6 +237,7 @@ public class TorqueInstance
Transaction.setTransactionManager(transactionManager);
}
+
/**
* Initializes the name of the default database and
* associates the database with the name <code>DEFAULT_NAME</code>
@@ -384,11 +386,12 @@ public class TorqueInstance
}
/**
- * Reads the settings for the DataSourceFactories from the configuration
- * and creates and/or cinfigures the DataSourceFactories for the databases.
+ * Reads the settings for the DataSourceFactories from the
+ * configuration and creates and/or configures the DataSourceFactories
+ * and the database objects.
* If no DataSorceFactory is assigned to the database with the name
* <code>DEFAULT_NAME</code>, a reference to the DataSourceFactory
- * of the default daztabase is made from the database with the name
+ * of the default database is made from the database with the name
* <code>DEFAULT_NAME</code>.
*
* @param conf the Configuration representing the properties file
@@ -413,6 +416,7 @@ public class TorqueInstance
throw new TorqueException(error);
}
+ // read dsfactory config (may contain schema)
try
{
for (Iterator<?> it = c.getKeys(); it.hasNext();)
@@ -427,21 +431,34 @@ public class TorqueInstance
Class<?> dsfClass = Class.forName(classname);
DataSourceFactory dsf =
(DataSourceFactory) dsfClass.newInstance();
- dsf.initialize(c.subset(handle));
+ Configuration subConf = c.subset(handle);
+ dsf.initialize(subConf);
Database database = getOrCreateDatabase(handle);
database.setDataSourceFactory(dsf);
+
+ // deprecated method of schema configuration
+ // TODO: remove in Torque 4.1
+ String schema = subConf.getString(Torque.SCHEMA_KEY, null);
+ if (!StringUtils.isEmpty(schema))
+ {
+ log.warn("Defining the schema in the dsfactory "
+ + "is deprecated, please configure it "
+ + "via the config key "
+ + "torque.database.${databasename}.schema");
+ }
+ database.setSchema(schema);
}
}
}
catch (RuntimeException e)
{
- log.error("Runtime Error reading adapter configuration", e);
+ log.error("Error reading DataSourceFactory configuration", e);
throw new TorqueRuntimeException(e);
}
catch (Exception e)
{
- log.error("Error reading adapter configuration", e);
+ log.error("Error reading DataSourceFactory configuration", e);
throw new TorqueException(e);
}
@@ -465,6 +482,70 @@ public class TorqueInstance
}
}
+ private void initSchemata(Configuration conf)
+ throws TorqueException
+ {
+ log.debug("initSchemata(" + conf + ")");
+
+ // read schema configuration from database setting
+ Configuration c = conf.subset(Torque.DATABASE_KEY);
+ if (c == null || c.isEmpty())
+ {
+ String error = "Invalid configuration: "
+ + "No keys starting with "
+ + Torque.TORQUE_KEY
+ + "."
+ + Torque.DATABASE_KEY
+ + " found in configuration";
+ log.error(error);
+ throw new TorqueException(error);
+ }
+ try
+ {
+ for (Iterator<?> it = c.getKeys(); it.hasNext();)
+ {
+ String key = (String) it.next();
+ int indexOfDot = key.indexOf('.');
+ if (indexOfDot == -1)
+ {
+ continue;
+ }
+ String handle = key.substring(0, indexOfDot);
+
+ log.debug("database handle: " + handle);
+ Configuration subConf = c.subset(handle);
+
+ Database database = getOrCreateDatabase(handle);
+
+ String schema = subConf.getString(Torque.SCHEMA_KEY, null);
+ // check database schema because schema may have already been
+ // set via the dsfactory
+ if (StringUtils.isEmpty(schema))
+ {
+ schema = database.getSchema();
+ }
+ if (StringUtils.isEmpty(schema))
+ {
+ schema = conf.getString(
+ Torque.DEFAULT_SCHEMA_KEY,
+ null);
+ }
+ database.setSchema(schema);
+ }
+ }
+ catch (RuntimeException e)
+ {
+ log.error("Error reading DataSourceFactory configuration", e);
+ throw new TorqueRuntimeException(e);
+ }
+ catch (Exception e)
+ {
+ log.error("Error reading DataSourceFactory configuration", e);
+ throw new TorqueException(e);
+ }
+
+ }
+
/**
* Initialization of Torque with a properties file.
*
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java?rev=1336091&r1=1336090&r2=1336091&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java
Wed May 9 11:09:40 2012
@@ -29,7 +29,6 @@ import org.apache.commons.beanutils.Mapp
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
-import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.torque.Torque;
@@ -53,9 +52,6 @@ public abstract class AbstractDataSource
/** "connection" Key for the configuration */
public static final String CONNECTION_KEY = "connection";
- /** "schema" Key for the configuration */
- public static final String SCHEMA_KEY = "schema";
-
/** "defaults" Key for the configuration */
public static final String DEFAULTS_KEY = "defaults";
@@ -67,17 +63,9 @@ public abstract class AbstractDataSource
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 */
private static Log log =
LogFactory.getLog(AbstractDataSourceFactory.class);
- /** Internal Marker for the Schema name of this database connection */
- private String schema = null;
-
/**
* Encapsulates setting configuration properties on
* <code>DataSource</code> objects.
@@ -248,30 +236,6 @@ public abstract class AbstractDataSource
}
/**
- * Sets the current schema for the database connection
- *
- * @param schema The current schema name
- */
- public void setSchema(String schema)
- {
- this.schema = schema;
- }
-
- /**
- * This method returns the current schema for the database connection
- *
- * @return The current schema name. Null means, no schema has been set.
- * @throws TorqueException Any exceptions caught during processing will be
- * rethrown wrapped into a TorqueException.
- * @deprecated use DatabaseInfo.setSchema() instead. Will be removed
- * in a future version of Torque.
- */
- public String getSchema()
- {
- return schema;
- }
-
- /**
* @return the <code>DataSource</code> configured by the factory.
* @throws TorqueException if the source can't be returned
*/
@@ -285,23 +249,6 @@ public abstract class AbstractDataSource
* @throws TorqueException Any exceptions caught during processing will be
* rethrown wrapped into a TorqueException.
*/
- public void initialize(Configuration configuration)
- throws TorqueException
- {
- if (configuration == null)
- {
- throw new TorqueException(
- "Torque cannot be initialized without "
- + "a valid configuration. Please check the log files "
- + "for further details.");
- }
-
- schema = configuration.getString(SCHEMA_KEY, null);
-
- if (StringUtils.isEmpty(schema))
- {
- Configuration conf = Torque.getConfiguration();
- schema = conf.getString(DEFAULT_SCHEMA_KEY, null);
- }
- }
+ public abstract void initialize(Configuration configuration)
+ throws TorqueException;
}
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java?rev=1336091&r1=1336090&r2=1336091&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/DataSourceFactory.java
Wed May 9 11:09:40 2012
@@ -61,26 +61,6 @@ public interface DataSourceFactory
throws TorqueException;
/**
- * Sets the current schema for the database connection
- *
- * @param schema The current schema name
- * @deprecated use Database.setSchema() instead. Will be removed
- * in a future version of Torque.
- */
- void setSchema(String schema);
-
- /**
- * This method returns the current schema for the database connection
- *
- * @return The current schema name. Null means, no schema has been set.
- * @throws TorqueException Any exceptions caught during processing will be
- * rethrown wrapped into a TorqueException.
- * @deprecated use Database.getSchema() instead. Will be removed
- * in a future version of Torque.
- */
- String getSchema();
-
- /**
* A hook which is called when the resources of the associated DataSource
* can be released.
* After close() is called, the other methods may not work any more
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java?rev=1336091&r1=1336090&r2=1336091&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java
Wed May 9 11:09:40 2012
@@ -126,8 +126,6 @@ public class JndiDataSourceFactory
*/
public void initialize(Configuration configuration) throws TorqueException
{
- super.initialize(configuration);
-
initJNDI(configuration);
initDataSource(configuration);
}
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/PerUserPoolDataSourceFactory.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/PerUserPoolDataSourceFactory.java?rev=1336091&r1=1336090&r2=1336091&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/PerUserPoolDataSourceFactory.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/PerUserPoolDataSourceFactory.java
Wed May 9 11:09:40 2012
@@ -63,8 +63,6 @@ public class PerUserPoolDataSourceFactor
*/
public void initialize(Configuration configuration) throws TorqueException
{
- super.initialize(configuration);
-
ConnectionPoolDataSource cpds = initCPDS(configuration);
PerUserPoolDataSource dataSource = initJdbc2Pool(configuration);
dataSource.setConnectionPoolDataSource(cpds);
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/SharedPoolDataSourceFactory.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/SharedPoolDataSourceFactory.java?rev=1336091&r1=1336090&r2=1336091&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/SharedPoolDataSourceFactory.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/dsfactory/SharedPoolDataSourceFactory.java
Wed May 9 11:09:40 2012
@@ -63,8 +63,6 @@ public class SharedPoolDataSourceFactory
*/
public void initialize(Configuration configuration) throws TorqueException
{
- super.initialize(configuration);
-
ConnectionPoolDataSource cpds = initCPDS(configuration);
SharedPoolDataSource dataSource = initJdbc2Pool(configuration);
dataSource.setConnectionPoolDataSource(cpds);
Modified:
db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java?rev=1336091&r1=1336090&r2=1336091&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/TorqueInstanceTest.java
Wed May 9 11:09:40 2012
@@ -36,11 +36,15 @@ public class TorqueInstanceTest extends
/** The name of the "default" dataSourceFactory. */
private static final String DEFAULT_NAME = "default";
- /**
- * The name of the "postgresql" dataSourceFactory
- */
+ /** The name of the "postgresql" dataSourceFactory */
private static final String POSTGRESQL_NAME = "postgresql";
+ /** The name of the "mysql" dataSourceFactory */
+ private static final String MYSQL_NAME = "mysql";
+
+ /** The name of the "oracle" dataSourceFactory */
+ private static final String ORACLE_NAME = "oracle";
+
/**
* Tests whether an external adapter is loaded correctly.
* @throws Exception if an error occurs during the Test.
@@ -81,7 +85,6 @@ public class TorqueInstanceTest extends
*/
public void testDatabases() throws Exception
{
- //Torque.init(getConfiguration());
Map<String, Database> databases = Torque.getDatabases();
// check whether all expected databases are contained in the Map
assertEquals(
@@ -101,6 +104,28 @@ public class TorqueInstanceTest extends
postgresqlDatabase);
}
+ /**
+ * Tests whether the schema information in the databases is filled
+ * correctly.
+ */
+ public void testSchemata() throws Exception
+ {
+ Torque.shutdown();
+ Torque.init("src/test/resources/torque-schematest.properties");
+
+ Map<String, Database> databases = Torque.getDatabases();
+
+ assertEquals(
+ "myschema",
+ databases.get(MYSQL_NAME).getSchema());
+ assertEquals(
+ "defaultschema",
+ databases.get(POSTGRESQL_NAME).getSchema());
+ assertEquals(
+ "orschema2",
+ databases.get(ORACLE_NAME).getSchema());
+ }
+
public void testShutdown() throws Exception
{
// because we have not properly initialized the DataSourceFactory,
Added:
db/torque/torque4/trunk/torque-runtime/src/test/resources/torque-schematest.properties
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/resources/torque-schematest.properties?rev=1336091&view=auto
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/test/resources/torque-schematest.properties
(added)
+++
db/torque/torque4/trunk/torque-runtime/src/test/resources/torque-schematest.properties
Wed May 9 11:09:40 2012
@@ -0,0 +1,39 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+
+torque.database.default = postgresql
+
+torque.database.mysql.adapter=mysql
+torque.dsfactory.mysql.factory=
org.apache.torque.dsfactory.SharedPoolDataSourceFactory
+torque.dsfactory.mysql.schema=myschema
+
+torque.database.postgresql.adapter=postgresql
+torque.dsfactory.postgresql.factory=
org.apache.torque.dsfactory.SharedPoolDataSourceFactory
+
+torque.database.oracle.adapter=oracle
+torque.dsfactory.oracle.factory=
org.apache.torque.dsfactory.SharedPoolDataSourceFactory
+torque.dsfactory.oracle.schema= orschema1
+torque.database.oracle.schema=orschema2
+
+torque.idbroker.prefetch=false
+
+torque.defaults.schema = defaultschema
+
+
+
+
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]