| |
| 1 |
package org.apache.ddlutils; |
| 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 |
|
* 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 |
|
* under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
import java.util.HashMap; |
| 23 |
import java.util.Map; |
| 24 |
import javax.sql.DataSource; |
|
import org.apache.ddlutils.platform.axion.AxionPlatform; |
| 26 |
import org.apache.ddlutils.platform.cloudscape.CloudscapePlatform; |
| 27 |
import org.apache.ddlutils.platform.db2.Db2Platform; |
| 28 |
import org.apache.ddlutils.platform.db2.Db2v8Platform; |
| 29 |
import org.apache.ddlutils.platform.derby.DerbyPlatform; |
| 30 |
import org.apache.ddlutils.platform.firebird.FirebirdPlatform; |
| 31 |
import org.apache.ddlutils.platform.hsqldb.HsqlDbPlatform; |
| 32 |
import org.apache.ddlutils.platform.interbase.InterbasePlatform; |
| 33 |
import org.apache.ddlutils.platform.maxdb.MaxDbPlatform; |
| 34 |
import org.apache.ddlutils.platform.mckoi.MckoiPlatform; |
| 35 |
import org.apache.ddlutils.platform.mssql.MSSqlPlatform; |
| 36 |
import org.apache.ddlutils.platform.mysql.MySqlPlatform; |
| 37 |
import org.apache.ddlutils.platform.mysql.MySql50Platform; |
| 38 |
import org.apache.ddlutils.platform.oracle.Oracle10Platform; |
|
import org.apache.ddlutils.platform.oracle.Oracle8Platform; |
| 40 |
import org.apache.ddlutils.platform.oracle.Oracle9Platform; |
| 41 |
import org.apache.ddlutils.platform.postgresql.PostgreSqlPlatform; |
| 42 |
import org.apache.ddlutils.platform.sapdb.SapDbPlatform; |
| 43 |
import org.apache.ddlutils.platform.sybase.SybaseASE15Platform; |
| 44 |
import org.apache.ddlutils.platform.sybase.SybasePlatform; |
| 45 |
|
| 46 |
/** |
| 47 |
* A factory of {@link org.apache.ddlutils.Platform} instances based on a case |
| 48 |
* insensitive database name. Note that this is a convenience class as the platforms |
| 49 |
* can also simply be created via their constructors. |
| 50 |
* |
| 51 |
* @version $Revision: 209952 $ |
| 52 |
*/ |
| 53 |
public class PlatformFactory |
| 54 |
{ |
| 55 |
/** The database name -> platform map. */ |
| 56 |
private static Map _platforms = null; |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the platform map. |
| 60 |
* |
| 61 |
* @return The platform list |
| 62 |
*/ |
| 63 |
private static synchronized Map getPlatforms() |
| 64 |
{ |
| 65 |
if (_platforms == null) |
|
{ |
| 67 |
// lazy initialization |
| 68 |
_platforms = new HashMap(); |
| 69 |
registerPlatforms(); |
| 70 |
} |
| 71 |
return _platforms; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Creates a new platform for the given (case insensitive) database name |
| 76 |
* or returns null if the database is not recognized. |
| 77 |
* |
| 78 |
* @param databaseName The name of the database (case is not important) |
| 79 |
* @return The platform or <code>null</code> if the database is not supported |
| 80 |
*/ |
| 81 |
public static synchronized Platform createNewPlatformInstance(String databaseName) throws DdlUtilsException |
| 82 |
{ |
|
Class platformClass = (Class)getPlatforms().get(databaseName.toLowerCase()); |
| 84 |
|
| 85 |
try |
| 86 |
{ |
| 87 |
return platformClass != null ? (Platform)platformClass.newInstance() : null; |
| 88 |
} |
| 89 |
catch (Exception ex) |
| 90 |
{ |
| 91 |
throw new DdlUtilsException("Could not create platform for database "+databaseName, ex); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Creates a new platform for the specified database. This is a shortcut method that uses |
| 97 |
* {@link PlatformUtils#determineDatabaseType(String, String)} to determine the parameter |
| 98 |
* for {@link #createNewPlatformInstance(String)}. Note that no database connection is |
| 99 |
* established when using this method. |
| 100 |
* |
| 101 |
* @param jdbcDriver The jdbc driver |
| 102 |
* @param jdbcConnectionUrl The connection url |
| 103 |
* @return The platform or <code>null</code> if the database is not supported |
| 104 |
*/ |
| 105 |
public static synchronized Platform createNewPlatformInstance(String jdbcDriver, String jdbcConnectionUrl) throws DdlUtilsException |
| 106 |
{ |
| 107 |
return createNewPlatformInstance(new PlatformUtils().determineDatabaseType(jdbcDriver, jdbcConnectionUrl)); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Creates a new platform for the specified database. This is a shortcut method that uses |
| 112 |
* {@link PlatformUtils#determineDatabaseType(DataSource)} to determine the parameter |
| 113 |
* for {@link #createNewPlatformInstance(String)}. Note that this method sets the data source |
| 114 |
* at the returned platform instance (method {@link Platform#setDataSource(DataSource)}). |
| 115 |
* |
| 116 |
* @param dataSource The data source for the database |
| 117 |
* @return The platform or <code>null</code> if the database is not supported |
| 118 |
*/ |
| 119 |
public static synchronized Platform createNewPlatformInstance(DataSource dataSource) throws DdlUtilsException |
| 120 |
{ |
| 121 |
Platform platform = createNewPlatformInstance(new PlatformUtils().determineDatabaseType(dataSource)); |
| 122 |
|
| 123 |
platform.setDataSource(dataSource); |
| 124 |
return platform; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Creates a new platform for the specified database. This is a shortcut method that uses |
| 129 |
* {@link PlatformUtils#determineDatabaseType(DataSource)} to determine the parameter |
| 130 |
* for {@link #createNewPlatformInstance(String)}. Note that this method sets the data source |
| 131 |
* at the returned platform instance (method {@link Platform#setDataSource(DataSource)}). |
| 132 |
* |
| 133 |
* @param dataSource The data source for the database |
| 134 |
* @param username The user name to use for connecting to the database |
| 135 |
* @param password The password to use for connecting to the database |
| 136 |
* @return The platform or <code>null</code> if the database is not supported |
| 137 |
*/ |
| 138 |
public static synchronized Platform createNewPlatformInstance(DataSource dataSource, String username, String password) throws DdlUtilsException |
| 139 |
{ |
| 140 |
Platform platform = createNewPlatformInstance(new PlatformUtils().determineDatabaseType(dataSource, username, password)); |
| 141 |
|
| 142 |
platform.setDataSource(dataSource); |
| 143 |
platform.setUsername(username); |
| 144 |
platform.setPassword(password); |
| 145 |
return platform; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns a list of all supported platforms. |
| 150 |
* |
| 151 |
* @return The names of the currently registered platforms |
| 152 |
*/ |
| 153 |
public static synchronized String[] getSupportedPlatforms() |
| 154 |
{ |
| 155 |
return (String[])getPlatforms().keySet().toArray(new String[0]); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Determines whether the indicated platform is supported. |
| 160 |
* |
| 161 |
* @param platformName The name of the platform |
| 162 |
* @return <code>true</code> if the platform is supported |
| 163 |
*/ |
| 164 |
public static boolean isPlatformSupported(String platformName) |
| 165 |
{ |
| 166 |
return getPlatforms().containsKey(platformName.toLowerCase()); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Registers a new platform. |
| 171 |
* |
| 172 |
* @param platformName The platform name |
| 173 |
* @param platformClass The platform class which must implement the {@link Platform} interface |
| 174 |
*/ |
| 175 |
public static synchronized void registerPlatform(String platformName, Class platformClass) |
| 176 |
{ |
| 177 |
addPlatform(getPlatforms(), platformName, platformClass); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Registers the known platforms. |
| 182 |
*/ |
|
private static void registerPlatforms() |
| 184 |
{ |
| 185 |
addPlatform(_platforms, AxionPlatform.DATABASENAME, AxionPlatform.class); |
| 186 |
addPlatform(_platforms, CloudscapePlatform.DATABASENAME, CloudscapePlatform.class); |
| 187 |
addPlatform(_platforms, Db2Platform.DATABASENAME, Db2Platform.class); |
| 188 |
addPlatform(_platforms, Db2v8Platform.DATABASENAME, Db2v8Platform.class); |
| 189 |
addPlatform(_platforms, DerbyPlatform.DATABASENAME, DerbyPlatform.class); |
| 190 |
addPlatform(_platforms, FirebirdPlatform.DATABASENAME, FirebirdPlatform.class); |
| 191 |
addPlatform(_platforms, HsqlDbPlatform.DATABASENAME, HsqlDbPlatform.class); |
| 192 |
addPlatform(_platforms, InterbasePlatform.DATABASENAME, InterbasePlatform.class); |
| 193 |
addPlatform(_platforms, MaxDbPlatform.DATABASENAME, MaxDbPlatform.class); |
| 194 |
addPlatform(_platforms, MckoiPlatform.DATABASENAME, MckoiPlatform.class); |
| 195 |
addPlatform(_platforms, MSSqlPlatform.DATABASENAME, MSSqlPlatform.class); |
| 196 |
addPlatform(_platforms, MySqlPlatform.DATABASENAME, MySqlPlatform.class); |
| 197 |
addPlatform(_platforms, MySql50Platform.DATABASENAME, MySql50Platform.class); |
| 198 |
addPlatform(_platforms, Oracle8Platform.DATABASENAME, Oracle8Platform.class); |
| 199 |
addPlatform(_platforms, Oracle9Platform.DATABASENAME, Oracle9Platform.class); |
| 200 |
addPlatform(_platforms, Oracle10Platform.DATABASENAME, Oracle10Platform.class); |
| 201 |
addPlatform(_platforms, PostgreSqlPlatform.DATABASENAME, PostgreSqlPlatform.class); |
| 202 |
addPlatform(_platforms, SapDbPlatform.DATABASENAME, SapDbPlatform.class); |
| 203 |
addPlatform(_platforms, SybasePlatform.DATABASENAME, SybasePlatform.class); |
| 204 |
addPlatform(_platforms, SybaseASE15Platform.DATABASENAME, SybaseASE15Platform.class); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Registers a new platform. |
| 209 |
* |
| 210 |
* @param platformMap The map to add the platform info to |
| 211 |
* @param platformName The platform name |
| 212 |
* @param platformClass The platform class which must implement the {@link Platform} interface |
| 213 |
*/ |
| 214 |
private static synchronized void addPlatform(Map platformMap, String platformName, Class platformClass) |
| 215 |
{ |
| 216 |
if (!Platform.class.isAssignableFrom(platformClass)) |
| 217 |
{ |
| 218 |
throw new IllegalArgumentException("Cannot register class "+platformClass.getName()+" because it does not implement the "+Platform.class.getName()+" interface"); |
|
} |
| 220 |
platformMap.put(platformName.toLowerCase(), platformClass); |
| 221 |
} |
| 222 |
} |
| |