View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2001 - 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 11 package com.sun.ebxml.registry.persistence.rdb; 12 13 import com.sun.ebxml.registry.RegistryException; 14 import com.sun.ebxml.registry.util.RegistryProperties; 15 import java.sql.Connection; 16 import java.sql.SQLException; 17 import java.util.ArrayList; 18 import java.util.HashMap; 19 import org.oasis.ebxml.registry.bindings.query.ResponseOption; 20 import org.oasis.ebxml.registry.bindings.query.types.ReturnTypeType; 21 import org.oasis.ebxml.registry.bindings.rim.RegistryObjectType; 22 import org.oasis.ebxml.registry.bindings.rim.User; 23 24 25 26 /*** 27 * Class Declaration for Class1 28 * @see 29 * @author Farrukh S. Najmi 30 * @author Adrian Chong 31 */ 32 public class SQLPersistenceManagerImpl implements com.sun.ebxml.registry.persistence.PersistenceManager { 33 34 private final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(this.getClass()); 35 36 /*** 37 * @link 38 * @shapeType PatternLink 39 * @pattern Singleton 40 * @supplierRole Singleton factory 41 */ 42 /*# private SQLPersistenceManagerImpl _sqlPersistenceManagerImpl; */ 43 private static SQLPersistenceManagerImpl instance = null; 44 45 /*** 46 * 47 * @associates <{com.sun.ebxml.registry.persistence.rdb.ExtrinsicObjectDAO}> 48 */ 49 String databaseURL = null; 50 51 java.sql.DatabaseMetaData metaData = null; 52 53 RegistryObjectDAO roDAO = new RegistryObjectDAO(); 54 RegistryEntryDAO reDAO = new RegistryEntryDAO(); 55 private ClassificationSchemeDAO classificationSchemeDAO = new 56 ClassificationSchemeDAO(); 57 private String driver; 58 private String user; 59 private String password; 60 private boolean useConnectionPool; 61 private ConnectionPool connectionPool; 62 63 private static HashMap tableNameMap = new HashMap(); 64 65 static { 66 tableNameMap.put("user", "user_"); 67 tableNameMap.put("name", "name_"); 68 tableNameMap.put("classificationscheme", "ClassScheme"); 69 } 70 71 private static HashMap columnNameMap = new HashMap(); 72 73 static { 74 columnNameMap.put("number", "number_"); 75 columnNameMap.put("name", "name_"); 76 columnNameMap.put("user", "user_"); 77 columnNameMap.put("timestamp", "timestamp_"); 78 } 79 80 private SQLPersistenceManagerImpl() { 81 loadUsernamePassword(); 82 loadDatabaseDriver(); 83 constructDatabaseURL(); 84 if (RegistryProperties.getInstance().getProperty("ebxmlrr.persistence.rdb.useConnectionPooling") 85 .equalsIgnoreCase("true")) { 86 useConnectionPool = true; 87 createConnectionPool(); 88 } 89 else { 90 useConnectionPool = false; 91 } 92 } 93 94 /*** Look up the driver name and load the database driver */ 95 private void loadDatabaseDriver() { 96 97 try { 98 driver = RegistryProperties.getInstance().getProperty("ebxmlrr.persistence.rdb.databaseDriver"); 99 Class.forName(driver); 100 101 log.debug("Loaded jdbc driver: " + driver); 102 103 } catch (ClassNotFoundException e) { 104 log.error(e); 105 } 106 } 107 108 /*** Lookup up the db URL fragments and form the complete URL */ 109 private void constructDatabaseURL() { 110 databaseURL = RegistryProperties.getInstance().getProperty("ebxmlrr.persistence.rdb.databaseURL"); 111 112 log.debug("dbURL = '" + databaseURL + "'"); 113 } 114 115 /***Load the username and password for database access*/ 116 private void loadUsernamePassword() { 117 user = RegistryProperties.getInstance().getProperty("ebxmlrr.persistence.rdb.databaseUser"); 118 password = RegistryProperties.getInstance().getProperty("ebxmlrr.persistence.rdb.databaseUserPassword"); 119 } 120 121 private void createConnectionPool() { 122 RegistryProperties registryProperties = RegistryProperties.getInstance(); 123 String initialSize = registryProperties.getProperty("ebxmlrr.persistence.rdb.pool.initialSize"); 124 int initConns = 1; 125 if (initialSize != null) { 126 initConns = Integer.parseInt(initialSize); 127 } 128 String maxSize = registryProperties.getProperty("ebxmlrr.persistence.rdb.pool.maxSize"); 129 int maxConns = 1; 130 if (maxSize != null) { 131 maxConns = Integer.parseInt(maxSize); 132 } 133 String connectionTimeOut = registryProperties.getProperty("ebxmlrr.persistence.rdb.pool.connectionTimeOut"); 134 int timeOut = 0; 135 if (connectionTimeOut != null) { 136 timeOut = Integer.parseInt(connectionTimeOut); 137 } 138 connectionPool = new ConnectionPool("ConnectionPool", databaseURL, user, 139 password, maxConns, initConns, timeOut); 140 } 141 142 /*** 143 * Get a database connection. The connection is of autocommit off and with 144 * transaction isolation level "transaction read committed" 145 */ 146 public Connection getConnection() throws SQLException { 147 try { 148 Connection connection = null; 149 150 if (useConnectionPool) { 151 connection = connectionPool.getConnection(); 152 } 153 else { 154 // create connection directly 155 connection = java.sql.DriverManager.getConnection(databaseURL, user 156 , password); 157 } 158 connection.setAutoCommit(false); 159 connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 160 return connection; 161 } 162 catch (SQLException e) { 163 System.err.println("Error: Could not create Connection to database. Check if database is already running in another program (e.g. registry servlet in tomcat)"); 164 throw e; 165 } 166 } 167 168 private void releaseConnection(Connection conn) throws SQLException { 169 if (!conn.isClosed() && !useConnectionPool) { 170 conn.close(); 171 } 172 else if(useConnectionPool) { 173 connectionPool.freeConnection(conn); 174 } 175 } 176 177 public static SQLPersistenceManagerImpl getInstance() { 178 if (instance == null) { 179 synchronized(SQLPersistenceManagerImpl.class) { 180 if (instance == null) { 181 instance = new SQLPersistenceManagerImpl(); 182 } 183 } 184 } 185 return instance; 186 } 187 188 //Sort objects by their type. 189 private void sortRegistryObjects(ArrayList registryObjects, 190 ArrayList associations, 191 ArrayList auditableEvents, 192 ArrayList classifications, 193 ArrayList schemes, 194 ArrayList classificationNodes, 195 ArrayList externalIds, 196 ArrayList externalLinks, 197 ArrayList extrinsicObjects, 198 ArrayList organizations, 199 ArrayList packages, 200 ArrayList serviceBindings, 201 ArrayList services, 202 ArrayList specificationLinks, 203 ArrayList users 204 ) throws RegistryException { 205 206 associations.clear(); 207 auditableEvents.clear(); 208 classifications.clear(); 209 schemes.clear(); 210 classificationNodes.clear(); 211 externalIds.clear(); 212 externalLinks.clear(); 213 extrinsicObjects.clear(); 214 organizations.clear(); 215 packages.clear(); 216 serviceBindings.clear(); 217 services.clear(); 218 specificationLinks.clear(); 219 users.clear(); 220 221 java.util.Iterator objIter = registryObjects.iterator(); 222 while (objIter.hasNext()) { 223 RegistryObjectType obj = (RegistryObjectType)objIter.next(); 224 225 if (obj instanceof org.oasis.ebxml.registry.bindings.rim.Association) { 226 associations.add(obj); 227 } 228 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.AuditableEvent) { 229 auditableEvents.add(obj); 230 } 231 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.Classification) { 232 classifications.add(obj); 233 } 234 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.ClassificationScheme) { 235 schemes.add(obj); 236 } 237 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.ClassificationNode) { 238 classificationNodes.add(obj); 239 } 240 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.ExternalIdentifier) { 241 externalIds.add(obj); 242 } 243 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.ExternalLink) { 244 externalLinks.add(obj); 245 } 246 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.ExtrinsicObject) { 247 extrinsicObjects.add(obj); 248 } 249 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.Organization) { 250 organizations.add(obj); 251 } 252 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.RegistryPackage) { 253 packages.add(obj); 254 } 255 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.ServiceBinding) { 256 serviceBindings.add(obj); 257 } 258 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.Service) { 259 services.add(obj); 260 } 261 else if (obj instanceof org.oasis.ebxml.registry.bindings.rim.SpecificationLink) { 262 specificationLinks.add(obj); 263 } 264 else if (obj instanceof User) { 265 users.add(obj); 266 } 267 else { 268 throw new RegistryException("Enexpected object type: " + obj.getClass().getName()); 269 } 270 } 271 272 } 273 274 /*** 275 * Does a bulk insert of a heterogeneous Collection of RegistrObjects. 276 * 277 */ 278 public void insert(User user, ArrayList registryObjects) throws RegistryException { 279 Connection conn = null; 280 try { 281 conn = getConnection(); 282 conn.setAutoCommit(false); 283 284 insert(user, conn, registryObjects, true); 285 releaseConnection(conn); 286 287 } catch (SQLException e) { 288 log.error(e); 289 try { 290 if (!conn.isClosed()) { 291 conn.rollback(); 292 } 293 releaseConnection(conn); 294 throw new RegistryException(e); 295 } 296 catch(SQLException se) { 297 throw new RegistryException(se); 298 } 299 } catch(RegistryException e) { 300 log.error(e); 301 try { 302 if (!conn.isClosed()) { 303 conn.rollback(); 304 } 305 releaseConnection(conn); 306 throw e; 307 } 308 catch(SQLException se) { 309 throw new RegistryException(se); 310 } 311 } 312 } 313 314 /*** 315 * Does a bulk insert of a heterogeneous Collection of RegistrObjects. 316 * 317 */ 318 public void insert(User user, Connection conn, ArrayList registryObjects, boolean doCommit) throws RegistryException { 319 320 321 try { 322 conn.setAutoCommit(false); 323 324 ArrayList associations = new ArrayList(); 325 ArrayList auditableEvents = new ArrayList(); 326 ArrayList classifications = new ArrayList(); 327 ArrayList schemes = new ArrayList(); 328 ArrayList classificationNodes = new ArrayList(); 329 ArrayList externalIds = new ArrayList(); 330 ArrayList externalLinks = new ArrayList(); 331 ArrayList extrinsicObjects = new ArrayList(); 332 ArrayList organizations = new ArrayList(); 333 ArrayList packages = new ArrayList(); 334 ArrayList serviceBindings = new ArrayList(); 335 ArrayList services = new ArrayList(); 336 ArrayList specificationLinks = new ArrayList(); 337 ArrayList users = new ArrayList(); 338 339 sortRegistryObjects(registryObjects, 340 associations, 341 auditableEvents, 342 classifications, 343 schemes, 344 classificationNodes, 345 externalIds, 346 externalLinks, 347 extrinsicObjects, 348 organizations, 349 packages, 350 serviceBindings, 351 services, 352 specificationLinks, 353 users 354 ); 355 356 if (associations.size() > 0) { 357 AssociationDAO associationDAO = new AssociationDAO(); 358 associationDAO.insert(user, conn, associations); 359 } 360 361 if (auditableEvents.size() > 0) { 362 AuditableEventDAO auditableEventDAO = new AuditableEventDAO(); 363 auditableEventDAO.insert(conn, auditableEvents); 364 } 365 366 if (classifications.size() > 0) { 367 ClassificationDAO classificationDAO = new ClassificationDAO(); 368 classificationDAO.insert(user, conn, classifications); 369 } 370 371 if (schemes.size() > 0) { 372 ClassificationSchemeDAO classificationSchemeDAO = new 373 ClassificationSchemeDAO(); 374 classificationSchemeDAO.insert(user, conn, schemes); 375 } 376 377 if (classificationNodes.size() > 0) { 378 ClassificationNodeDAO classificationNodeDAO = new ClassificationNodeDAO(); 379 classificationNodeDAO.insert(user, conn, classificationNodes); 380 } 381 382 if (externalIds.size() > 0) { 383 ExternalIdentifierDAO externalIdentifierDAO = new ExternalIdentifierDAO(); 384 externalIdentifierDAO.insert(user, conn, externalIds); 385 } 386 387 if (externalLinks.size() > 0) { 388 ExternalLinkDAO externalLinkDAO = new ExternalLinkDAO(); 389 externalLinkDAO.insert(user, conn, externalLinks); 390 } 391 392 if (extrinsicObjects.size() > 0) { 393 ExtrinsicObjectDAO extrinsicObjectDAO = new ExtrinsicObjectDAO(); 394 extrinsicObjectDAO.insert(user, conn, extrinsicObjects); 395 } 396 397 if (organizations.size() > 0) { 398 OrganizationDAO organizationDAO = new OrganizationDAO(); 399 organizationDAO.insert(user, conn, organizations); 400 } 401 402 if (packages.size() > 0) { 403 RegistryPackageDAO registryPackageDAO = new RegistryPackageDAO(); 404 registryPackageDAO.insert(user, conn, packages); 405 } 406 407 if (serviceBindings.size() > 0) { 408 ServiceBindingDAO serviceBindingDAO = new ServiceBindingDAO(); 409 serviceBindingDAO.insert(user, conn, serviceBindings); 410 } 411 412 if (services.size() > 0) { 413 ServiceDAO serviceDAO = new ServiceDAO(); 414 serviceDAO.insert(user, conn, services); 415 } 416 417 if (specificationLinks.size() > 0) { 418 SpecificationLinkDAO specificationLinkDAO = new SpecificationLinkDAO(); 419 specificationLinkDAO.insert(user, conn, specificationLinks); 420 } 421 422 if (users.size() > 0) { 423 UserDAO userDAO = new UserDAO(); 424 userDAO.insert(user, conn, users); 425 } 426 427 if (doCommit) { 428 System.err.println("Now committing transaction"); 429 conn.commit(); 430 releaseConnection(conn); 431 } 432 433 434 } 435 catch (SQLException e) { 436 try { 437 if (!conn.isClosed()) { 438 conn.rollback(); 439 } 440 releaseConnection(conn); 441 throw new RegistryException(e); 442 } 443 catch(SQLException se) { 444 throw new RegistryException(se); 445 } 446 } 447 } 448 449 /*** 450 * Does a bulk update of a heterogeneous Collection of RegistrObjects. 451 * 452 */ 453 public void update(User user, ArrayList registryObjects) throws RegistryException{ 454 Connection conn = null; 455 try { 456 conn = getConnection(); 457 conn.setAutoCommit(false); 458 459 ArrayList associations = new ArrayList(); 460 ArrayList auditableEvents = new ArrayList(); 461 ArrayList classifications = new ArrayList(); 462 ArrayList schemes = new ArrayList(); 463 ArrayList classificationNodes = new ArrayList(); 464 ArrayList externalIds = new ArrayList(); 465 ArrayList externalLinks = new ArrayList(); 466 ArrayList extrinsicObjects = new ArrayList(); 467 ArrayList organizations = new ArrayList(); 468 ArrayList packages = new ArrayList(); 469 ArrayList serviceBindings = new ArrayList(); 470 ArrayList services = new ArrayList(); 471 ArrayList specificationLinks = new ArrayList(); 472 ArrayList users = new ArrayList(); 473 474 sortRegistryObjects(registryObjects, 475 associations, 476 auditableEvents, 477 classifications, 478 schemes, 479 classificationNodes, 480 externalIds, 481 externalLinks, 482 extrinsicObjects, 483 organizations, 484 packages, 485 serviceBindings, 486 services, 487 specificationLinks, 488 users 489 ); 490 491 if (associations.size() > 0) { 492 AssociationDAO associationDAO = new AssociationDAO(); 493 associationDAO.update(user, conn, associations); 494 } 495 if (auditableEvents.size() > 0) { 496 // Should we allow update AuditableEvent? 497 // AuditableEventDAO auditableEventDAO = new AuditableEventDAO(); 498 //auditableEventDAO.update(user, conn, auditableEvents); 499 } 500 if (classifications.size() > 0) { 501 ClassificationDAO classificationDAO = new ClassificationDAO(); 502 classificationDAO.update(user, conn, classifications); 503 } 504 if (schemes.size() > 0) { 505 ClassificationSchemeDAO classificationSchemeDAO = new ClassificationSchemeDAO(); 506 classificationSchemeDAO.update(user, conn, schemes); 507 } 508 if (classificationNodes.size() > 0) { 509 ClassificationNodeDAO classificationNodeDAO = new ClassificationNodeDAO(); 510 classificationNodeDAO.update(user, conn, classificationNodes); 511 } 512 if (externalIds.size() > 0) { 513 // ExternalId is no longer the first level, right? 514 ExternalIdentifierDAO externalIdentifierDAO = new ExternalIdentifierDAO(); 515 externalIdentifierDAO.update(user, conn, externalIds); 516 } 517 if (externalLinks.size() > 0) { 518 ExternalLinkDAO externalLinkDAO = new ExternalLinkDAO(); 519 externalLinkDAO.update(user, conn, externalLinks); 520 } 521 if (extrinsicObjects.size() > 0) { 522 ExtrinsicObjectDAO extrinsicObjectDAO = new ExtrinsicObjectDAO(); 523 extrinsicObjectDAO.update(user, conn, extrinsicObjects); 524 } 525 if (organizations.size() > 0) { 526 OrganizationDAO organizationDAO = new OrganizationDAO(); 527 organizationDAO.update(user, conn, organizations); 528 } 529 if (packages.size() > 0) { 530 RegistryPackageDAO registryPackageDAO = new RegistryPackageDAO(); 531 registryPackageDAO.update(user, conn, packages); 532 } 533 if (serviceBindings.size() > 0) { 534 ServiceBindingDAO serviceBindingDAO = new ServiceBindingDAO(); 535 serviceBindingDAO.update(user, conn, serviceBindings); 536 } 537 if (services.size() > 0) { 538 ServiceDAO serviceDAO = new ServiceDAO(); 539 serviceDAO.update(user, conn, services); 540 } 541 if (specificationLinks.size() > 0) { 542 SpecificationLinkDAO specificationLinkDAO = new SpecificationLinkDAO(); 543 specificationLinkDAO.update(user, conn, specificationLinks); 544 } 545 if (users.size() > 0) { 546 UserDAO userDAO = new UserDAO(); 547 userDAO.update(user, conn, users); 548 } 549 550 conn.commit(); 551 releaseConnection(conn); 552 553 } catch (SQLException e) { 554 try { 555 if (!conn.isClosed()) { 556 conn.rollback(); 557 } 558 releaseConnection(conn); 559 throw new RegistryException(e); 560 } 561 catch(SQLException se) { 562 throw new RegistryException(se); 563 } 564 } catch(RegistryException e) { 565 try { 566 if (!conn.isClosed()) { 567 conn.rollback(); 568 } 569 releaseConnection(conn); 570 throw e; 571 } 572 catch(SQLException se) { 573 throw new RegistryException(se); 574 } 575 } 576 } 577 578 /*** 579 * Update the status of specified objects to the specified status. 580 * 581 */ 582 public void updateStatus(User user, ArrayList registryObjectsIds, org.oasis.ebxml.registry.bindings.rim.types.StatusType status, org.oasis.ebxml.registry.bindings.rs.RegistryErrorList el) throws RegistryException { 583 Connection connection = null; 584 585 try { 586 connection = getConnection(); 587 reDAO.updateStatusForHeterogeneousObjects(user, connection, registryObjectsIds, status, el); 588 connection.commit(); 589 releaseConnection(connection); 590 } 591 catch (SQLException e) { 592 try { 593 if (!connection.isClosed()) { 594 connection.rollback(); 595 } 596 releaseConnection(connection); 597 throw new RegistryException(e); 598 } 599 catch(SQLException se) { 600 throw new RegistryException(se); 601 } 602 } catch(RegistryException e) { 603 try { 604 if (!connection.isClosed()) { 605 connection.rollback(); 606 } 607 releaseConnection(connection); 608 throw e; 609 } 610 catch(SQLException se) { 611 throw new RegistryException(se); 612 } 613 } 614 } 615 616 /*** 617 * Does a bulk delete of a heterogeneous Collection of RegistrObjects. If 618 * any RegistryObject cannot be found, it will make no change to the 619 * database and throw RegistryException 620 * 621 */ 622 public void delete(User user, ArrayList registryObjectIds) throws RegistryException{ 623 Connection connection = null; 624 try { 625 connection = getConnection(); 626 connection.setAutoCommit(false); 627 628 if (registryObjectIds.size() > 0) { 629 if (!roDAO.registryObjectsExist(connection, registryObjectIds) 630 .isEmpty()) { 631 throw new RegistryException("Some objects not found, do nothing"); 632 } 633 HashMap map = roDAO.sortIdsByObjectType(connection, registryObjectIds); 634 java.util.Set objectTypes = map.keySet(); 635 java.util.Iterator iter = objectTypes.iterator(); 636 // System.err.println(registryObjectIds.size()); 637 AssociationDAO associationDAO = new AssociationDAO(); 638 AuditableEventDAO auditableEventDAO = new AuditableEventDAO(); 639 ClassificationDAO classificationDAO = new ClassificationDAO(); 640 ClassificationSchemeDAO classificationSchemeDAO = new ClassificationSchemeDAO(); 641 ClassificationNodeDAO classificationNodeDAO = new ClassificationNodeDAO(); 642 ExternalIdentifierDAO externalIdentifierDAO = new ExternalIdentifierDAO(); 643 ExternalLinkDAO externalLinkDAO = new ExternalLinkDAO(); 644 ExtrinsicObjectDAO extrinsicObjectDAO = new ExtrinsicObjectDAO(); 645 OrganizationDAO organizationDAO = new OrganizationDAO(); 646 RegistryPackageDAO registryPackageDAO = new RegistryPackageDAO(); 647 ServiceBindingDAO serviceBindingDAO = new ServiceBindingDAO(); 648 ServiceDAO serviceDAO = new ServiceDAO(); 649 SpecificationLinkDAO specificationLinkDAO = new SpecificationLinkDAO(); 650 UserDAO userDAO = new UserDAO(); 651 652 while(iter.hasNext()) { 653 String objectType = (String)iter.next(); 654 System.err.println("objectType: " + objectType); 655 if (objectType.equalsIgnoreCase("Association")) { 656 associationDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 657 } 658 else if (objectType.equalsIgnoreCase("AuditableEvent")) { 659 auditableEventDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 660 } 661 else if (objectType.equalsIgnoreCase("Classification")) { 662 classificationDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 663 } 664 else if (objectType.equalsIgnoreCase("ClassificationScheme")) { 665 classificationSchemeDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 666 } 667 else if (objectType.equalsIgnoreCase("ClassificationNode")) { 668 classificationNodeDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 669 } 670 else if (objectType.equalsIgnoreCase("ExtrinsicObject")) { 671 extrinsicObjectDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 672 } 673 else if (objectType.equalsIgnoreCase("ExternalIdentifier")) { 674 externalIdentifierDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 675 } 676 else if (objectType.equalsIgnoreCase("ExternalLink")) { 677 externalLinkDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 678 } 679 else if (objectType.equalsIgnoreCase("Organization")) { 680 organizationDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 681 } 682 else if (objectType.equalsIgnoreCase("RegistryPackage")) { 683 registryPackageDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 684 } 685 else if (objectType.equalsIgnoreCase("ServiceBinding")) { 686 serviceBindingDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 687 } 688 else if (objectType.equalsIgnoreCase("Service")) { 689 serviceDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 690 } 691 else if (objectType.equalsIgnoreCase("SpecificationLink")) { 692 specificationLinkDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 693 } 694 else if (objectType.equalsIgnoreCase("User")) { 695 userDAO.delete(user, connection, (ArrayList)(map.get(objectType))); 696 } 697 else { 698 throw new RegistryException("Unexpected objectType " 699 + objectType); 700 } 701 702 } // end while 703 } 704 705 connection.commit(); 706 releaseConnection(connection); 707 708 } catch (SQLException e) { 709 try { 710 if (!connection.isClosed()) { 711 connection.rollback(); 712 } 713 releaseConnection(connection); 714 throw new RegistryException(e); 715 } 716 catch(SQLException se) { 717 throw new RegistryException(se); 718 } 719 } catch(RegistryException e) { 720 try { 721 if (!connection.isClosed()) { 722 connection.rollback(); 723 } 724 releaseConnection(connection); 725 throw e; 726 } 727 catch(SQLException se) { 728 throw new RegistryException(se); 729 } 730 } 731 } 732 733 /*** 734 * Adds specified slots to specified object. 735 * 736 */ 737 public void addSlots(String objectId, ArrayList slots) throws RegistryException { 738 Connection connection = null; 739 try { 740 connection = getConnection(); 741 //Now insert Slots for this object 742 743 SlotDAO slotDAO = new SlotDAO(); 744 if (slots.size() > 0) { 745 slotDAO.insert(connection, objectId, slots, false); 746 connection.commit(); 747 releaseConnection(connection); 748 } 749 750 } 751 catch (SQLException e) { 752 try { 753 if (!connection.isClosed()) { 754 connection.rollback(); 755 } 756 releaseConnection(connection); 757 throw new RegistryException(e); 758 } 759 catch(SQLException se) { 760 throw new RegistryException(se); 761 } 762 } catch(RegistryException e) { 763 try { 764 if (!connection.isClosed()) { 765 connection.rollback(); 766 } 767 releaseConnection(connection); 768 throw e; 769 } 770 catch(SQLException se) { 771 throw new RegistryException(se); 772 } 773 } 774 } 775 776 /*** 777 * Removes specified slots from specified object. 778 * 779 */ 780 public void removeSlots(String objectId, ArrayList slots) throws RegistryException { 781 Connection connection = null; 782 try { 783 connection = getConnection(); 784 SlotDAO slotDAO = new SlotDAO(); 785 slotDAO.deleteByParentIdAndSlots(connection, objectId, slots); 786 connection.commit(); 787 releaseConnection(connection); 788 } catch (SQLException e) { 789 try { 790 if (!connection.isClosed()) { 791 connection.rollback(); 792 } 793 releaseConnection(connection); 794 throw new RegistryException(e); 795 } 796 catch(SQLException se) { 797 throw new RegistryException(se); 798 } 799 } catch(RegistryException e) { 800 try { 801 if (!connection.isClosed()) { 802 connection.rollback(); 803 } 804 releaseConnection(connection); 805 throw e; 806 } 807 catch(SQLException se) { 808 throw new RegistryException(se); 809 } 810 } 811 } 812 813 814 private java.sql.DatabaseMetaData getMetaData(Connection conn) throws SQLException { 815 if (metaData == null) { 816 metaData = conn.getMetaData(); 817 } 818 return metaData; 819 } 820 821 /*** 822 * Executes an SQL Query. 823 */ 824 public ArrayList executeSQLQuery(String sqlQuery, ResponseOption responseOption, String tableName, ArrayList objectRefs) throws RegistryException { 825 ArrayList res=null; 826 Connection conn = null; 827 try { 828 conn = getConnection(); 829 conn.setAutoCommit(false); 830 831 java.sql.ResultSet rs = null; 832 833 tableName = mapTableName(tableName); 834 ReturnTypeType returnType = responseOption.getReturnType(); 835 boolean returnComposedObjects = responseOption.getReturnComposedObjects(); 836 837 //Check if user may have set an incorrect returnType. 838 //If so fix silently per spec to the next less specific returnType. 839 if (returnType.getType() == ReturnTypeType.REGISTRYENTRY_TYPE) { 840 rs = getMetaData(conn).getColumns(null, null, tableName, "expiration"); 841 842 if (!(rs.next())) { 843 //Not a RegistryEntry. Make it a RegistryObject 844 returnType = ReturnTypeType.REGISTRYOBJECT; 845 } 846 rs.close(); 847 } 848 849 java.sql.Statement stmt = conn.createStatement(); 850 851 System.err.println("Executing query: '" + sqlQuery + "'"); 852 rs = stmt.executeQuery(sqlQuery); 853 854 855 java.util.Iterator iter = null; 856 switch (returnType.getType()) { 857 case ReturnTypeType.OBJECTREF_TYPE: 858 res = new ArrayList(); 859 while(rs.next()) { 860 org.oasis.ebxml.registry.bindings.rim.ObjectRef or = new org.oasis.ebxml.registry.bindings.rim.ObjectRef(); 861 String id = rs.getString(1); 862 or.setId(id); 863 res.add(or); 864 } 865 break; 866 case ReturnTypeType.REGISTRYOBJECT_TYPE: 867 res = roDAO.getRegistryObjectList(conn, rs, responseOption); 868 break; 869 case ReturnTypeType.REGISTRYENTRY_TYPE: 870 res = reDAO.getRegistryEntryList(conn, rs, responseOption); 871 break; 872 case ReturnTypeType.LEAFCLASS_TYPE: 873 case ReturnTypeType.LEAFCLASSWITHREPOSITORYITEM_TYPE: 874 /* left for debugging rs in future 875 while(rs.next()) { 876 String id = rs.getString("id"); 877 System.err.println(id); 878 } 879 880 rs.beforeFirst(); 881 */ 882 883 res = getLeafObjectList(conn, rs, tableName, responseOption, objectRefs); 884 885 /* left for debugging rs in future 886 rs.beforeFirst(); 887 while(rs.next()) { 888 String id = rs.getString("id"); 889 System.err.println(id); 890 } 891 */ 892 break; 893 default: 894 throw new RegistryException("Invalid returnType: " + returnType); 895 } 896 stmt.close(); 897 releaseConnection(conn); 898 } catch (SQLException e) { 899 try { 900 //Leave this in for now to help faster debugging during development. 901 e.printStackTrace(); 902 903 if (conn != null) { 904 if (!conn.isClosed()) { 905 conn.rollback(); 906 } 907 releaseConnection(conn); 908 } 909 throw new RegistryException(e); 910 } 911 catch(SQLException se) { 912 throw new RegistryException(se); 913 } 914 } catch(RegistryException e) { 915 try { 916 if (!conn.isClosed()) { 917 conn.rollback(); 918 } 919 releaseConnection(conn); 920 throw e; 921 } 922 catch(SQLException se) { 923 throw new RegistryException(se); 924 } 925 } 926 return res; 927 } 928 929 private ArrayList getLeafObjectList(Connection conn, java.sql.ResultSet rs, String tableName, ResponseOption responseOption, ArrayList objectRefs) throws RegistryException { 930 ArrayList res = null; 931 932 if (tableName.equalsIgnoreCase(AssociationDAO.getTableNameStatic())) { 933 AssociationDAO associationDAO = new AssociationDAO(); 934 res = associationDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 935 } 936 else if (tableName.equalsIgnoreCase(AuditableEventDAO.getTableNameStatic())) { 937 AuditableEventDAO auditableEventDAO = new AuditableEventDAO(); 938 res = auditableEventDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 939 } 940 else if (tableName.equalsIgnoreCase(ClassificationDAO.getTableNameStatic())) { 941 ClassificationDAO classificationDAO = new ClassificationDAO(); 942 res = classificationDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 943 } 944 else if (tableName.equalsIgnoreCase(ClassificationSchemeDAO.getTableNameStatic())) { 945 ClassificationSchemeDAO classificationSchemeDAO = new ClassificationSchemeDAO(); 946 res = classificationSchemeDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 947 } 948 else if (tableName.equalsIgnoreCase(ClassificationNodeDAO.getTableNameStatic())) { 949 ClassificationNodeDAO classificationNodeDAO = new ClassificationNodeDAO(); 950 res = classificationNodeDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 951 } 952 else if (tableName.equalsIgnoreCase(ExternalIdentifierDAO.getTableNameStatic())) { 953 ExternalIdentifierDAO externalIdentifierDAO = new ExternalIdentifierDAO(); 954 res = externalIdentifierDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 955 } 956 else if (tableName.equalsIgnoreCase(ExternalLinkDAO.getTableNameStatic())) { 957 ExternalLinkDAO externalLinkDAO = new ExternalLinkDAO(); 958 res = externalLinkDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 959 } 960 else if (tableName.equalsIgnoreCase(ExtrinsicObjectDAO.getTableNameStatic())) { 961 ExtrinsicObjectDAO extrinsicObjectDAO = new ExtrinsicObjectDAO(); 962 res = extrinsicObjectDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 963 } 964 else if (tableName.equalsIgnoreCase(OrganizationDAO.getTableNameStatic())) { 965 OrganizationDAO organizationDAO = new OrganizationDAO(); 966 res = organizationDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 967 } 968 else if (tableName.equalsIgnoreCase(RegistryObjectDAO.getTableNameStatic())) { 969 RegistryObjectDAO registryObjectDAO = new RegistryObjectDAO(); 970 res = registryObjectDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 971 } 972 else if (tableName.equalsIgnoreCase(RegistryPackageDAO.getTableNameStatic())) { 973 RegistryPackageDAO registryPackageDAO = new RegistryPackageDAO(); 974 res = registryPackageDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 975 } 976 else if (tableName.equalsIgnoreCase(ServiceBindingDAO.getTableNameStatic())) { 977 ServiceBindingDAO serviceBindingDAO = new ServiceBindingDAO(); 978 res = serviceBindingDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 979 } 980 else if (tableName.equalsIgnoreCase(ServiceDAO.getTableNameStatic())) { 981 ServiceDAO serviceDAO = new ServiceDAO(); 982 res = serviceDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 983 } 984 else if (tableName.equalsIgnoreCase(SpecificationLinkDAO.getTableNameStatic())) { 985 SpecificationLinkDAO specificationLinkDAO = new SpecificationLinkDAO(); 986 res = specificationLinkDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 987 } 988 else if (tableName.equalsIgnoreCase(UserDAO.getTableNameStatic())) { 989 UserDAO userDAO = new UserDAO(); 990 res = userDAO.getLeafObjectList(conn, rs, responseOption, objectRefs); 991 } 992 993 return res; 994 } 995 996 /*** 997 * Returns ArrayList of ids of non-existent RegistryObject 998 */ 999 public ArrayList registryObjectsExist(ArrayList ids) throws 1000 RegistryException { 1001 Connection conn = null; 1002 try { 1003 conn = getConnection(); 1004 ArrayList notExist = roDAO.registryObjectsExist(conn, ids); 1005 releaseConnection(conn); 1006 return notExist; 1007 } 1008 catch (SQLException e) { 1009 log.error(e); 1010 try { 1011 if (!conn.isClosed()) { 1012 conn.rollback(); 1013 } 1014 releaseConnection(conn); 1015 throw new RegistryException(e); 1016 } 1017 catch(SQLException se) { 1018 throw new RegistryException(se); 1019 } 1020 } 1021 catch(RegistryException e) { 1022 log.error(e); 1023 try { 1024 if (!conn.isClosed()) { 1025 conn.rollback(); 1026 } 1027 releaseConnection(conn); 1028 throw e; 1029 } 1030 catch(SQLException se) { 1031 throw new RegistryException(se); 1032 } 1033 } 1034 } 1035 1036 /*** 1037 * Gets the specified object using specified id and className 1038 * 1039 */ 1040 public RegistryObjectType getRegistryObject(String id, String className) throws RegistryException { 1041 RegistryObjectType ro = null; 1042 1043 String tableName = mapTableName(className); 1044 String sqlQuery = "Select * from " + tableName + " WHERE id = '" + id + "' "; 1045 1046 ResponseOption responseOption = new ResponseOption(); 1047 responseOption.setReturnType(ReturnTypeType.LEAFCLASS); 1048 responseOption.setReturnComposedObjects(true); 1049 1050 ArrayList objectRefs = new ArrayList(); 1051 ArrayList al = executeSQLQuery(sqlQuery, responseOption, tableName, objectRefs); 1052 if (al.size() == 1) { 1053 ro = (RegistryObjectType)al.get(0); 1054 } 1055 1056 return ro; 1057 } 1058 1059 public String mapTableName(String name) { 1060 String newName = (String)tableNameMap.get(name.toLowerCase().trim()); 1061 if (newName == null) { 1062 newName = name; 1063 } 1064 return newName; 1065 } 1066 1067 public String mapColumnName(String name) { 1068 String newName = (String)columnNameMap.get(name.toLowerCase().trim()); 1069 if (newName == null) { 1070 newName = name; 1071 } 1072 return newName; 1073 } 1074 1075 /*** 1076 * Get a HashMap with registry object id as key and owner id as value 1077 */ 1078 public HashMap getOwnersMap(ArrayList ids) throws RegistryException { 1079 1080 Connection conn = null; 1081 try { 1082 conn = getConnection(); 1083 HashMap ownersMap = roDAO.getOwnersMap(conn, ids); 1084 releaseConnection(conn); 1085 return ownersMap; 1086 } 1087 catch (SQLException e) { 1088 log.error(e); 1089 try { 1090 if (!conn.isClosed()) { 1091 conn.rollback(); 1092 } 1093 releaseConnection(conn); 1094 throw new RegistryException(e); 1095 } 1096 catch(SQLException se) { 1097 throw new RegistryException(se); 1098 } 1099 } 1100 catch(RegistryException e) { 1101 log.error(e); 1102 try { 1103 if (!conn.isClosed()) { 1104 conn.rollback(); 1105 } 1106 releaseConnection(conn); 1107 throw e; 1108 } 1109 catch(SQLException se) { 1110 throw new RegistryException(se); 1111 } 1112 } 1113 1114 } 1115 1116 public static void main(String[] args) { 1117 SQLPersistenceManagerImpl impl = SQLPersistenceManagerImpl.getInstance(); 1118 1119 String sqlQuery = "SELECT * FROM RegistryPackage WHERE id = 'urn:uuid:4e71dd00-05e0-41b0-b30d-9f3b49b8098c'"; 1120 ResponseOption responseOption = new ResponseOption(); 1121 responseOption.setReturnComposedObjects(true); 1122 responseOption.setReturnType(ReturnTypeType.LEAFCLASS); 1123 1124 String tableName = "RegistryObject"; 1125 ArrayList objectRefs = new ArrayList(); 1126 try { 1127 long time1 = System.currentTimeMillis(); 1128 objectRefs = impl.executeSQLQuery(sqlQuery, 1129 responseOption, 1130 tableName, 1131 objectRefs); 1132 long time2 = System.currentTimeMillis(); 1133 System.err.println("Elapsed time in millis: " + (time2-time1)); 1134 } 1135 catch (RegistryException e) { 1136 e.printStackTrace(); 1137 System.exit(-1); 1138 } 1139 System.exit(0); 1140 } 1141 }

This page was automatically generated by Maven