01: package org.apache.ojb.broker.platforms;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
19: import org.apache.ojb.broker.util.logging.LoggerFactory;
20: import org.apache.ojb.broker.util.ClassHelper;
21:
22: import java.util.HashMap;
23:
24: /**
25: * this factory class is responsible to create Platform objects that
26: * define RDBMS platform specific behaviour.
27: * @version 1.0
28: * @author Thomas Mahler
29: */
30: public class PlatformFactory {
31: private static HashMap platforms = new HashMap();
32:
33: /**
34: * returns the Platform matching to the JdbcConnectionDescriptor jcd.
35: * The method jcd.getDbms(...) is used to determine the Name of the
36: * platform.
37: * BRJ : cache platforms
38: * @param jcd the JdbcConnectionDescriptor defining the platform
39: */
40: public static Platform getPlatformFor(JdbcConnectionDescriptor jcd) {
41: String dbms = jcd.getDbms();
42: Platform result = null;
43: String platformName = null;
44:
45: result = (Platform) getPlatforms().get(dbms);
46: if (result == null) {
47: try {
48: platformName = getClassnameFor(dbms);
49: Class platformClass = ClassHelper
50: .getClass(platformName);
51: result = (Platform) platformClass.newInstance();
52:
53: } catch (Throwable t) {
54: LoggerFactory.getDefaultLogger().warn(
55: "[PlatformFactory] problems with platform "
56: + platformName, t);
57: LoggerFactory
58: .getDefaultLogger()
59: .warn(
60: "[PlatformFactory] OJB will use PlatformDefaultImpl instead");
61:
62: result = new PlatformDefaultImpl();
63: }
64: getPlatforms().put(dbms, result); // cache the Platform
65: }
66: return result;
67: }
68:
69: /**
70: * compute the name of the concrete Class representing the Platform
71: * specified by <code>platform</code>
72: * @param platform the name of the platform as specified in the repository
73: */
74: private static String getClassnameFor(String platform) {
75: String pf = "Default";
76: if (platform != null) {
77: pf = platform;
78: }
79: return "org.apache.ojb.broker.platforms.Platform"
80: + pf.substring(0, 1).toUpperCase() + pf.substring(1)
81: + "Impl";
82: }
83:
84: /**
85: * Gets the platforms.
86: * @return Returns a HashMap
87: */
88: private static HashMap getPlatforms() {
89: return platforms;
90: }
91: }
|