1 24 25 package org.objectweb.cjdbc.controller.backend; 26 27 import java.net.ConnectException ; 28 import java.sql.Connection ; 29 import java.sql.DatabaseMetaData ; 30 import java.sql.ResultSet ; 31 import java.sql.Statement ; 32 33 import org.objectweb.cjdbc.common.i18n.Translate; 34 import org.objectweb.cjdbc.common.log.Trace; 35 import org.objectweb.cjdbc.controller.connection.DriverManager; 36 37 44 public class DriverCompliance 45 { 46 private boolean isCompliant = false; 47 private boolean hasBeenTested = false; 48 private boolean supportSetQueryTimeout = false; 49 private boolean supportGetGeneratedKeys = false; 50 private boolean supportGetColumnCount = false; 51 private boolean supportGetColumnClassName = false; 52 private boolean supportGetColumnTypeName = false; 53 private boolean supportGetColumnType = false; 54 private boolean supportGetColumnDisplaySize = false; 55 private boolean supportGetTableName = false; 56 private boolean supportSetCursorName = false; 57 private boolean supportSetFetchSize = false; 58 private boolean supportSetMaxRows = false; 59 60 private Trace logger; 61 62 private static final int TIMEOUT_VALUE = 1000; 63 private static final String DEFAULT_TEST_STATEMENT = "select 1"; 64 private String databaseProductName = "C-JDBC"; 65 66 71 public DriverCompliance(Trace logger) 72 { 73 this.logger = logger; 74 } 75 76 89 public boolean complianceTest(String backendUrl, String login, 90 String password, String driverPath, String driverClassName, 91 String connectionTestStatement) throws ConnectException 92 { 93 if (hasBeenTested) 94 return isCompliant; 95 96 isCompliant = false; 97 98 Connection c = null; 99 try 100 { 101 c = DriverManager.getConnection(backendUrl, login, password, driverPath, 102 driverClassName); 103 } 104 catch (Exception e) 105 { 106 if (logger.isDebugEnabled()) 107 logger.debug(Translate.get("backend.driver.test.connection.failed", e), 108 e); 109 else 110 logger.error(Translate.get("backend.driver.test.connection.failed", e)); 111 throw new ConnectException (e.getMessage()); 112 } 113 if (logger.isDebugEnabled()) 114 logger.debug(Translate.get("backend.driver.test.connection.ok")); 115 116 DatabaseMetaData connectionMetaData; 117 try 118 { 119 connectionMetaData = c.getMetaData(); 120 } 121 catch (Exception e) 122 { 123 if (logger.isDebugEnabled()) 124 logger 125 .debug(Translate.get("backend.driver.test.metadata.failed", e), e); 126 else 127 logger.error(Translate.get("backend.driver.test.metadata.failed", e)); 128 return isCompliant; 129 } 130 if (logger.isDebugEnabled()) 131 logger.debug(Translate.get("backend.driver.test.metadata.ok")); 132 133 try 134 { 135 this.databaseProductName = connectionMetaData.getDatabaseProductName(); 136 logger.info(Translate 137 .get("backend.detected.as", this.databaseProductName)); 138 } 139 catch (Exception e) 140 { 141 logger.warn(Translate 142 .get("backend.driver.test.database.productname.failed")); 143 } 144 145 Statement s; 146 try 147 { 148 s = c.createStatement(); 149 } 150 catch (Exception e) 151 { 152 if (logger.isDebugEnabled()) 153 logger.debug(Translate.get("backend.driver.test.statement.failed", e), 154 e); 155 else 156 logger.error(Translate.get("backend.driver.test.statement.failed", e)); 157 return isCompliant; 158 } 159 160 try 161 { 162 if (connectionTestStatement == null) 163 { 164 if (logger.isDebugEnabled()) 165 logger.debug(Translate.get("backend.driver.using.default.statement", 166 DEFAULT_TEST_STATEMENT)); 167 connectionTestStatement = DEFAULT_TEST_STATEMENT; 168 } 169 s.execute(connectionTestStatement); 170 } 171 catch (Exception e) 172 { 173 if (logger.isDebugEnabled()) 174 logger.debug(Translate.get("backend.driver.test.statement.invalid", 175 new String []{connectionTestStatement, e.getMessage()}), e); 176 else 177 logger.error(Translate.get("backend.driver.test.statement.invalid", 178 new String []{connectionTestStatement, e.getMessage()})); 179 return isCompliant; 180 } 181 if (logger.isDebugEnabled()) 182 logger.debug(Translate.get("backend.driver.test.statement.ok")); 183 184 try 185 { 186 s.setCursorName("testcursor"); 187 supportSetCursorName = true; 188 if (logger.isDebugEnabled()) 189 logger 190 .debug(Translate.get("backend.driver.statement.setCursorName.ok")); 191 } 192 catch (Exception e1) 193 { 194 logger.warn(Translate 195 .get("backend.driver.statement.setCursorName.failed")); 196 supportSetMaxRows = false; 197 } 198 199 try 200 { 201 s.setFetchSize(25); 202 supportSetFetchSize = true; 203 if (logger.isDebugEnabled()) 204 logger.debug(Translate.get("backend.driver.statement.setFetchSize.ok")); 205 } 206 catch (Exception e1) 207 { 208 logger 209 .warn(Translate.get("backend.driver.statement.setFetchSize.failed")); 210 supportSetMaxRows = false; 211 } 212 213 try 214 { 215 s.setMaxRows(5); 216 supportSetMaxRows = true; 217 if (logger.isDebugEnabled()) 218 logger.debug(Translate.get("backend.driver.statement.setMaxRows.ok")); 219 } 220 catch (Exception e1) 221 { 222 logger.warn(Translate.get("backend.driver.statement.setMaxRows.failed")); 223 supportSetMaxRows = false; 224 } 225 226 try 227 { 228 s.getGeneratedKeys(); 229 supportGetGeneratedKeys = true; 230 if (logger.isDebugEnabled()) 231 logger.debug(Translate 232 .get("backend.driver.statement.getGeneratedKeys.ok")); 233 } 234 catch (Exception e1) 235 { 236 logger.warn(Translate 237 .get("backend.driver.statement.getGeneratedKeys.failed")); 238 supportGetGeneratedKeys = false; 239 } 240 catch (AbstractMethodError e1) 241 { 242 logger.warn(Translate 243 .get("backend.driver.statement.getGeneratedKeys.failed")); 244 supportGetGeneratedKeys = false; 245 } 246 catch (java.lang.NoSuchMethodError e1) 247 { 248 logger.warn(Translate 249 .get("backend.driver.statement.getGeneratedKeys.failed")); 250 supportGetGeneratedKeys = false; 251 } 252 253 257 272 try 273 { 274 s.setQueryTimeout(TIMEOUT_VALUE); 275 supportSetQueryTimeout = true; 276 } 277 catch (Exception e) 278 { 279 logger.warn(Translate.get("backend.driver.setQueryTimeout.failed", e)); 280 } 281 if (supportSetQueryTimeout && logger.isDebugEnabled()) 282 logger.debug(Translate.get("backend.driver.setQueryTimeout.ok")); 283 284 ResultSet rs; 285 try 286 { 287 String [] types = {"TABLE", "VIEW"}; 288 rs = connectionMetaData.getTables(null, null, "%", types); 289 } 290 catch (Exception e) 291 { 292 if (logger.isDebugEnabled()) 293 logger.debug(Translate.get("backend.driver.metadata.getTables.failed", 294 e), e); 295 else 296 logger.error(Translate.get("backend.driver.metadata.getTables.failed", 297 e)); 298 return isCompliant; 299 } 300 if (logger.isDebugEnabled()) 301 logger.debug(Translate.get("backend.driver.metadata.getTables.ok")); 302 303 java.sql.ResultSetMetaData rsMetaData; 304 try 305 { 306 rsMetaData = rs.getMetaData(); 307 } 308 catch (Exception e) 309 { 310 if (logger.isDebugEnabled()) 311 logger.debug(Translate.get( 312 "backend.driver.resultset.getMetaData.failed", e), e); 313 else 314 logger.error(Translate.get( 315 "backend.driver.resultset.getMetaData.failed", e)); 316 return isCompliant; 317 } 318 if (logger.isDebugEnabled()) 319 logger.debug(Translate.get("backend.driver.resultset.getMetaData.ok")); 320 321 try 322 { 323 if (rs.next() && (rsMetaData.getColumnCount() > 0)) 324 { 325 rs.getObject(1); 326 } 327 else 328 logger.warn(Translate.get("backend.driver.resultset.getObject.unable")); 329 } 330 catch (Exception e) 331 { 332 if (logger.isDebugEnabled()) 333 logger.debug(Translate.get("backend.driver.resultset.getObject.failed", 334 e), e); 335 else 336 logger.error(Translate.get("backend.driver.resultset.getObject.failed", 337 e)); 338 return isCompliant; 339 } 340 if (logger.isDebugEnabled()) 341 logger.debug(Translate.get("backend.driver.resultset.getObject.ok")); 342 343 try 344 { 345 rsMetaData.getColumnCount(); 346 supportGetColumnCount = true; 347 } 348 catch (Exception e) 349 { 350 if (logger.isDebugEnabled()) 351 logger.debug(Translate.get( 352 "backend.driver.metadata.getColumnCount.failed", e), e); 353 else 354 logger.error(Translate.get( 355 "backend.driver.metadata.getColumnCount.failed", e)); 356 return isCompliant; 357 } 358 if (supportGetColumnCount && logger.isDebugEnabled()) 359 logger.debug(Translate.get("backend.driver.metadata.getColumnCount.ok")); 360 361 try 362 { 363 rsMetaData.getColumnName(1); 364 } 365 catch (Exception e) 366 { 367 if (logger.isDebugEnabled()) 368 logger.debug(Translate.get( 369 "backend.driver.metadata.getColumnName.failed", e), e); 370 else 371 logger.error(Translate.get( 372 "backend.driver.metadata.getColumnName.failed", e)); 373 return isCompliant; 374 } 375 if (logger.isDebugEnabled()) 376 logger.debug(Translate.get("backend.driver.metadata.getColumnName.ok")); 377 378 try 379 { 380 rsMetaData.getTableName(1); 381 supportGetTableName = true; 382 } 383 catch (Exception e) 384 { 385 logger.warn(Translate.get("backend.driver.metadata.getTableName.failed", 386 e)); 387 } 388 389 if (supportGetTableName && logger.isDebugEnabled()) 390 logger.debug(Translate.get("backend.driver.metadata.getTableName.ok")); 391 392 try 393 { 394 rsMetaData.getColumnDisplaySize(1); 395 supportGetColumnDisplaySize = true; 396 } 397 catch (Exception e) 398 { 399 logger.warn(Translate.get( 400 "backend.driver.metadata.getColumnDisplaySize.failed", e)); 401 } 402 if (supportGetColumnDisplaySize && logger.isDebugEnabled()) 403 logger.debug(Translate 404 .get("backend.driver.metadata.getColumnDisplaySize.ok")); 405 406 try 407 { 408 rsMetaData.getColumnType(1); 409 supportGetColumnType = true; 410 } 411 catch (Exception e) 412 { 413 logger.warn(Translate.get("backend.driver.metadata.getColumnType.failed", 414 e)); 415 } 416 if (supportGetColumnType && logger.isDebugEnabled()) 417 logger.debug(Translate.get("backend.driver.metadata.getColumnType.ok")); 418 419 try 420 { 421 rsMetaData.getColumnTypeName(1); 422 supportGetColumnTypeName = true; 423 } 424 catch (Exception e) 425 { 426 logger.warn(Translate.get( 427 "backend.driver.metadata.getColumnTypeName.failed", e)); 428 } 429 if (supportGetColumnTypeName && logger.isDebugEnabled()) 430 logger.debug(Translate 431 .get("backend.driver.metadata.getColumnTypeName.ok")); 432 433 try 434 { 435 rsMetaData.getColumnClassName(1); 436 supportGetColumnClassName = true; 437 } 438 catch (Exception e) 439 { 440 logger.warn(Translate.get( 441 "backend.driver.metadata.getColumnClassName.failed", e)); 442 } 443 if (supportGetColumnClassName && logger.isDebugEnabled()) 444 logger.debug(Translate 445 .get("backend.driver.metadata.getColumnClassName.ok")); 446 447 isCompliant = true; 448 hasBeenTested = true; 449 return isCompliant; 450 } 451 452 455 public boolean isCompliant() 456 { 457 return isCompliant; 458 } 459 460 463 public boolean supportGetGeneratedKeys() 464 { 465 return supportGetGeneratedKeys; 466 } 467 468 471 public boolean supportGetColumnClassName() 472 { 473 return supportGetColumnClassName; 474 } 475 476 479 public boolean supportGetColumnCount() 480 { 481 return supportGetColumnCount; 482 } 483 484 487 public boolean supportGetColumnDisplaySize() 488 { 489 return supportGetColumnDisplaySize; 490 } 491 492 495 public boolean supportGetColumnType() 496 { 497 return supportGetColumnType; 498 } 499 500 503 public boolean supportGetColumnTypeName() 504 { 505 return supportGetColumnTypeName; 506 } 507 508 511 public boolean supportGetTableName() 512 { 513 return supportGetTableName; 514 } 515 516 519 public boolean supportSetQueryTimeout() 520 { 521 return supportSetQueryTimeout; 522 } 523 524 527 public boolean supportSetMaxRows() 528 { 529 return supportSetMaxRows; 530 } 531 532 537 public boolean supportSetCursorName() 538 { 539 return supportSetCursorName; 540 } 541 542 547 public boolean supportSetFetchSize() 548 { 549 return supportSetFetchSize; 550 } 551 552 557 public String getDatabaseProductName() 558 { 559 return this.databaseProductName; 560 } 561 } | Popular Tags |