Return-Path: Delivered-To: apmail-db-ojb-dev-archive@www.apache.org Received: (qmail 41143 invoked from network); 3 Aug 2006 11:37:35 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 3 Aug 2006 11:37:35 -0000 Received: (qmail 34346 invoked by uid 500); 3 Aug 2006 11:37:35 -0000 Delivered-To: apmail-db-ojb-dev-archive@db.apache.org Received: (qmail 34204 invoked by uid 500); 3 Aug 2006 11:37:34 -0000 Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "OJB Developers List" Reply-To: "OJB Developers List" Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 34193 invoked by uid 500); 3 Aug 2006 11:37:34 -0000 Received: (qmail 34190 invoked by uid 99); 3 Aug 2006 11:37:34 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Aug 2006 04:37:34 -0700 X-ASF-Spam-Status: No, hits=-8.6 required=10.0 tests=ALL_TRUSTED,INFO_TLD,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Aug 2006 04:37:34 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 191251A981A; Thu, 3 Aug 2006 04:37:14 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r428360 - /db/ojb/trunk/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java Date: Thu, 03 Aug 2006 11:37:13 -0000 To: ojb-commits@db.apache.org From: arminw@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060803113714.191251A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: arminw Date: Thu Aug 3 04:37:13 2006 New Revision: 428360 URL: http://svn.apache.org/viewvc?rev=428360&view=rev Log: fix OJB 108, make Platform class instantiation more tolerant against platform token. Allow full qualified custom Platform names. Modified: db/ojb/trunk/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java Modified: db/ojb/trunk/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java?rev=428360&r1=428359&r2=428360&view=diff ============================================================================== --- db/ojb/trunk/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java (original) +++ db/ojb/trunk/src/java/org/apache/ojb/broker/platforms/PlatformFactory.java Thu Aug 3 04:37:13 2006 @@ -15,56 +15,153 @@ * limitations under the License. */ -import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; -import org.apache.ojb.broker.util.logging.LoggerFactory; -import org.apache.ojb.broker.util.logging.Logger; -import org.apache.ojb.broker.util.ClassHelper; - 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.ClassHelper; +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 manage {@link Platform} objects that * define RDBMS platform specific behaviour. * - * @author Thomas Mahler * @version $Id$ */ public class PlatformFactory { - private Logger log = LoggerFactory.getLogger(PlatformFactory.class); - private Map platforms = new HashMap(); + private static Logger log = LoggerFactory.getLogger(PlatformFactory.class); + + private Map names = new HashMap(); + private HashMap platforms = new HashMap(); + + { + // 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 Platform getPlatformFor(JdbcConnectionDescriptor jcd) { String dbms = jcd.getDbms(); - Platform result; - 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 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) { - log.warn("Problems with platform " + platformName + ": " + t.getMessage() - + ". OJB will use PlatformDefaultImpl instead"); - - result = new PlatformDefaultImpl(); + // ignore + } + } + 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 } - getPlatforms().put(dbms, result); // cache the Platform + } + 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 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; } @@ -72,24 +169,41 @@ /** * compute the name of the concrete Class representing the Platform * specified by platform + * * @param platform the name of the platform as specified in the repository */ - private String getClassnameFor(String platform) + private String createClassName(String platform) { - String pf = "Default"; - if (platform != null) + String pf; + if(StringUtils.isNotBlank(platform)) { pf = platform; } + 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 Map getPlatforms() - { - return platforms; - } +// for internal testing +// public static void main(String[] args) +// { +// PlatformFactory pf = new PlatformFactory(); +// Iterator it = pf.names.values().iterator(); +// while(it.hasNext()) +// { +// String name = (String) it.next(); +// try +// { +// System.out.println("Platform: " + pf.instantiatePlatform(pf.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