db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r428359 - /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java
Date Thu, 03 Aug 2006 11:36:23 GMT
Author: arminw
Date: Thu Aug  3 04:36:23 2006
New Revision: 428359

URL: http://svn.apache.org/viewvc?rev=428359&view=rev
Log:
fix OJB-108, make Platform class instantiation more tolerant against platform token. Allow
full qualified custom Platform names.

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java?rev=428359&r1=428358&r2=428359&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java
Thu Aug  3 04:36:23 2006
@@ -15,55 +15,154 @@
  * limitations under the License.
  */
 
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.SystemUtils;
+import org.apache.ojb.broker.OJBRuntimeException;
 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
-import org.apache.ojb.broker.util.logging.LoggerFactory;
 import org.apache.ojb.broker.util.ClassHelper;
-
-import java.util.HashMap;
+import org.apache.ojb.broker.util.logging.Logger;
+import org.apache.ojb.broker.util.logging.LoggerFactory;
 
 /**
- * this factory class is responsible to create Platform objects that
+ * This factory class is responsible to create Platform objects that
  * define RDBMS platform specific behaviour.
- * @version 	1.0
- * @author		Thomas Mahler
+ *
+ * @version $Id: $
  */
 public class PlatformFactory
 {
+    private static Logger log = LoggerFactory.getLogger(PlatformFactory.class);
+
+    private static Map names = new HashMap();
     private static HashMap platforms = new HashMap();
 
+    static
+    {
+        // comment out the correct typed mappings (with correct upper
+        // and lower case, the first letter will always transformed to upper case)
+        // , because these mappings will be automatically resolved
+        //
+        //names.put("db2", "Db2");
+        //names.put("default", "Default");
+        //names.put("derby", "Derby");
+        //names.put("firebird", "Firebird");
+        //names.put("hsqldb", "Hsqldb");
+        names.put("hsql", "Hsqldb");
+        //names.put("informix", "Informix");
+        names.put("maxdb", "MaxDB");
+        names.put("msaccess", "MsAccess");
+        names.put("mssql", "MsSQLServer");
+        names.put("mssqlserver", "MsSQLServer");
+        names.put("mysql", "MySQL");
+        names.put("oracle", "Oracle");
+        names.put("oracle9i", "Oracle9i");
+        names.put("postgre", "PostgreSQL");
+        names.put("postgresql", "PostgreSQL");
+        names.put("sapdb", "Sapdb");
+        names.put("sybase", "Sybase");
+        names.put("sybaseasa", "SybaseASA");
+        names.put("sybasease", "SybaseASE");
+    }
+
     /**
      * returns the Platform matching to the JdbcConnectionDescriptor jcd.
      * The method jcd.getDbms(...) is used to determine the Name of the
      * platform.
      * BRJ : cache platforms
+     *
      * @param jcd the JdbcConnectionDescriptor defining the platform
      */
     public static Platform getPlatformFor(JdbcConnectionDescriptor jcd)
     {
         String dbms = jcd.getDbms();
-        Platform result = null;
-        String platformName = null;
+        if(dbms == null)
+        {
+            throw new NullPointerException("The Platform is not specified. " + jcd);
+        }
+        Platform result = (Platform) platforms.get(dbms);
+        if(result == null)
+        {
+            result = resolvePlatform(dbms);
+            if(log.isEnabledFor(Logger.INFO))
+            {
+                log.info("Resolve platform class for name '" + dbms + "', Platform instance:
" + result);
+            }
+            platforms.put(dbms, result); // cache the Platform
+        }
+        return result;
+    }
 
-        result = (Platform) getPlatforms().get(dbms);
-        if (result == null)
+    private static Platform resolvePlatform(final String platformName)
+    {
+        Platform result = null;
+        String name = null;
+        try
+        {
+            name = createClassName(platformName);
+            result = instantiatePlatform(name);
+        }
+        catch(ClassNotFoundException e)
+        {
+            // ignore
+        }
+        if(result == null)
         {
             try
             {
-                platformName = getClassnameFor(dbms);
-                Class platformClass = ClassHelper.getClass(platformName);
-                result = (Platform) platformClass.newInstance();
-
+                result = instantiatePlatform(platformName);
             }
-            catch (Throwable t)
+            catch(ClassNotFoundException e)
             {
-                LoggerFactory.getDefaultLogger().warn(
-                        "[PlatformFactory] problems with platform " + platformName, t);
-                LoggerFactory.getDefaultLogger().warn(
-                        "[PlatformFactory] OJB will use PlatformDefaultImpl instead");
-
-                result = new PlatformDefaultImpl();
+                // ignore
             }
-            getPlatforms().put(dbms, result); // cache the Platform
+        }
+        if(result == null)
+        {
+            try
+            {
+                String str = (String) names.get(platformName.toLowerCase());
+                if(str != null)
+                {
+                    String resolvedName = createClassName(str);
+                    result = instantiatePlatform(resolvedName);
+                    log.warn("The correct Platform token is '" + str
+                            + "' instead of the specified '" + platformName + "'");
+                }
+            }
+            catch(ClassNotFoundException e)
+            {
+                // ignore
+            }
+        }
+        if(result == null)
+        {
+            String eol = SystemUtils.LINE_SEPARATOR;
+            String msg = eol + "Can't resolve Platform class based on specified name and
internal created names:" + eol
+                    + "    Attempt 1: " + platformName + " ---> class not found!" + eol
+                    + "    Attempt 2: " + name + " ---> class not found!"
+                    + eol + "!!Please check the specified Platform token or full qualified
Platform class name!!";
+            throw new OJBRuntimeException(msg);
+        }
+        return result;
+    }
+
+    private static Platform instantiatePlatform(String name) throws ClassNotFoundException
+    {
+        Platform result;
+        try
+        {
+            result = (Platform) ClassHelper.newInstance(name);
+        }
+        catch(InstantiationException e)
+        {
+            throw new OJBRuntimeException("Platform class " + name + " exists, but can't
be instantiated", e);
+        }
+        catch(IllegalAccessException e)
+        {
+            throw new OJBRuntimeException("Platform class " + name + " exists, but access
isn't allowed", e);
         }
         return result;
     }
@@ -71,24 +170,40 @@
     /**
      * compute the name of the concrete Class representing the Platform
      * specified by <code>platform</code>
+     *
      * @param platform the name of the platform as specified in the repository
      */
-    private static String getClassnameFor(String platform)
+    private static String createClassName(String platform)
     {
-        String pf = "Default";
-        if (platform != null)
+        String pf;
+        if(StringUtils.isNotBlank(platform))
         {
             pf = platform;
         }
-        return "org.apache.ojb.broker.platforms.Platform" + pf.substring(0, 1).toUpperCase()
+ pf.substring(1) + "Impl";
+        else
+        {
+            log.error("Platform class not specified, please set a correct Platform token
or " +
+                    "the full Platform class name");
+            throw new OJBRuntimeException("Platform class not specified!");
+        }
+        return Platform.class.getName() + pf.substring(0, 1).toUpperCase() + pf.substring(1)
+ "Impl";
     }
 
-    /**
-     * Gets the platforms.
-     * @return Returns a HashMap
-     */
-    private static HashMap getPlatforms()
-    {
-        return platforms;
-    }
+// for internal testing
+//    public static void main(String[] args)
+//    {
+//        Iterator it = names.values().iterator();
+//        while(it.hasNext())
+//        {
+//            String name =  (String) it.next();
+//            try
+//            {
+//                System.out.println("Platform: " + instantiatePlatform(createClassName(name)));
+//            }
+//            catch(ClassNotFoundException e)
+//            {
+//                System.err.println("### INCORRECT NAME: " + name);
+//            }
+//        }
+//    }
 }



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


Mime
View raw message