View Javadoc

1   package org.apache.torque.engine.platform;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.HashMap;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /***
25   * Factory class responsible to create Platform objects that
26   * define RDBMS platform specific behaviour.
27   *
28   * @author Thomas Mahler
29   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
30   * @version $Id: PlatformFactory.java 239626 2005-08-24 12:19:51Z henning $
31   */
32  public class PlatformFactory
33  {
34      private static HashMap platforms = new HashMap();
35      private static Log log = LogFactory.getLog(PlatformFactory.class);
36  
37      /***
38       * Returns the Platform for a platform name.
39       *
40       * @param dbms name of the platform
41       */
42      public static Platform getPlatformFor(String dbms)
43      {
44          Platform result = null;
45          String platformName = null;
46  
47          result = (Platform) getPlatforms().get(dbms);
48          if (result == null)
49          {
50              try
51              {
52                  platformName = getClassnameFor(dbms);
53                  Class platformClass = Class.forName(platformName);
54                  result = (Platform) platformClass.newInstance();
55  
56              }
57              catch (Throwable t)
58              {
59                  log.warn("problems with platform " + platformName
60                          + ": " + t.getMessage());
61                  log.warn("Torque will use PlatformDefaultImpl instead");
62  
63                  result = new PlatformDefaultImpl();
64              }
65              getPlatforms().put(dbms, result); // cache the Platform
66          }
67          return result;
68      }
69  
70      /***
71       * compute the name of the concrete Class representing the Platform
72       * specified by <code>platform</code>
73       *
74       * @param platform the name of the platform as specified in the repository
75       */
76      private static String getClassnameFor(String platform)
77      {
78          String pf = "Default";
79          if (platform != null)
80          {
81              pf = platform;
82          }
83          return "org.apache.torque.engine.platform.Platform" + pf.substring(0, 1).toUpperCase() + pf.substring(1) + "Impl";
84      }
85  
86      /***
87       * Gets the platforms.
88       *
89       * @return Returns a HashMap
90       */
91      private static HashMap getPlatforms()
92      {
93          return platforms;
94      }
95  }