Ignore:
Timestamp:
2011-07-04T10:49:50+12:00 (13 years ago)
Author:
sjm84
Message:

Added more functionality to ArchiveRetrieve, added an additonal parameter to printXMLNode in GSXML that controls whether text nodes are to be printed or not and also made the infodb type of collections more easily accessible

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/ServiceCluster.java

    r24213 r24221  
    11/*
    2  *    ServiceCluster.java
    3  *    Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
    4  *
    5  *    This program is free software; you can redistribute it and/or modify
    6  *    it under the terms of the GNU General Public License as published by
    7  *    the Free Software Foundation; either version 2 of the License, or
    8  *    (at your option) any later version.
    9  *
    10  *    This program is distributed in the hope that it will be useful,
    11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *    GNU General Public License for more details.
    14  *
    15  *    You should have received a copy of the GNU General Public License
    16  *    along with this program; if not, write to the Free Software
    17  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    18  */
     2*    ServiceCluster.java
     3*    Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
     4*
     5*    This program is free software; you can redistribute it and/or modify
     6*    it under the terms of the GNU General Public License as published by
     7*    the Free Software Foundation; either version 2 of the License, or
     8*    (at your option) any later version.
     9*
     10*    This program is distributed in the hope that it will be useful,
     11*    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13*    GNU General Public License for more details.
     14*
     15*    You should have received a copy of the GNU General Public License
     16*    along with this program; if not, write to the Free Software
     17*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     18*/
    1919// leave the package name as is for now - should be changed to something better
    2020// cluster? groups?
     
    4141
    4242/* ServiceCluster - a groups of services that are related in some way
    43  * Implements ModuleInterface. Contains a list of services provided by the cluster, along with metadata about the cluster itself.
    44  * a collection is a special type of cluster
    45  *  @author <a href="mailto:[email protected]">Katherine Don</a>
    46  *  @version $Revision$
    47  *  @see ModuleInterface
    48  */
     43* Implements ModuleInterface. Contains a list of services provided by the cluster, along with metadata about the cluster itself.
     44* a collection is a special type of cluster
     45*  @author <a href="mailto:[email protected]">Katherine Don</a>
     46*  @version $Revision$
     47*  @see ModuleInterface
     48*/
    4949public class ServiceCluster 
    50     implements ModuleInterface {
    51 
    52     static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.ServiceCluster.class.getName());
    53 
    54     protected static final String CONFIG_ENCODING = "utf-8";
    55 
    56     protected static final String DEFAULT_LANG = "en"; // hack for now, should be read from the coll cfg file? or site cfg file for cluster
    57    
    58     /** base directory for the site that this cluster belongs to*/
    59     protected String site_home = null;
    60     /** http address of the site that this cluster belongs to */
    61     protected String site_http_address = null;
    62     /** The name of the cluster - for a collection, this is the collection name*/
    63     protected String cluster_name = null;
    64     /** collection type : mg or mgpp */
    65     protected String col_type = "";
    66 
    67     /** a reference to the message router */
    68     protected MessageRouter router = null;
    69     /** The map of services.
    70      *
    71      * Maps Services to ServiceRack objects
    72      * @see ServiceRack
    73      *
    74      */
    75     protected HashMap service_map=null;
    76     /** maps pseudo service names to real service names - needed if we have two services with the same name for one collection */
    77     protected HashMap service_name_map=null;
    78    
    79     /** XML converter for String to DOM and vice versa */
    80     protected XMLConverter converter=null;
    81 
    82     /** container doc for description elements */
    83     protected Document doc = null;
    84     /** list of services */
    85     protected Element service_list = null;
    86     /** list of metadata - all metadata, regardless of language goes in here */
    87     protected Element metadata_list = null;
    88     /** language specific stuff */
    89     //protected Element lang_specific_metadata_list = null;
    90     protected Element display_item_list = null;
    91     /** the element that will have any descriptions passed back in */
    92     protected Element description = null;
    93 
    94     /** list of plugin */
    95     protected Element plugin_item_list = null;
    96    
    97     public void setSiteHome(String home) {
    98     this.site_home = home;
    99     }
    100 
    101     public void setSiteAddress(String address) {
    102     this.site_http_address = address;
    103     }
    104 
    105     public void cleanUp() {
    106     Iterator i = this.service_map.values().iterator();
    107     while (i.hasNext()) {
    108         ServiceRack s = (ServiceRack)i.next();
    109         s.cleanUp();
    110     }
    111     }
    112     public void setClusterName(String name) {
    113     this.cluster_name = name;   
    114     this.description.setAttribute(GSXML.NAME_ATT, name);
    115     }
    116 
    117     public void setMessageRouter(MessageRouter m) {
    118     this.router = m;
    119     }
    120 
    121     public ServiceCluster() {
    122     this.service_map = new HashMap();
    123     this.service_name_map = new HashMap();
    124     this.converter = new XMLConverter();
    125     this.doc = this.converter.newDOM();
    126     this.description = this.doc.createElement(GSXML.CLUSTER_ELEM);
    127     this.display_item_list = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
    128     this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    129     this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
    130     }
    131 
    132     /**
    133      * Configures the cluster.
    134      *
    135      * gsdlHome and clusterName must be set before configure is called.
    136      *
    137      * reads the site configuration file, and configures itself
    138      * this calls configure(Element) with the XML element node from the config
    139      * file.
    140      * configure(Element) should be used if the config file has already been
    141      * parsed.  This method will work with any subclass.
    142      *
    143      * @return true if configure successful, false otherwise.
    144      */
    145     public boolean configure() {
    146 
    147     if (this.site_home == null || this.cluster_name== null) {
    148         logger.error("site_home and cluster_name must be set before configure called!");
    149         return false;
    150     }
    151     logger.info("configuring service cluster");
    152     // read the site configuration file
    153     File config_file = new File(GSFile.siteConfigFile(this.site_home));
    154 
    155     if (!config_file.exists()) {
    156         logger.error("couldn't configure cluster: "+this.cluster_name +", "+config_file+" does not exist");
    157         return false;
    158     }
    159 
    160     Document doc = this.converter.getDOM(config_file, CONFIG_ENCODING);
    161     if (doc == null) {
    162         logger.error("couldn't parse config file "+config_file.getPath());
    163         return false;
    164     }
    165    
    166     // get the appropriate service cluster element
    167     Element cluster_list = (Element)GSXML.getChildByTagName(doc.getDocumentElement(), GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
    168     Element sc = GSXML.getNamedElement(cluster_list, GSXML.CLUSTER_ELEM,
    169                        GSXML.NAME_ATT, this.cluster_name);
    170    
    171     return this.configure(sc);
    172     }
    173    
    174    
    175     public boolean configure(Element service_cluster_info) {
    176    
    177     // get the metadata - for now just add it to the list
    178     Element meta_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    179     if (meta_list !=null) {
    180         if (!addMetadata(meta_list)) {
    181        
    182         logger.error(" couldn't configure the metadata");
    183         }
    184     }
    185    
    186     // get the display info
    187     Element display_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
    188     if (display_list !=null) {
    189         if (!addDisplayItems(display_list)) {
    190        
    191         logger.error("couldn't configure the display items");
    192         }
    193     }
    194    
    195     //get the plugin info
    196     Element import_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.IMPORT_ELEM);
    197     if (import_list != null)
    198     {
    199         Element plugin_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
    200         if (plugin_list !=null) {
    201             if (!addPlugins(plugin_list)) {
    202        
    203                 logger.error("couldn't configure the plugins");
    204                 }
    205         }
    206     }   
    207    
    208     // do the service racks
    209     // empty the service map in case this is a reconfigure
    210     clearServices();
    211     Element service_rack_list = (Element)GSXML.getChildByTagName(service_cluster_info, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
    212     if (service_rack_list == null) {
    213         // is this an error? could you ever have a service cluster
    214         // without service racks???
    215         logger.error("cluster has no service racks!!");
    216     } else {
    217        
    218         if (!configureServiceRackList(service_rack_list, null)) {
    219         logger.error("couldn't configure the  service racks!!");
    220         return false;
    221         }
    222     }
    223 
    224     return true;
    225     }
    226    
    227     /** adds metadata from a metadataList into the metadata_list xml
    228      */
    229     protected boolean addMetadata(Element metadata_list) {
    230     if (metadata_list == null) return false;
    231     NodeList metanodes = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
    232     if (metanodes.getLength()>0) { 
    233         for(int k=0; k<metanodes.getLength(); k++) {
    234         this.metadata_list.appendChild(this.doc.importNode(metanodes.item(k), true));
    235         }
    236     }
    237    
    238     return true;
    239     }
    240 
    241     protected boolean addDisplayItems(Element display_list) {
    242    
    243     if (display_list==null) return false;
    244     NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
    245     if (displaynodes.getLength()>0) {   
    246         for(int k=0; k<displaynodes.getLength(); k++) {
    247         Element d = (Element) displaynodes.item(k);
    248         String lang = d.getAttribute(GSXML.LANG_ATT);
    249         if (lang==null||lang.equals("")) {
    250             //set the lang to teh default
    251             d.setAttribute(GSXML.LANG_ATT, DEFAULT_LANG);
    252         }
    253         String name = d.getAttribute(GSXML.NAME_ATT);
    254         Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, name);
    255         if (this_item==null) {
    256             this_item = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM);
    257             this_item.setAttribute(GSXML.NAME_ATT, name);
    258             this.display_item_list.appendChild(this_item);
    259         }
    260        
    261         this_item.appendChild(this.doc.importNode(d, true));
    262         }
    263     }
    264    
    265     return true;
    266     }
    267    
    268     protected boolean addPlugins(Element plugin_list) {
    269         if (plugin_list == null) return false;
    270     NodeList pluginNodes = plugin_list.getElementsByTagName(GSXML.PLUGIN_ELEM);
    271     if (pluginNodes.getLength() > 0) {
    272         for (int k = 0; k < pluginNodes.getLength(); k++)
     50implements ModuleInterface {
     51
     52    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.ServiceCluster.class.getName());
     53
     54    protected static final String CONFIG_ENCODING = "utf-8";
     55
     56    protected static final String DEFAULT_LANG = "en"; // hack for now, should be read from the coll cfg file? or site cfg file for cluster
     57   
     58    /** base directory for the site that this cluster belongs to*/
     59    protected String site_home = null;
     60    /** http address of the site that this cluster belongs to */
     61    protected String site_http_address = null;
     62    /** The name of the cluster - for a collection, this is the collection name*/
     63    protected String cluster_name = null;
     64    /** collection type : mg, mgpp or lucene */
     65    protected String col_type = "";
     66    /** database type : gdbm, jdbm or sqlite*/
     67    protected String db_type = "";
     68   
     69    /** a reference to the message router */
     70    protected MessageRouter router = null;
     71    /** The map of services.
     72    *
     73    * Maps Services to ServiceRack objects
     74    * @see ServiceRack
     75    *
     76    */
     77    protected HashMap service_map=null;
     78    /** maps pseudo service names to real service names - needed if we have two services with the same name for one collection */
     79    protected HashMap service_name_map=null;
     80   
     81    /** XML converter for String to DOM and vice versa */
     82    protected XMLConverter converter=null;
     83
     84    /** container doc for description elements */
     85    protected Document doc = null;
     86    /** list of services */
     87    protected Element service_list = null;
     88    /** list of metadata - all metadata, regardless of language goes in here */
     89    protected Element metadata_list = null;
     90    /** language specific stuff */
     91    //protected Element lang_specific_metadata_list = null;
     92    protected Element display_item_list = null;
     93    /** the element that will have any descriptions passed back in */
     94    protected Element description = null;
     95
     96    /** list of plugin */
     97    protected Element plugin_item_list = null;
     98   
     99    public void setSiteHome(String home) {
     100        this.site_home = home;
     101    }
     102
     103    public void setSiteAddress(String address) {
     104        this.site_http_address = address;
     105    }
     106
     107    public void cleanUp() {
     108        Iterator i = this.service_map.values().iterator();
     109        while (i.hasNext()) {
     110            ServiceRack s = (ServiceRack)i.next();
     111            s.cleanUp();
     112        }
     113    }
     114    public void setClusterName(String name) {
     115        this.cluster_name = name;   
     116        this.description.setAttribute(GSXML.NAME_ATT, name);
     117    }
     118
     119    public void setMessageRouter(MessageRouter m) {
     120        this.router = m;
     121    }
     122
     123    public ServiceCluster() {
     124        this.service_map = new HashMap();
     125        this.service_name_map = new HashMap();
     126        this.converter = new XMLConverter();
     127        this.doc = this.converter.newDOM();
     128        this.description = this.doc.createElement(GSXML.CLUSTER_ELEM);
     129        this.display_item_list = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
     130        this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     131        this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
     132    }
     133
     134    /**
     135    * Configures the cluster.
     136    *
     137    * gsdlHome and clusterName must be set before configure is called.
     138    *
     139    * reads the site configuration file, and configures itself
     140    * this calls configure(Element) with the XML element node from the config
     141    * file.
     142    * configure(Element) should be used if the config file has already been
     143    * parsed.  This method will work with any subclass.
     144    *
     145    * @return true if configure successful, false otherwise.
     146    */
     147    public boolean configure() {
     148
     149        if (this.site_home == null || this.cluster_name== null) {
     150            logger.error("site_home and cluster_name must be set before configure called!");
     151            return false;
     152        }
     153        logger.info("configuring service cluster");
     154        // read the site configuration file
     155        File config_file = new File(GSFile.siteConfigFile(this.site_home));
     156
     157        if (!config_file.exists()) {
     158            logger.error("couldn't configure cluster: "+this.cluster_name +", "+config_file+" does not exist");
     159            return false;
     160        }
     161
     162        Document doc = this.converter.getDOM(config_file, CONFIG_ENCODING);
     163        if (doc == null) {
     164            logger.error("couldn't parse config file "+config_file.getPath());
     165            return false;
     166        }
     167       
     168        // get the appropriate service cluster element
     169        Element cluster_list = (Element)GSXML.getChildByTagName(doc.getDocumentElement(), GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
     170        Element sc = GSXML.getNamedElement(cluster_list, GSXML.CLUSTER_ELEM,
     171        GSXML.NAME_ATT, this.cluster_name);
     172       
     173        return this.configure(sc);
     174    }
     175   
     176   
     177    public boolean configure(Element service_cluster_info) {
     178       
     179        // get the metadata - for now just add it to the list
     180        Element meta_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     181        if (meta_list !=null) {
     182            if (!addMetadata(meta_list)) {
     183               
     184                logger.error(" couldn't configure the metadata");
     185            }
     186        }
     187       
     188        // get the display info
     189        Element display_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
     190        if (display_list !=null) {
     191            if (!addDisplayItems(display_list)) {
     192               
     193                logger.error("couldn't configure the display items");
     194            }
     195        }
     196       
     197        //get the plugin info
     198        Element import_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.IMPORT_ELEM);
     199        if (import_list != null)
    273200        {
    274             this.plugin_item_list.appendChild(this.doc.importNode(pluginNodes.item(k), true));
    275         }
    276     }
    277    
    278     return true;
    279     }
    280 
    281    
    282   protected void clearServices() {
    283     service_map.clear();
    284     this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
    285   }
    286     /** creates and configures all the services - extra_info is some more xml
     201            Element plugin_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
     202            if (plugin_list !=null) {
     203                if (!addPlugins(plugin_list)) {
     204                   
     205                    logger.error("couldn't configure the plugins");
     206                }
     207            }
     208        }   
     209       
     210        // do the service racks
     211        // empty the service map in case this is a reconfigure
     212        clearServices();
     213        Element service_rack_list = (Element)GSXML.getChildByTagName(service_cluster_info, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
     214        if (service_rack_list == null) {
     215            // is this an error? could you ever have a service cluster
     216            // without service racks???
     217            logger.error("cluster has no service racks!!");
     218        } else {
     219           
     220            if (!configureServiceRackList(service_rack_list, null)) {
     221                logger.error("couldn't configure the  service racks!!");
     222                return false;
     223            }
     224        }
     225
     226        return true;
     227    }
     228   
     229    /** adds metadata from a metadataList into the metadata_list xml
     230    */
     231    protected boolean addMetadata(Element metadata_list) {
     232        if (metadata_list == null) return false;
     233        NodeList metanodes = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
     234        if (metanodes.getLength()>0) { 
     235            for(int k=0; k<metanodes.getLength(); k++) {
     236                this.metadata_list.appendChild(this.doc.importNode(metanodes.item(k), true));
     237            }
     238        }
     239       
     240        return true;
     241    }
     242
     243    protected boolean addDisplayItems(Element display_list) {
     244       
     245        if (display_list==null) return false;
     246        NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
     247        if (displaynodes.getLength()>0) {   
     248            for(int k=0; k<displaynodes.getLength(); k++) {
     249                Element d = (Element) displaynodes.item(k);
     250                String lang = d.getAttribute(GSXML.LANG_ATT);
     251                if (lang==null||lang.equals("")) {
     252                    //set the lang to teh default
     253                    d.setAttribute(GSXML.LANG_ATT, DEFAULT_LANG);
     254                }
     255                String name = d.getAttribute(GSXML.NAME_ATT);
     256                Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, name);
     257                if (this_item==null) {
     258                    this_item = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM);
     259                    this_item.setAttribute(GSXML.NAME_ATT, name);
     260                    this.display_item_list.appendChild(this_item);
     261                }
     262               
     263                this_item.appendChild(this.doc.importNode(d, true));
     264            }
     265        }
     266       
     267        return true;
     268    }
     269   
     270    protected boolean addPlugins(Element plugin_list) {
     271        if (plugin_list == null) return false;
     272        NodeList pluginNodes = plugin_list.getElementsByTagName(GSXML.PLUGIN_ELEM);
     273        if (pluginNodes.getLength() > 0) {
     274            for (int k = 0; k < pluginNodes.getLength(); k++)
     275            {
     276                this.plugin_item_list.appendChild(this.doc.importNode(pluginNodes.item(k), true));
     277            }
     278        }
     279       
     280        return true;
     281    }
     282
     283   
     284    protected void clearServices() {
     285        service_map.clear();
     286        this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
     287    }
     288    /** creates and configures all the services - extra_info is some more xml
    287289that is passed to teh service  - eg used for coll config files for Collection
    288     */
    289     protected boolean configureServiceRackList(Element service_rack_list,
    290                        Element extra_info) {
    291 
    292     // create all the services
    293     NodeList nodes = service_rack_list.getElementsByTagName(GSXML.SERVICE_CLASS_ELEM);
    294     if (nodes.getLength()==0) {
    295         logger.error("ServiceCluster configuration error: cluster "+this.cluster_name+" has no service modules!");
    296         return false;
    297     }
    298 
    299     for(int i=0; i<nodes.getLength(); i++) {
    300    
    301         // the xml request to send to the serviceRack to query what
    302         // services it provides
    303         Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
    304         Element request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", "", "");
    305         message.appendChild(request);
    306 
    307         Element n = (Element)nodes.item(i);
    308         String servicetype = n.getAttribute(GSXML.NAME_ATT);
    309        
    310         ServiceRack s = null;
    311        
    312         try {
    313         // try for a default service in standard package
    314         s = (ServiceRack)Class.forName("org.greenstone.gsdl3.service."+servicetype).newInstance();
    315        
    316         } catch (Exception e) {}
    317         if (s == null) {
    318         try {
    319             // name as is, in case package is already specified
    320             s = (ServiceRack)Class.forName(servicetype).newInstance();
    321         } catch (Exception e) {}
    322         }
    323 
    324         if (s == null) {
    325         logger.error("Couldn't get an instance of class "+servicetype+", or org.greenstone.gsdl3.service."+servicetype);
    326         continue;
    327         }
    328 
    329        
    330         s.setSiteHome(this.site_home);
    331         s.setSiteAddress(this.site_http_address);
    332         s.setClusterName(this.cluster_name);
    333         s.setMessageRouter(this.router);
    334         // pass the xml node to the service for configuration
    335         if (s.configure(n, extra_info)) {
    336        
    337         // find out the supported service types for this service module
    338         Node types = s.process(message);
    339         NodeList typenodes = ((Element)types).getElementsByTagName(GSXML.SERVICE_ELEM);   
    340        
    341         for (int j=0; j<typenodes.getLength();j++) {
    342             String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);       
    343             if (service_map.get(service)!=null) {
    344             char extra = '0';
    345             String new_service = service+extra;   
    346            
    347             while (service_map.get(new_service)!=null) {
    348                 extra++;
    349                 new_service = service+extra;
    350             }
    351             this.service_name_map.put(new_service, service);
    352             service=new_service;
    353             ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
    354             }
    355             this.service_map.put(service, s);
    356             // also add info to the ServiceInfo XML element
    357             this.service_list.appendChild(this.doc.importNode(typenodes.item(j), true));
    358         }
    359         }
     290    */
     291    protected boolean configureServiceRackList(Element service_rack_list,
     292    Element extra_info) {
     293
     294        // create all the services
     295        NodeList nodes = service_rack_list.getElementsByTagName(GSXML.SERVICE_CLASS_ELEM);
     296        if (nodes.getLength()==0) {
     297            logger.error("ServiceCluster configuration error: cluster "+this.cluster_name+" has no service modules!");
     298            return false;
     299        }
     300
     301        for(int i=0; i<nodes.getLength(); i++) {
     302           
     303            // the xml request to send to the serviceRack to query what
     304            // services it provides
     305            Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
     306            Element request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", "", "");
     307            message.appendChild(request);
     308
     309            Element n = (Element)nodes.item(i);
     310            String servicetype = n.getAttribute(GSXML.NAME_ATT);
     311           
     312            ServiceRack s = null;
     313           
     314            try {
     315                // try for a default service in standard package
     316                s = (ServiceRack)Class.forName("org.greenstone.gsdl3.service."+servicetype).newInstance();
     317               
     318            } catch (Exception e) {}
     319            if (s == null) {
     320                try {
     321                    // name as is, in case package is already specified
     322                    s = (ServiceRack)Class.forName(servicetype).newInstance();
     323                } catch (Exception e) {}
     324            }
     325
     326            if (s == null) {
     327                logger.error("Couldn't get an instance of class "+servicetype+", or org.greenstone.gsdl3.service."+servicetype);
     328                continue;
     329            }
     330
     331           
     332            s.setSiteHome(this.site_home);
     333            s.setSiteAddress(this.site_http_address);
     334            s.setClusterName(this.cluster_name);
     335            s.setMessageRouter(this.router);
     336            // pass the xml node to the service for configuration
     337            if (s.configure(n, extra_info)) {
     338               
     339                // find out the supported service types for this service module
     340                Node types = s.process(message);
     341                NodeList typenodes = ((Element)types).getElementsByTagName(GSXML.SERVICE_ELEM);   
     342               
     343                for (int j=0; j<typenodes.getLength();j++) {
     344                    String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);       
     345                    if (service_map.get(service)!=null) {
     346                        char extra = '0';
     347                        String new_service = service+extra;   
     348                       
     349                        while (service_map.get(new_service)!=null) {
     350                            extra++;
     351                            new_service = service+extra;
     352                        }
     353                        this.service_name_map.put(new_service, service);
     354                        service=new_service;
     355                        ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
     356                    }
     357                    this.service_map.put(service, s);
     358                    // also add info to the ServiceInfo XML element
     359                    this.service_list.appendChild(this.doc.importNode(typenodes.item(j), true));
     360                }
     361            }
     362        }
     363       
     364        return true;
     365       
     366       
     367    }
     368
     369
     370    /**
     371    * Process an XML document - uses Strings
     372    *  just calls process(Node).
     373    *
     374    * @param in the Document to process - a string
     375    * @return the resultant document as a string - contains any error messages
     376    * @see String
     377    */
     378    public String process(String in) {
     379
     380        Document doc = this.converter.getDOM(in);
     381       
     382        Node res = process(doc);
     383        return this.converter.getString(res);
     384       
     385    }
     386   
     387    /** process XML as Node
     388    *
     389    */
     390    public Node process(Node message_node) {
     391
     392        Element message = this.converter.nodeToElement(message_node);
     393
     394        NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
     395        Document mess_doc = message.getOwnerDocument();
     396        Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
     397        if (requests.getLength()==0) {
     398            logger.error("no requests for cluster:"+this.cluster_name);
     399            // no requests
     400            return mainResult; // for now
     401        }
     402        for (int i=0; i<requests.getLength(); i++) {
     403            Element request = (Element)requests.item(i);
     404            String to = request.getAttribute(GSXML.TO_ATT);
     405
     406            // the cluster name should be first, check, then remove
     407            String clustername = GSPath.getFirstLink(to);
     408            if (!clustername.equals(this.cluster_name)){
     409                logger.error("cluster name wrong! was "+clustername+" should have been "+this.cluster_name);
     410                continue; // ignore this request
     411            }
     412            to = GSPath.removeFirstLink(to);
     413            request.setAttribute(GSXML.TO_ATT, to);
     414           
     415            if (to.equals("")) { // this command is for me
     416                Element response = processMessage(request);
     417                mainResult.appendChild(response);
     418               
     419            } else { // the request is for one of my services
     420                String service = GSPath.getFirstLink(to);
     421               
     422                if (!this.service_map.containsKey(service)) {
     423                    logger.error("non-existant service, "+service+", specified!");
     424                    continue;
     425                }
     426                String real_service = service;
     427                if (this.service_name_map.containsKey(service)) {
     428                    real_service = (String)this.service_name_map.get(service);
     429                    // need to change the to att in the request - give the real service name
     430                    to = request.getAttribute(GSXML.TO_ATT);
     431                    String old_to = to;
     432                    to = GSPath.replaceFirstLink(to, real_service);
     433                    request.setAttribute(GSXML.TO_ATT, to);
     434                }
     435                // have to pass the request to the service
     436                Element single_message = mess_doc.createElement(GSXML.MESSAGE_ELEM);
     437                single_message.appendChild(request);
     438                Node response_message = ((ModuleInterface)this.service_map.get(service)).process(single_message);
     439                if (response_message != null) {
     440                    Element response = (Element) GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM);
     441                    String from = response.getAttribute(GSXML.FROM_ATT);
     442                    if (!real_service.equals(service)) {
     443                        // replace the real service name with the pseudo service name
     444                        from = GSPath.replaceFirstLink(from, service);
     445                        // also need to do it in the service itself
     446                        // shoudl this be done here??
     447                        Element service_elem = (Element) GSXML.getChildByTagName(response, GSXML.SERVICE_ELEM);
     448                        if (service_elem!= null) {
     449                            service_elem.setAttribute(GSXML.NAME_ATT, service);
     450                        }
     451                    }
     452                    from = GSPath.prependLink(from, this.cluster_name);
     453                    response.setAttribute(GSXML.FROM_ATT, from);
     454                    mainResult.appendChild(this.doc.importNode(response, true));
     455                }
     456               
     457            } // else
     458           
     459           
     460        } // for each request
     461        return mainResult;
     462    }
     463
     464    /** handles requests made to the ServiceCluster itself
     465    *
     466    * @param req - the request Element- <request>
     467    * @return the result Element - should be <response>
     468    */
     469    protected Element processMessage(Element request) {
     470
     471        Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     472        response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
     473        String type = request.getAttribute(GSXML.TYPE_ATT);
     474        String lang = request.getAttribute(GSXML.LANG_ATT);
     475        response.setAttribute(GSXML.TYPE_ATT, type);
     476       
     477        if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
     478            // create the collection element
     479            Element description = (Element)this.description.cloneNode(false);
     480            // set collection type : mg or mgpp
     481            description.setAttribute(GSXML.TYPE_ATT, col_type);
     482            description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
     483
     484            response.appendChild(description);
     485            // check the param list
     486            Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     487            if (param_list == null) {               
     488                addAllDisplayInfo(description, lang);               
     489                description.appendChild(this.service_list);
     490                description.appendChild(this.metadata_list);
     491                description.appendChild(this.plugin_item_list);
     492                return response;
     493            }
     494           
     495            // go through the param list and see what components are wanted
     496            NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
     497            for (int i=0; i<params.getLength(); i++) {     
     498               
     499                Element param = (Element)params.item(i);               
     500                // Identify the structure information desired
     501                if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM)) {
     502                    String info = param.getAttribute(GSXML.VALUE_ATT);
     503                    if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
     504                        description.appendChild(this.service_list);
     505                    } else if (info.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER)) {
     506                        description.appendChild(metadata_list);
     507                    } else if (info.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
     508                        addAllDisplayInfo(description, lang);
     509                    } else if (info.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
     510                        description.appendChild(plugin_item_list);
     511                    }
     512                }
     513            }
     514            return response;
     515        }
     516        /*
     517    if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING)) {
     518        logger.error("Received format string request");
     519        String service = request.getAttribute("service");
     520        logger.error("Service is " + service);
     521        String classifier = null;
     522        if(service.equals("ClassifierBrowse"))
     523        {
     524            classifier = request.getAttribute("classifier");
     525            logger.error("Classifier is " + classifier);
     526        }
     527        Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
     528        String format_string = GSXML.getNodeText(format_element);
     529        logger.error("Format string: " + format_string);
     530        logger.error("Config file location = " + GSFile.collectionConfigFile(this.site_home, this.cluster_name));
     531
     532        // check for version file
     533
     534        String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.pathSeparator;
     535        logger.error("Directory is " + directory);
     536
     537        String version_filename = "";
     538
     539        if(service.equals("ClassifierBrowse"))
     540            version_filename = directory + "browse_"+classifier+"_format_statement_version.txt";
     541        else
     542            version_filename = directory + "query_format_statement_version.txt";
     543
     544        File version_file = new File(version_filename);
     545        logger.error("Version filename is " + version_filename);
     546
     547        String version_number = "1";
     548        BufferedWriter writer; // = new BufferedWriter(new FileWriter(version_filename));
     549        //RandomAccessFile version_file_random_access;
     550
     551        try{
     552
     553            if(version_file.exists())
     554            {
     555                // Read version
     556                BufferedReader reader = new BufferedReader(new FileReader(version_filename));
     557                //version_file_random_access = new RandomAccessFile(version_file, "r");
     558                //logger.error("
     559                //version_number = version_file_random_access.readInt();
     560                version_number = reader.readLine();
     561                int aInt = Integer.parseInt(version_number) + 1;
     562                version_number = Integer.toString(aInt);
     563                reader.close();
     564                //version_file_random_access.close();
     565            }
     566            else{
     567                // Create
     568                version_file.createNewFile();
     569                // write 1 to file
     570                writer = new BufferedWriter(new FileWriter(version_filename));
     571                //version_file_random_access = new RandomAccessFile(version_file, "w");
     572                //version_file_random_access.writeInt(version_number);
     573                writer.write(version_number);
     574                writer.close();
     575                //version_file_random_access.close();
     576            }
     577
     578            // Write version file
     579            String format_statement_filename = "";
     580
     581            if(service.equals("ClassifierBrowse"))
     582                format_statement_filename = directory + "browse_"+classifier+"_format_statement_v" + version_number + ".txt";
     583            else
     584                format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
     585
     586            logger.error("Format statement filename is " + format_statement_filename);
     587
     588            writer = new BufferedWriter(new FileWriter(format_statement_filename));
     589            writer.write(format_string);
     590            writer.close();
     591
     592            // Update version number
     593            //version_file_random_access = new RandomAccessFile(version_file, "w");
     594            //version_file_random_access.writeInt(version_number);
     595            //version_file_random_access.close();
     596
     597            writer = new BufferedWriter(new FileWriter(version_filename));
     598            //version_file_random_access = new RandomAccessFile(version_file, "w");
     599            //version_file_random_access.writeInt(version_number);
     600            writer.write(version_number);
     601            writer.close();
     602
     603
     604
     605        } catch (IOException e) {
     606            logger.error("IO Exception "+e);
     607            //System.exit(1);
     608        }
     609
     610
     611    }*/
     612        if (type.equals(GSXML.REQUEST_TYPE_SYSTEM)) {
     613            response = processSystemRequest(request);
     614        } else { // unknown type
     615            logger.error("cant handle request of type "+ type);
     616           
     617        }
     618        return response;
     619    }
     620
     621    protected Element processSystemRequest(Element request) {
     622
     623        Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     624        response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
     625        response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SYSTEM);
     626
     627        // a list of system requests - should put any error messages
     628        // or success messages into response
     629        NodeList commands = request.getElementsByTagName(GSXML.SYSTEM_ELEM);
     630        String message=null;   
     631        for (int i=0; i<commands.getLength(); i++) {
     632            // all the commands should be Elements
     633            Element elem = (Element)commands.item(i);
     634            String action = elem.getAttribute(GSXML.TYPE_ATT);
     635            if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE)) {
     636                String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
     637                if (subset.equals("")) {
     638                    // need to reconfigure the service cluster
     639                   
     640                    if (this.configure()) {
     641                        Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " reconfigured");
     642                        response.appendChild(s);
     643                       
     644                    } else {
     645                        Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " could not be reconfigured");
     646                        response.appendChild(s);
     647                    }
     648                } else if (this.configureSubset(subset)) {
     649                    Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " "+subset+" reconfigured");
     650                    response.appendChild(s);
     651                } else {
     652                    Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " "+subset + " could not be reconfigured");
     653                    response.appendChild(s);
     654                }
     655                continue;
     656            } // configure action
     657           
     658            String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
     659            String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
     660            if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE)) {
     661                Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "activate action not yet implemented - does it even make sense in this context??");
     662                response.appendChild(s);
     663            } else if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE)) {
     664                if (module_type.equals(GSXML.SERVICE_ELEM)) {
     665                    // deactivate the service
     666                    // remove from service_map
     667                    this.service_map.remove(module_name);
     668                    Element service_elem = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, module_name);
     669                    service_list.removeChild(service_elem);         
     670                    message = module_type+": "+module_name+" deactivated";
     671                } else {
     672                    message = "can't deactivate "+module_type+" type modules!";}
     673                Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
     674                response.appendChild(s);
     675            } else {
     676                logger.error("cant process system request, action "+action);
     677                continue;
     678            }
     679        } // for each command
     680        return response;
     681    }
     682
     683    /**
     684    * do a configure on only part of the collection
     685    */
     686    protected boolean configureSubset(String subset) {
     687       
     688        File configFile = new File(GSFile.siteConfigFile(this.site_home));
     689        if (!configFile.exists() ) {
     690            logger.error("site config file: "+configFile.getPath()+" not found!");
     691            // wont be able to do any of the requests
     692            return false;
     693           
     694        }
     695       
     696        Document site_config_doc  = this.converter.getDOM(configFile);
     697        if (site_config_doc == null) {
     698            logger.error("could not read in site config file: "+configFile.getPath());
     699            return false;
     700        }
     701       
     702        Element site_config_elem = site_config_doc.getDocumentElement();
     703        Element cluster_config_elem = GSXML.getNamedElement((Element)GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER), GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, this.cluster_name);
     704        if (cluster_config_elem == null) {
     705            logger.error("site config file: "+configFile.getPath()+" has no element for cluster "+this.cluster_name);
     706            // wont be able to do any of teh requests
     707            return false;
     708           
     709        }
     710        if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
     711            Element service_rack_list = (Element)GSXML.getChildByTagName(cluster_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
     712            clearServices();
     713            return configureServiceRackList(service_rack_list, null);
     714        } else if (subset.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER)) {
     715            this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     716            Element metadata_list = (Element)GSXML.getChildByTagName(cluster_config_elem, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     717            return addMetadata(metadata_list);
     718        } else if (subset.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
     719            this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
     720            Element import_list = (Element)GSXML.getChildByTagName(cluster_config_elem,GSXML.IMPORT_ELEM);
     721            if (import_list != null)
     722            {
     723                Element plugin_item_list = (Element)GSXML.getChildByTagName(cluster_config_elem,GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
     724                return addPlugins(plugin_item_list);
     725            }
     726            else
     727            return false;
     728        } else {
     729            logger.error("cannot process system request, configure "+subset);
     730            return false;
     731        }
     732
    360733    }
    361    
    362     return true;
    363    
    364    
    365     }
    366 
    367 
    368     /**
    369      * Process an XML document - uses Strings
    370      *  just calls process(Node).
    371      *
    372      * @param in the Document to process - a string
    373      * @return the resultant document as a string - contains any error messages
    374      * @see String
    375      */
    376     public String process(String in) {
    377 
    378     Document doc = this.converter.getDOM(in);
    379    
    380     Node res = process(doc);
    381     return this.converter.getString(res);
    382    
    383     }
    384    
    385     /** process XML as Node
    386      *
    387      */
    388     public Node process(Node message_node) {
    389 
    390     Element message = this.converter.nodeToElement(message_node);
    391 
    392     NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
    393     Document mess_doc = message.getOwnerDocument();
    394     Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
    395     if (requests.getLength()==0) {
    396         logger.error("no requests for cluster:"+this.cluster_name);
    397         // no requests
    398         return mainResult; // for now
    399     }
    400     for (int i=0; i<requests.getLength(); i++) {
    401         Element request = (Element)requests.item(i);
    402         String to = request.getAttribute(GSXML.TO_ATT);
    403 
    404         // the cluster name should be first, check, then remove
    405         String clustername = GSPath.getFirstLink(to);
    406         if (!clustername.equals(this.cluster_name)){
    407         logger.error("cluster name wrong! was "+clustername+" should have been "+this.cluster_name);
    408         continue; // ignore this request
    409         }
    410         to = GSPath.removeFirstLink(to);
    411         request.setAttribute(GSXML.TO_ATT, to);
    412        
    413         if (to.equals("")) { // this command is for me
    414         Element response = processMessage(request);
    415         mainResult.appendChild(response);
    416        
    417         } else { // the request is for one of my services
    418         String service = GSPath.getFirstLink(to);
    419        
    420         if (!this.service_map.containsKey(service)) {
    421             logger.error("non-existant service, "+service+", specified!");
    422             continue;
    423         }
    424         String real_service = service;
    425         if (this.service_name_map.containsKey(service)) {
    426             real_service = (String)this.service_name_map.get(service);
    427             // need to change the to att in the request - give the real service name
    428             to = request.getAttribute(GSXML.TO_ATT);
    429             String old_to = to;
    430             to = GSPath.replaceFirstLink(to, real_service);
    431             request.setAttribute(GSXML.TO_ATT, to);
    432         }
    433         // have to pass the request to the service
    434         Element single_message = mess_doc.createElement(GSXML.MESSAGE_ELEM);
    435         single_message.appendChild(request);
    436         Node response_message = ((ModuleInterface)this.service_map.get(service)).process(single_message);
    437         if (response_message != null) {
    438             Element response = (Element) GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM);
    439             String from = response.getAttribute(GSXML.FROM_ATT);
    440             if (!real_service.equals(service)) {
    441             // replace the real service name with the pseudo service name
    442             from = GSPath.replaceFirstLink(from, service);
    443             // also need to do it in the service itself
    444             // shoudl this be done here??
    445             Element service_elem = (Element) GSXML.getChildByTagName(response, GSXML.SERVICE_ELEM);
    446             if (service_elem!= null) {
    447                 service_elem.setAttribute(GSXML.NAME_ATT, service);
    448             }
    449             }
    450             from = GSPath.prependLink(from, this.cluster_name);
    451             response.setAttribute(GSXML.FROM_ATT, from);
    452             mainResult.appendChild(this.doc.importNode(response, true));
    453         }
    454          
    455         } // else
    456        
    457        
    458     } // for each request
    459     return mainResult;
    460     }
    461 
    462     /** handles requests made to the ServiceCluster itself
    463      *
    464      * @param req - the request Element- <request>
    465      * @return the result Element - should be <response>
    466      */
    467     protected Element processMessage(Element request) {
    468 
    469     Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
    470     response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
    471     String type = request.getAttribute(GSXML.TYPE_ATT);
    472     String lang = request.getAttribute(GSXML.LANG_ATT);
    473     response.setAttribute(GSXML.TYPE_ATT, type);
    474    
    475     if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
    476         // create the collection element
    477         Element description = (Element)this.description.cloneNode(false);
    478         // set collection type : mg or mgpp
    479         description.setAttribute(GSXML.TYPE_ATT, col_type);
    480 
    481         response.appendChild(description);
    482         // check the param list
    483         Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    484         if (param_list == null) {               
    485         addAllDisplayInfo(description, lang);               
    486         description.appendChild(this.service_list);
    487         description.appendChild(this.metadata_list);
    488         description.appendChild(this.plugin_item_list);
    489         return response;
    490         }
    491        
    492         // go through the param list and see what components are wanted
    493         NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
    494         for (int i=0; i<params.getLength(); i++) {     
    495        
    496         Element param = (Element)params.item(i);               
    497         // Identify the structure information desired
    498         if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM)) {
    499             String info = param.getAttribute(GSXML.VALUE_ATT);
    500             if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
    501             description.appendChild(this.service_list);
    502             } else if (info.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER)) {
    503             description.appendChild(metadata_list);
    504             } else if (info.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
    505             addAllDisplayInfo(description, lang);
    506             } else if (info.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
    507                 description.appendChild(plugin_item_list);
    508             }
    509         }
    510         }
    511         return response;
    512     }
    513     /*
    514     if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING)) {
    515         logger.error("Received format string request");
    516         String service = request.getAttribute("service");
    517         logger.error("Service is " + service);
    518         String classifier = null;
    519         if(service.equals("ClassifierBrowse"))
    520         {
    521             classifier = request.getAttribute("classifier");
    522             logger.error("Classifier is " + classifier);
    523         }
    524         Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
    525         String format_string = GSXML.getNodeText(format_element);
    526         logger.error("Format string: " + format_string);
    527         logger.error("Config file location = " + GSFile.collectionConfigFile(this.site_home, this.cluster_name));
    528 
    529         // check for version file
    530 
    531         String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.pathSeparator;
    532         logger.error("Directory is " + directory);
    533 
    534         String version_filename = "";
    535 
    536         if(service.equals("ClassifierBrowse"))
    537             version_filename = directory + "browse_"+classifier+"_format_statement_version.txt";
    538         else
    539             version_filename = directory + "query_format_statement_version.txt";
    540 
    541         File version_file = new File(version_filename);
    542         logger.error("Version filename is " + version_filename);
    543 
    544         String version_number = "1";
    545         BufferedWriter writer; // = new BufferedWriter(new FileWriter(version_filename));
    546         //RandomAccessFile version_file_random_access;
    547 
    548         try{
    549 
    550             if(version_file.exists())
    551             {
    552                 // Read version
    553                  BufferedReader reader = new BufferedReader(new FileReader(version_filename));
    554                  //version_file_random_access = new RandomAccessFile(version_file, "r");
    555                  //logger.error("
    556                  //version_number = version_file_random_access.readInt();
    557                  version_number = reader.readLine();
    558                  int aInt = Integer.parseInt(version_number) + 1;
    559                  version_number = Integer.toString(aInt);
    560                  reader.close();
    561                  //version_file_random_access.close();
    562             }
    563             else{
    564                 // Create
    565                 version_file.createNewFile();
    566                 // write 1 to file
    567                 writer = new BufferedWriter(new FileWriter(version_filename));
    568                 //version_file_random_access = new RandomAccessFile(version_file, "w");
    569                 //version_file_random_access.writeInt(version_number);
    570                 writer.write(version_number);
    571                 writer.close();
    572                 //version_file_random_access.close();
    573             }
    574 
    575             // Write version file
    576             String format_statement_filename = "";
    577 
    578             if(service.equals("ClassifierBrowse"))
    579                 format_statement_filename = directory + "browse_"+classifier+"_format_statement_v" + version_number + ".txt";
    580             else
    581                 format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
    582 
    583             logger.error("Format statement filename is " + format_statement_filename);
    584 
    585             writer = new BufferedWriter(new FileWriter(format_statement_filename));
    586             writer.write(format_string);
    587             writer.close();
    588 
    589             // Update version number
    590             //version_file_random_access = new RandomAccessFile(version_file, "w");
    591             //version_file_random_access.writeInt(version_number);
    592             //version_file_random_access.close();
    593 
    594             writer = new BufferedWriter(new FileWriter(version_filename));
    595             //version_file_random_access = new RandomAccessFile(version_file, "w");
    596             //version_file_random_access.writeInt(version_number);
    597             writer.write(version_number);
    598             writer.close();
    599 
    600 
    601 
    602         } catch (IOException e) {
    603             logger.error("IO Exception "+e);
    604             //System.exit(1);
    605         }
    606 
    607 
    608     }*/
    609     if (type.equals(GSXML.REQUEST_TYPE_SYSTEM)) {
    610         response = processSystemRequest(request);
    611     } else { // unknown type
    612         logger.error("cant handle request of type "+ type);
    613        
    614     }
    615     return response;
    616     }
    617 
    618     protected Element processSystemRequest(Element request) {
    619 
    620     Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
    621     response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
    622     response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SYSTEM);
    623 
    624     // a list of system requests - should put any error messages
    625     // or success messages into response
    626     NodeList commands = request.getElementsByTagName(GSXML.SYSTEM_ELEM);
    627     String message=null;   
    628     for (int i=0; i<commands.getLength(); i++) {
    629         // all the commands should be Elements
    630         Element elem = (Element)commands.item(i);
    631         String action = elem.getAttribute(GSXML.TYPE_ATT);
    632         if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE)) {
    633         String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
    634         if (subset.equals("")) {
    635             // need to reconfigure the service cluster
    636            
    637             if (this.configure()) {
    638             Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " reconfigured");
    639             response.appendChild(s);
    640            
    641             } else {
    642             Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " could not be reconfigured");
    643             response.appendChild(s);
    644             }
    645         } else if (this.configureSubset(subset)) {
    646             Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " "+subset+" reconfigured");
    647             response.appendChild(s);
    648         } else {
    649             Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " "+subset + " could not be reconfigured");
    650             response.appendChild(s);
    651         }
    652         continue;
    653         } // configure action
    654        
    655         String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
    656         String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
    657         if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE)) {
    658         Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "activate action not yet implemented - does it even make sense in this context??");
    659         response.appendChild(s);
    660         } else if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE)) {
    661         if (module_type.equals(GSXML.SERVICE_ELEM)) {
    662             // deactivate the service
    663             // remove from service_map
    664             this.service_map.remove(module_name);
    665             Element service_elem = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, module_name);
    666             service_list.removeChild(service_elem);         
    667             message = module_type+": "+module_name+" deactivated";
    668         } else {
    669             message = "can't deactivate "+module_type+" type modules!";}
    670         Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
    671         response.appendChild(s);
    672         } else {
    673         logger.error("cant process system request, action "+action);
    674         continue;
    675         }
    676     } // for each command
    677     return response;
    678     }
    679 
    680     /**
    681      * do a configure on only part of the collection
    682      */
    683     protected boolean configureSubset(String subset) {
    684    
    685     File configFile = new File(GSFile.siteConfigFile(this.site_home));
    686     if (!configFile.exists() ) {
    687         logger.error("site config file: "+configFile.getPath()+" not found!");
    688         // wont be able to do any of the requests
    689         return false;
    690        
    691     }
    692    
    693     Document site_config_doc  = this.converter.getDOM(configFile);
    694     if (site_config_doc == null) {
    695         logger.error("could not read in site config file: "+configFile.getPath());
    696         return false;
    697     }
    698    
    699     Element site_config_elem = site_config_doc.getDocumentElement();
    700     Element cluster_config_elem = GSXML.getNamedElement((Element)GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER), GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, this.cluster_name);
    701     if (cluster_config_elem == null) {
    702         logger.error("site config file: "+configFile.getPath()+" has no element for cluster "+this.cluster_name);
    703         // wont be able to do any of teh requests
    704         return false;
    705        
    706     }
    707     if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
    708         Element service_rack_list = (Element)GSXML.getChildByTagName(cluster_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
    709         clearServices();
    710         return configureServiceRackList(service_rack_list, null);
    711     } else if (subset.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER)) {
    712         this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    713         Element metadata_list = (Element)GSXML.getChildByTagName(cluster_config_elem, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    714         return addMetadata(metadata_list);
    715     } else if (subset.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
    716         this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
    717         Element import_list = (Element)GSXML.getChildByTagName(cluster_config_elem,GSXML.IMPORT_ELEM);
    718         if (import_list != null)
    719         {
    720             Element plugin_item_list = (Element)GSXML.getChildByTagName(cluster_config_elem,GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
    721             return addPlugins(plugin_item_list);
    722             }
    723         else
    724             return false;
    725     } else {
    726         logger.error("cannot process system request, configure "+subset);
    727         return false;
    728     }
    729 
    730     }
    731 
    732 
    733     protected boolean addAllDisplayInfo(Element description, String lang) {
    734    
    735     NodeList items = this.display_item_list.getChildNodes();
    736     for (int i=0; i<items.getLength(); i++) { // for each key
    737         Element m = (Element) items.item(i);
    738         // find the child with the correct language
    739         Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
    740         if (new_m==null && lang != DEFAULT_LANG) {
    741         // use the default lang
    742         new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
    743         }
    744         if (new_m==null) {
    745         // just get the first one
    746         new_m = (Element)GSXML.getChildByTagName(m, GSXML.DISPLAY_TEXT_ELEM);
    747         }
    748         description.appendChild(new_m.cloneNode(true));
    749     }
    750     return true;
    751    
    752     }
    753 
    754    
    755     protected Element getDisplayTextElement(String key, String lang) {
    756    
    757     Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, key);
    758     if (this_item == null) {
    759         return null;
    760     }
    761 
    762     Element this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
    763     if (this_lang == null && lang != DEFAULT_LANG) {
    764         // try the default
    765         this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
    766     }
    767     if (this_lang == null) {
    768         // just return the first one
    769         return GSXML.getFirstElementChild(this_item);//(Element)this_item.getFirstChild().cloneNode(true);
    770     }
    771     return (Element)this_lang.cloneNode(true);
    772    
    773     }
    774     public HashMap getServiceMap() {
    775         return service_map;
    776     }
     734
     735
     736    protected boolean addAllDisplayInfo(Element description, String lang) {
     737       
     738        NodeList items = this.display_item_list.getChildNodes();
     739        for (int i=0; i<items.getLength(); i++) { // for each key
     740            Element m = (Element) items.item(i);
     741            // find the child with the correct language
     742            Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
     743            if (new_m==null && lang != DEFAULT_LANG) {
     744                // use the default lang
     745                new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
     746            }
     747            if (new_m==null) {
     748                // just get the first one
     749                new_m = (Element)GSXML.getChildByTagName(m, GSXML.DISPLAY_TEXT_ELEM);
     750            }
     751            description.appendChild(new_m.cloneNode(true));
     752        }
     753        return true;
     754       
     755    }
     756
     757   
     758    protected Element getDisplayTextElement(String key, String lang) {
     759       
     760        Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, key);
     761        if (this_item == null) {
     762            return null;
     763        }
     764
     765        Element this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
     766        if (this_lang == null && lang != DEFAULT_LANG) {
     767            // try the default
     768            this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
     769        }
     770        if (this_lang == null) {
     771            // just return the first one
     772            return GSXML.getFirstElementChild(this_item);//(Element)this_item.getFirstChild().cloneNode(true);
     773        }
     774        return (Element)this_lang.cloneNode(true);
     775       
     776    }
     777    public HashMap getServiceMap() {
     778        return service_map;
     779    }
    777780}
    778781
Note: See TracChangeset for help on using the changeset viewer.