KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > platforms > PlatformFactory


1 package org.apache.ojb.broker.platforms;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
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 {
32     private static HashMap JavaDoc platforms = new HashMap JavaDoc();
33
34     /**
35      * returns the Platform matching to the JdbcConnectionDescriptor jcd.
36      * The method jcd.getDbms(...) is used to determine the Name of the
37      * platform.
38      * BRJ : cache platforms
39      * @param jcd the JdbcConnectionDescriptor defining the platform
40      */

41     public static Platform getPlatformFor(JdbcConnectionDescriptor jcd)
42     {
43         String JavaDoc dbms = jcd.getDbms();
44         Platform result = null;
45         String JavaDoc platformName = null;
46
47         result = (Platform) getPlatforms().get(dbms);
48         if (result == null)
49         {
50             try
51             {
52                 platformName = getClassnameFor(dbms);
53                 Class JavaDoc platformClass = ClassHelper.getClass(platformName);
54                 result = (Platform) platformClass.newInstance();
55
56             }
57             catch (Throwable JavaDoc t)
58             {
59                 LoggerFactory.getDefaultLogger().warn(
60                         "[PlatformFactory] problems with platform " + platformName, t);
61                 LoggerFactory.getDefaultLogger().warn(
62                         "[PlatformFactory] OJB will use PlatformDefaultImpl instead");
63
64                 result = new PlatformDefaultImpl();
65             }
66             getPlatforms().put(dbms, result); // cache the Platform
67
}
68         return result;
69     }
70
71     /**
72      * compute the name of the concrete Class representing the Platform
73      * specified by <code>platform</code>
74      * @param platform the name of the platform as specified in the repository
75      */

76     private static String JavaDoc getClassnameFor(String JavaDoc platform)
77     {
78         String JavaDoc pf = "Default";
79         if (platform != null)
80         {
81             pf = platform;
82         }
83         return "org.apache.ojb.broker.platforms.Platform" + pf.substring(0, 1).toUpperCase() + pf.substring(1) + "Impl";
84     }
85
86     /**
87      * Gets the platforms.
88      * @return Returns a HashMap
89      */

90     private static HashMap JavaDoc getPlatforms()
91     {
92         return platforms;
93     }
94 }
95
Popular Tags