Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PlatformFactory |
|
| 2.0;2 |
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 | import java.util.Map; | |
24 | ||
25 | import org.apache.commons.logging.Log; | |
26 | import org.apache.commons.logging.LogFactory; | |
27 | ||
28 | /** | |
29 | * Factory class responsible for creating Platform objects that define RDBMS platform specific behaviour. | |
30 | */ | |
31 | 0 | public class PlatformFactory { |
32 | 0 | private static Map<String, Platform> platforms = new HashMap<String, Platform>(); |
33 | 0 | private static Log log = LogFactory.getLog(PlatformFactory.class); |
34 | ||
35 | /** | |
36 | * Returns the Platform for a platform name. | |
37 | * | |
38 | * @param dbms | |
39 | * name of the platform | |
40 | */ | |
41 | public static Platform getPlatformFor(String dbms) { | |
42 | 0 | Platform result = null; |
43 | 0 | String platformName = null; |
44 | ||
45 | 0 | result = (Platform) getPlatforms().get(dbms); |
46 | 0 | if (result == null) { |
47 | try { | |
48 | 0 | platformName = getClassnameFor(dbms); |
49 | 0 | Class<?> platformClass = Class.forName(platformName); |
50 | 0 | result = (Platform) platformClass.newInstance(); |
51 | ||
52 | 0 | } catch (Throwable t) { |
53 | 0 | log.warn("problems with platform " + platformName + ": " + t.getMessage()); |
54 | 0 | log.warn("Torque will use PlatformDefaultImpl instead"); |
55 | ||
56 | 0 | result = new PlatformDefaultImpl(); |
57 | 0 | } |
58 | 0 | getPlatforms().put(dbms, result); // cache the Platform |
59 | } | |
60 | 0 | return result; |
61 | } | |
62 | ||
63 | /** | |
64 | * compute the name of the concrete Class representing the Platform specified by <code>platform</code> | |
65 | * | |
66 | * @param platform | |
67 | * the name of the platform as specified in the repository | |
68 | */ | |
69 | private static String getClassnameFor(String platform) { | |
70 | 0 | String pf = "Default"; |
71 | 0 | if (platform != null) { |
72 | 0 | pf = platform; |
73 | } | |
74 | 0 | return "org.apache.torque.engine.platform.Platform" + pf.substring(0, 1).toUpperCase() + pf.substring(1) + "Impl"; |
75 | } | |
76 | ||
77 | /** | |
78 | * Gets the platforms. | |
79 | * | |
80 | * @return Returns a HashMap | |
81 | */ | |
82 | private static Map<String, Platform> getPlatforms() { | |
83 | 0 | return platforms; |
84 | } | |
85 | } |