| 1 16 package org.apache.juddi.datastore.jdbc; 17 18 import java.sql.Connection ; 19 import java.sql.SQLException ; 20 import java.util.Vector ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.juddi.datastore.DataStore; 25 import org.apache.juddi.datatype.Address; 26 import org.apache.juddi.datatype.CategoryBag; 27 import org.apache.juddi.datatype.DiscoveryURLs; 28 import org.apache.juddi.datatype.IdentifierBag; 29 import org.apache.juddi.datatype.KeyedReference; 30 import org.apache.juddi.datatype.OverviewDoc; 31 import org.apache.juddi.datatype.SharedRelationships; 32 import org.apache.juddi.datatype.TModelBag; 33 import org.apache.juddi.datatype.assertion.PublisherAssertion; 34 import org.apache.juddi.datatype.binding.BindingTemplate; 35 import org.apache.juddi.datatype.binding.BindingTemplates; 36 import org.apache.juddi.datatype.binding.InstanceDetails; 37 import org.apache.juddi.datatype.binding.TModelInstanceDetails; 38 import org.apache.juddi.datatype.binding.TModelInstanceInfo; 39 import org.apache.juddi.datatype.business.BusinessEntity; 40 import org.apache.juddi.datatype.business.Contact; 41 import org.apache.juddi.datatype.business.Contacts; 42 import org.apache.juddi.datatype.publisher.Publisher; 43 import org.apache.juddi.datatype.request.FindQualifiers; 44 import org.apache.juddi.datatype.response.AssertionStatusItem; 45 import org.apache.juddi.datatype.response.BusinessInfo; 46 import org.apache.juddi.datatype.response.CompletionStatus; 47 import org.apache.juddi.datatype.response.PublisherInfo; 48 import org.apache.juddi.datatype.response.RelatedBusinessInfo; 49 import org.apache.juddi.datatype.response.ServiceInfo; 50 import org.apache.juddi.datatype.response.ServiceInfos; 51 import org.apache.juddi.datatype.response.TModelInfo; 52 import org.apache.juddi.datatype.service.BusinessService; 53 import org.apache.juddi.datatype.service.BusinessServices; 54 import org.apache.juddi.datatype.tmodel.TModel; 55 import org.apache.juddi.error.RegistryException; 56 import org.apache.juddi.error.UnknownUserException; 57 import org.apache.juddi.util.Config; 58 import org.apache.juddi.util.jdbc.ConnectionManager; 59 import org.apache.juddi.util.jdbc.Transaction; 60 import org.apache.juddi.uuidgen.UUIDGen; 61 import org.apache.juddi.uuidgen.UUIDGenFactory; 62 63 67 public class JDBCDataStore implements DataStore 68 { 69 private static Log log = LogFactory.getLog(JDBCDataStore.class); 71 72 private Connection connection = null; 74 75 private Transaction transaction = null; 77 78 82 public JDBCDataStore() 83 { 84 try { 85 this.connection = ConnectionManager.aquireConnection(); 86 } 87 catch(SQLException sqlex) { 88 log.error("Exception occured while attempting to " + 89 "aquire a JDBC connection: "+sqlex.getMessage()); 90 } 91 } 92 93 97 public void release() 98 { 99 try { 100 if (connection != null) 101 { 102 this.connection.close(); 103 this.connection = null; 104 } 105 } 106 catch(SQLException sqlex) { 107 log.error("Exception occured while attempting to " + 108 "close a JDBC connection: "+sqlex.getMessage()); 109 } 110 } 111 112 115 public Connection getConnection() 116 { 117 return this.connection; 118 } 119 120 123 public void beginTrans() 124 throws org.apache.juddi.error.RegistryException 125 { 126 try { 127 this.transaction = new Transaction(); 128 this.transaction.begin(connection); 129 } 130 catch(SQLException sqlex) { 131 throw new RegistryException(sqlex); 132 } 133 } 134 135 138 public void commit() 139 throws org.apache.juddi.error.RegistryException 140 { 141 try { 142 this.transaction.commit(); 143 } 144 catch(SQLException sqlex) { 145 throw new RegistryException(sqlex); 146 } 147 } 148 149 152 public void rollback() 153 throws org.apache.juddi.error.RegistryException 154 { 155 try { 156 this.transaction.rollback(); 157 } 158 catch(SQLException sqlex) { 159 throw new RegistryException(sqlex); 160 } 161 } 162 163 171 public Publisher getPublisher(String publisherID) 172 throws RegistryException 173 { 174 if (publisherID == null) 176 return null; 177 178 Publisher publisher = null; 179 180 try { 181 publisher = PublisherTable.select(publisherID,connection); 182 } 183 catch(java.sql.SQLException sqlex) { 184 throw new RegistryException(sqlex); 185 } 186 187 return publisher; 188 } 189 190 201 public boolean isAdministrator(String publisherID) 202 throws org.apache.juddi.error.RegistryException 203 { 204 if ((publisherID == null) || (publisherID.length() == 0)) 205 throw new UnknownUserException("publisherID = "+publisherID); 206 207 try 208 { 209 Publisher publisher = PublisherTable.select(publisherID,connection); 210 if (publisher == null) 211 throw new UnknownUserException("publisherID = "+publisherID); 212 else 213 return publisher.isAdmin(); 214 } 215 catch(java.sql.SQLException sqlex) 216 { 217 log.error(sqlex.getMessage()); 218 throw new RegistryException(sqlex); 219 } 220 } 221 222 233 public boolean isEnabled(String publisherID) 234 throws org.apache.juddi.error.RegistryException 235 { 236 if ((publisherID == null) || (publisherID.length() == 0)) 237 throw new UnknownUserException("publisherID = "+publisherID); 238 239 try 240 { 241 Publisher publisher = PublisherTable.select(publisherID,connection); 242 if (publisher == null) 243 throw new UnknownUserException("publisherID = "+publisherID); 244 else 245 return publisher.isEnabled(); 246 } 247 catch(java.sql.SQLException sqlex) 248 { 249 log.error(sqlex.getMessage()); 250 throw new RegistryException(sqlex); 251 } 252 } 253 254 260 public String generateToken(Publisher publisher) 261 throws RegistryException 262 { 263 UUIDGen uuidgen = UUIDGenFactory.getUUIDGen(); 264 265 String token = "authToken:"+uuidgen.uuidgen(); 266 267 log.info("Generated token '"+token+"' for user: '" + 268 publisher.getPublisherID()+"/"+publisher.getName()+"'"); 269 270 return token; 271 } 272 273 276 public void storeAuthToken(String token,Publisher publisher) 277 throws org.apache.juddi.error.RegistryException 278 { 279 if ((token != null) && (publisher != null)) 280 { 281 try { 282 AuthTokenTable.insert(token,publisher,connection); 283 } 284 catch(java.sql.SQLException sqlex) { 285 throw new RegistryException(sqlex); 286 } 287 } 288 } 289 290 293 public void retireAuthToken(String token) 294 throws org.apache.juddi.error.RegistryException 295 { 296 if (token != null) 297 { 298 try { 299 AuthTokenTable.invalidate(token,connection); 300 AuthTokenTable.touch(token,connection); 301 } 302 catch(java.sql.SQLException sqlex) { 303 throw new RegistryException(sqlex); 304 } 305 } 306 } 307 308 311 public Publisher getAuthTokenPublisher(String token) 312 throws org.apache.juddi.error.RegistryException 313 { 314 Publisher publisher = null; 315 316 if (token != null) 317 { 318 try { 319 publisher = AuthTokenTable.selectPublisher(token,connection); 320 } 321 catch(java.sql.SQLException sqlex) { 322 throw new RegistryException(sqlex); 323 } 324 } 325 326 return publisher; 327 } 328 329 332 public boolean isAuthTokenExpired(String token) 333 throws org.apache.juddi.error.RegistryException 334 { 335 boolean expired = false; 336 337 if (token != null) 338 { 339 try 340 { 341 long tokenState = AuthTokenTable.selectTokenState(token, connection); 342 if (tokenState <= 0) { 343 return expired = true; 344 } 345 346 long lastUsed = AuthTokenTable.selectLastUsed(token,connection); 347 if (lastUsed > 0) { 349 long timeOut = Config.getLongProperty("juddi.authTokenTimeout",3600) * 1000L; long currTime = System.currentTimeMillis(); 351 if ((currTime-lastUsed) >= timeOut) 352 expired = true; 353 } 354 } 355 catch(java.sql.SQLException sqlex) { 356 throw new RegistryException(sqlex); 357 } 358 } 359 360 return expired; 361 } 362 363 366 public void touchAuthToken(String token) 367 throws org.apache.juddi.error.RegistryException 368 { 369 if (token != null) 370 { 371 try { 372 AuthTokenTable.touch(token,connection); 373 } 374 catch(java.sql.SQLException sqlex) { 375 throw new RegistryException(sqlex); 376 } 377 } 378 } 379 380 383 public void saveBusiness(BusinessEntity business,String publisherID) 384 throws org.apache.juddi.error.RegistryException 385 { 386 try 387 { 388 if ((business != null) && (connection != null)) 389 { 390 String businessKey = business.getBusinessKey(); 391 392 BusinessEntityTable.insert(business,publisherID,connection); 394 395 if (business.getNameVector() != null) 397 BusinessNameTable.insert(businessKey,business.getNameVector(),connection); 398 399 if (business.getDescriptionVector() != null) 401 BusinessDescTable.insert(businessKey,business.getDescriptionVector(),connection); 402 403 IdentifierBag idBag = business.getIdentifierBag(); 405 if ((idBag != null) && (idBag.getKeyedReferenceVector() != null)) 406 BusinessIdentifierTable.insert(businessKey,idBag.getKeyedReferenceVector(),connection); 407 408 CategoryBag catBag = business.getCategoryBag(); 410 if ((catBag != null) && (catBag.getKeyedReferenceVector() != null)) 411 BusinessCategoryTable.insert(businessKey,catBag.getKeyedReferenceVector(),connection); 412 413 DiscoveryURLs discURLs = business.getDiscoveryURLs(); 415 if ((discURLs != null) && (discURLs.getDiscoveryURLVector() != null)) 416 DiscoveryURLTable.insert(businessKey,discURLs.getDiscoveryURLVector(),connection); 417 418 Contacts contacts = business.getContacts(); 420 if (contacts != null) 421 { 422 Vector contactVector = contacts.getContactVector(); 423 if ((contactVector != null) && (contactVector.size() > 0)) 424 { 425 ContactTable.insert(businessKey,contacts.getContactVector(),connection); 427 428 int listSize = contactVector.size(); 430 for (int contactID=0; contactID<listSize; contactID++) 431 { 432 Contact contact = (Contact)contactVector.elementAt(contactID); 433 ContactDescTable.insert(businessKey,contactID,contact.getDescriptionVector(),connection); 434 EmailTable.insert(businessKey,contactID,contact.getEmailVector(),connection); 435 PhoneTable.insert(businessKey,contactID,contact.getPhoneVector(),connection); 436 437 Vector addrList = contact.getAddressVector(); 439 if ((addrList != null) && (addrList.size() > 0)) 440 { 441 AddressTable.insert(businessKey,contactID,addrList,connection); 442 for (int addrID=0; addrID<addrList.size(); addrID++) 443 { 444 Address address = (Address)addrList.elementAt(addrID); 445 AddressLineTable.insert(businessKey,contactID,addrID,address.getAddressLineVector(),connection); 446 } 447 } 448 } 449 } 450 } 451 452 BusinessServices services = business.getBusinessServices(); 454 if ((services != null) && (services.getBusinessServiceVector() != null)) 455 { 456 UUIDGen uuidgen = UUIDGenFactory.getUUIDGen(); 457 Vector serviceVector = services.getBusinessServiceVector(); 458 int serviceListSize = serviceVector.size(); 459 for (int j=0; j<serviceListSize; j++) 460 { 461 BusinessService service = (BusinessService)serviceVector.elementAt(j); 462 service.setBusinessKey(businessKey); 463 464 String serviceKey = service.getServiceKey(); 466 if ((serviceKey == null) || (serviceKey.length() == 0)) { 467 service.setServiceKey(uuidgen.uuidgen()); 468 } 469 470 saveService(service); 471 } 472 } 473 } 474 } 475 catch(java.sql.SQLException sqlex) 476 { 477 throw new RegistryException(sqlex); 478 } 479 } 480 481 484 public BusinessEntity fetchBusiness(String businessKey) 485 throws org.apache.juddi.error.RegistryException 486 { 487 BusinessEntity business = null; 488 489 try 490 { 491 if ((businessKey != null) && (connection != null)) 492 { 493 business = BusinessEntityTable.select(businessKey,connection); 494 business.setNameVector(BusinessNameTable.select(businessKey,connection)); 495 business.setDescriptionVector(BusinessDescTable.select(businessKey,connection)); 496 497 Vector idVector = BusinessIdentifierTable.select(businessKey,connection); 498 if (idVector.size() > 0) 499 { 500 IdentifierBag identifierBag = new IdentifierBag(); 501 identifierBag.setKeyedReferenceVector(idVector); 502 business.setIdentifierBag(identifierBag); 503 } 504 505 Vector catVector = BusinessCategoryTable.select(businessKey,connection); 506 if (catVector.size() > 0) 507 { 508 CategoryBag categoryBag = new CategoryBag(); 509 categoryBag.setKeyedReferenceVector(catVector); 510 business.setCategoryBag(categoryBag); 511 } 512 513 DiscoveryURLs discoveryURLs = new DiscoveryURLs(); 514 discoveryURLs.setDiscoveryURLVector(DiscoveryURLTable.select(businessKey,connection)); 515 business.setDiscoveryURLs(discoveryURLs); 516 517 Vector contactList = ContactTable.select(businessKey,connection); 519 for (int contactID=0; contactID<contactList.size(); contactID++) 520 { 521 Contact contact = (Contact)contactList.elementAt(contactID); 522 contact.setPhoneVector(PhoneTable.select(businessKey,contactID,connection)); 523 contact.setEmailVector(EmailTable.select(businessKey,contactID,connection)); 524 525 Vector addressList = AddressTable.select(businessKey,contactID,connection); 526 for (int addressID=0; addressID<addressList.size(); addressID++) 527 { 528 Address address = (Address)addressList.elementAt(addressID); 529 address.setAddressLineVector(AddressLineTable.select(businessKey,contactID,addressID,connection)); 530 } 531 contact.setAddressVector(addressList); 532 } 533 534 Contacts contacts = new Contacts(); 535 contacts.setContactVector(contactList); 536 business.setContacts(contacts); 537 538 Vector serviceVector = fetchServiceByBusinessKey(businessKey); 540 BusinessServices services = new BusinessServices(); 541 services.setBusinessServiceVector(serviceVector); 542 business.setBusinessServices(services); 543 } 544 } 545 catch(java.sql.SQLException sqlex) 546 { 547 throw new RegistryException(sqlex); 548 } 549 550 return business; 551 } 552 553 556 public void deleteBusiness(String businessKey) 557 throws org.apache.juddi.error.RegistryException 558 { 559 try 560 { 561 if ((businessKey != null) && (connection != null)) 562 { 563 deleteServiceByBusinessKey(businessKey); 565 566 AddressLineTable.delete(businessKey,connection); 568 AddressTable.delete(businessKey,connection); 569 EmailTable.delete(businessKey,connection); 570 PhoneTable.delete(businessKey,connection); 571 ContactDescTable.delete(businessKey,connection); 572 ContactTable.delete(businessKey,connection); 573 DiscoveryURLTable.delete(businessKey,connection); 574 BusinessIdentifierTable.delete(businessKey,connection); 575 BusinessCategoryTable.delete(businessKey,connection); 576 BusinessDescTable.delete(businessKey,connection); 577 BusinessNameTable.delete(businessKey,connection); 578 579 BusinessEntityTable.delete(businessKey,connection); 581 } 582 } 583 catch(java.sql.SQLException sqlex) 584 { 585 log.error(sqlex.getMessage(),sqlex); 586 throw new RegistryException(sqlex); 587 } 588 } 589 590 593 public boolean isBusinessPublisher(String businessKey,String publisherID) 594 throws org.apache.juddi.error.RegistryException 595 { 596 try 597 { 598 if ((publisherID != null) && (businessKey != null) && (connection != null)) 599 return BusinessEntityTable.verifyOwnership(businessKey,publisherID,connection); 600 } 601 catch(java.sql.SQLException sqlex) 602 { 603 throw new RegistryException(sqlex); 604 } 605 606 return false; 608 } 609 610 613 public boolean isValidBusinessKey(String businessKey) 614 throws org.apache.juddi.error.RegistryException 615 { 616 try 617 { 618 if ((businessKey != null) && (connection != null) && 619 (BusinessEntityTable.select(businessKey,connection) != null)) 620 return true; 621 } 622 catch(java.sql.SQLException sqlex) 623 { 624 throw new RegistryException(sqlex); 625 } 626 627 return false; 629 } 630 631 634 public void saveService(BusinessService service) 635 throws org.apache.juddi.error.RegistryException 636 { 637 try 638 { 639 if ((service != null) && (connection != null)) 640 { 641 String serviceKey = service.getServiceKey(); 642 643 BusinessServiceTable.insert(service,connection); 645 646 if (service.getNameVector() != null) 648 ServiceNameTable.insert(serviceKey,service.getNameVector(),connection); 649 650 if (service.getDescriptionVector() != null) 652 ServiceDescTable.insert(serviceKey,service.getDescriptionVector(),connection); 653 654 CategoryBag catBag = service.getCategoryBag(); 656 if ((catBag != null) && (catBag.getKeyedReferenceVector() != null)) 657 ServiceCategoryTable.insert(serviceKey,catBag.getKeyedReferenceVector(),connection); 658 659 BindingTemplates bindings = service.getBindingTemplates(); 661 if (bindings == null) 662 return; 664 Vector bindingList = bindings.getBindingTemplateVector(); 666 if (bindingList == null) 667 return; 669 UUIDGen uuidgen = UUIDGenFactory.getUUIDGen(); 671 672 int listSize = bindingList.size(); 674 for (int i=0; i<listSize; i++) 675 { 676 BindingTemplate binding = (BindingTemplate)bindingList.elementAt(i); 677 binding.setServiceKey(serviceKey); 678 679 String bindingKey = binding.getBindingKey(); 681 if ((bindingKey == null) || (bindingKey.length() == 0)) { 682 binding.setBindingKey(uuidgen.uuidgen()); 683 } 684 685 saveBinding(binding); 686 } 687 } 688 } 689 catch(java.sql.SQLException sqlex) 690 { 691 throw new RegistryException(sqlex); 692 } 693 } 694 695 698 public BusinessService fetchService(String serviceKey) 699 throws org.apache.juddi.error.RegistryException 700 { 701 BusinessService service = null; 702 703 try 704 { 705 if ((serviceKey != null) && (connection != null)) 706 { 707 service = BusinessServiceTable.select(serviceKey,connection); 708 service.setNameVector(ServiceNameTable.select(serviceKey,connection)); 709 service.setDescriptionVector(ServiceDescTable.select(serviceKey,connection)); 710 711 Vector catVector = ServiceCategoryTable.select(serviceKey,connection); 712 if (catVector.size() > 0) 713 { 714 CategoryBag bag = new CategoryBag(); 715 bag.setKeyedReferenceVector(catVector); 716 service.setCategoryBag(bag); 717 } 718 719 Vector bindingVector = fetchBindingByServiceKey(serviceKey); 721 BindingTemplates bindings = new BindingTemplates(); 722 bindings.setBindingTemplateVector(bindingVector); 723 service.setBindingTemplates(bindings); 724 } 725 } 726 catch(java.sql.SQLException sqlex) 727 { 728 throw new RegistryException(sqlex); 729 } 730 731 return service; 732 } 733 734 737 public void deleteService(String serviceKey) 738 throws org.apache.juddi.error.RegistryException 739 { 740 try 741 { 742 if ((serviceKey != null) && (connection != null)) 743 { 744 deleteBindingByServiceKey(serviceKey); 746 747 ServiceNameTable.delete(serviceKey,connection); 749 ServiceDescTable.delete(serviceKey,connection); 750 ServiceCategoryTable.delete(serviceKey,connection); 751 752 BusinessServiceTable.delete(serviceKey,connection); 754 } 755 } 756 catch(java.sql.SQLException sqlex) 757 { 758 throw new RegistryException(sqlex); 759 } 760 } 761 762 765 private Vector fetchServiceByBusinessKey(String businessKey) 766 throws org.apache.juddi.error.RegistryException 767 { 768 Vector serviceList = new Vector (); 769 770 try 771 { 772 if ((businessKey != null) && (connection != null)) 773 { 774 Vector tempList = BusinessServiceTable.selectByBusinessKey(businessKey,connection); 775 for (int i=0; i<tempList.size(); i++) 776 { 777 BusinessService service = (BusinessService)tempList.elementAt(i); 778 serviceList.add(fetchService(service.getServiceKey())); 779 } 780 } 781 } 782 catch(java.sql.SQLException sqlex) 783 { 784 throw new RegistryException(sqlex); 785 } 786 787 return serviceList; 788 } 789 790 793 private void deleteServiceByBusinessKey(String businessKey) 794 throws org.apache.juddi.error.RegistryException 795 { 796 try 797 { 798 if ((businessKey != null) && (connection != null)) 799 { 800 Vector services = BusinessServiceTable.selectByBusinessKey(businessKey,connection); 802 803 int listSize = services.size(); 805 for (int i=0; i<listSize; i++) 806 { 807 BusinessService service = (BusinessService)services.elementAt(i); 808 deleteService(service.getServiceKey()); 809 } 810 } 811 } 812 catch(java.sql.SQLException sqlex) 813 { 814 throw new RegistryException(sqlex); 815 } 816 817 } 818 819 822 public boolean isValidServiceKey(String serviceKey) 823 throws org.apache.juddi.error.RegistryException 824 { 825 try 826 { 827 if ((serviceKey != null) && (connection != null) && 828 (BusinessServiceTable.select(serviceKey,connection) != null)) 829 return true; 830 } 831 catch(java.sql.SQLException sqlex) 832 { 833 throw new RegistryException(sqlex); 834 } 835 836 return false; 838 } 839 840 843 public boolean isServicePublisher(String serviceKey,String publisherID) 844 throws org.apache.juddi.error.RegistryException 845 { 846 try 847 { 848 if ((publisherID != null) && (serviceKey != null) && (connection != null)) 849 return BusinessServiceTable.verifyOwnership(serviceKey,publisherID,connection); 850 } 851 catch(java.sql.SQLException sqlex) 852 { 853 throw new RegistryException(sqlex); 854 } 855 856 return false; 858 } 859 860 863 public void saveBinding(BindingTemplate binding) 864 throws org.apache.juddi.error.RegistryException 865 { 866 try 867 { 868 if ((binding != null) && (connection != null)) 869 { 870 String bindingKey = binding.getBindingKey(); 871 872 BindingTemplateTable.insert(binding,connection); 874 875 CategoryBag catBag = binding.getCategoryBag(); 877 if ((catBag != null) && (catBag.getKeyedReferenceVector() != null)) 878 BindingCategoryTable.insert(bindingKey,catBag.getKeyedReferenceVector(),connection); 879 880 if (binding.getDescriptionVector() != null) 882 BindingDescTable.insert(bindingKey,binding.getDescriptionVector(),connection); 883 884 TModelInstanceDetails details = binding.getTModelInstanceDetails(); 885 if (details == null) 886 return; 887 888 Vector detailsVector = details.getTModelInstanceInfoVector(); 889 if (detailsVector == null) 890 return; 891 892 TModelInstanceInfoTable.insert(bindingKey,detailsVector,connection); 893 894 Vector infoList = detail
|