Changeset 24221


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

Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3
Files:
6 edited

Legend:

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

    r24118 r24221  
    588588        }
    589589       
    590         //GSXML.printXMLNode(xml_message);
    591590        Node xml_result = this.recept.process(xml_message);
    592591        encodeURLs (xml_result, response);
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/Collection.java

    r24203 r24221  
    11/*
    2  *    Collection.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*    Collection.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*/
    1919package org.greenstone.gsdl3.collection;
    2020
     
    4949
    5050/**
    51  * Represents a collection in Greenstone. A collection is an extension of
    52  * a ServiceCluster - it has local data that the services use.
    53  *
    54  * @author <a href="mailto:[email protected]">Katherine Don</a>
    55  * @see ModuleInterface
    56  */
     51* Represents a collection in Greenstone. A collection is an extension of
     52* a ServiceCluster - it has local data that the services use.
     53*
     54* @author <a href="mailto:[email protected]">Katherine Don</a>
     55* @see ModuleInterface
     56*/
    5757public class Collection
    58     extends ServiceCluster {
    59 
    60     static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
    61 
    62     /** is this collection being tidied */
    63     protected boolean useBook = false;
    64     /** is this collection public or private */
    65     protected boolean is_public = true;
    66 
    67     /** does this collection provide the OAI service */
    68     protected boolean has_oai = true;
    69     /** time when this collection was built */
    70     protected long lastmodified = 0;
    71     /** earliestDatestamp of this collection. Necessary for OAI */
    72     protected long earliestDatestamp = 0;
    73 
    74 
    75     /** An element containing the serviceRackList element of buildConfig.xml, used to determine whether it contains
    76      *  the OAIPMH serviceRack
    77      */
    78     //protected Element service_rack_list = null;
    79    
    80     protected XMLTransformer transformer = null;
    81     /** same as setClusterName */
    82     public void setCollectionName(String name) {
    83     setClusterName(name);
    84     }
    85 
    86     public Collection() {
    87     super();
    88     this.description = this.doc.createElement(GSXML.COLLECTION_ELEM);
     58extends ServiceCluster {
     59
     60    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
     61
     62    /** is this collection being tidied */
     63    protected boolean useBook = false;
     64    /** is this collection public or private */
     65    protected boolean is_public = true;
     66
     67    /** does this collection provide the OAI service */
     68    protected boolean has_oai = true;
     69    /** time when this collection was built */
     70    protected long lastmodified = 0;
     71    /** earliestDatestamp of this collection. Necessary for OAI */
     72    protected long earliestDatestamp = 0;
     73
     74
     75    /** An element containing the serviceRackList element of buildConfig.xml, used to determine whether it contains
     76    *  the OAIPMH serviceRack
     77    */
     78    //protected Element service_rack_list = null;
    8979   
    90     }
     80    protected XMLTransformer transformer = null;
     81    /** same as setClusterName */
     82    public void setCollectionName(String name) {
     83        setClusterName(name);
     84    }
     85
     86    public Collection() {
     87        super();
     88        this.description = this.doc.createElement(GSXML.COLLECTION_ELEM);
     89       
     90    }
    9191   
    92     /**
    93      * Configures the collection.
    94      *
    95      * gsdlHome and collectionName must be set before configure is called.
    96      *
    97      * the file buildcfg.xml is located in gsdlHome/collect/collectionName
    98      * collection metadata is obtained, and services loaded.
    99      *
    100      * @return true/false on success/fail
    101      */
    102     public boolean configure() {
     92    /**
     93    * Configures the collection.
     94    *
     95    * gsdlHome and collectionName must be set before configure is called.
     96    *
     97    * the file buildcfg.xml is located in gsdlHome/collect/collectionName
     98    * collection metadata is obtained, and services loaded.
     99    *
     100    * @return true/false on success/fail
     101    */
     102    public boolean configure() {
     103       
     104        if (this.site_home == null || this.cluster_name== null) {
     105            logger.error("Collection: site_home and collection_name must be set before configure called!");
     106            return false;
     107        }
     108       
     109        Element coll_config_xml = loadCollConfigFile();
     110        Element build_config_xml = loadBuildConfigFile();
     111       
     112        if (coll_config_xml==null||build_config_xml==null) {
     113            return false;
     114        }
     115       
     116        // get the collection type attribute
     117        Element search = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SEARCH_ELEM);
     118        if(search != null) {
     119            col_type = search.getAttribute(GSXML.TYPE_ATT);
     120        }
     121       
     122        Element browse = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.INFODB_ELEM);
     123        if(browse != null) {
     124            db_type = browse.getAttribute(GSXML.TYPE_ATT);
     125        }
     126        else
     127        {
     128            db_type = "gdbm"; //The default
     129        }
     130        System.err.println("DB_TYPE = "  + db_type);
     131       
     132        // process the metadata and display items
     133        findAndLoadInfo(coll_config_xml, build_config_xml);
     134       
     135        // now do the services
     136        configureServiceRacks(coll_config_xml, build_config_xml);
     137
     138        return true;
     139       
     140    }
     141
     142    public boolean useBook() {
     143        return useBook;
     144    }
    103145   
    104     if (this.site_home == null || this.cluster_name== null) {
    105         logger.error("Collection: site_home and collection_name must be set before configure called!");
    106         return false;
     146    public boolean isPublic() {
     147        return is_public;
     148    }
     149    // Not used anymore by the OAIReceptionist to find out the earliest datestamp
     150    // amongst all oai collections in the repository. May be useful generally.
     151    public long getLastmodified() {
     152        return lastmodified;
     153    }
     154    //used by the OAIReceptionist to find out the earliest datestamp amongst all oai collections in the repository
     155    public long getEarliestDatestamp() {
     156        return earliestDatestamp;
     157    }
     158
     159    /** whether the service_map in ServiceCluster.java contains the service 'OAIPMH'
     160    *  11/06/2007 xiao
     161    */
     162    public boolean hasOAI() {
     163        return has_oai;
     164    }
     165    /**
     166    * load in the collection config file into a DOM Element
     167    */   
     168    protected Element loadCollConfigFile() {
     169
     170        File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
     171       
     172        if (!coll_config_file.exists()) {
     173            logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+coll_config_file+" does not exist");
     174            return null;
     175        }
     176        // get the xml for both files
     177        Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
     178        Element coll_config_elem = null;
     179        if (coll_config_doc != null) {
     180            coll_config_elem = coll_config_doc.getDocumentElement();
     181        }
     182        return coll_config_elem;
     183
    107184    }
    108185   
    109     Element coll_config_xml = loadCollConfigFile();
    110     Element build_config_xml = loadBuildConfigFile();
    111    
    112     if (coll_config_xml==null||build_config_xml==null) {
    113         return false;
    114     }
    115    
    116     // get the collection type attribute
    117     Element search = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SEARCH_ELEM);
    118     if(search!=null) {
    119       col_type = search.getAttribute(GSXML.TYPE_ATT);
    120     }
    121    
    122     // process the metadata and display items
    123     findAndLoadInfo(coll_config_xml, build_config_xml);
    124    
    125     // now do the services
    126     configureServiceRacks(coll_config_xml, build_config_xml);
    127 
    128     return true;
    129    
    130     }
    131 
    132     public boolean useBook() {
    133         return useBook;
    134     }
    135    
    136     public boolean isPublic() {
    137     return is_public;
    138     }
    139     // Not used anymore by the OAIReceptionist to find out the earliest datestamp
    140     // amongst all oai collections in the repository. May be useful generally.
    141     public long getLastmodified() {
    142       return lastmodified;
    143     }
    144     //used by the OAIReceptionist to find out the earliest datestamp amongst all oai collections in the repository
    145     public long getEarliestDatestamp() {
    146     return earliestDatestamp;
    147     }
    148 
    149     /** whether the service_map in ServiceCluster.java contains the service 'OAIPMH'
    150      *  11/06/2007 xiao
    151      */
    152     public boolean hasOAI() {
    153       return has_oai;
    154     }
    155     /**
    156      * load in the collection config file into a DOM Element
    157      */   
    158     protected Element loadCollConfigFile() {
    159 
    160     File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
    161    
    162     if (!coll_config_file.exists()) {
    163         logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+coll_config_file+" does not exist");
    164         return null;
    165     }
    166     // get the xml for both files
    167     Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
    168     Element coll_config_elem = null;
    169     if (coll_config_doc != null) {
    170         coll_config_elem = coll_config_doc.getDocumentElement();
    171     }
    172     return coll_config_elem;
    173 
    174     }
    175    
    176     /**
    177      * load in the collection build config file into a DOM Element
    178      */       
    179     protected Element loadBuildConfigFile() {
    180    
    181     File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
    182     if (!build_config_file.exists()) {
    183         logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+build_config_file+" does not exist");
    184         return null;
    185     }
    186     Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
    187     Element build_config_elem = null;
    188     if (build_config_doc != null) {
    189         build_config_elem = build_config_doc.getDocumentElement();
    190     }
    191 
    192     lastmodified = build_config_file.lastModified();
    193  
    194     return build_config_elem;
    195     }
    196 
    197     /**
    198      * find the metadata and display elems from the two config files and add it to the appropriate lists
    199      */
    200     protected boolean findAndLoadInfo(Element coll_config_xml,
    201                       Element build_config_xml){
    202              
    203       // metadata
    204       Element meta_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    205       addMetadata(meta_list);
    206       meta_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    207       addMetadata(meta_list);
    208      
    209       meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
    210       GSXML.addMetadata(this.doc, meta_list, "httpPath", this.site_http_address+"/collect/"+this.cluster_name);
    211       addMetadata(meta_list);
    212      
    213       // display stuff
    214       Element display_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
    215       if (display_list != null) {
    216         resolveMacros(display_list);
    217         addDisplayItems(display_list);
    218       }
    219      
    220       //check whether the html are tidy or not
    221       Element import_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.IMPORT_ELEM);
    222       if (import_list != null) {
    223         Element plugin_list = (Element)GSXML.getChildByTagName(import_list, GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
    224         addPlugins(plugin_list);
    225         if (plugin_list != null){
    226           Element plugin_elem = (Element)GSXML.getNamedElement(plugin_list, GSXML.PLUGIN_ELEM, GSXML.NAME_ATT, "HTMLPlugin");
    227           if (plugin_elem != null) {
    228             //get the option
    229             Element option_elem = (Element)GSXML.getNamedElement(plugin_elem, GSXML.PARAM_OPTION_ELEM, GSXML.NAME_ATT, "-use_realistic_book");
    230             if (option_elem != null) {
    231               useBook = true;
    232             }
    233           }
    234         }
    235       }
    236       meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
    237       if (useBook == true)
    238         GSXML.addMetadata(this.doc, meta_list, "tidyoption", "tidy");
    239       else
    240         GSXML.addMetadata(this.doc, meta_list, "tidyoption", "untidy");
    241       addMetadata(meta_list);
    242      
    243       // check whether we are public or not
    244       if (meta_list != null) {
    245         Element meta_elem = (Element) GSXML.getNamedElement(metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "public");
    246         if (meta_elem != null) {
    247           String value = GSXML.getValue(meta_elem).toLowerCase().trim();
    248           if (value.equals("false")) {
    249             is_public = false;
    250           }
    251         }
    252       }
    253       return true;
    254 
    255     }
    256 
    257   protected boolean configureServiceRacks(Element coll_config_xml,
    258                       Element build_config_xml){
    259     clearServices();
    260     Element service_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
    261     configureServiceRackList(service_list, coll_config_xml);
    262        
    263     // collection Config may also contain manually added service racks
    264     service_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
    265     if (service_list != null) {
    266       configureServiceRackList(service_list, build_config_xml);
    267      
    268       // Check for oai
    269       Element oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, OAIXML.NAME, OAIXML.OAIPMH);
    270       if (oai_service_rack == null) {
    271     has_oai = false;
    272     logger.info("No oai for collection: " + this.cluster_name);
    273    
    274       } else {
    275     has_oai = true;
    276    
    277     // extract earliestDatestamp from the buildconfig.xml for OAI
    278     Element metadata_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
    279 
    280     if(metadata_list != null) {
    281         NodeList children = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
    282                // can't do getChildNodes(), because whitespace, such as newlines, creates Text nodes
    283         for (int i = 0; i < children.getLength(); i++) {
    284         Element metadata = (Element)children.item(i);
    285         if(metadata.getAttribute(GSXML.NAME_ATT).equals(OAIXML.EARLIEST_DATESTAMP)) {
    286             String earliestDatestampStr = GSXML.getValue(metadata);
    287             if(!earliestDatestampStr.equals("")) {
    288             earliestDatestamp = Long.parseLong(earliestDatestampStr);
    289             }
    290             break; // found a metadata element with name=earliestDatestamp in buildconfig
    291         }
    292         }
    293     }
    294    
    295     // If at the end of this, there is no value for earliestDatestamp, print out a warning
    296     logger.warn("No earliestDatestamp in buildConfig.xml for collection: " + this.cluster_name + ". Defaulting to 0.");
    297 
    298       }
    299     } else { // no list of services (no ServiceRackList), so no oai_service_rack either
    300     // explicitly set has_oai to false here, since it's initialised to true by default
    301     has_oai = false;
    302     }
    303     return true;
    304   }
    305  
    306   protected boolean resolveMacros(Element display_list) {
    307     if (display_list==null) return false;
    308     NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
    309     if (displaynodes.getLength()>0) {
    310         String http_site = this.site_http_address;
    311         String http_collection = this.site_http_address +"/collect/"+this.cluster_name;
    312         for(int k=0; k<displaynodes.getLength(); k++) {
    313         Element d = (Element) displaynodes.item(k);
    314         String text = GSXML.getNodeText(d);
    315         text = StringUtils.replace(text, "_httpsite_", http_site);
    316         text = StringUtils.replace(text, "_httpcollection_", http_collection);
    317         GSXML.setNodeText(d, text);
    318         }
    319     }
    320     return true;
    321     }
    322     /**
    323      * do a configure on only part of the collection
    324      */
    325     protected boolean configureSubset(String subset) {
    326 
    327     // need the coll config files
    328     Element coll_config_elem = loadCollConfigFile();
    329     Element build_config_elem = loadBuildConfigFile();
    330     if (coll_config_elem == null||build_config_elem == null) {
    331         // wont be able to do any of the requests
    332         return false;
    333     }   
    334    
    335     if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
    336       return configureServiceRacks(coll_config_elem, build_config_elem);
     186    /**
     187    * load in the collection build config file into a DOM Element
     188    */       
     189    protected Element loadBuildConfigFile() {
     190       
     191        File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
     192        if (!build_config_file.exists()) {
     193            logger.error("Collection: couldn't configure collection: "+this.cluster_name+", "+build_config_file+" does not exist");
     194            return null;
     195        }
     196        Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
     197        Element build_config_elem = null;
     198        if (build_config_doc != null) {
     199            build_config_elem = build_config_doc.getDocumentElement();
     200        }
     201
     202        lastmodified = build_config_file.lastModified();
     203
     204        return build_config_elem;
     205    }
     206
     207    /**
     208    * find the metadata and display elems from the two config files and add it to the appropriate lists
     209    */
     210    protected boolean findAndLoadInfo(Element coll_config_xml,
     211    Element build_config_xml){
     212       
     213        // metadata
     214        Element meta_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     215        addMetadata(meta_list);
     216        meta_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     217        addMetadata(meta_list);
     218       
     219        meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     220        GSXML.addMetadata(this.doc, meta_list, "httpPath", this.site_http_address+"/collect/"+this.cluster_name);
     221        addMetadata(meta_list);
     222       
     223        // display stuff
     224        Element display_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
     225        if (display_list != null) {
     226            resolveMacros(display_list);
     227            addDisplayItems(display_list);
     228        }
     229       
     230        //check whether the html are tidy or not
     231        Element import_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.IMPORT_ELEM);
     232        if (import_list != null) {
     233            Element plugin_list = (Element)GSXML.getChildByTagName(import_list, GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
     234            addPlugins(plugin_list);
     235            if (plugin_list != null){
     236                Element plugin_elem = (Element)GSXML.getNamedElement(plugin_list, GSXML.PLUGIN_ELEM, GSXML.NAME_ATT, "HTMLPlugin");
     237                if (plugin_elem != null) {
     238                    //get the option
     239                    Element option_elem = (Element)GSXML.getNamedElement(plugin_elem, GSXML.PARAM_OPTION_ELEM, GSXML.NAME_ATT, "-use_realistic_book");
     240                    if (option_elem != null) {
     241                        useBook = true;
     242                    }
     243                }
     244            }
     245        }
     246        meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     247        if (useBook == true)
     248        GSXML.addMetadata(this.doc, meta_list, "tidyoption", "tidy");
     249        else
     250        GSXML.addMetadata(this.doc, meta_list, "tidyoption", "untidy");
     251        addMetadata(meta_list);
     252       
     253        // check whether we are public or not
     254        if (meta_list != null) {
     255            Element meta_elem = (Element) GSXML.getNamedElement(metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "public");
     256            if (meta_elem != null) {
     257                String value = GSXML.getValue(meta_elem).toLowerCase().trim();
     258                if (value.equals("false")) {
     259                    is_public = false;
     260                }
     261            }
     262        }
     263        return true;
     264
     265    }
     266
     267    protected boolean configureServiceRacks(Element coll_config_xml,
     268    Element build_config_xml){
     269        clearServices();
     270        Element service_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
     271        configureServiceRackList(service_list, coll_config_xml);
     272       
     273        // collection Config may also contain manually added service racks
     274        service_list = (Element)GSXML.getChildByTagName(coll_config_xml, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
     275        if (service_list != null) {
     276            configureServiceRackList(service_list, build_config_xml);
     277           
     278            // Check for oai
     279            Element oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, OAIXML.NAME, OAIXML.OAIPMH);
     280            if (oai_service_rack == null) {
     281                has_oai = false;
     282                logger.info("No oai for collection: " + this.cluster_name);
     283               
     284            } else {
     285                has_oai = true;
     286               
     287                // extract earliestDatestamp from the buildconfig.xml for OAI
     288                Element metadata_list = (Element)GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     289
     290                if(metadata_list != null) {
     291                    NodeList children = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
     292                    // can't do getChildNodes(), because whitespace, such as newlines, creates Text nodes
     293                    for (int i = 0; i < children.getLength(); i++) {
     294                        Element metadata = (Element)children.item(i);
     295                        if(metadata.getAttribute(GSXML.NAME_ATT).equals(OAIXML.EARLIEST_DATESTAMP)) {
     296                            String earliestDatestampStr = GSXML.getValue(metadata);
     297                            if(!earliestDatestampStr.equals("")) {
     298                                earliestDatestamp = Long.parseLong(earliestDatestampStr);
     299                            }
     300                            break; // found a metadata element with name=earliestDatestamp in buildconfig
     301                        }
     302                    }
     303                }
     304               
     305                // If at the end of this, there is no value for earliestDatestamp, print out a warning
     306                logger.warn("No earliestDatestamp in buildConfig.xml for collection: " + this.cluster_name + ". Defaulting to 0.");
     307
     308            }
     309        } else { // no list of services (no ServiceRackList), so no oai_service_rack either
     310            // explicitly set has_oai to false here, since it's initialised to true by default
     311            has_oai = false;
     312        }
     313        return true;
     314    }
     315
     316    protected boolean resolveMacros(Element display_list) {
     317        if (display_list==null) return false;
     318        NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
     319        if (displaynodes.getLength()>0) {
     320            String http_site = this.site_http_address;
     321            String http_collection = this.site_http_address +"/collect/"+this.cluster_name;
     322            for(int k=0; k<displaynodes.getLength(); k++) {
     323                Element d = (Element) displaynodes.item(k);
     324                String text = GSXML.getNodeText(d);
     325                text = StringUtils.replace(text, "_httpsite_", http_site);
     326                text = StringUtils.replace(text, "_httpcollection_", http_collection);
     327                GSXML.setNodeText(d, text);
     328            }
     329        }
     330        return true;
     331    }
     332    /**
     333    * do a configure on only part of the collection
     334    */
     335    protected boolean configureSubset(String subset) {
     336
     337        // need the coll config files
     338        Element coll_config_elem = loadCollConfigFile();
     339        Element build_config_elem = loadBuildConfigFile();
     340        if (coll_config_elem == null||build_config_elem == null) {
     341            // wont be able to do any of the requests
     342            return false;
     343        }   
     344       
     345        if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
     346            return configureServiceRacks(coll_config_elem, build_config_elem);
     347        }
     348
     349        if (subset.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER) || subset.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
     350            return findAndLoadInfo(coll_config_elem, build_config_elem);
     351           
     352        }
     353       
     354        logger.error("Collection: cant process system request, configure "+subset);
     355        return false;
    337356    }
    338357
    339     if (subset.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER) || subset.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
    340         return findAndLoadInfo(coll_config_elem, build_config_elem);
    341        
    342     }
    343    
    344     logger.error("Collection: cant process system request, configure "+subset);
    345     return false;
    346     }
    347 
    348  /** handles requests made to the ServiceCluster itself
    349      *
    350      * @param req - the request Element- <request>
    351      * @return the result Element - should be <response>
    352      */
    353     protected Element processMessage(Element request) {
    354 
    355     Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
    356     response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
    357     String type = request.getAttribute(GSXML.TYPE_ATT);
    358     String lang = request.getAttribute(GSXML.LANG_ATT);
    359     response.setAttribute(GSXML.TYPE_ATT, type);
    360 
    361     logger.error("Collection received a message, attempting to process");
    362 
    363     if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING)) {
    364         logger.error("Received format string request");
    365 
    366         String subaction = request.getAttribute("subaction");
    367         logger.error("Subaction is " + subaction);
    368 
    369         String service = request.getAttribute("service");
    370         logger.error("Service is " + service);
    371 
    372         String classifier = null;
    373         if(service.equals("ClassifierBrowse"))
    374         {
    375             classifier = request.getAttribute("classifier");
    376             logger.error("Classifier is " + classifier);
    377         }
    378 
    379         Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
    380         //String format_string = GSXML.getNodeText(format_element);
    381         Element format_statement = (Element) format_element.getFirstChild();
    382 
    383         //logger.error("Format string: " + format_string);
    384         logger.error("Config file location = " + GSFile.collectionConfigFile(this.site_home, this.cluster_name));
    385 
    386         // check for version file
    387 
    388         String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
    389         logger.error("Directory is " + directory);
    390 
    391         String version_filename = "";
    392         if(service.equals("ClassifierBrowse"))
    393             version_filename = directory + "browse_"+classifier+"_format_statement_version.txt";
    394         else
    395             version_filename = directory + "query_format_statement_version.txt";
    396 
    397         File version_file = new File(version_filename);
    398         logger.error("Version filename is " + version_filename);
    399 
    400 
    401         if(subaction.equals("update"))
    402         {
    403             String version_number = "1";
    404             BufferedWriter writer;
    405 
    406             try{
    407 
    408                 if(version_file.exists())
    409                 {
    410                     // Read version
    411                     BufferedReader reader = new BufferedReader(new FileReader(version_filename));
    412                     version_number = reader.readLine();
    413                     int aInt = Integer.parseInt(version_number) + 1;
    414                     version_number = Integer.toString(aInt);
    415                     reader.close();
    416                 }
    417                 else{
    418                     // Create
    419                     version_file.createNewFile();
    420                     writer = new BufferedWriter(new FileWriter(version_filename));
    421                     writer.write(version_number);
    422                     writer.close();
    423                 }
    424 
    425                 // Write version file
    426                 String format_statement_filename = "";
    427 
    428                 if(service.equals("ClassifierBrowse"))
    429                     format_statement_filename = directory + "browse_"+classifier+"_format_statement_v" + version_number + ".txt";
    430                 else
    431                     format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
    432 
    433                 logger.error("Format statement filename is " + format_statement_filename);
    434 
    435                 // Write format statement
    436                 String format_string = this.converter.getString(format_statement); //GSXML.xmlNodeToString(format_statement);
    437                 writer = new BufferedWriter(new FileWriter(format_statement_filename));
    438                 writer.write(format_string);
    439                 writer.close();
    440 
    441                 // Update version number
    442                 writer = new BufferedWriter(new FileWriter(version_filename));
    443                 writer.write(version_number);
    444                 writer.close();
    445 
    446             } catch (IOException e) {
    447                 logger.error("IO Exception "+e);
    448             }
    449         }
    450 
    451         if(subaction.equals("save"))
    452         {
    453             logger.error("SAVE format statement");
    454 
    455             try{
    456 
    457                 // Convert format string to a document
    458                 //DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    459                 //DocumentBuilder builder = factory.newDocumentBuilder();
    460                 //String input = "<html><head><title></title></head><body>" + format_string + "</body></html>";
    461                 //String input = format_string.substring(0,format_string.length()-1)+"</xml>";
    462                 //logger.error(input);
    463                 //InputSource is = new InputSource( new StringReader( input ) );
    464                 //logger.error("About to parse format string");
    465                 //Document format_statement = (Document) builder.parse( is );
    466                 //logger.error("Done parsing format string");
    467 
    468                 // open collectionConfig.xml and read in to w3 Document
    469                 String collection_config = directory + "collectionConfig.xml";
    470                 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
    471            
    472                 //String tag_name = "";
    473                 int k;
    474                 int index;
    475                 Element elem;
    476                 // Try importing entire tree to this.doc so we can add and remove children at ease
    477                 //Node current_node = this.doc.importNode(GSXML.getChildByTagName(config, "CollectionConfig"),true);
    478                 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
    479                 NodeList current_node_list;
    480    
    481                 logger.error("Service is "+service);
    482 
    483                 if(service.equals("ClassifierBrowse"))
    484                 {
    485                     //tag_name = "browse";
    486                     // if CLX then need to look in <classifier> X then <format>
    487                     // default is <browse><format>
    488 
    489                     logger.error("Looking for browse");
    490                     current_node = GSXML.getChildByTagName(current_node, "browse");
    491 
    492                     // find CLX
    493                     if(classifier != null)
    494                     {
    495                         logger.error("Classifier is not null");
    496                         logger.error("Classifier is "+classifier);
    497                         current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
    498                         index = Integer.parseInt(classifier.substring(2)) - 1;
    499                         logger.error("classifier index is "+index);
    500                         // index should be given by X-1
    501                         current_node = current_node_list.item(index);
    502                         // what if classifier does not have a format tag?
    503                         if(GSXML.getChildByTagName(current_node, "format") == null)
    504                         {
    505                             logger.error("ERROR: valid classifier but does not have a format child");
    506                             // well then create a format tag
    507                             Element format_tag = config.createElement("format");
    508                             current_node = (Node) current_node.appendChild(format_tag);
    509                             //current_node = (Node) format_tag;
    510                         }
    511                        
    512                         else{
    513                             current_node = GSXML.getChildByTagName(current_node, "format");
    514                         }
    515 
    516                     }
    517                     else{
    518                         logger.error("Classifier is null");
    519                         // To support all classifiers, set classifier to null?  There is the chance here that the format tag does not exist
    520                         if(GSXML.getChildByTagName(current_node, "format") == null)
    521                         {
    522                             logger.error("ERROR: classifier does not have a format child");
    523                             // well then create a format tag
    524                             Element format_tag = config.createElement("format");
    525                             current_node = (Node) current_node.appendChild(format_tag);
    526                             //current_node = (Node) format_tag;
    527                         }
    528                         else
    529                             current_node = GSXML.getChildByTagName(current_node, "format");
    530                     }
    531                 }
    532                 else if(service.equals("AllClassifierBrowse"))
    533                 {
    534                     logger.error("Looking for browse");
    535                     current_node = GSXML.getChildByTagName(current_node, "browse");
    536                     if(GSXML.getChildByTagName(current_node, "format") == null)
    537                     {
    538                         logger.error("ERROR AllClassifierBrowse: all classifiers do not have a format child");
    539                         // well then create a format tag
    540                         Element format_tag = config.createElement("format");
    541                         current_node = (Node) current_node.appendChild(format_tag);
    542                         //current_node = (Node) format_tag;
    543                     }
    544                     else
    545                         current_node = GSXML.getChildByTagName(current_node, "format");
    546                 }
    547                 else
    548                 {
    549                     // look in <format> with no attributes
    550                     logger.error("I presume this is search");
    551            
    552                     current_node_list = GSXML.getChildrenByTagName(current_node, "search");
    553                     for(k=0; k<current_node_list.getLength(); k++)
    554                     {
    555                         current_node = current_node_list.item(k);
    556                         // if current_node has no attributes then break
    557                         elem = (Element) current_node;
    558                         if(elem.hasAttribute("name")==false)
    559                             break;
    560                     }
    561                 }
    562 
    563                 // Current_node should be a format tag
    564                 elem = (Element) current_node;
    565    
    566                 logger.error("*Current_node = " + elem.getNodeName());
    567 
    568                 // seems we want to remove current child/ren and replace with format_statement's child/ren?
    569 
    570                 // remove existing
    571                 current_node_list = elem.getChildNodes();
    572                 logger.error("About to remove old children");
    573                 for(k=0; k<current_node_list.getLength(); k++)
    574                 {
    575                     current_node = elem.removeChild(current_node_list.item(k));
    576                 }
    577 
    578                 logger.error("old nodes removed");
    579 
    580                 // append new but we have a string!
    581                 //GSXML.setNodeText(elem, "THIS IS A TEST");
    582                 //GSXML.setNodeText(elem, format_string);
    583 
    584                 current_node_list = format_statement.getChildNodes();
    585                 for(k=0; k<current_node_list.getLength(); k++)
    586                 {
    587                     //if(transformed.getNodeType() == Node.DOCUMENT_NODE)
    588                     //transformed = ((Document)transformed).getDocumentElement();
    589                     //logger.error("Node type: "+current_node_list.item(k).getNodeType());
    590                     if(current_node_list.item(k).getNodeType() != Node.PROCESSING_INSTRUCTION_NODE)
    591                         elem.appendChild(config.importNode(current_node_list.item(k),true));
    592                 }
    593                 logger.error("new nodes added");
    594 
    595                 //String text = GSXML.getNodeText(elem);
    596                 //logger.error(text);
    597                 //text = text.replaceAll("_httpsite_", http_site);
    598                 //text = text.replaceAll("_httpcollection_", http_collection);
    599                 //GSXML.setNodeText(d, text);
    600 
    601                 // Now convert config document to string for writing to file
    602                 logger.error("Convert config to string");
    603                 String new_config = this.converter.getString(config);
    604 
    605                 new_config = StringUtils.replace(new_config, "&lt;", "<");
    606                 new_config = StringUtils.replace(new_config, "&gt;", ">");
    607                 new_config = StringUtils.replace(new_config, "&quot;", "\"");
    608    
    609                 // Write to file (not original! for now)
    610                 BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config+".new"));
    611                 writer.write(new_config);
    612                 writer.close();
    613                 logger.error("All is happy with collection");
    614 
    615              } catch( Exception ex ) {
    616                 logger.error("There was an exception "+ex);
    617 
    618                 StringWriter sw = new StringWriter();
    619                 PrintWriter pw = new PrintWriter(sw, true);
    620                 ex.printStackTrace(pw);
    621                 pw.flush();
    622                 sw.flush();
    623                 logger.error(sw.toString());
    624             }
    625 
    626         }
    627     }
    628     else { // unknown type
    629         return super.processMessage(request);
    630 
    631     }
    632     return response;
    633     }
     358    /** handles requests made to the ServiceCluster itself
     359    *
     360    * @param req - the request Element- <request>
     361    * @return the result Element - should be <response>
     362    */
     363    protected Element processMessage(Element request) {
     364
     365        Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     366        response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
     367        String type = request.getAttribute(GSXML.TYPE_ATT);
     368        String lang = request.getAttribute(GSXML.LANG_ATT);
     369        response.setAttribute(GSXML.TYPE_ATT, type);
     370
     371        logger.error("Collection received a message, attempting to process");
     372
     373        if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING)) {
     374            logger.error("Received format string request");
     375
     376            String subaction = request.getAttribute("subaction");
     377            logger.error("Subaction is " + subaction);
     378
     379            String service = request.getAttribute("service");
     380            logger.error("Service is " + service);
     381
     382            String classifier = null;
     383            if(service.equals("ClassifierBrowse"))
     384            {
     385                classifier = request.getAttribute("classifier");
     386                logger.error("Classifier is " + classifier);
     387            }
     388
     389            Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
     390            //String format_string = GSXML.getNodeText(format_element);
     391            Element format_statement = (Element) format_element.getFirstChild();
     392
     393            //logger.error("Format string: " + format_string);
     394            logger.error("Config file location = " + GSFile.collectionConfigFile(this.site_home, this.cluster_name));
     395
     396            // check for version file
     397
     398            String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
     399            logger.error("Directory is " + directory);
     400
     401            String version_filename = "";
     402            if(service.equals("ClassifierBrowse"))
     403            version_filename = directory + "browse_"+classifier+"_format_statement_version.txt";
     404            else
     405            version_filename = directory + "query_format_statement_version.txt";
     406
     407            File version_file = new File(version_filename);
     408            logger.error("Version filename is " + version_filename);
     409
     410
     411            if(subaction.equals("update"))
     412            {
     413                String version_number = "1";
     414                BufferedWriter writer;
     415
     416                try{
     417
     418                    if(version_file.exists())
     419                    {
     420                        // Read version
     421                        BufferedReader reader = new BufferedReader(new FileReader(version_filename));
     422                        version_number = reader.readLine();
     423                        int aInt = Integer.parseInt(version_number) + 1;
     424                        version_number = Integer.toString(aInt);
     425                        reader.close();
     426                    }
     427                    else{
     428                        // Create
     429                        version_file.createNewFile();
     430                        writer = new BufferedWriter(new FileWriter(version_filename));
     431                        writer.write(version_number);
     432                        writer.close();
     433                    }
     434
     435                    // Write version file
     436                    String format_statement_filename = "";
     437
     438                    if(service.equals("ClassifierBrowse"))
     439                    format_statement_filename = directory + "browse_"+classifier+"_format_statement_v" + version_number + ".txt";
     440                    else
     441                    format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
     442
     443                    logger.error("Format statement filename is " + format_statement_filename);
     444
     445                    // Write format statement
     446                    String format_string = this.converter.getString(format_statement); //GSXML.xmlNodeToString(format_statement);
     447                    writer = new BufferedWriter(new FileWriter(format_statement_filename));
     448                    writer.write(format_string);
     449                    writer.close();
     450
     451                    // Update version number
     452                    writer = new BufferedWriter(new FileWriter(version_filename));
     453                    writer.write(version_number);
     454                    writer.close();
     455
     456                } catch (IOException e) {
     457                    logger.error("IO Exception "+e);
     458                }
     459            }
     460
     461            if(subaction.equals("save"))
     462            {
     463                logger.error("SAVE format statement");
     464
     465                try{
     466
     467                    // Convert format string to a document
     468                    //DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     469                    //DocumentBuilder builder = factory.newDocumentBuilder();
     470                    //String input = "<html><head><title></title></head><body>" + format_string + "</body></html>";
     471                    //String input = format_string.substring(0,format_string.length()-1)+"</xml>";
     472                    //logger.error(input);
     473                    //InputSource is = new InputSource( new StringReader( input ) );
     474                    //logger.error("About to parse format string");
     475                    //Document format_statement = (Document) builder.parse( is );
     476                    //logger.error("Done parsing format string");
     477
     478                    // open collectionConfig.xml and read in to w3 Document
     479                    String collection_config = directory + "collectionConfig.xml";
     480                    Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
     481                   
     482                    //String tag_name = "";
     483                    int k;
     484                    int index;
     485                    Element elem;
     486                    // Try importing entire tree to this.doc so we can add and remove children at ease
     487                    //Node current_node = this.doc.importNode(GSXML.getChildByTagName(config, "CollectionConfig"),true);
     488                    Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
     489                    NodeList current_node_list;
     490
     491                    logger.error("Service is "+service);
     492
     493                    if(service.equals("ClassifierBrowse"))
     494                    {
     495                        //tag_name = "browse";
     496                        // if CLX then need to look in <classifier> X then <format>
     497                        // default is <browse><format>
     498
     499                        logger.error("Looking for browse");
     500                        current_node = GSXML.getChildByTagName(current_node, "browse");
     501
     502                        // find CLX
     503                        if(classifier != null)
     504                        {
     505                            logger.error("Classifier is not null");
     506                            logger.error("Classifier is "+classifier);
     507                            current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
     508                            index = Integer.parseInt(classifier.substring(2)) - 1;
     509                            logger.error("classifier index is "+index);
     510                            // index should be given by X-1
     511                            current_node = current_node_list.item(index);
     512                            // what if classifier does not have a format tag?
     513                            if(GSXML.getChildByTagName(current_node, "format") == null)
     514                            {
     515                                logger.error("ERROR: valid classifier but does not have a format child");
     516                                // well then create a format tag
     517                                Element format_tag = config.createElement("format");
     518                                current_node = (Node) current_node.appendChild(format_tag);
     519                                //current_node = (Node) format_tag;
     520                            }
     521                           
     522                            else{
     523                                current_node = GSXML.getChildByTagName(current_node, "format");
     524                            }
     525
     526                        }
     527                        else{
     528                            logger.error("Classifier is null");
     529                            // To support all classifiers, set classifier to null?  There is the chance here that the format tag does not exist
     530                            if(GSXML.getChildByTagName(current_node, "format") == null)
     531                            {
     532                                logger.error("ERROR: classifier does not have a format child");
     533                                // well then create a format tag
     534                                Element format_tag = config.createElement("format");
     535                                current_node = (Node) current_node.appendChild(format_tag);
     536                                //current_node = (Node) format_tag;
     537                            }
     538                            else
     539                            current_node = GSXML.getChildByTagName(current_node, "format");
     540                        }
     541                    }
     542                    else if(service.equals("AllClassifierBrowse"))
     543                    {
     544                        logger.error("Looking for browse");
     545                        current_node = GSXML.getChildByTagName(current_node, "browse");
     546                        if(GSXML.getChildByTagName(current_node, "format") == null)
     547                        {
     548                            logger.error("ERROR AllClassifierBrowse: all classifiers do not have a format child");
     549                            // well then create a format tag
     550                            Element format_tag = config.createElement("format");
     551                            current_node = (Node) current_node.appendChild(format_tag);
     552                            //current_node = (Node) format_tag;
     553                        }
     554                        else
     555                        current_node = GSXML.getChildByTagName(current_node, "format");
     556                    }
     557                    else
     558                    {
     559                        // look in <format> with no attributes
     560                        logger.error("I presume this is search");
     561                       
     562                        current_node_list = GSXML.getChildrenByTagName(current_node, "search");
     563                        for(k=0; k<current_node_list.getLength(); k++)
     564                        {
     565                            current_node = current_node_list.item(k);
     566                            // if current_node has no attributes then break
     567                            elem = (Element) current_node;
     568                            if(elem.hasAttribute("name")==false)
     569                            break;
     570                        }
     571                    }
     572
     573                    // Current_node should be a format tag
     574                    elem = (Element) current_node;
     575                   
     576                    logger.error("*Current_node = " + elem.getNodeName());
     577
     578                    // seems we want to remove current child/ren and replace with format_statement's child/ren?
     579
     580                    // remove existing
     581                    current_node_list = elem.getChildNodes();
     582                    logger.error("About to remove old children");
     583                    for(k=0; k<current_node_list.getLength(); k++)
     584                    {
     585                        current_node = elem.removeChild(current_node_list.item(k));
     586                    }
     587
     588                    logger.error("old nodes removed");
     589
     590                    // append new but we have a string!
     591                    //GSXML.setNodeText(elem, "THIS IS A TEST");
     592                    //GSXML.setNodeText(elem, format_string);
     593
     594                    current_node_list = format_statement.getChildNodes();
     595                    for(k=0; k<current_node_list.getLength(); k++)
     596                    {
     597                        //if(transformed.getNodeType() == Node.DOCUMENT_NODE)
     598                        //transformed = ((Document)transformed).getDocumentElement();
     599                        //logger.error("Node type: "+current_node_list.item(k).getNodeType());
     600                        if(current_node_list.item(k).getNodeType() != Node.PROCESSING_INSTRUCTION_NODE)
     601                        elem.appendChild(config.importNode(current_node_list.item(k),true));
     602                    }
     603                    logger.error("new nodes added");
     604
     605                    //String text = GSXML.getNodeText(elem);
     606                    //logger.error(text);
     607                    //text = text.replaceAll("_httpsite_", http_site);
     608                    //text = text.replaceAll("_httpcollection_", http_collection);
     609                    //GSXML.setNodeText(d, text);
     610
     611                    // Now convert config document to string for writing to file
     612                    logger.error("Convert config to string");
     613                    String new_config = this.converter.getString(config);
     614
     615                    new_config = StringUtils.replace(new_config, "&lt;", "<");
     616                    new_config = StringUtils.replace(new_config, "&gt;", ">");
     617                    new_config = StringUtils.replace(new_config, "&quot;", "\"");
     618                   
     619                    // Write to file (not original! for now)
     620                    BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config+".new"));
     621                    writer.write(new_config);
     622                    writer.close();
     623                    logger.error("All is happy with collection");
     624
     625                } catch( Exception ex ) {
     626                    logger.error("There was an exception "+ex);
     627
     628                    StringWriter sw = new StringWriter();
     629                    PrintWriter pw = new PrintWriter(sw, true);
     630                    ex.printStackTrace(pw);
     631                    pw.flush();
     632                    sw.flush();
     633                    logger.error(sw.toString());
     634                }
     635
     636            }
     637        }
     638        else { // unknown type
     639            return super.processMessage(request);
     640
     641        }
     642        return response;
     643    }
    634644
    635645}
  • 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
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/ArchiveRetrieve.java

    r24211 r24221  
    11/*
    2 *    AbstractDocumentRetrieve.java
     2*    ArchiveRetrieve.java
    33*    a base class for retrieval services
    44
     
    2121package org.greenstone.gsdl3.service;
    2222
     23import org.greenstone.gsdl3.util.DBInfo;
    2324import org.greenstone.gsdl3.util.GSPath;
    2425import org.greenstone.gsdl3.util.GSXML;
     
    3132import java.io.File;
    3233import java.util.HashMap;
    33 
    34 /** Abstract class for Document Retrieval Services
    35 *
    36 * @author <a href="mailto:[email protected]">Katherine Don</a>
    37 */
    38 
    39 public class ArchiveRetrieve extends ServiceRack {
    40 
     34import java.util.Vector;
     35
     36public class ArchiveRetrieve extends ServiceRack
     37{
    4138    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ArchiveRetrieve.class.getName());
    4239
    43     protected static final String ARCHIVE_FILE_PATH_RETRIEVE_SERVICE = "ArchiveFilePathRetrieve";
     40    protected static final String DOCUMENT_FILE_PATH_RETRIEVE_SERVICE = "DocumentFilePathRetrieve";
     41    protected static final String ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE = "AssociatedImportFilesRetrieve";
     42    protected static final String SOURCE_FILE_OID_RETRIEVE = "SourceFileOIDRetrieve";
    4443   
    4544    protected SimpleCollectionDatabase coll_db = null;
    46    
    47     /** constructor */
    48     public ArchiveRetrieve()
    49     {
    50     }
    5145
    5246    /** configure this service */
    5347    public boolean configure(Element info, Element extra_info)
    5448    {
    55         if (!super.configure(info, extra_info)){
     49        if (!super.configure(info, extra_info))
     50        {
    5651            return false;
    5752        }
     
    6055        this.config_info = info;
    6156       
    62         Element archiveFilePathRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
    63         archiveFilePathRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
    64         archiveFilePathRetrieveService.setAttribute(GSXML.NAME_ATT, ARCHIVE_FILE_PATH_RETRIEVE_SERVICE);
    65         this.short_service_info.appendChild(archiveFilePathRetrieveService);
     57        Element documentFilePathRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
     58        documentFilePathRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
     59        documentFilePathRetrieveService.setAttribute(GSXML.NAME_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
     60        this.short_service_info.appendChild(documentFilePathRetrieveService);
     61       
     62        Element associatedImportFilesRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
     63        associatedImportFilesRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
     64        associatedImportFilesRetrieveService.setAttribute(GSXML.NAME_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
     65        this.short_service_info.appendChild(associatedImportFilesRetrieveService);
     66       
     67        Element sourceFileDocIDRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
     68        sourceFileDocIDRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
     69        sourceFileDocIDRetrieveService.setAttribute(GSXML.NAME_ATT, SOURCE_FILE_OID_RETRIEVE);
     70        this.short_service_info.appendChild(sourceFileDocIDRetrieveService);
    6671       
    6772        return true;
     
    7075    protected Element getServiceDescription(String service_id, String lang, String subset)
    7176    {
    72         Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
    73         service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
    74         service_elem.setAttribute(GSXML.NAME_ATT, ARCHIVE_FILE_PATH_RETRIEVE_SERVICE);
    75         return service_elem;
    76     }
    77    
    78     protected Element processArchiveFilePathRetrieve(Element request)
     77        if (service_id.equals(DOCUMENT_FILE_PATH_RETRIEVE_SERVICE))
     78        {
     79            Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
     80            service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
     81            service_elem.setAttribute(GSXML.NAME_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
     82            return service_elem;
     83        }
     84        else if (service_id.equals(ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE))
     85        {
     86            Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
     87            service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
     88            service_elem.setAttribute(GSXML.NAME_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
     89            return service_elem;
     90        }
     91        else if (service_id.equals(SOURCE_FILE_OID_RETRIEVE))
     92        {
     93            Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
     94            service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
     95            service_elem.setAttribute(GSXML.NAME_ATT, SOURCE_FILE_OID_RETRIEVE);
     96            return service_elem;
     97        }
     98        return null;
     99    }
     100   
     101    protected Element processDocumentFilePathRetrieve(Element request)
    79102    {
    80103        // Create a new (empty) result message
    81104        Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
    82         result.setAttribute(GSXML.FROM_ATT, ARCHIVE_FILE_PATH_RETRIEVE_SERVICE);
     105        result.setAttribute(GSXML.FROM_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
    83106        result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    84107       
     
    89112        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    90113        if (param_list == null) {
    91             GSXML.addError(this.doc, result, "DocumentMetadataRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     114            GSXML.addError(this.doc, result, "DocumentFilePathRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
    92115            return result; 
    93116        }
    94117        HashMap params = GSXML.extractParams(param_list, false);
    95118       
    96         String docID = (String) params.get("docID");
     119        String oid = (String) params.get("oid");
    97120        String collection = (String) params.get("c");
    98121       
    99         String assocFilePath = getAssocFilePathFromDocID(docID, collection, lang, uid);
     122        String assocFilePath = getAssocFilePathFromDocID(oid, collection, lang, uid);
    100123       
    101124        String docFilePath = this.site_home + File.separatorChar +
     
    105128            assocFilePath + File.separatorChar +
    106129            "doc.xml";
    107        
    108         Element metaElem = this.doc.createElement(GSXML.METADATA_ELEM);
    109         metaElem.setAttribute("name", "docFilePath");
    110         metaElem.setAttribute("value", docFilePath);
    111        
    112         logger.error("DOCFILEPATH = " + docFilePath);
    113        
    114         result.appendChild(metaElem);
     130
     131        Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     132        metadataList.appendChild(createMetadataElement("docfilepath", docFilePath));
     133        result.appendChild(metadataList);
    115134       
    116135        return result;
    117136    }
    118137   
    119     protected Element processArchiveAssociatedImportFilesRetrieve(Element request)
    120     {
    121         // find out what kind of database we have
    122         //Element database_type_elem = (Element) GSXML.getChildByTagName(this.config_info, GSXML.DATABASE_TYPE_ELEM);
    123         //String database_type = "jdbm";
    124         /*if (database_type_elem != null) {
    125             database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
    126         }
    127         if (database_type == null || database_type.equals("")) {
    128             database_type = "gdbm"; // the default
    129         }
    130        
    131         String db_ext = null;
    132         if (database_type.equalsIgnoreCase("jdbm")) {
    133             db_ext = ".jdb";
    134         } else {
    135             db_ext = ".gdb"; // assume gdbm
    136         }
    137        
    138        
    139                 coll_db = new SimpleCollectionDatabase(database_type);
    140         if (!coll_db.databaseOK()) {
    141             logger.error("Couldn't create the collection database of type "+database_type);
     138    protected Element processSourceFileOIDRetrieveService(Element request)
     139    {
     140        //Create a new (empty) result message
     141        Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
     142        result.setAttribute(GSXML.FROM_ATT, SOURCE_FILE_OID_RETRIEVE);
     143        result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
     144       
     145        String lang = request.getAttribute(GSXML.LANG_ATT);
     146        String uid = request.getAttribute(GSXML.USER_ID_ATT);
     147       
     148        // Get the parameters of the request
     149        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     150        if (param_list == null)
     151        {
     152            GSXML.addError(this.doc, result, "DocumentFilePathRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     153            return result; 
     154        }
     155        HashMap params = GSXML.extractParams(param_list, false);
     156       
     157        String srcFile = (String) params.get("srcfile");
     158        String collection = (String) params.get("c");
     159       
     160        //Find out what kind of database we have
     161        String databaseType = getDatabaseTypeFromCollection(collection, lang, uid);
     162        if (databaseType == null || databaseType.equals(""))
     163        {
     164            databaseType = "gdbm"; // the default
     165        }
     166       
     167        String dbExt = null;
     168        if (databaseType.equalsIgnoreCase("jdbm"))
     169        {
     170            dbExt = ".jdb";
     171        }
     172        else
     173        {
     174            dbExt = ".gdb"; // assume gdbm
     175        }
     176       
     177        coll_db = new SimpleCollectionDatabase(databaseType);
     178        if (!coll_db.databaseOK())
     179        {
     180            logger.error("Couldn't create the collection database of type "+databaseType);
    142181            return null;
    143182        }
    144         */
    145    
    146             /*coll_db.openDatabase(
     183
     184        coll_db.openDatabase
     185        (
    147186            this.site_home + File.separatorChar +
    148187            "collect" + File.separatorChar +
    149188            collection + File.separatorChar +
    150189            "archives" + File.separatorChar +
    151             "archiveinf-doc" + db_ext,
    152             SimpleCollectionDatabase.READ);
    153        
    154         logger.error("STUFF " + coll_db.getInfo(docID));
    155        
    156         coll_db.getInfo(docID);*/
    157         return null;
    158     }
    159    
    160     public String getAssocFilePathFromDocID(String docID, String collection, String lang, String uid)
     190            "archiveinf-src" + dbExt,
     191            SimpleCollectionDatabase.READ
     192        );
     193       
     194        DBInfo info = coll_db.getInfo(srcFile);
     195       
     196        if (info == null)
     197        {
     198            return result;
     199        }
     200       
     201        String oid = info.getInfo("oid");
     202       
     203        Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     204        metadataList.appendChild(createMetadataElement("oid", oid));
     205        result.appendChild(metadataList);
     206       
     207        return result;
     208    }
     209   
     210    protected Element processAssociatedImportFilesRetrieve(Element request)
     211    {
     212        //Create a new (empty) result message
     213        Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
     214        result.setAttribute(GSXML.FROM_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
     215        result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
     216       
     217        String lang = request.getAttribute(GSXML.LANG_ATT);
     218        String uid = request.getAttribute(GSXML.USER_ID_ATT);
     219       
     220        // Get the parameters of the request
     221        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     222        if (param_list == null)
     223        {
     224            GSXML.addError(this.doc, result, "AssociatedImportFilesRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
     225            return result; 
     226        }
     227        HashMap params = GSXML.extractParams(param_list, false);
     228       
     229        String oid = (String) params.get("oid");
     230        String collection = (String) params.get("c");
     231       
     232        String databaseType = getDatabaseTypeFromCollection(collection, lang, uid);
     233        if (databaseType == null || databaseType.equals(""))
     234        {
     235            databaseType = "gdbm"; // the default
     236        }
     237       
     238        String dbExt = null;
     239        if (databaseType.equalsIgnoreCase("jdbm"))
     240        {
     241            dbExt = ".jdb";
     242        }
     243        else
     244        {
     245            dbExt = ".gdb"; // assume gdbm
     246        }
     247       
     248        coll_db = new SimpleCollectionDatabase(databaseType);
     249        if (!coll_db.databaseOK())
     250        {
     251            logger.error("Couldn't create the collection database of type "+databaseType);
     252            return null;
     253        }
     254
     255        coll_db.openDatabase
     256        (
     257            this.site_home + File.separatorChar +
     258            "collect" + File.separatorChar +
     259            collection + File.separatorChar +
     260            "archives" + File.separatorChar +
     261            "archiveinf-doc" + dbExt,
     262            SimpleCollectionDatabase.READ
     263        );
     264       
     265        DBInfo info = coll_db.getInfo(oid);
     266       
     267        if (info == null)
     268        {
     269            return result;
     270        }
     271       
     272        String srcFile = info.getInfo("src-file");
     273        Vector data = info.getMultiInfo("assoc-file");
     274       
     275        Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     276        metadataList.appendChild(createMetadataElement("srcfile", srcFile));
     277       
     278        for (int i = 0; i < data.size(); i++)
     279        {
     280            metadataList.appendChild(createMetadataElement("assocfile", (String)data.get(i)));
     281        }
     282       
     283        result.appendChild(metadataList);
     284       
     285        return result;
     286    }
     287   
     288    public Element createMetadataElement(String name, String value)
     289    {
     290        Element metaElem = this.doc.createElement(GSXML.METADATA_ELEM);
     291        metaElem.setAttribute("name", name);
     292        metaElem.setAttribute("value", value);
     293        return metaElem;
     294    }
     295   
     296    public String getAssocFilePathFromDocID(String oid, String collection, String lang, String uid)
    161297    {
    162298        Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
     
    165301       
    166302        Element paramList = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    167 
    168         Element assocParam = this.doc.createElement(GSXML.PARAM_ELEM);
    169         assocParam.setAttribute("name", "metadata");
    170         assocParam.setAttribute("value", "assocfilepath");
    171         paramList.appendChild(assocParam);
     303        paramList.appendChild(createMetadataElement("metadata", "assocfilepath"));
    172304       
    173305        mr_query_request.appendChild(paramList);
    174306
    175         Element docList = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    176         Element doc = this.doc.createElement(GSXML.DOC_NODE_ELEM);
    177         doc.setAttribute(GSXML.NODE_ID_ATT, docID);
    178         docList.appendChild(doc);
    179         mr_query_request.appendChild(docList);
     307        Element docListElem = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
     308        Element docElem = this.doc.createElement(GSXML.DOC_NODE_ELEM);
     309        docElem.setAttribute(GSXML.NODE_ID_ATT, oid);
     310        docListElem.appendChild(docElem);
     311        mr_query_request.appendChild(docListElem);
    180312
    181313        Element response = (Element) this.router.process(mr_query_message);
     
    189321        return metadataElem.getFirstChild().getNodeValue();
    190322    }
     323   
     324    public String getDatabaseTypeFromCollection(String collection, String lang, String uid)
     325    {
     326        //Find out what kind of database we have
     327        Element dbTypeMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
     328        Element dbTypeRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, collection, lang, uid);
     329        dbTypeMessage.appendChild(dbTypeRequest);
     330        Element dbTypeResponse = (Element)this.router.process(dbTypeMessage);
     331       
     332        String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.COLLECTION_ELEM);
     333        Element collectionElem = (Element) GSXML.getNodeByPath(dbTypeResponse, path);
     334       
     335        if (collectionElem != null)
     336        {
     337            return collectionElem.getAttribute(GSXML.DB_TYPE_ATT);
     338        }
     339        return null;
     340    }
    191341}   
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/BerryBasket.java

    r23814 r24221  
    287287    // Get the parameters of the request
    288288    Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    289        
    290         //GSXML.printXMLNode(param_list);
    291289
    292290    if (param_list == null) {
     
    336334    // Get the parameters of the request
    337335    Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    338        
    339         //GSXML.printXMLNode(param_list);
    340336
    341337    if (param_list == null) {
     
    375371
    376372    protected Element processItemNum(Element request){
    377         // GSXML.printXMLNode(request);
    378373    Hashtable docsMap = updateDocMap(request);
    379374
     
    489484        // Get the parameters of the request
    490485        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    491        
    492         GSXML.printXMLNode(param_list);
    493486       
    494487        if (param_list == null) {
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r24214 r24221  
    8585  public static final String DOC_TYPE_ELEM = "docType";
    8686  public static final String SEARCH_ELEM = "search";
     87  public static final String INFODB_ELEM = "infodb";
    8788  public static final String INDEX_ELEM = "index";
    8889  public static final String INDEX_STEM_ELEM = "indexStem";
     
    125126  public static final String LANG_ATT = "lang";
    126127  public static final String TYPE_ATT = "type";
     128  public static final String DB_TYPE_ATT = "dbType";
    127129  public static final String VALUE_ATT = "value";
    128130  public static final String DEFAULT_ATT = "default";
     
    920922  }
    921923 
    922   public static void printXMLNode(Node e) {
    923     printXMLNode(e, 0) ;
     924  public static void printXMLNode(Node e, boolean printText) {
     925    printXMLNode(e, 0, printText) ;
    924926  }
    925927 
     
    977979  }
    978980 
    979   public static void printXMLNode(Node e, int depth) { //recursive method call using DOM API...
    980    
     981  public static void printXMLNode(Node e, int depth, boolean printText) { //recursive method call using DOM API...
     982   
     983    if(e == null){return;}
     984   
    981985    for (int i=0 ; i<depth ; i++)
    982986      System.out.print(' ') ;
    983987   
    984988    if (e.getNodeType() == Node.TEXT_NODE){
    985       // shouldn't we actually print the text here????
    986       System.out.println("text") ;
     989      if(printText){
     990        System.out.println(e.getNodeValue());
     991      }
     992      else {
     993        System.out.println("text");
     994      }
    987995      return ;
    988996    }
     
    10141022      int len = children.getLength();
    10151023      for (int i = 0; i < len; i++) {
    1016         printXMLNode(children.item(i), depth + 1);
     1024        printXMLNode(children.item(i), depth + 1, printText);
    10171025      }
    10181026     
Note: See TracChangeset for help on using the changeset viewer.