Changeset 4096


Ignore:
Timestamp:
2003-04-07T16:51:23+12:00 (21 years ago)
Author:
kjdon
Message:

rearranging stuff so that you can reconfigure all teh bits individually without having to restart tomcat - am part of teh way through

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/core/MessageRouter.java

    r4022 r4096  
    6060
    6161    /** site home - the home directory for the site */
    62     protected String site_home_=null;
     62    protected String site_home=null;
    6363    /** the http address for this site */
    64     protected String site_http_address_=null;
     64    protected String site_http_address=null;
    6565   
    6666    /** the global name for this site */
    67     protected String global_site_name_=null;
     67    protected String global_site_name=null;
    6868
    6969    /** map of names to Module objects */
    70     protected HashMap module_map_=null;
     70    protected HashMap module_map=null;
    7171
    7272    /** container Document to create XML Nodes */
    73     protected Document doc_=null;
     73    protected Document doc=null;
    7474    /** the full description of this site */
    75     protected Element description_ = null;
     75    //protected Element description = null;
    7676
    7777    // should these things be separated into local and remote??
    7878
    7979    /** list of collections that can be reached */
    80     protected Element collection_list_ = null;
     80    protected Element collection_list = null;
    8181    /** list of service clusters that can be reached */
    82     protected Element cluster_list_ = null;
    83     /** list of single services that can be reached
    84      * can we have single services? or chould all be part of a cluster */
    85     protected Element service_list_ = null;
    86 
     82    protected Element cluster_list = null;
     83    /** list of single services that can be reached */
     84    protected Element service_list = null;
    8785    /** list of sites that can be reached */
    88     protected Element site_list_ = null;
     86    protected Element site_list = null;
    8987
    9088    /** a converter class to parse XML and create Docs */
    91     protected XMLConverter converter_=null;
     89    protected XMLConverter converter=null;
    9290
    9391    //***************************************************************
     
    9795    /** constructor */
    9896    public MessageRouter() {
    99     module_map_ = new HashMap();
    100     converter_ = new XMLConverter();
    101     doc_ = converter_.newDOM();
    102    
    103    
    104     }
    105 
    106     /** site_home_ must be set before configure called */
     97    this.converter = new XMLConverter();
     98    this.doc = this.converter.newDOM();
     99   
     100   
     101    }
     102
     103    /** site_home must be set before configure called */
    107104    public void setSiteHome(String home) {
    108     site_home_ = home;
     105    this.site_home = home;
    109106    }
    110107
     
    112109     * configures the system
    113110     *
    114      * looks in site_home_/collect for collections, reads config file
    115      * site_home_/sitecfg.xml
     111     * looks in site_home/collect for collections, reads config file
     112     * site_home/sitecfg.xml
    116113     *
    117114     */
    118115    public boolean configure() {
    119116
    120     if (site_home_==null) {
    121         System.err.println("You must set site_home_ before calling configure");
     117    System.out.println("MessageRouter:configuring site");
     118
     119    if (this.site_home==null) {
     120        System.err.println("You must set site_home before calling configure");
    122121        return false;
    123122    }
    124 
    125         // start the description_ XML tree
    126     description_ = doc_.createElement(GSXML.RESPONSE_ELEM);
    127     description_.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
    128     description_.setAttribute(GSXML.FROM_ATT, "");
    129     collection_list_ = doc_.createElement(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER);
    130     cluster_list_ = doc_.createElement(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
    131     service_list_ = doc_.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
    132     site_list_ = doc_.createElement(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
    133 
    134 
     123   
    135124    // read thru own config file - create services and connect to sites
    136     File configFile = new File(GSFile.siteConfigFile(site_home_));
    137 
    138     if (configFile.exists() ) {
    139         configureSystem(configFile);
    140     }
    141     else {
     125    File configFile = new File(GSFile.siteConfigFile(this.site_home));
     126
     127    if (!configFile.exists() ) {
    142128        System.err.println("MessageRouter: site config file: "+configFile.getPath()+" not found!");
    143129        return false;
    144        
    145     }
    146 
    147     // read thru the collect directory and activate all the valid collections
    148     File collectDir = new File(GSFile.collectDir(site_home_));
    149     if (collectDir.exists()) {
    150         System.out.println("Reading thru directory "+collectDir.getPath()+" to find collections.");
    151         File[] contents = collectDir.listFiles();
    152         for (int i=0; i<contents.length;i++) {
    153         if(contents[i].isDirectory()) {
    154            
    155             String colName = contents[i].getName();
    156             if (!colName.startsWith("CVS")) {
    157             activateCollection(colName);
    158             }
    159         }
    160         }
    161     } // collectDir
    162    
    163     description_.appendChild(collection_list_);
    164 
     130    }
     131   
     132    this.module_map = new HashMap();
     133
     134    Element config = this.converter.getDOM(configFile).getDocumentElement();
     135   
     136    Element local_site_name = (Element)GSXML.getChildByTagName(config, GSXML.SITE_NAME_ELEM);
     137    if (local_site_name == null) {
     138        System.err.println("MessageRouter configure error:no site name in config file");
     139        return false;
     140    } else {
     141        this.global_site_name = local_site_name.getAttribute(GSXML.VALUE_ATT);
     142    }
     143   
     144    Element http_address = (Element)GSXML.getChildByTagName(config, GSXML.SITE_HTTP_ADDRESS_ELEM);
     145    if (http_address == null) {
     146        System.err.println("MessageRouter configure error: no http address  in config file");
     147        return false;
     148    } else {
     149        this.site_http_address = http_address.getAttribute(GSXML.VALUE_ATT);
     150    }
     151   
     152    // load up the services
     153    Element service_rack_list = (Element)GSXML.getChildByTagName(config, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
     154    configureServices(service_rack_list);
     155   
     156    // load up the service clusters
     157    Element cluster_list = (Element)GSXML.getChildByTagName(config, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);   
     158    configureClusters(cluster_list);
     159
     160    // load up the collections
     161    configureCollections();
     162
     163    // load up the external sites - this also adds their services/clusters/collections to the other lists - so must be done last
     164    Element site_list = (Element)GSXML.getChildByTagName(config, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
     165    configureSites(site_list);
     166   
     167       
    165168    return true;
    166169   
    167170    }
    168 
     171   
     172   
    169173    /**
    170174     * Process an XML request - as a String
     
    176180    public String process(String xml_in) {
    177181   
    178     Document doc = converter_.getDOM(xml_in);
     182    Document doc = this.converter.getDOM(xml_in);
    179183   
    180184    Element result = process(doc.getDocumentElement());
    181     return converter_.getString(result);
     185    return this.converter.getString(result);
    182186    }
    183187   
     
    191195    public Element process(Element message) {
    192196
    193     //System.out.println("MR received request");
    194     //System.out.println(converter_.getString(message));
     197    System.out.println("MR received request");
     198    System.out.println(this.converter.getString(message));
    195199               
    196200    // check that its a correct message tag
     
    202206    NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
    203207
    204     Element mainResult = doc_.createElement(GSXML.MESSAGE_ELEM);
     208    Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
    205209   
    206210    // empty request
     
    222226        // its a message for the message router
    223227        result = processMessage(req);
    224         mainResult.appendChild(doc_.importNode(result, true));
     228        mainResult.appendChild(this.doc.importNode(result, true));
    225229        } else {
    226230        // find the module to pass it on to
     
    229233        mess.appendChild(req);
    230234        String obj = GSPath.getFirstLink(path);
    231         if (module_map_.containsKey(obj)) {
    232             result = ((ModuleInterface)module_map_.get(obj)).process(mess);
     235        if (this.module_map.containsKey(obj)) {
     236            result = ((ModuleInterface)this.module_map.get(obj)).process(mess);
    233237            if (result !=null ) {
    234238            // append the contents of the message to the mainResult - there will only be one response at this stage
    235             mainResult.appendChild(doc_.importNode(GSXML.getChildByTagName(result, GSXML.RESPONSE_ELEM), true));
     239            mainResult.appendChild(this.doc.importNode(GSXML.getChildByTagName(result, GSXML.RESPONSE_ELEM), true));
    236240            } else {
    237241            System.err.println("MessageRouter Error: request had null result!");
    238242            }
    239243        } else {   
    240             System.err.println("MessageRouter Error: request has illegal module name in:\n"+converter_.getString(req));
     244            System.err.println("MessageRouter Error: request has illegal module name in:\n"+this.converter.getString(req));
    241245        }
    242246        }
     
    244248    } // for each request
    245249   
    246     //System.out.println("MR returned response");
    247     //System.out.println(converter_.getString(mainResult));
     250    System.out.println("MR returned response");
     251    System.out.println(this.converter.getString(mainResult));
    248252
    249253    return mainResult;
     
    251255    }
    252256   
    253     //********************************************************************
     257    // ********************************************************************
    254258    // auxiliary configure methods
    255259    // *******************************************************************
    256260
    257     /**
    258      * configures the message router based on the site config file
    259      * - siteConfig.xml
    260      *
    261      */
    262     protected boolean configureSystem(File file) {
    263 
    264     System.out.println("MessageRouter:configuring site");
    265     Element config = converter_.getDOM(file).getDocumentElement();
    266 
    267     Element local_site_name = (Element)GSXML.getChildByTagName(config,
    268                          GSXML.SITE_NAME_ELEM);
    269     if (local_site_name == null) {
    270         System.err.println("MessageRouter configure error:no site name in config file");
    271         return false;
    272     } else {
    273         global_site_name_ = local_site_name.getAttribute(GSXML.VALUE_ATT);
    274     }
    275    
    276     Element http_address = (Element)GSXML.getChildByTagName(config,
    277                         GSXML.SITE_HTTP_ADDRESS_ELEM);
    278     if (http_address == null) {
    279         System.err.println("MessageRouter configure error: no http address  in config file");
    280         return false;
    281     } else {
    282         site_http_address_ = http_address.getAttribute(GSXML.VALUE_ATT);
    283     }
    284    
     261
     262    protected boolean configureServices(Element service_rack_list) {
     263
     264    this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
     265
     266    // load up the individual services
     267    System.out.println("loading service modules...");
     268   
     269    if (service_rack_list == null) {
     270        System.out.println("... none to be loaded");
     271        return true;
     272    }
     273   
     274    NodeList service_racks = service_rack_list.getElementsByTagName(GSXML.SERVICE_CLASS_ELEM);
     275    if (service_racks.getLength()==0) {
     276        System.out.println("... none to be loaded");
     277        return true;
     278    }
     279       
     280    Element service_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
     281    Element service_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", "");
     282    service_message.appendChild(service_request);
     283    service_request.setAttribute(GSXML.INFO_ATT, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
     284       
     285    for(int i=0; i<service_racks.getLength(); i++) {
     286        Element n = (Element)service_racks.item(i);
     287        String service_name = n.getAttribute(GSXML.NAME_ATT);
     288        System.out.println("..."+service_name);
     289        try {
     290        ServiceRack s = (ServiceRack)Class.forName("org.greenstone.gsdl3.service."+service_name).newInstance();
     291        s.setSiteHome(this.site_home);
     292        s.setSiteAddress(this.site_http_address);
     293        s.setMessageRouter(this);
     294        // pass the XML node to the service for service configuration
     295        s.configure(n, null);
     296
     297        // find out the supported services for this service module
     298        Element service_response = (Element) s.process(service_message);
     299        NodeList services = service_response.getElementsByTagName(GSXML.SERVICE_ELEM);
     300        if (services.getLength()==0) {
     301            System.err.println("MessageRouter configure error: serviceRack "+service_name+" has no services!");
     302        } else {
     303            for (int j=0; j<services.getLength();j++) {
     304            String service = ((Element)services.item(j)).getAttribute(GSXML.NAME_ATT);
     305            this.module_map.put(service, s);
     306           
     307                // add short info to service_list_ XML
     308            this.service_list.appendChild(this.doc.importNode(services.item(j), true));
     309            }
     310        }
     311        } catch (Exception e ) {
     312        System.err.println("MessageRouter configure exception:  in ServiceRack class specification:  "+ e.getMessage());
     313        e.printStackTrace();
     314        }
     315    } // for each service module
     316    return true;
     317    }
     318   
     319    protected boolean configureClusters(Element cluster_list) {
     320
     321    this.cluster_list = this.doc.createElement(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
    285322    // load up the service clusters
    286323    System.out.println("loading service clusters ...");
    287     Node cluster_list = GSXML.getChildByTagName(config,
    288                  GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
    289324    if (cluster_list == null) {
    290325        System.out.println("... none to be loaded");
    291     } else {
    292         NodeList service_clusters = cluster_list.getChildNodes();
    293         if (service_clusters.getLength()==0) {
    294         System.out.println("... none to be loaded");
    295         } else {
    296        
    297         for (int i=0; i<service_clusters.getLength(); i++) {
    298             if (service_clusters.item(i).getNodeName().equals(GSXML.CLUSTER_ELEM)) {
    299             Element cluster = (Element)service_clusters.item(i);
    300             String name = cluster.getAttribute(GSXML.NAME_ATT);
    301             System.out.println("..."+name);
    302             ServiceCluster sc = new ServiceCluster();
    303             sc.setSiteHome(site_home_);
    304             sc.setSiteAddress(site_http_address_);
    305             sc.setClusterName(name);
    306             sc.setMessageRouter(this);
    307             sc.configure(cluster);
    308             module_map_.put(name, sc);
    309             //add short info to
    310             Element e = doc_.createElement(GSXML.CLUSTER_ELEM);
    311             e.setAttribute(GSXML.NAME_ATT, name);
    312             cluster_list_.appendChild(e);
    313 
     326        return true;
     327    }
     328    NodeList service_clusters = cluster_list.getElementsByTagName(GSXML.CLUSTER_ELEM);
     329    if (service_clusters.getLength()==0) {
     330        System.out.println("... none to be loaded");
     331        return true;
     332    }
     333   
     334    for (int i=0; i<service_clusters.getLength(); i++) {
     335        Element cluster = (Element)service_clusters.item(i);
     336        String name = cluster.getAttribute(GSXML.NAME_ATT);
     337        System.out.println("..."+name);
     338        ServiceCluster sc = new ServiceCluster();
     339        sc.setSiteHome(this.site_home);
     340        sc.setSiteAddress(this.site_http_address);
     341        sc.setClusterName(name);
     342        sc.setMessageRouter(this);
     343        sc.configure(cluster);
     344        this.module_map.put(name, sc); // this replaces the old one if there was one already present
     345        //add short info to cluster list
     346        Element e = this.doc.createElement(GSXML.CLUSTER_ELEM);
     347        e.setAttribute(GSXML.NAME_ATT, name);
     348        this.cluster_list.appendChild(e);
     349       
     350    }
     351    return true;
     352    }
     353   
     354    protected boolean configureCollections() {
     355
     356    this.collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER);
     357
     358    // read thru the collect directory and activate all the valid collections
     359    File collectDir = new File(GSFile.collectDir(this.site_home));
     360    if (collectDir.exists()) {
     361        System.out.println("Reading thru directory "+collectDir.getPath()+" to find collections.");
     362        File[] contents = collectDir.listFiles();
     363        for (int i=0; i<contents.length;i++) {
     364        if(contents[i].isDirectory()) {
     365           
     366            String colName = contents[i].getName();
     367            if (!colName.startsWith("CVS")) {
     368            activateCollection(colName);
    314369            }
    315370        }
    316371        }
    317        
    318     }
    319 
    320     // load up the individual services
    321     System.out.println("loading service modules...");
    322    
    323     Node module_list = GSXML.getChildByTagName(config,
    324                            GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
    325     if (module_list == null) {
    326         System.out.println("... none to be loaded");
    327     } else {
    328         NodeList service_impls = module_list.getChildNodes();
    329         if (service_impls.getLength()==0) {
    330         System.out.println("... none to be loaded");
    331         } else {
    332        
    333         Element service_message = doc_.createElement(GSXML.MESSAGE_ELEM);
    334         Element service_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, "", "");
    335         service_message.appendChild(service_request);
    336         service_request.setAttribute(GSXML.INFO_ATT, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
    337        
    338         for(int i=0; i<service_impls.getLength(); i++) {
    339             if (service_impls.item(i).getNodeName().equals(GSXML.SERVICE_CLASS_ELEM)) {
    340             Element n = (Element)service_impls.item(i);
    341             String service_name = n.getAttribute(GSXML.NAME_ATT);
    342             System.out.println("..."+service_name);
    343             try {
    344                 ServiceRack s = (ServiceRack)Class.forName("org.greenstone.gsdl3.service."+service_name).newInstance();
    345                 s.setSiteHome(site_home_);
    346                 s.setSiteAddress(site_http_address_);
    347                 s.setMessageRouter(this);
    348                 // pass the XML node to the service for service configuration
    349                 s.configure(n, null);
    350 
    351                 // find out the supported services for this service module
    352                 Element service_response = (Element) s.process(service_message);
    353                 NodeList services = service_response.getElementsByTagName(GSXML.SERVICE_ELEM);
    354                 if (services.getLength()==0) {
    355                 System.err.println("MessageRouter configure error: serviceRack "+service_name+" has no services!");
    356                 } else {
    357                 for (int j=0; j<services.getLength();j++) {
    358                     String service = ((Element)services.item(j)).getAttribute(GSXML.NAME_ATT);
    359                     module_map_.put(service, s);
    360                    
    361                 // add short info to service_list_ XML
    362                     service_list_.appendChild(doc_.importNode(services.item(j), true));
    363                 }
    364                 }
    365             } catch (Exception e ) {
    366                 System.err.println("MessageRouter configure exception:  in ServiceRack class specification:  "+ e.getMessage());
    367                 e.printStackTrace();
    368             }
    369             } // if
    370         } // for each service module
    371         }
    372     }
    373    
     372    } // collectDir
     373    return true;
     374    }
     375
     376    protected boolean configureSites(Element site_list) {
     377
     378    this.site_list = this.doc.createElement(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
    374379    // load up the sites
    375380    System.out.println("loading external sites...");
    376     Node site_list = GSXML.getChildByTagName(config, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
    377381    if (site_list ==null ) {
    378382        System.out.println("...none found");
    379     } else {
    380         NodeList sites = site_list.getChildNodes();
    381         if (sites.getLength()==0) {
    382         System.out.println("...none found");
     383        return true;
     384    }
     385    NodeList sites = site_list.getElementsByTagName(GSXML.SITE_ELEM);
     386    if (sites.getLength()==0) {
     387        System.out.println("...none found");
     388        return true;
     389    }
     390    for (int i=0; i<sites.getLength(); i++) {
     391        Element s = (Element)sites.item(i);
     392        Communicator comm=null;
     393        String type = s.getAttribute(GSXML.TYPE_ATT);
     394        String name = s.getAttribute(GSXML.NAME_ATT);
     395        if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
     396        comm = new SOAPCommunicator();
     397        if (comm.configure(s)) {
     398            comm.setLocalSiteName(this.global_site_name);
     399           
     400            // add to map of modules
     401            this.module_map.put(name, comm);
     402            this.site_list.appendChild(this.doc.importNode(s, true));
     403            // need to get collection list and service
     404            // list from here- this may introduce a
     405            // configure problem - leave as  is for now,
     406            // but fix up later - kjdon
     407            if (!getRemoteSiteInfo(comm, name)) {
     408            System.err.println("couldn't get info from site "+name);
     409            }
     410        } else {
     411            System.err.println("couldn't configure soap site:"+name);
     412        }
     413       
    383414        } else {
    384        
    385         for (int i=0; i<sites.getLength(); i++) {
    386             if (sites.item(i).getNodeName().equals(GSXML.SITE_ELEM)) {
    387             Element s = (Element)sites.item(i);
    388             Communicator comm=null;
    389             String type = s.getAttribute(GSXML.TYPE_ATT);
    390             String name = s.getAttribute(GSXML.NAME_ATT);
    391             if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
    392                 comm = new SOAPCommunicator();
    393                 if (comm.configure(s)) {
    394                 comm.setLocalSiteName(global_site_name_);
    395                
    396                 // add to map of modules
    397                 module_map_.put(name, comm);
    398                 site_list_.appendChild(doc_.importNode(s, true));
    399                 // need to get collection list and service
    400                 // list from here- this may introduce a
    401                 // configure problem - leave as  is for now,
    402                 // but fix up later - kjdon
    403                 if (!getRemoteSiteInfo(comm, name)) {
    404                     System.err.println("couldn't get info from site "+name);
    405                 }
    406                 } else {
    407                 System.err.println("couldn't configure soap site:"+name);
    408                 }
    409                
    410             } else {
    411                 System.err.print("cant talk to server of type:"+type);
    412                 System.err.println(", so not making a connection to "+name);
    413             }
    414            
    415             }
    416         }
    417         }
    418     }
    419     // add children to description_
    420     description_.appendChild(cluster_list_);
    421     description_.appendChild(service_list_);
    422     description_.appendChild(site_list_);
    423        
     415        System.err.print("cant talk to server of type:"+type);
     416        System.err.println(", so not making a connection to "+name);
     417        }
     418       
     419    }
    424420    return true;
    425 
    426     }
    427 
     421    }
     422   
    428423   
    429424    /** get site info from external site
     
    437432    System.out.println("MessageRouter: getting info from site:"+site_name);
    438433
    439     Element info_request = doc_.createElement(GSXML.MESSAGE_ELEM);
    440     Element req = doc_.createElement(GSXML.REQUEST_ELEM);
     434    Element info_request = this.doc.createElement(GSXML.MESSAGE_ELEM);
     435    Element req = this.doc.createElement(GSXML.REQUEST_ELEM);
    441436    info_request.appendChild(req);
    442437    req.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
     
    457452        // other collections ??
    458453        e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(col_name, site_name));
    459         collection_list_.appendChild(doc_.importNode(e, true));
     454        this.collection_list.appendChild(this.doc.importNode(e, true));
    460455        }
    461456    }
     
    468463        String serv_name = e.getAttribute(GSXML.NAME_ATT);
    469464        e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(serv_name, site_name));
    470         service_list_.appendChild(doc_.importNode(e, true));
     465        this.service_list.appendChild(this.doc.importNode(e, true));
    471466        }
    472467    }
     
    479474        String clus_name = e.getAttribute(GSXML.NAME_ATT);
    480475        e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(clus_name, site_name));
    481         cluster_list_.appendChild(doc_.importNode(e, true));
     476        this.cluster_list.appendChild(this.doc.importNode(e, true));
    482477        }
    483478    }
     
    485480    }
    486481
    487 
    488     /** creates and configures a new collection
    489      *
    490      *@param col_name the name of the collection
    491      *@return true if collection created ok
    492      */
    493     protected boolean activateCollection(String col_name) {
    494 
    495     System.out.println("MessageRouter:Activating collection: "+col_name+".");
    496              
    497     Collection c = new Collection();
    498     c.setCollectionName(col_name);
    499     c.setSiteHome(site_home_);
    500     c.setSiteAddress(site_http_address_);
    501     if (c.configure()) {
    502         // this could be a reactivation, so delete the old version
    503         deactivate(GSXML.COLLECTION_ELEM, col_name);
    504         // add to list of collections
    505         module_map_.put(col_name, c);
    506         //add short description_ to collection_list_
    507         Element e = doc_.createElement(GSXML.COLLECTION_ELEM);
    508         e.setAttribute(GSXML.NAME_ATT, col_name);
    509         collection_list_.appendChild(e);
    510         return true;
    511     } else {
    512         System.err.println("MessageRouter:Couldn't configure collection: "+
    513                    col_name+".");
    514         return false;
    515     }
    516    
    517     }
    518 
    519     protected boolean activateServiceCluster(String cluster_name) {
    520     return false;
    521 
    522     }
    523 
    524     protected boolean activateServiceRack(String module_name) {
    525     return false;
    526     }
    527482
    528483    //*****************************************************************
     
    539494    // message for self, should be type=describe/configure at this stage
    540495    String type = req.getAttribute(GSXML.TYPE_ATT);
    541     Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
     496    Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
    542497    response.setAttribute(GSXML.FROM_ATT, "");
    543498    if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
     499        response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
    544500        // check the param list
    545501        Element param_list = (Element) GSXML.getChildByTagName(req, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    546502        if (param_list == null) {
    547         // no params - just return teh full description
    548         return description_; // this is already in a response elem
    549         }
    550         System.out.println("poarams found, getting subset");
    551         // use the new response with the appropriate info
    552         response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
     503        response.appendChild(this.collection_list);
     504        response.appendChild(this.cluster_list);
     505        response.appendChild(this.site_list);
     506        response.appendChild(this.service_list);
     507        return response;
     508        }
     509        System.out.println("params found, getting subset");
    553510       
    554511        // go through the param list and see what components are wanted
     
    563520            String info = param.getAttribute(GSXML.VALUE_ATT);
    564521            if (info.equals(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER)) {
    565             response.appendChild(collection_list_);
     522            response.appendChild(this.collection_list);
    566523           
    567524            } else if (info.equals(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER)) {
    568             response.appendChild(cluster_list_);
     525            response.appendChild(this.cluster_list);
    569526           
    570527            } else if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
    571             return response;
     528            response.appendChild(this.service_list);
    572529            } else if (info.equals(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER)) {
    573             response.appendChild(site_list_);
     530            response.appendChild(this.site_list);
    574531            }
    575532        }
     
    578535        return response;
    579536       
    580     } else if (type.equals(GSXML.REQUEST_TYPE_CONFIGURE)) {
     537    }
     538
     539    if (type.equals(GSXML.REQUEST_TYPE_CONFIGURE)) {
    581540       
    582541        // a list of configure requests - should put any error messages
     
    601560            }
    602561        } else if (action.equals(GSXML.CONFIG_ACTION_DEACTIVATE)) {
    603             deactivate(module_type, module_name);
     562            deactivateModule(module_type, module_name);
     563           
     564        }
     565        }
     566       
     567        return response;
     568       
     569    }
     570   
     571    if (type.equals(GSXML.REQUEST_TYPE_SYSTEM)) {
     572       
     573        // a list of system requests - should put any error messages
     574        // or success messages into response
     575        NodeList commands = req.getElementsByTagName("system");//GSXML.SYSTEM_ELEM);
     576        Element site_config_elem = null;
     577        for (int i=0; i<commands.getLength(); i++) {
     578        // all the commands should be Elements
     579        Element elem = (Element)commands.item(i);
     580        String action = elem.getAttribute(GSXML.TYPE_ATT);
     581        if (action.equals("configure")) {
     582            String subset = elem.getAttribute("subset");
     583            if (subset.equals("")) {
     584            // need to reconfigure the MR
     585            this.configure();
     586            Element s = GSXML.createTextElement(this.doc, "status", "mr reconfigured");
     587            response.appendChild(s);
    604588           
    605         }
    606         }
    607        
     589            } else {
     590            // else it a specific request
     591            boolean success = false;
     592            if (subset.equals(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER)) {
     593                success = configureCollections();
     594            } else {
     595
     596                // need the site config file
     597                if (site_config_elem==null) {
     598               
     599                File configFile = new File(GSFile.siteConfigFile(this.site_home));
     600                if (!configFile.exists() ) {
     601                    System.err.println("MessageRouter: site config file: "+configFile.getPath()+" not found!");
     602                    continue;
     603                }
     604                site_config_elem  = this.converter.getDOM(configFile).getDocumentElement();
     605                }
     606                if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
     607                Element service_rack_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
     608               
     609                success = configureServices(service_rack_list);
     610                } else if (subset.equals(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER)) {
     611                Element cluster_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER); 
     612               
     613                success = configureClusters(cluster_list);
     614                } else if (subset.equals(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER)) {
     615                Element site_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
     616                success = configureSites(site_list);
     617                }
     618            }
     619            String message=null;
     620            if (success) {
     621                message = subset + "reconfigured successfully";
     622            } else {
     623                message = "Error in reconfiguring "+subset;
     624            }
     625            Element s = GSXML.createTextElement(this.doc, "status", message);
     626            response.appendChild(s);
     627            }
     628           
     629           
     630        } else {
     631            String module_name = elem.getAttribute("moduleName");
     632            String module_type = elem.getAttribute("moduleType");
     633
     634            if (action.equals("deactivate")) {
     635            deactivateModule(module_type, module_name);
     636            Element s = GSXML.createTextElement(this.doc, "status", module_type+": "+module_name+" deactivated");
     637            response.appendChild(s);
     638            }
     639        }
     640        } // for all commands
    608641        return response;
    609        
     642           
     643
    610644    }
    611645    // if get here something has gone wrong
    612646    System.err.println("MessageRouter: cant process request:");
    613     System.err.println(converter_.getString(req));
     647    System.err.println(this.converter.getString(req));
    614648    return null;
    615649       
    616650    }
    617651
    618     protected boolean deactivate(String type, String name) {
    619    
    620     if (module_map_.containsKey(name)) {
     652    // ****************************************************
     653    // other methods
     654    // ****************************************************
     655
     656    /** creates and configures a new collection
     657     *
     658     *@param col_name the name of the collection
     659     *@return true if collection created ok
     660     */
     661    protected boolean activateCollection(String col_name) {
     662
     663    System.out.println("MessageRouter:Activating collection: "+col_name+".");
     664             
     665    Collection c = new Collection();
     666    c.setCollectionName(col_name);
     667    c.setSiteHome(this.site_home);
     668    c.setSiteAddress(this.site_http_address);
     669    if (c.configure()) {
     670        // this could be a reactivation, so delete the old version
     671        deactivateModule(GSXML.COLLECTION_ELEM, col_name);
     672        // add to list of collections
     673        this.module_map.put(col_name, c);
     674        //add short description_ to collection_list_
     675        Element e = this.doc.createElement(GSXML.COLLECTION_ELEM);
     676        e.setAttribute(GSXML.NAME_ATT, col_name);
     677        this.collection_list.appendChild(e);
     678        return true;
     679    } else {
     680        System.err.println("MessageRouter:Couldn't configure collection: "+
     681                   col_name+".");
     682        return false;
     683    }
     684   
     685    }
     686
     687    protected boolean activateServiceCluster(String cluster_name) {
     688    return false;
     689
     690    }
     691
     692    protected boolean activateServiceRack(String module_name) {
     693    return false;
     694    }
     695
     696    protected boolean deactivateModule(String type, String name) {
     697   
     698    if (this.module_map.containsKey(name)) {
    621699
    622700        System.out.println("MessageRouter: deactivating "+name);
    623         module_map_.remove(name);
     701        this.module_map.remove(name);
    624702
    625703        // also remove the xml bit from description list
    626704        if (type.equals(GSXML.COLLECTION_ELEM)) {
    627         Element this_col = GSXML.getNamedElement(collection_list_, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
    628         collection_list_.removeChild(this_col);
     705        Element this_col = GSXML.getNamedElement(this.collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
     706        if (this_col != null) {
     707            this.collection_list.removeChild(this_col);
     708        }
    629709        return true;
    630710        } else if (type.equals(GSXML.SERVICE_ELEM)) {
    631         Element this_service = GSXML.getNamedElement(service_list_, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, name);
    632         service_list_.removeChild(this_service);
     711        Element this_service = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, name);
     712        if (this_service != null) {
     713            this.service_list.removeChild(this_service);
     714        }
    633715        return true;
    634716        } else if (type.equals(GSXML.CLUSTER_ELEM)) {
    635         Element this_cluster = GSXML.getNamedElement(cluster_list_, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, name);
    636         cluster_list_.removeChild(this_cluster);
     717        Element this_cluster = GSXML.getNamedElement(this.cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, name);
     718        if (this_cluster != null) {
     719            this.cluster_list.removeChild(this_cluster);
     720        }
    637721        return true;
     722        } else if (type.equals(GSXML.SITE_ELEM)) {
     723        Element this_site = GSXML.getNamedElement(this.site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, name);
     724        if (this_site != null) {
     725            this.site_list.removeChild(this_site);
     726
     727            // also need to remove from the other lists any
     728            // services/clusters/collections that belong to this site
     729        }
    638730        } else {
    639731        System.err.println("couldn't deactivate coll");
     
    642734        }
    643735    }
    644     // else not activated
     736    // else not deactivated
    645737    return false;
    646738
Note: See TracChangeset for help on using the changeset viewer.