Changeset 15191 for greenstone3/branches/customizingGreenstone3/src/java/org/greenstone/gsdl3/core/MessageRouter.java
- Timestamp:
- 2008-04-14T16:20:45+12:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
greenstone3/branches/customizingGreenstone3/src/java/org/greenstone/gsdl3/core/MessageRouter.java
r14534 r15191 61 61 * @see Communicator 62 62 * 63 63 * Since some service classes are moved into a separate directory in order for them to be checked out from a different repository, 64 64 * we modify the configureServices method to search some of the classes in other place if they are not found in the service directory. 65 */65 */ 66 66 public class MessageRouter implements ModuleInterface { 67 67 … … 75 75 protected String site_http_address=null; 76 76 77 77 78 protected String library_name = null; 78 79 … … 91 92 /** list of collections that can be reached */ 92 93 protected Element collection_list = null; 93 94 /** list of collections that are loaded but are private */ 95 protected Element private_collection_list = null; 96 97 94 98 /** list of collections that are public and OAI-supportive */ 95 99 protected Element oai_collection_list = null; … … 117 121 118 122 public void cleanUp() { 119 if (this.module_map != null) { 120 Iterator i = this.module_map.values().iterator(); 121 while (i.hasNext()) { 122 ((ModuleInterface)i.next()).cleanUp(); 123 } 124 } 123 cleanUpModuleMapEntire(); 125 124 } 126 125 … … 133 132 } 134 133 135 /** site_name must be set before configure is called */134 /** library_name must be set before configure is called */ 136 135 public void setLibraryName(String library_name) { 137 136 this.library_name = library_name; … … 150 149 public boolean configure() { 151 150 152 logger.info("configuring site");151 logger.info("configuring the Message Router"); 153 152 154 153 if (this.site_name==null) { … … 175 174 // set up the authenticator 176 175 Authenticator.setDefault(new Authenticator(){ 177 178 179 180 176 protected PasswordAuthentication getPasswordAuthentication(){ 177 return new PasswordAuthentication(user, new String(passwd).toCharArray()); 178 } 179 }); 181 180 182 181 } catch (Exception e) { … … 187 186 } 188 187 189 // read thru own config file - create services and connect to sites190 File configFile = new File(GSFile.siteConfigFile(this.site_home));191 192 if (!configFile.exists() ) {193 logger.error(" site config file: "+configFile.getPath()+" not found!");194 return false;195 }196 197 Document config_doc = this.converter.getDOM(configFile);198 if (config_doc == null) {199 logger.error(" couldn't parse site config file: "+configFile.getPath());200 return false;201 }202 203 config_info = config_doc.getDocumentElement();204 205 188 this.module_map = new HashMap(); 206 189 207 // load up the services: serviceRackList 208 Element service_rack_list = (Element)GSXML.getChildByTagName(config_info, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER); 209 configureServices(service_rack_list); 210 211 // load up the service clusters 212 Element cluster_list = (Element)GSXML.getChildByTagName(config_info, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER); 213 configureClusters(cluster_list); 214 215 // load up the collections 216 configureCollections(); 217 218 // load up the external sites - this also adds their services/clusters/collections to the other lists - so must be done last 219 Element site_list = (Element)GSXML.getChildByTagName(config_info, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER); 220 configureSites(site_list); 221 222 223 return true; 224 190 // This stuff may be done at a reconfigure also 191 return configureLocalSite(); 192 225 193 } 226 194 … … 250 218 public Element process(Element message) { 251 219 252 logger.debug("MR received request");253 //logger.info(this.converter.getString(message));254 220 // check that its a correct message tag 255 221 if (!message.getTagName().equals(GSXML.MESSAGE_ELEM)) { … … 288 254 // its a messaging request - modifies the requests/responses 289 255 result = modifyMessages(req, message, mainResult); 290 } else if (type_att.equals(OAIXML.OAI_SET_LIST)) {291 logger.info("oaiSetList request received");292 //this is the oai receptionist asking for a list of oai-support collections293 result = this.doc.createElement(GSXML.RESPONSE_ELEM);294 result.appendChild(oai_collection_list);//getCollectionList();295 256 } else { 296 257 // standard request … … 300 261 mainResult.appendChild(this.doc.importNode(result, true)); 301 262 } else { 263 // The message needs to go to another module. The same message can 264 // be passed to multiple modules - they will be in a comma 265 // separated list in the 'to' attribute 302 266 String [] modules = path.split(","); 303 267 … … 315 279 if (this.module_map.containsKey(obj)) { 316 280 copied_request.setAttribute(GSXML.TO_ATT, this_mod); 317 // logger.info("::::::::::::::: \n"+this.converter.getString(req));318 281 result = ((ModuleInterface)this.module_map.get(obj)).process(mess); 319 282 if (result !=null ) { … … 345 308 return mainResult; 346 309 } 347 310 public Element getCollectionList() { 311 return collection_list; 312 } 348 313 // ******************************************************************** 349 // auxiliary configure methods314 // auxiliary configure and cleanup methods 350 315 // ******************************************************************* 351 316 352 317 /** Calls clean up on all modules referenced in the module_map and 318 removes them . */ 319 protected void cleanUpModuleMapEntire() { 320 if (this.module_map != null) { 321 Iterator i = this.module_map.values().iterator(); 322 while (i.hasNext()) { 323 ((ModuleInterface)i.next()).cleanUp(); 324 i.remove(); 325 } 326 } 327 } 328 329 /** 330 Goes through the children of list, and for each local/site-specific 331 name attribute, calls cleanUp on the module and removes it from the 332 module_map and removes it from the list 333 */ 334 protected void cleanUpModuleMapSubset(Element list, String remote_site) { 335 logger.error(this.converter.getString(list)); 336 NodeList elements = list.getChildNodes(); // we are assuming no extraneous nodes 337 for(int i=elements.getLength()-1; i>=0; i--) { 338 Element item = (Element)elements.item(i); 339 String name = item.getAttribute(GSXML.NAME_ATT); 340 String potential_site_name = GSPath.getFirstLink(name); 341 if (remote_site != null) { 342 if (remote_site.equals(potential_site_name)) { 343 list.removeChild(item); 344 } 345 } else { 346 if (name.equals(potential_site_name)) {// there was no site 347 list.removeChild(item); 348 ModuleInterface m = (ModuleInterface)this.module_map.remove(name); 349 m.cleanUp(); // clean up any open files/connections etc 350 m=null; 351 } 352 } 353 } 354 355 logger.error(this.converter.getString(list)); 356 357 } 358 359 /** removes all site modules from module_map, and any stored info about this sites collections and services */ 360 protected void cleanUpAllExternalSiteInfo() { 361 362 NodeList site_nodes = this.site_list.getChildNodes(); 363 for(int i=site_nodes.getLength()-1; i>=0; i--) { 364 Element item = (Element)site_nodes.item(i); 365 String name = item.getAttribute(GSXML.NAME_ATT); 366 // will remove the node from site_list 367 deactivateModule(GSXML.SITE_ELEM, name); 368 } 369 370 } 371 /** read thru own site config file - create services and connect to sites 372 */ 373 protected boolean configureLocalSite() { 374 375 // this may be a reconfigure, so clean up the old moduleMap 376 cleanUpModuleMapEntire(); 377 378 File configFile = new File(GSFile.siteConfigFile(this.site_home)); 379 380 if (!configFile.exists() ) { 381 logger.error(" site config file: "+configFile.getPath()+" not found!"); 382 return false; 383 } 384 385 Document config_doc = this.converter.getDOM(configFile); 386 if (config_doc == null) { 387 logger.error(" couldn't parse site config file: "+configFile.getPath()); 388 return false; 389 } 390 391 this.config_info = config_doc.getDocumentElement(); 392 393 // load up the services: serviceRackList 394 this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER); 395 Element service_rack_list_elem = (Element)GSXML.getChildByTagName(config_info, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER); 396 configureServices(service_rack_list_elem); 397 398 // load up the service clusters 399 this.cluster_list = this.doc.createElement(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER); 400 Element cluster_list_elem = (Element)GSXML.getChildByTagName(config_info, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER); 401 configureClusters(cluster_list_elem); 402 403 // load up the collections 404 this.collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER); 405 this.private_collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER); 406 this.oai_collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER); 407 configureCollections(); 408 409 // load up the external sites - this also adds their services/clusters/collections to the other lists - so must be done last 410 this.site_list = this.doc.createElement(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER); 411 Element site_list_elem = (Element)GSXML.getChildByTagName(config_info, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER); 412 configureExternalSites(site_list_elem); 413 414 return true; 415 416 } 417 353 418 protected boolean configureServices(Element service_rack_list) { 354 419 355 this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);356 420 357 421 // load up the individual services … … 380 444 Class service_class = null; 381 445 try { 382 446 service_class = Class.forName("org.greenstone.gsdl3.service."+service_name); 383 447 } catch(ClassNotFoundException e) { 384 448 385 386 387 449 try { 450 //try the service_name alone in case the package name is already specified 451 service_class = Class.forName(service_name); 388 452 389 390 391 453 }catch(ClassNotFoundException ae) { 454 logger.info(ae.getMessage()); 455 } 392 456 } 393 457 try { 394 458 395 //ServiceRack s = (ServiceRack)Class.forName("org.greenstone.gsdl3.service."+service_name).newInstance();396 459 ServiceRack s = (ServiceRack)service_class.newInstance(); 397 460 s.setSiteHome(this.site_home); … … 400 463 s.setMessageRouter(this); 401 464 // pass the XML node to the service for service configuration 402 s.configure(n, null); 465 if (!s.configure(n, null)) { 466 logger.error ("couldn't configure ServiceRack "+service_name); 467 continue; 468 } 403 469 404 470 // find out the supported services for this service module … … 426 492 protected boolean configureClusters(Element config_cluster_list) { 427 493 428 this.cluster_list = this.doc.createElement(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);429 494 // load up the service clusters 430 495 logger.info("loading service clusters ..."); … … 448 513 sc.setClusterName(name); 449 514 sc.setMessageRouter(this); 450 sc.configure(cluster); 515 if (!sc.configure(cluster)) { 516 logger.error ("couldn't configure ServiceCluster "+name); 517 continue; 518 } 519 451 520 this.module_map.put(name, sc); // this replaces the old one if there was one already present 452 521 //add short info to cluster list … … 459 528 } 460 529 530 /** looks through the collect directory and activates any collections it finds. If this is a reconfigure, clean up must be done first before calling this */ 461 531 protected boolean configureCollections() { 462 463 this.collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER);464 this.oai_collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER);465 532 466 533 // read thru the collect directory and activate all the valid collections … … 482 549 } 483 550 484 protected boolean configureSites(Element config_site_list) { 485 486 this.site_list = this.doc.createElement(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER); 551 /** creates and configures a new collection 552 if this is done for a reconfigure, the collection should be deactivated first. 553 * 554 *@param col_name the name of the collection 555 *@return true if collection created ok 556 */ 557 protected boolean activateCollectionByName(String col_name) { 558 559 logger.info("Activating collection: "+col_name+"."); 560 561 // Look for the etc/collectionInit.xml file, and see what sort of Collection to load 562 Collection c = null; 563 File init_file = new File(GSFile.collectionInitFile(this.site_home, col_name)); 564 565 if (init_file.exists()) { 566 Document init_doc = this.converter.getDOM(init_file); 567 if (init_doc != null) { 568 Element init_elem = init_doc.getDocumentElement(); 569 if (init_elem != null) { 570 String coll_class_name = init_elem.getAttribute("class"); 571 if (!coll_class_name.equals("")) { 572 try { 573 c = (Collection)Class.forName("org.greenstone.gsdl3.collection."+coll_class_name).newInstance(); 574 } catch (Exception e) { 575 logger.info(" couldn't create a new collection, type "+coll_class_name+", defaulting to class Collection"); 576 } 577 } 578 } 579 } 580 } 581 if (c==null) { // we haven't found another classname to use 582 c = new Collection(); 583 } 584 585 c.setCollectionName(col_name); 586 c.setSiteHome(this.site_home); 587 c.setSiteAddress(this.site_http_address); 588 c.setMessageRouter(this); 589 if (c.configure()) { 590 logger.info("have just configured collection " + col_name); 591 // add to list of collections 592 this.module_map.put(col_name, c); 593 Element e = this.doc.createElement(GSXML.COLLECTION_ELEM); 594 e.setAttribute(GSXML.NAME_ATT, col_name); 595 596 if(c.isPublic()) { 597 // only public collections will appear on the home page 598 // add short description_ to collection_list_ 599 this.collection_list.appendChild(e); 600 601 if (c.hasOAI()) { 602 Element ane = this.doc.createElement(GSXML.COLLECTION_ELEM); 603 //The collection name is returned as site_name:coll_name, which is in fact the set specification 604 ane.setAttribute(GSXML.NAME_ATT, site_name + ":" + col_name); 605 ane.setAttribute(OAIXML.LASTMODIFIED, "" + c.getLastmodified()); 606 607 this.oai_collection_list.appendChild(ane); 608 //logger.info(GSXML.xmlNodeToString(oai_collection_list)); 609 } 610 611 } else { 612 this.private_collection_list.appendChild(e); 613 } 614 return true; 615 } else { 616 logger.error("Couldn't configure collection: "+ 617 col_name+"."); 618 return false; 619 } 620 } 621 622 623 /** Goes through the siteList and activates each site found. If this is done for a reconfigure, a clean up must be done first ****HOW??? */ 624 protected boolean configureExternalSites(Element config_site_list) { 625 487 626 // load up the sites 488 627 logger.info("loading external sites..."); … … 511 650 } 512 651 652 protected boolean activateSiteByName(String site_name) { 653 logger.info("Activating site: "+site_name+"."); 654 655 File configFile = new File(GSFile.siteConfigFile(this.site_home)); 656 657 if (!configFile.exists() ) { 658 logger.error(" site config file: "+configFile.getPath()+" not found!"); 659 return false; 660 } 661 Document config_doc = this.converter.getDOM(configFile); 662 if (config_doc == null) { 663 logger.error(" couldn't parse site config file: "+configFile.getPath()); 664 return false; 665 } 666 Element config_elem = config_doc.getDocumentElement(); 667 668 Element config_site_list = (Element)GSXML.getChildByTagName(config_elem, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER); 669 if (config_site_list ==null ) { 670 logger.error("activateSite, no sites found"); 671 return false; 672 } 673 // this is a name to identify the current site in the Communicator 674 String local_site_name = config_site_list.getAttribute("localSiteName"); 675 if (local_site_name.equals("")) { 676 local_site_name = site_name; 677 } 678 679 Element this_site_elem = GSXML.getNamedElement(config_site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, site_name); 680 if (this_site_elem == null) { 681 logger.error("activateSite, site "+site_name+" not found"); 682 return false; 683 } 684 685 return activateSite(this_site_elem, local_site_name); 686 } 687 513 688 protected boolean activateSite(Element site_elem, String local_site_name) { 689 514 690 Communicator comm=null; 515 691 String type = site_elem.getAttribute(GSXML.TYPE_ATT); 516 692 String name = site_elem.getAttribute(GSXML.NAME_ATT); 517 693 if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) { 694 logger.info("activating SOAP site "+name); 518 695 comm = new SOAPCommunicator(); 519 696 if (comm.configure(site_elem)) { … … 527 704 // have to be added later 528 705 if (!getRemoteSiteInfo(comm, name)) { 529 logger.error(" couldn't get info from site "+name);706 logger.error(" couldn't get info from site"); 530 707 } 531 708 } else { 532 logger.error(" couldn't configure s oap site:"+name);709 logger.error(" couldn't configure site"); 533 710 return false; 534 711 } … … 599 776 600 777 778 779 protected boolean activateServiceClusterByName(String cluster_name) { 780 return false; 781 782 } 783 784 protected boolean activateServiceRackByName(String module_name) { 785 return false; 786 } 787 788 protected boolean deactivateModule(String type, String name) { 789 790 logger.info("deactivating "+ type+" module: "+name); 791 if (this.module_map.containsKey(name)) { 792 793 logger.info("found the module"); 794 ModuleInterface m = (ModuleInterface)this.module_map.remove(name); 795 // also remove the xml bit from description list 796 if (type.equals(GSXML.COLLECTION_ELEM)) { 797 if (((Collection)m).isPublic()) { 798 Element this_col = GSXML.getNamedElement(this.collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name); 799 if (this_col != null) { 800 this.collection_list.removeChild(this_col); 801 } 802 if (((Collection)m).hasOAI()) { 803 this_col = GSXML.getNamedElement(this.oai_collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name); 804 if (this_col != null) { 805 this.oai_collection_list.removeChild(this_col); 806 } 807 } 808 } else { 809 // a private collection 810 Element this_col = GSXML.getNamedElement(this.private_collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name); 811 if (this_col != null) { 812 this.private_collection_list.removeChild(this_col); 813 } 814 } 815 } else if (type.equals(GSXML.SERVICE_ELEM)) { 816 Element this_service = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, name); 817 if (this_service != null) { 818 this.service_list.removeChild(this_service); 819 } 820 } else if (type.equals(GSXML.CLUSTER_ELEM)) { 821 Element this_cluster = GSXML.getNamedElement(this.cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, name); 822 if (this_cluster != null) { 823 this.cluster_list.removeChild(this_cluster); 824 } 825 } else if (type.equals(GSXML.SITE_ELEM)) { 826 Element this_site = GSXML.getNamedElement(this.site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, name); 827 if (this_site != null) { 828 this.site_list.removeChild(this_site); 829 830 // also remove this sites colls, services, clusters etc 831 cleanUpModuleMapSubset(this.collection_list, name); 832 cleanUpModuleMapSubset(this.cluster_list, name); 833 cleanUpModuleMapSubset(this.service_list, name); 834 835 // can remote collections be in the oai_coll list, or private coll list ?? 836 } 837 } else { 838 logger.error("invalid module type: "+type+", can't remove info about this module"); 839 } 840 841 m.cleanUp(); // clean up any open files/connections etc - can cause trouble on windows 842 m=null; 843 return true; 844 } 845 // else not deactivated 846 logger.error(name+" module not found"); 847 return false; 848 849 } 850 601 851 //***************************************************************** 602 852 // auxiliary process methods … … 650 900 651 901 } 652 902 903 if (type.equals(OAIXML.OAI_SET_LIST)) { 904 logger.info("oaiSetList request received"); 905 //this is the oai receptionist asking for a list of oai-support collections 906 response.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SET_LIST ); 907 response.appendChild(this.oai_collection_list); 908 return response; 909 } 910 911 653 912 if (type.equals(GSXML.REQUEST_TYPE_SYSTEM)) { 654 913 … … 667 926 if (subset.equals("")) { 668 927 // need to reconfigure the MR 669 this.configure ();928 this.configureLocalSite(); 670 929 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "MessageRouter reconfigured successfully"); 671 930 response.appendChild(s); … … 674 933 // else it a specific request 675 934 if (subset.equals(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER)) { 935 // get rid of all the old collection stuff (not counting remote ones) before activating all the new ones 936 cleanUpModuleMapSubset(this.collection_list, null); 937 cleanUpModuleMapSubset(this.private_collection_list, null); 676 938 success = configureCollections(); 677 939 } else { … … 694 956 if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) { 695 957 Element service_rack_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER); 696 958 cleanUpModuleMapSubset(this.service_list, null); 697 959 success = configureServices(service_rack_list); 698 960 } else if (subset.equals(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER)) { 699 961 Element cluster_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER); 700 962 cleanUpModuleMapSubset(this.cluster_list, null); 701 963 success = configureClusters(cluster_list); 702 964 } else if (subset.equals(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER)) { 703 965 Element site_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER); 704 success = configureSites(site_list); 966 cleanUpAllExternalSiteInfo(); 967 success = configureExternalSites(site_list); 705 968 } 706 969 } … … 721 984 722 985 if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE)) { 723 deactivateModule(module_type, module_name); 724 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" deactivated"); 725 response.appendChild(s); 986 success = deactivateModule(module_type, module_name); 987 if (success) { 988 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" deactivated"); 989 response.appendChild(s); 990 } else { 991 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" could not be deactivated"); 992 response.appendChild(s); 993 } 994 726 995 } else if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE)) { 996 // we need to deactivate the module first, in case this is a 997 // reconfigure 998 deactivateModule(module_type, module_name); 727 999 if (module_type.equals(GSXML.COLLECTION_ELEM)) { 728 1000 success = activateCollectionByName(module_name); … … 753 1025 754 1026 } 1027 1028 //* Used to copy nodes from one message to another. E.g. copy a response node to the next request. Not sure if this is actually used anywhere yet... */ 1029 755 1030 protected Element modifyMessages(Element request, Element message, Element result) { 756 1031 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM); … … 814 1089 // **************************************************** 815 1090 816 /** creates and configures a new collection 817 * 818 *@param col_name the name of the collection 819 *@return true if collection created ok 820 */ 821 protected boolean activateCollectionByName(String col_name) { 822 823 logger.info("Activating collection: "+col_name+"."); 824 825 // Look for the etc/collectionInit.xml file, and see what sort of Collection to load 826 Collection c = null; 827 File init_file = new File(GSFile.collectionInitFile(this.site_home, col_name)); 828 829 if (init_file.exists()) { 830 Document init_doc = this.converter.getDOM(init_file); 831 if (init_doc != null) { 832 Element init_elem = init_doc.getDocumentElement(); 833 if (init_elem != null) { 834 String coll_class_name = init_elem.getAttribute("class"); 835 if (!coll_class_name.equals("")) { 836 try { 837 c = (Collection)Class.forName("org.greenstone.gsdl3.collection."+coll_class_name).newInstance(); 838 } catch (Exception e) { 839 logger.info(" couldn't create a new collection, type "+coll_class_name+", defaulting to class Collection"); 840 } 841 } 842 } 843 } 844 } 845 if (c==null) { // we haven't found another classname to use 846 c = new Collection(); 847 } 848 849 c.setCollectionName(col_name); 850 c.setSiteHome(this.site_home); 851 c.setSiteAddress(this.site_http_address); 852 c.setMessageRouter(this); 853 if (c.configure()) { 854 // this could be a reactivation, so delete the old version 855 deactivateModule(GSXML.COLLECTION_ELEM, col_name); 856 // add to list of collections 857 this.module_map.put(col_name, c); 858 859 if(c.isPublic()) { 860 // only public collections will appear on the home page 861 // add short description_ to collection_list_ 862 Element e = this.doc.createElement(GSXML.COLLECTION_ELEM); 863 e.setAttribute(GSXML.NAME_ATT, col_name); 864 // if (c.hasOAI()) { 865 // e.setAttribute(OAIXML.HAS_OAI, "true"); 866 // } else { 867 // e.setAttribute(OAIXML.HAS_OAI, "false"); 868 // } 869 if (c.hasOAI() == true) { 870 Element ane = this.doc.createElement(GSXML.COLLECTION_ELEM); 871 //The collection name is returned as site_name:coll_name, which is in fact the set specification 872 ane.setAttribute(GSXML.NAME_ATT, site_name + ":" + col_name); 873 ane.setAttribute(OAIXML.LASTMODIFIED, "" + c.getLastmodified()); 874 875 this.oai_collection_list.appendChild(ane); 876 //logger.info(GSXML.xmlNodeToString(oai_collection_list)); 877 } 878 this.collection_list.appendChild(e); 879 } 880 return true; 881 } else { 882 logger.error("Couldn't configure collection: "+ 883 col_name+"."); 884 return false; 885 } 886 } 887 protected Element getCollectionList() { 888 return collection_list; 889 } 890 891 892 protected boolean activateSiteByName(String site_name) { 893 logger.info("Activating site: "+site_name+"."); 894 895 // just in case this is a reactivation, deactivate this site first 896 deactivateModule(GSXML.SITE_ELEM, site_name); 897 File configFile = new File(GSFile.siteConfigFile(this.site_home)); 898 899 if (!configFile.exists() ) { 900 logger.error(" site config file: "+configFile.getPath()+" not found!"); 901 return false; 902 } 903 Document config_doc = this.converter.getDOM(configFile); 904 if (config_doc == null) { 905 logger.error(" couldn't parse site config file: "+configFile.getPath()); 906 return false; 907 } 908 Element config_elem = config_doc.getDocumentElement(); 909 910 Element config_site_list = (Element)GSXML.getChildByTagName(config_elem, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER); 911 if (config_site_list ==null ) { 912 logger.error("activateSite, no sites found"); 913 return false; 914 } 915 // this is a name to identify the current site in the Communicator 916 String local_site_name = config_site_list.getAttribute("localSiteName"); 917 if (local_site_name.equals("")) { 918 local_site_name = site_name; 919 } 920 921 Element this_site_elem = GSXML.getNamedElement(config_site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, site_name); 922 if (this_site_elem == null) { 923 logger.error("activateSite, site "+site_name+" not found"); 924 return false; 925 } 926 927 return activateSite(this_site_elem, local_site_name); 928 } 929 930 protected boolean activateServiceClusterByName(String cluster_name) { 931 return false; 932 933 } 934 935 protected boolean activateServiceRackByName(String module_name) { 936 return false; 937 } 938 939 protected boolean deactivateModule(String type, String name) { 940 941 if (this.module_map.containsKey(name)) { 942 943 logger.info(" deactivating "+name); 944 ModuleInterface m = (ModuleInterface)this.module_map.remove(name); 945 m.cleanUp(); // clean up any open files/connections etc - can cause trouble on windows 946 // also remove the xml bit from description list 947 if (type.equals(GSXML.COLLECTION_ELEM)) { 948 Element this_col = GSXML.getNamedElement(this.collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name); 949 if (this_col != null) { 950 this.collection_list.removeChild(this_col); 951 } 952 return true; 953 } else if (type.equals(GSXML.SERVICE_ELEM)) { 954 Element this_service = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, name); 955 if (this_service != null) { 956 this.service_list.removeChild(this_service); 957 } 958 return true; 959 } else if (type.equals(GSXML.CLUSTER_ELEM)) { 960 Element this_cluster = GSXML.getNamedElement(this.cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, name); 961 if (this_cluster != null) { 962 this.cluster_list.removeChild(this_cluster); 963 } 964 return true; 965 } else if (type.equals(GSXML.SITE_ELEM)) { 966 Element this_site = GSXML.getNamedElement(this.site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, name); 967 if (this_site != null) { 968 this.site_list.removeChild(this_site); 969 970 // also remove this sites colls, services, clusters etc 971 removeRemoteSiteInfo(this.collection_list, GSXML.COLLECTION_ELEM, name); 972 removeRemoteSiteInfo(this.cluster_list, GSXML.CLUSTER_ELEM, name); 973 removeRemoteSiteInfo(this.service_list, GSXML.SERVICE_ELEM, name); 974 } 975 } else { 976 logger.error(" couldn't deactivate coll"); 977 // couldn't do it 978 return false; 979 } 980 } 981 // else not deactivated 982 return false; 983 984 } 985 986 /** 987 * this looks through a list (module_list) of elements named module_name, 988 * and removes any whose names start with site_name 989 */ 990 protected void removeRemoteSiteInfo(Element module_list, 991 String module_name, 992 String site_name) { 993 994 NodeList modules = module_list.getElementsByTagName(module_name); 995 // will this work?? 996 for (int i=modules.getLength()-1; i>=0; i--) { 997 998 String name = ((Element)modules.item(i)).getAttribute(GSXML.NAME_ATT); 999 if (GSPath.getFirstLink(name).equals(site_name)) { 1000 module_list.removeChild(modules.item(i)); 1001 } 1002 } 1003 } 1091 1004 1092 1005 1093 }
Note:
See TracChangeset
for help on using the changeset viewer.