| 1 | |
package org.apache.ojb.broker.metadata; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
import java.sql.Connection; |
| 19 | |
import java.sql.DatabaseMetaData; |
| 20 | |
import java.sql.SQLException; |
| 21 | |
import java.util.HashMap; |
| 22 | |
|
| 23 | |
import javax.sql.DataSource; |
| 24 | |
import org.apache.commons.beanutils.PropertyUtils; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class JdbcMetadataUtils |
| 32 | |
{ |
| 33 | |
|
| 34 | |
|
| 35 | |
public static final String PROPERTY_PROTOCOL = "protocol"; |
| 36 | |
|
| 37 | |
|
| 38 | |
public static final String PROPERTY_SUBPROTOCOL = "subprotocol"; |
| 39 | |
|
| 40 | |
|
| 41 | |
public static final String PROPERTY_DBALIAS = "dbAlias"; |
| 42 | |
|
| 43 | |
|
| 44 | |
public static final String PLATFORM_DB2 = "Db2"; |
| 45 | |
|
| 46 | |
public static final String PLATFORM_FIREBIRD = "Firebird"; |
| 47 | |
|
| 48 | |
public static final String PLATFORM_HSQLDB = "Hsqldb"; |
| 49 | |
|
| 50 | |
public static final String PLATFORM_INFORMIX = "Informix"; |
| 51 | |
|
| 52 | |
public static final String PLATFORM_MAXDB = "MaxDB"; |
| 53 | |
|
| 54 | |
public static final String PLATFORM_MCKOI = "McKoi"; |
| 55 | |
|
| 56 | |
public static final String PLATFORM_MSACCESS = "MsAccess"; |
| 57 | |
|
| 58 | |
public static final String PLATFORM_MSSQLSERVER = "MsSQLServer"; |
| 59 | |
|
| 60 | |
public static final String PLATFORM_MYSQL = "MySQL"; |
| 61 | |
|
| 62 | |
public static final String PLATFORM_ORACLE = "Oracle"; |
| 63 | |
|
| 64 | |
public static final String PLATFORM_ORACLE9I = "Oracle9i"; |
| 65 | |
|
| 66 | |
public static final String PLATFORM_POSTGRESQL = "PostgreSQL"; |
| 67 | |
|
| 68 | |
public static final String PLATFORM_SYBASE = "Sybase"; |
| 69 | |
|
| 70 | |
public static final String PLATFORM_SYBASEASA = "SybaseASA"; |
| 71 | |
|
| 72 | |
public static final String PLATFORM_SYBASEASE = "SybaseASE"; |
| 73 | |
|
| 74 | |
public static final String PLATFORM_WLORACLE9I = "WLOracle9i"; |
| 75 | |
|
| 76 | |
|
| 77 | |
public static final String DRIVER_DB2 = "COM.ibm.db2.jdbc.app.DB2Driver"; |
| 78 | |
|
| 79 | |
public static final String DRIVER_DB2_INET = "com.inet.drda.DRDADriver"; |
| 80 | |
|
| 81 | |
public static final String DRIVER_FIREBIRD = "org.firebirdsql.jdbc.FBDriver"; |
| 82 | |
|
| 83 | |
public static final String DRIVER_HSQLDB = "org.hsqldb.jdbcDriver"; |
| 84 | |
|
| 85 | |
public static final String DRIVER_INET_POOLED = "com.inet.pool.PoolDriver"; |
| 86 | |
|
| 87 | |
public static final String DRIVER_INFORMIX = "com.informix.jdbc.IfxDriver"; |
| 88 | |
|
| 89 | |
public static final String DRIVER_JTDS = "net.sourceforge.jtds.jdbc.Driver"; |
| 90 | |
|
| 91 | |
public static final String DRIVER_MAXDB = "com.sap.dbtech.jdbc.DriverSapDB"; |
| 92 | |
|
| 93 | |
public static final String DRIVER_MCKOI = "com.mckoi.JDBCDriver"; |
| 94 | |
|
| 95 | |
public static final String DRIVER_MSSQLSERVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; |
| 96 | |
|
| 97 | |
public static final String DRIVER_MSSQLSERVER_INET = "com.inet.tds.TdsDriver"; |
| 98 | |
|
| 99 | |
public static final String DRIVER_MSSQLSERVER_JSQLCONNECT = "com.jnetdirect.jsql.JSQLDriver"; |
| 100 | |
|
| 101 | |
public static final String DRIVER_MYSQL = "com.mysql.jdbc.Driver"; |
| 102 | |
|
| 103 | |
public static final String DRIVER_MYSQL_OLD = "org.gjt.mm.mysql.Driver"; |
| 104 | |
|
| 105 | |
public static final String DRIVER_ORACLE = "oracle.jdbc.driver.OracleDriver"; |
| 106 | |
|
| 107 | |
public static final String DRIVER_ORACLE_INET = "com.inet.ora.OraDriver"; |
| 108 | |
|
| 109 | |
public static final String DRIVER_POSTGRESQL = "org.postgresql.Driver"; |
| 110 | |
|
| 111 | |
public static final String DRIVER_SAPDB = DRIVER_MAXDB; |
| 112 | |
|
| 113 | |
public static final String DRIVER_SYBASE = "com.sybase.jdbc2.jdbc.SybDriver"; |
| 114 | |
|
| 115 | |
public static final String DRIVER_SYBASE_OLD = "com.sybase.jdbc.SybDriver"; |
| 116 | |
|
| 117 | |
public static final String DRIVER_SYBASE_INET = "com.inet.syb.SybDriver"; |
| 118 | |
|
| 119 | |
|
| 120 | |
public static final String SUBPROTOCOL_DB2 = "db2"; |
| 121 | |
|
| 122 | |
public static final String SUBPROTOCOL_DB2_INET = "inetdb2"; |
| 123 | |
|
| 124 | |
public static final String SUBPROTOCOL_FIREBIRD = "firebirdsql"; |
| 125 | |
|
| 126 | |
public static final String SUBPROTOCOL_HSQLDB = "hsqldb"; |
| 127 | |
|
| 128 | |
public static final String SUBPROTOCOL_INFORMIX = "informix-sqli"; |
| 129 | |
|
| 130 | |
public static final String SUBPROTOCOL_MAXDB = "sapdb"; |
| 131 | |
|
| 132 | |
public static final String SUBPROTOCOL_MCKOI = "mckoi"; |
| 133 | |
|
| 134 | |
public static final String SUBPROTOCOL_MSSQLSERVER = "microsoft:sqlserver"; |
| 135 | |
|
| 136 | |
public static final String SUBPROTOCOL_MSSQLSERVER_INET = "inetdae"; |
| 137 | |
|
| 138 | |
public static final String SUBPROTOCOL_MSSQLSERVER6_INET = "inetdae6"; |
| 139 | |
|
| 140 | |
public static final String SUBPROTOCOL_MSSQLSERVER7_INET = "inetdae7"; |
| 141 | |
|
| 142 | |
public static final String SUBPROTOCOL_MSSQLSERVER7A_INET = "inetdae7a"; |
| 143 | |
|
| 144 | |
public static final String SUBPROTOCOL_MSSQLSERVER_INET_POOLED = "inetpool:inetdae"; |
| 145 | |
|
| 146 | |
public static final String SUBPROTOCOL_MSSQLSERVER6_INET_POOLED = "inetpool:inetdae6"; |
| 147 | |
|
| 148 | |
public static final String SUBPROTOCOL_MSSQLSERVER7_INET_POOLED = "inetpool:inetdae7"; |
| 149 | |
|
| 150 | |
public static final String SUBPROTOCOL_MSSQLSERVER7A_INET_POOLED = "inetpool:inetdae7a"; |
| 151 | |
|
| 152 | |
public static final String SUBPROTOCOL_MSSQLSERVER_JSQLCONNECT = "JSQLConnect"; |
| 153 | |
|
| 154 | |
public static final String SUBPROTOCOL_MSSQLSERVER_JTDS = "jtds:sqlserver"; |
| 155 | |
|
| 156 | |
public static final String SUBPROTOCOL_MYSQL = "mysql"; |
| 157 | |
|
| 158 | |
public static final String SUBPROTOCOL_ORACLE = "oracle"; |
| 159 | |
|
| 160 | |
public static final String SUBPROTOCOL_ORACLE_INET = "inetora"; |
| 161 | |
|
| 162 | |
public static final String SUBPROTOCOL_POSTGRESQL = "postgresql"; |
| 163 | |
|
| 164 | |
public static final String SUBPROTOCOL_SAPDB = SUBPROTOCOL_MAXDB; |
| 165 | |
|
| 166 | |
public static final String SUBPROTOCOL_SYBASE = "sybase:Tds"; |
| 167 | |
|
| 168 | |
public static final String SUBPROTOCOL_SYBASE_INET = "inetsyb"; |
| 169 | |
|
| 170 | |
public static final String SUBPROTOCOL_SYBASE_INET_POOLED = "inetpool:inetsyb"; |
| 171 | |
|
| 172 | |
public static final String SUBPROTOCOL_SYBASE_JTDS = "jtds:sybase"; |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
private HashMap jdbcSubProtocolToPlatform = new HashMap(); |
| 177 | |
|
| 178 | |
private HashMap jdbcDriverToPlatform = new HashMap(); |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
public JdbcMetadataUtils() |
| 184 | |
{ |
| 185 | |
|
| 186 | |
|
| 187 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_DB2, PLATFORM_DB2); |
| 188 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_DB2_INET, PLATFORM_DB2); |
| 189 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_FIREBIRD, PLATFORM_FIREBIRD); |
| 190 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_HSQLDB, PLATFORM_HSQLDB); |
| 191 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_INFORMIX, PLATFORM_INFORMIX); |
| 192 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MAXDB, PLATFORM_MAXDB); |
| 193 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER, PLATFORM_MSSQLSERVER); |
| 194 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER_INET, PLATFORM_MSSQLSERVER); |
| 195 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER6_INET, PLATFORM_MSSQLSERVER); |
| 196 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER7_INET, PLATFORM_MSSQLSERVER); |
| 197 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER7A_INET, PLATFORM_MSSQLSERVER); |
| 198 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER_INET_POOLED, PLATFORM_MSSQLSERVER); |
| 199 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER6_INET_POOLED, PLATFORM_MSSQLSERVER); |
| 200 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER7_INET_POOLED, PLATFORM_MSSQLSERVER); |
| 201 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER7A_INET_POOLED, PLATFORM_MSSQLSERVER); |
| 202 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MSSQLSERVER_JTDS, PLATFORM_MSSQLSERVER); |
| 203 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_MYSQL, PLATFORM_MYSQL); |
| 204 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_ORACLE, PLATFORM_ORACLE); |
| 205 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_ORACLE_INET, PLATFORM_ORACLE); |
| 206 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_POSTGRESQL, PLATFORM_POSTGRESQL); |
| 207 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_SYBASE, PLATFORM_SYBASE); |
| 208 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_SYBASE_INET, PLATFORM_SYBASE); |
| 209 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_SYBASE_INET_POOLED, PLATFORM_SYBASE); |
| 210 | |
jdbcSubProtocolToPlatform.put(SUBPROTOCOL_SYBASE_JTDS, PLATFORM_SYBASE); |
| 211 | |
|
| 212 | |
jdbcDriverToPlatform.put(DRIVER_DB2, PLATFORM_DB2); |
| 213 | |
jdbcDriverToPlatform.put(DRIVER_DB2_INET, PLATFORM_DB2); |
| 214 | |
jdbcDriverToPlatform.put(DRIVER_FIREBIRD, PLATFORM_FIREBIRD); |
| 215 | |
jdbcDriverToPlatform.put(DRIVER_HSQLDB, PLATFORM_HSQLDB); |
| 216 | |
jdbcDriverToPlatform.put(DRIVER_INFORMIX, PLATFORM_INFORMIX); |
| 217 | |
jdbcDriverToPlatform.put(DRIVER_MAXDB, PLATFORM_MAXDB); |
| 218 | |
jdbcDriverToPlatform.put(DRIVER_MCKOI, PLATFORM_MCKOI); |
| 219 | |
jdbcDriverToPlatform.put(DRIVER_MSSQLSERVER, PLATFORM_MSSQLSERVER); |
| 220 | |
jdbcDriverToPlatform.put(DRIVER_MSSQLSERVER_INET, PLATFORM_MSSQLSERVER); |
| 221 | |
jdbcDriverToPlatform.put(DRIVER_MSSQLSERVER_JSQLCONNECT, PLATFORM_MSSQLSERVER); |
| 222 | |
jdbcDriverToPlatform.put(DRIVER_MYSQL, PLATFORM_MYSQL); |
| 223 | |
jdbcDriverToPlatform.put(DRIVER_MYSQL_OLD, PLATFORM_MYSQL); |
| 224 | |
jdbcDriverToPlatform.put(DRIVER_ORACLE, PLATFORM_ORACLE); |
| 225 | |
jdbcDriverToPlatform.put(DRIVER_ORACLE_INET, PLATFORM_ORACLE); |
| 226 | |
jdbcDriverToPlatform.put(DRIVER_POSTGRESQL, PLATFORM_POSTGRESQL); |
| 227 | |
jdbcDriverToPlatform.put(DRIVER_SYBASE, PLATFORM_SYBASE); |
| 228 | |
jdbcDriverToPlatform.put(DRIVER_SYBASE_OLD, PLATFORM_SYBASE); |
| 229 | |
jdbcDriverToPlatform.put(DRIVER_SYBASE_INET, PLATFORM_SYBASE); |
| 230 | |
} |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
public void fillJCDFromDataSource(JdbcConnectionDescriptor jcd, DataSource dataSource, String username, String password) throws MetadataException |
| 246 | |
{ |
| 247 | |
String realUsername = (jcd.getUserName() != null ? jcd.getUserName() : username); |
| 248 | |
String realPassword = (jcd.getPassWord() != null ? jcd.getPassWord() : password); |
| 249 | |
Connection connection = null; |
| 250 | |
DatabaseMetaData metadata = null; |
| 251 | |
|
| 252 | |
try |
| 253 | |
{ |
| 254 | |
|
| 255 | |
if (realUsername != null) |
| 256 | |
{ |
| 257 | |
connection = dataSource.getConnection(realUsername, realPassword); |
| 258 | |
} |
| 259 | |
else |
| 260 | |
{ |
| 261 | |
connection = dataSource.getConnection(); |
| 262 | |
} |
| 263 | |
|
| 264 | |
metadata = connection.getMetaData(); |
| 265 | |
} |
| 266 | |
catch (Throwable t) |
| 267 | |
{ |
| 268 | |
if (connection != null) |
| 269 | |
{ |
| 270 | |
try |
| 271 | |
{ |
| 272 | |
connection.close(); |
| 273 | |
} |
| 274 | |
catch (SQLException ex) |
| 275 | |
{} |
| 276 | |
} |
| 277 | |
throw new MetadataException("Could not get the metadata from the given datasource", t); |
| 278 | |
} |
| 279 | |
|
| 280 | |
try |
| 281 | |
{ |
| 282 | |
HashMap urlComponents = parseConnectionUrl(metadata.getURL()); |
| 283 | |
|
| 284 | |
if (urlComponents.containsKey(PROPERTY_DBALIAS)) |
| 285 | |
{ |
| 286 | |
jcd.setProtocol((String)urlComponents.get(PROPERTY_PROTOCOL)); |
| 287 | |
jcd.setSubProtocol((String)urlComponents.get(PROPERTY_SUBPROTOCOL)); |
| 288 | |
jcd.setDbAlias((String)urlComponents.get(PROPERTY_DBALIAS)); |
| 289 | |
if (jdbcSubProtocolToPlatform.containsKey(jcd.getSubProtocol())) |
| 290 | |
{ |
| 291 | |
|
| 292 | |
jcd.setDbms((String)jdbcSubProtocolToPlatform.get(jcd.getSubProtocol())); |
| 293 | |
} |
| 294 | |
} |
| 295 | |
} |
| 296 | |
catch (Throwable t) |
| 297 | |
{ |
| 298 | |
try |
| 299 | |
{ |
| 300 | |
connection.close(); |
| 301 | |
} |
| 302 | |
catch (SQLException ex) |
| 303 | |
{} |
| 304 | |
throw new MetadataException("Could not get the metadata from the given datasource", t); |
| 305 | |
} |
| 306 | |
try |
| 307 | |
{ |
| 308 | |
|
| 309 | |
Integer majorVersion = (Integer)PropertyUtils.getProperty(metadata, "JDBCMajorVersion"); |
| 310 | |
Integer minorVersion = (Integer)PropertyUtils.getProperty(metadata, "JDBCMinorVersion"); |
| 311 | |
|
| 312 | |
jcd.setJdbcLevel(Double.parseDouble(majorVersion.toString()+"."+minorVersion.toString())); |
| 313 | |
} |
| 314 | |
catch (Throwable t) |
| 315 | |
{ |
| 316 | |
|
| 317 | |
jcd.setJdbcLevel(2.0); |
| 318 | |
} |
| 319 | |
try |
| 320 | |
{ |
| 321 | |
connection.close(); |
| 322 | |
} |
| 323 | |
catch (SQLException ex) |
| 324 | |
{} |
| 325 | |
} |
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
public HashMap parseConnectionUrl(String jdbcConnectionUrl) |
| 335 | |
{ |
| 336 | |
HashMap result = new HashMap(); |
| 337 | |
|
| 338 | |
if (jdbcConnectionUrl == null) |
| 339 | |
{ |
| 340 | |
return result; |
| 341 | |
} |
| 342 | |
|
| 343 | |
int pos = jdbcConnectionUrl.indexOf(':'); |
| 344 | |
int lastPos; |
| 345 | |
|
| 346 | |
result.put(PROPERTY_PROTOCOL, jdbcConnectionUrl.substring(0, pos)); |
| 347 | |
|
| 348 | |
lastPos = pos; |
| 349 | |
pos = jdbcConnectionUrl.indexOf(':', lastPos + 1); |
| 350 | |
|
| 351 | |
String subProtocol = jdbcConnectionUrl.substring(lastPos + 1, pos); |
| 352 | |
|
| 353 | |
|
| 354 | |
if ("inetpool".equals(subProtocol)) |
| 355 | |
{ |
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
int tmpPos = jdbcConnectionUrl.indexOf(':', pos + 1); |
| 361 | |
|
| 362 | |
if ("inetpool:jdbc".equals(jdbcConnectionUrl.substring(lastPos + 1, tmpPos))) |
| 363 | |
{ |
| 364 | |
pos = tmpPos; |
| 365 | |
tmpPos = jdbcConnectionUrl.indexOf(':', pos + 1); |
| 366 | |
} |
| 367 | |
subProtocol += ":" + jdbcConnectionUrl.substring(pos + 1, tmpPos); |
| 368 | |
} |
| 369 | |
else if ("jtds".equals(subProtocol) || |
| 370 | |
"microsoft".equals(subProtocol) || |
| 371 | |
"sybase".equals(subProtocol)) |
| 372 | |
{ |
| 373 | |
pos = jdbcConnectionUrl.indexOf(':', pos + 1); |
| 374 | |
subProtocol = ":" + jdbcConnectionUrl.substring(lastPos + 1, pos); |
| 375 | |
} |
| 376 | |
|
| 377 | |
result.put(PROPERTY_SUBPROTOCOL, subProtocol); |
| 378 | |
result.put(PROPERTY_DBALIAS, jdbcConnectionUrl.substring(pos + 1)); |
| 379 | |
|
| 380 | |
return result; |
| 381 | |
} |
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
public String findPlatformFor(String jdbcSubProtocol, String jdbcDriver) |
| 392 | |
{ |
| 393 | |
String platform = (String)jdbcSubProtocolToPlatform.get(jdbcSubProtocol); |
| 394 | |
|
| 395 | |
if (platform == null) |
| 396 | |
{ |
| 397 | |
platform = (String)jdbcDriverToPlatform.get(jdbcDriver); |
| 398 | |
} |
| 399 | |
return platform; |
| 400 | |
} |
| 401 | |
} |