db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1163638 - in /db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque: TorqueInstance.java adapter/DB.java adapter/DBFactory.java
Date Wed, 31 Aug 2011 14:28:36 GMT
Author: tv
Date: Wed Aug 31 14:28:36 2011
New Revision: 1163638

URL: http://svn.apache.org/viewvc?rev=1163638&view=rev
Log:
Add auto-detection of the database adapter. Use xxx.adapter=auto in Torque.properties.

Modified:
    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/adapter/DB.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBFactory.java

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=1163638&r1=1163637&r2=1163638&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 Aug 31 14:28:36 2011
@@ -29,6 +29,7 @@ import java.util.Map;
 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;
@@ -99,7 +100,7 @@ public class TorqueInstance
      * are serialized then unserialized prior to Torque being reinitialized.
      * This condition exists in a normal catalina restart.
      */
-    private Map<String, MapBuilder> mapBuilderCache 
+    private Map<String, MapBuilder> mapBuilderCache
             = Collections.synchronizedMap(new HashMap<String, MapBuilder>());
 ;
 
@@ -143,8 +144,8 @@ public class TorqueInstance
         // look for properties that are in the "torque" namespace.
 
         initDefaultDbName(conf);
-        initAdapters(conf);
         initDataSourceFactories(conf);
+        initAdapters(conf);
 
         // setup manager mappings
         initManagerMappings(conf);
@@ -252,9 +253,30 @@ public class TorqueInstance
                     String adapter = c.getString(key);
                     String handle = key.substring(0, key.indexOf('.'));
 
-                    DB db;
+                    Database database = getOrCreateDatabase(handle);
+                    DB db = null;
 
-                    db = DBFactory.create(adapter);
+                    if (StringUtils.equals(DB.AUTODETECT_ADAPTER, adapter))
+                    {
+                    	Connection con = null;
+                    	try
+                    	{
+                    		con = database.getDataSourceFactory().getDataSource().getConnection();
+							db = DBFactory.autoDetectAdapter(con);
+						}
+                    	catch (SQLException e)
+                    	{
+                    		log.error("Could not get product information from JDBC", e);
+						}
+                    	finally
+                    	{
+                    		closeConnection(con);
+                    	}
+                    }
+                    else
+                    {
+                    	db = DBFactory.create(adapter);
+                    }
 
                     // Not supported, try manually defined adapter class
                     if (db == null)
@@ -263,8 +285,6 @@ public class TorqueInstance
                         db = DBFactory.create(adapter, adapterClassName);
                     }
 
