Changeset 10292 for trunk/gsdl3


Ignore:
Timestamp:
2005-07-25T16:10:42+12:00 (19 years ago)
Author:
kjdon
Message:

changed the site loading/reloading stuff a bit. now get GSDL3Home from GlobalProperties, site_name is set by instantiator, not site_home.

File:
1 edited

Legend:

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

    r9914 r10292  
    3434// other java classes
    3535import java.io.File;
    36 import java.util.HashMap;
    37 import java.util.Iterator;
    3836import java.io.Reader;
    3937import java.io.StringReader;
    4038import java.net.Authenticator;
    4139import java.net.PasswordAuthentication;     
     40import java.util.HashMap;
     41import java.util.Iterator;
     42import java.util.Properties;
    4243
    4344/**
     
    6162public class MessageRouter implements  ModuleInterface {
    6263
     64    /** the (directory) name of the site */
     65    protected String site_name = null;
    6366    /** site home - the home directory for the site */
    6467    protected String site_home=null;
     
    6669    protected String site_http_address=null;
    6770   
    68     /** the global name for this site */
    69     protected String global_site_name=null;
    70 
    7171    /** map of names to Module objects */
    7272    protected HashMap module_map=null;
     
    8686    /** list of sites that can be reached */
    8787    protected Element site_list = null;
    88 
     88   
     89   
    8990    /** a converter class to parse XML and create Docs */
    9091    protected XMLConverter converter=null;
    9192
     93   
    9294    //***************************************************************
    9395    // public methods
     
    108110    }
    109111    }
    110     /** site_home must be set before configure called */
    111     public void setSiteHome(String home) {
    112     this.site_home = home;
    113     }
    114 
     112
     113    /** site_name must be set before configure is called */
     114    public void setSiteName(String site_name) {
     115    this.site_name = site_name;
     116    }
    115117    /**
    116118     * configures the system
    117119     *
    118120     * looks in site_home/collect for collections, reads config file
    119      * site_home/sitecfg.xml
     121     * site_home/siteConfig.xml
    120122     *
    121123     */
     
    124126    System.out.println("MessageRouter:configuring site");
    125127
    126     if (this.site_home==null) {
    127         System.err.println("MessageRouter: You must set site_home before calling configure");
     128    if (this.site_name==null) {
     129        System.err.println("MessageRouter: You must set site_name before calling configure");
    128130        return false;
    129131    }
    130    
     132    this.site_home = GSFile.siteHome(GlobalProperties.getGSDL3Home(), this.site_name);
     133    this.site_http_address = GlobalProperties.getGSDL3WebAddress()+"/sites/"+this.site_name;
     134   
     135    // are we behind a firewall?? - is there a better place to set up the proxy?
     136    String host = GlobalProperties.getProperty("proxy.host");
     137    String port = GlobalProperties.getProperty("proxy.port");
     138    final String user = GlobalProperties.getProperty("proxy.user");
     139    final String passwd = GlobalProperties.getProperty("proxy.password");
     140     
     141    if (host != null && !host.equals("") && port !=null && !port.equals("")) {
     142        System.setProperty("http.proxyType", "4");
     143        System.setProperty("http.proxyHost", host);
     144        System.setProperty("http.proxyPort", port);
     145        System.setProperty("http.proxySet", "true");
     146        // have we got a user/password?
     147        if (user != null && !user.equals("") && passwd != null && !passwd.equals("")) {
     148        try {
     149            // set up the authenticator
     150            Authenticator.setDefault(new Authenticator(){
     151                protected PasswordAuthentication getPasswordAuthentication(){
     152                return new PasswordAuthentication(user, new String(passwd).toCharArray());
     153                }
     154            });
     155           
     156        } catch (Exception e) {
     157            System.err.println("MessageRouter Error: couldn't set up an authenticator the proxy");
     158           
     159        }
     160        }
     161    }
     162
    131163    // read thru own config file - create services and connect to sites
    132164    File configFile = new File(GSFile.siteConfigFile(this.site_home));
     
    137169    }
    138170   
    139     this.module_map = new HashMap();
    140 
    141171    Document config_doc = this.converter.getDOM(configFile);
    142172    if (config_doc == null) {
     
    144174        return false;
    145175    }
     176   
    146177    Element config_elem = config_doc.getDocumentElement();
    147178   
    148     Element local_site_name = (Element)GSXML.getChildByTagName(config_elem, GSXML.SITE_NAME_ELEM);
    149     if (local_site_name == null) {
    150         System.err.println("MessageRouter configure error:no site name in config file");
    151         return false;
    152     } else {
    153         this.global_site_name = local_site_name.getAttribute(GSXML.VALUE_ATT);
    154     }
    155    
    156     Element http_address = (Element)GSXML.getChildByTagName(config_elem, GSXML.SITE_HTTP_ADDRESS_ELEM);
    157     if (http_address == null) {
    158         System.err.println("MessageRouter configure error: no http address  in config file");
    159         return false;
    160     } else {
    161         this.site_http_address = http_address.getAttribute(GSXML.VALUE_ATT);
    162     }
    163 
    164     Element proxy = (Element)GSXML.getChildByTagName(config_elem, "proxy");
    165     if (proxy != null) {
    166         String host = proxy.getAttribute("host");
    167         String port = proxy.getAttribute("port");
    168         final String user = proxy.getAttribute("user");
    169         final String passwd = proxy.getAttribute("password");
    170         if (host.equals("") || port.equals("") || user.equals("")||passwd.equals("")) {
    171         System.err.println("MessageRouter.configure Error: A proxy was specified in the config file, but attributes host, port, user, password were not all present");
    172         } else {
    173         try {
    174             // set up the proxy
    175             System.setProperty("http.proxyType", "4");
    176             System.setProperty("http.proxyHost", host);
    177             System.setProperty("http.proxyPort", port);
    178             System.setProperty("http.proxySet", "true");
    179             Authenticator.setDefault(new Authenticator(){
    180                 protected PasswordAuthentication getPasswordAuthentication(){
    181                 return new PasswordAuthentication(user, new
    182                     String(passwd).toCharArray());
    183                 }
    184             });
    185            
    186         } catch (Exception e) {
    187             System.err.println("MessageRouter Error: couldn't set up the proxy");
    188         }
    189         }
    190     }
    191                      
     179    this.module_map = new HashMap();
     180   
    192181    // load up the services
    193182    Element service_rack_list = (Element)GSXML.getChildByTagName(config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
     
    379368    }
    380369   
    381     protected boolean configureClusters(Element cluster_list) {
     370    protected boolean configureClusters(Element config_cluster_list) {
    382371
    383372    this.cluster_list = this.doc.createElement(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
    384373    // load up the service clusters
    385374    System.out.println("loading service clusters ...");
    386     if (cluster_list == null) {
     375    if (config_cluster_list == null) {
    387376        System.out.println("... none to be loaded");
    388377        return true;
    389378    }
    390     NodeList service_clusters = cluster_list.getElementsByTagName(GSXML.CLUSTER_ELEM);
     379    NodeList service_clusters = config_cluster_list.getElementsByTagName(GSXML.CLUSTER_ELEM);
    391380    if (service_clusters.getLength()==0) {
    392381        System.out.println("... none to be loaded");
     
    428417            String colName = contents[i].getName();
    429418            if (!colName.startsWith("CVS")) {
    430             activateCollection(colName);
     419            activateCollectionByName(colName);
    431420            }
    432421        }
     
    436425    }
    437426
    438     protected boolean configureSites(Element site_list) {
     427    protected boolean configureSites(Element config_site_list) {
    439428
    440429    this.site_list = this.doc.createElement(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
    441430    // load up the sites
    442431    System.out.println("loading external sites...");
    443     if (site_list ==null ) {
     432    if (config_site_list ==null ) {
    444433        System.out.println("...none found");
    445434        return true;
    446435    }
    447     NodeList sites = site_list.getElementsByTagName(GSXML.SITE_ELEM);
     436
     437    NodeList sites = config_site_list.getElementsByTagName(GSXML.SITE_ELEM);
    448438    if (sites.getLength()==0) {
    449439        System.out.println("...none found");
    450440        return true;
    451441    }
     442
     443    // this is a name to identify the current site in the Communicator
     444    String local_site_name = config_site_list.getAttribute(GSXML.LOCAL_SITE_NAME_ATT);
     445    if (local_site_name.equals("")) {
     446        local_site_name = site_name;
     447    }
     448
    452449    for (int i=0; i<sites.getLength(); i++) {
    453450        Element s = (Element)sites.item(i);
    454         Communicator comm=null;
    455         String type = s.getAttribute(GSXML.TYPE_ATT);
    456         String name = s.getAttribute(GSXML.NAME_ATT);
    457         if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
    458         comm = new SOAPCommunicator();
    459         if (comm.configure(s)) {
    460             comm.setLocalSiteName(this.global_site_name);
    461            
    462             // add to map of modules
    463             this.module_map.put(name, comm);
    464             this.site_list.appendChild(this.doc.importNode(s, true));
    465             // need to get collection list and service
    466             // list from here- if the site isn't up yet, the site will
    467             // have to be added later
    468             if (!getRemoteSiteInfo(comm, name)) {
    469             System.err.println("MessageRouter: couldn't get info from site "+name);
    470             }
    471         } else {
    472             System.err.println("MessageRouter: couldn't configure soap site:"+name);
    473         }
     451        activateSite(s, local_site_name);       
     452    }
     453    return true;
     454    }
     455   
     456    protected boolean activateSite(Element site_elem, String local_site_name) {
     457    Communicator comm=null;
     458    String type = site_elem.getAttribute(GSXML.TYPE_ATT);
     459    String name = site_elem.getAttribute(GSXML.NAME_ATT);
     460    if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
     461        comm = new SOAPCommunicator();
     462        if (comm.configure(site_elem)) {
     463        comm.setLocalSiteName(local_site_name);
    474464       
     465        // add to map of modules
     466        this.module_map.put(name, comm);
     467        this.site_list.appendChild(this.doc.importNode(site_elem, true));
     468        // need to get collection list and service
     469        // list from here- if the site isn't up yet, the site will
     470        // have to be added later
     471        if (!getRemoteSiteInfo(comm, name)) {
     472            System.err.println("MessageRouter: couldn't get info from site "+name);
     473        }
    475474        } else {
    476         System.err.print("MessageRouter: cant talk to server of type:"+type);
    477         System.err.println(", so not making a connection to "+name);
     475        System.err.println("MessageRouter: couldn't configure soap site:"+name);
     476        return false;
    478477        }
    479478       
     479    } else {
     480        System.err.print("MessageRouter: cant talk to server of type:"+type + ", so not making a connection to "+name);
     481        return false;
    480482    }
    481483    return true;
    482484    }
    483    
    484485   
    485486    /** get site info from external site
     
    668669            } else if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE)) {
    669670            if (module_type.equals(GSXML.COLLECTION_ELEM)) {
    670                 success = activateCollection(module_name);
     671                success = activateCollectionByName(module_name);
    671672            } else if (module_type.equals(GSXML.SITE_ELEM)) {
    672                 success = activateSite(module_name);
     673                success = activateSiteByName(module_name);
    673674            } else if (module_type.equals(GSXML.CLUSTER_ELEM)) {
    674                 success = activateServiceCluster(module_name);
     675                success = activateServiceClusterByName(module_name);
    675676            }
    676677            if (success) {
     
    761762     *@return true if collection created ok
    762763     */
    763     protected boolean activateCollection(String col_name) {
     764    protected boolean activateCollectionByName(String col_name) {
    764765
    765766    System.out.println("MessageRouter:Activating collection: "+col_name+".");
     
    809810    }
    810811   
    811     protected boolean activateSite(String site_name) {
     812    protected boolean activateSiteByName(String site_name) {
    812813    System.out.println("MessageRouter:Activating site: "+site_name+".");
    813814   
     
    827828    Element config_elem = config_doc.getDocumentElement();
    828829   
    829     Element site_list = (Element)GSXML.getChildByTagName(config_elem, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
    830     if (site_list ==null ) {
     830    Element config_site_list = (Element)GSXML.getChildByTagName(config_elem, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
     831    if (config_site_list ==null ) {
    831832        System.err.println("MessageRouter:activateSite, no sites found");
    832833        return false;
    833834    }
    834     Element this_site_elem = GSXML.getNamedElement(site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, site_name);
     835    // this is a name to identify the current site in the Communicator
     836    String local_site_name = config_site_list.getAttribute("localSiteName");
     837    if (local_site_name.equals("")) {
     838        local_site_name = site_name;
     839    }
     840
     841    Element this_site_elem = GSXML.getNamedElement(config_site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, site_name);
    835842    if (this_site_elem == null) {
    836843        System.err.println("MessageRouter:activateSite, site "+site_name+" not found");
     
    838845    }
    839846   
    840     Communicator comm=null;
    841     String type = this_site_elem.getAttribute(GSXML.TYPE_ATT);
    842     if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
    843         comm = new SOAPCommunicator();
    844         if (comm.configure(this_site_elem)) {
    845         comm.setLocalSiteName(this.global_site_name);
    846        
    847         // add to map of modules
    848         this.module_map.put(site_name, comm);
    849         this.site_list.appendChild(this.doc.importNode(this_site_elem, true));
    850         // need to get collection list and service
    851         // list from here- if the site isn't up yet, the site will
    852         // have to be added later
    853         if (!getRemoteSiteInfo(comm, site_name)) {
    854             System.err.println("MessageRouter: couldn't get info from site "+site_name);
    855         }
    856         } else {
    857         System.err.println("MessageRouter: couldn't configure soap site:"+site_name);
    858         return false;
    859         }
    860        
    861     } else {
    862         System.err.print("MessageRouter: cant talk to server of type:"+type);
    863         System.err.println(", so not making a connection to "+site_name);
    864         return false;
    865     }
    866     return true;
    867    
    868     }
    869    
    870     protected boolean activateServiceCluster(String cluster_name) {
     847    return activateSite(this_site_elem, local_site_name);
     848    }
     849   
     850    protected boolean activateServiceClusterByName(String cluster_name) {
    871851    return false;
    872852
    873853    }
    874854
    875     protected boolean activateServiceRack(String module_name) {
     855    protected boolean activateServiceRackByName(String module_name) {
    876856    return false;
    877857    }
     
    934914    NodeList modules = module_list.getElementsByTagName(module_name);
    935915    // will this work??
    936     for (int i=0; i<modules.getLength(); i++) {
     916    for (int i=modules.getLength()-1; i>=0; i--) {
    937917       
    938918        String name = ((Element)modules.item(i)).getAttribute(GSXML.NAME_ATT);
    939919        if (GSPath.getFirstLink(name).equals(site_name)) {
    940920        module_list.removeChild(modules.item(i));
    941         i--; // move the pointer back
    942921        }
    943922    }
Note: See TracChangeset for help on using the changeset viewer.