001    package org.apache.ojb.broker.platforms;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
019    import org.apache.ojb.broker.util.logging.LoggerFactory;
020    import org.apache.ojb.broker.util.ClassHelper;
021    
022    import java.util.HashMap;
023    
024    /**
025     * this factory class is responsible to create Platform objects that
026     * define RDBMS platform specific behaviour.
027     * @version     1.0
028     * @author              Thomas Mahler
029     */
030    public class PlatformFactory
031    {
032        private static HashMap platforms = new HashMap();
033    
034        /**
035         * returns the Platform matching to the JdbcConnectionDescriptor jcd.
036         * The method jcd.getDbms(...) is used to determine the Name of the
037         * platform.
038         * BRJ : cache platforms
039         * @param jcd the JdbcConnectionDescriptor defining the platform
040         */
041        public static Platform getPlatformFor(JdbcConnectionDescriptor jcd)
042        {
043            String dbms = jcd.getDbms();
044            Platform result = null;
045            String platformName = null;
046    
047            result = (Platform) getPlatforms().get(dbms);
048            if (result == null)
049            {
050                try
051                {
052                    platformName = getClassnameFor(dbms);
053                    Class platformClass = ClassHelper.getClass(platformName);
054                    result = (Platform) platformClass.newInstance();
055    
056                }
057                catch (Throwable t)
058                {
059                    LoggerFactory.getDefaultLogger().warn(
060                            "[PlatformFactory] problems with platform " + platformName, t);
061                    LoggerFactory.getDefaultLogger().warn(
062                            "[PlatformFactory] OJB will use PlatformDefaultImpl instead");
063    
064                    result = new PlatformDefaultImpl();
065                }
066                getPlatforms().put(dbms, result); // cache the Platform
067            }
068            return result;
069        }
070    
071        /**
072         * compute the name of the concrete Class representing the Platform
073         * specified by <code>platform</code>
074         * @param platform the name of the platform as specified in the repository
075         */
076        private static String getClassnameFor(String platform)
077        {
078            String pf = "Default";
079            if (platform != null)
080            {
081                pf = platform;
082            }
083            return "org.apache.ojb.broker.platforms.Platform" + pf.substring(0, 1).toUpperCase() + pf.substring(1) + "Impl";
084        }
085    
086        /**
087         * Gets the platforms.
088         * @return Returns a HashMap
089         */
090        private static HashMap getPlatforms()
091        {
092            return platforms;
093        }
094    }