-                    Database database = getOrCreateDatabase(handle);
-
                     // register the adapter for this name
                     database.setAdapter(db);
                     log.debug("Adding " + adapter + " -> "

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DB.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DB.java?rev=1163638&r1=1163637&r2=1163638&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DB.java
(original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DB.java
Wed Aug 31 14:28:36 2011
@@ -26,7 +26,10 @@ import java.util.Date;
 
 import org.apache.torque.TorqueException;
 import org.apache.torque.sql.Query;
+import org.apache.torque.util.functions.AbstractFunction;
+import org.apache.torque.util.functions.Aggregate;
 import org.apache.torque.util.functions.FunctionEnum;
+import org.apache.torque.util.functions.SQLFunction;
 
 /**
  * <code>DB</code> defines the interface for a Torque database
@@ -109,6 +112,11 @@ public interface DB extends Serializable
     String DRIVER_KEY = "driver";
 
     /**
+     * Special adapter for auto-detection.
+     */
+    String AUTODETECT_ADAPTER = "auto";
+
+    /**
      * This method is used to ignore case.
      *
      * @param in The string to transform to upper case.

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBFactory.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBFactory.java?rev=1163638&r1=1163637&r2=1163638&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBFactory.java
(original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/DBFactory.java
Wed Aug 31 14:28:36 2011
@@ -19,9 +19,15 @@ package org.apache.torque.adapter;
  * under the License.
  */
 
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * This class creates different {@link org.apache.torque.adapter.DB}
  * objects based on specified JDBC driver name.
@@ -36,7 +42,10 @@ import java.util.Map;
  */
 public final class DBFactory
 {
-    /**
+	/** Local logger */
+	private static final Log log = LogFactory.getLog(DBFactory.class);
+
+	/**
      * JDBC driver to Torque Adapter map.
      */
     private static Map<String, Class<? extends DB>> adapters
@@ -94,9 +103,26 @@ public final class DBFactory
         adapters.put("informix", DBInformix.class);
         adapters.put("odbc", DBOdbc.class);
         adapters.put("msaccess", DBOdbc.class);
-
         adapters.put("derby", DBDerby.class);
 
+        // add database product names to be used for auto-detection
+        adapters.put("DB2/NT", DBDB2App.class);
+        adapters.put("DB2/LINUX", DBDB2App.class);
+        adapters.put("DB2/6000", DBDB2App.class);
+        adapters.put("DB2/HPUX", DBDB2App.class);
+        adapters.put("DB2/SUN", DBDB2App.class);
+        adapters.put("DB2/LINUX390", DBDB2App.class);
+        adapters.put("DB2/AIX64", DBDB2App.class);
+        adapters.put("HSQL Database Engine", DBHypersonicSQL.class);
+        adapters.put("Microsoft SQL Server Database", DBMSSQL.class);
+        adapters.put("Microsoft SQL Server", DBMSSQL.class);
+        adapters.put("MySQL", DBMM.class);
+        adapters.put("Oracle", DBOracle.class);
+        adapters.put("PostgreSQL", DBPostgres.class);
+        adapters.put("Sybase SQL Server", DBSybase.class);
+        adapters.put("Informix Dynamic Server", DBInformix.class);
+        adapters.put("Apache Derby", DBDerby.class);
+
         adapters.put("", DBNone.class);
     }
 
@@ -110,6 +136,46 @@ public final class DBFactory
     }
 
     /**
+     * Creates a new instance of the Torque database adapter based on
+     * the JDBC meta-data
+     *
+     * @param con a database connection
+     * @return An instance of a Torque database adapter, or null if
+     *         no adapter could be detected.
+     * @throws InstantiationException if the adapter could not be
+     *         instantiated
+     * @throws SQLException if there are problems getting the JDBC meta data
+     */
+    public static DB autoDetectAdapter(Connection con)
+        throws InstantiationException, SQLException
+    {
+    	DatabaseMetaData dmd = con.getMetaData();
+        String dbmsName = dmd.getDatabaseProductName();
+
+    	Class<? extends DB> adapterClass = adapters.get(dbmsName);
+
+    	if (adapterClass == null)
+        {
+            throw new InstantiationException("Could not detect adapter for database: " +
dbmsName);
+        }
+
+        log.info("Mapped database product " + dbmsName + " to adapter " + adapterClass.getSimpleName());
+
+        try
+        {
+            DB adapter = adapterClass.newInstance();
+            return adapter;
+        }
+        catch (IllegalAccessException e)
+        {
+            throw new InstantiationException(
+                "Could not instantiate adapter for database: "
+                + dbmsName
+                + ": Assure that adapter classes are in your classpath");
+        }
+    }
+
+    /**
      * Creates a new instance of the Torque database adapter associated
      * with the specified JDBC driver or adapter key.
      *
@@ -132,7 +198,7 @@ public final class DBFactory
 
         try
         {
-            DB adapter = (DB) adapterClass.newInstance();
+            DB adapter = adapterClass.newInstance();
             // adapter.setJDBCDriver(driver);
             return adapter;
         }
@@ -164,7 +230,7 @@ public final class DBFactory
 
         try
         {
-            adapterClass = (Class<?>) Class.forName(className);
+            adapterClass = Class.forName(className);
         }
         catch (ClassNotFoundException e)
         {



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message