View Javadoc

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