Ignore:
Timestamp:
2012-11-06T15:27:10+13:00 (11 years ago)
Author:
kjdon
Message:

trying to tidy up the collection classes. Move some general stuff to servicecluster and reuse the code a bit more

File:
1 edited

Legend:

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

    r25973 r26446  
    3232import org.greenstone.gsdl3.util.GSPath;
    3333import org.greenstone.gsdl3.util.GSXML;
     34import org.greenstone.gsdl3.util.SimpleMacroResolver;
    3435import org.greenstone.gsdl3.util.UserContext;
    3536import org.greenstone.gsdl3.util.XMLConverter;
     
    4243 * Implements ModuleInterface. Contains a list of services provided by the cluster, along with metadata about the cluster itself.
    4344 * a collection is a special type of cluster
    44  *  @author <a href="mailto:[email protected]">Katherine Don</a>
    45  *  @version $Revision$
    4645 *  @see ModuleInterface
    4746 */
     
    6160    /** The name of the cluster - for a collection, this is the collection name */
    6261    protected String cluster_name = null;
    63     /** collection type : mg, mgpp or lucene */
    64     protected String col_type = "";
    65     /** database type : gdbm, jdbm or sqlite */
    66     protected String db_type = "";
    67 
     62 
    6863    /** a reference to the message router */
    6964    protected MessageRouter router = null;
     
    8580    /** XML converter for String to DOM and vice versa */
    8681    protected XMLConverter converter = null;
     82  /** a MacroResolver for resolving macros in displayItems */
     83  protected SimpleMacroResolver macro_resolver = null;
    8784
    8885    /** container doc for description elements */
     
    9289    /** list of metadata - all metadata, regardless of language goes in here */
    9390    protected Element metadata_list = null;
    94     /** language specific stuff */
    95     //protected Element lang_specific_metadata_list = null;
     91  /** language specific display items */
     92
    9693    protected Element display_item_list = null;
     94  /** default values for servlet params */
     95  protected Element library_param_list = null;
    9796    /** the element that will have any descriptions passed back in */
    9897    protected Element description = null;
    9998
    10099    /** list of plugin */
    101     protected Element plugin_item_list = null;
     100    //protected Element plugin_item_list = null;
    102101
    103102    protected Element _globalFormat = null;
     
    139138        this.service_name_map = new HashMap<String, String>();
    140139        this.converter = new XMLConverter();
     140        this.macro_resolver = new SimpleMacroResolver();
    141141        this.doc = this.converter.newDOM();
    142142        this.description = this.doc.createElement(GSXML.CLUSTER_ELEM);
    143143        this.display_item_list = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
    144144        this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
    145         this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
     145        this.library_param_list = this.doc.createElement("libraryParamList");
     146        this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
     147        //this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
    146148    }
    147149
     
    156158     * parsed. This method will work with any subclass.
    157159     *
     160     * This is called by ServiceCluster itself when asked to do a reconfigure
    158161     * @return true if configure successful, false otherwise.
    159162     */
     
    167170        }
    168171        logger.info("configuring service cluster");
     172        macro_resolver.addMacro("_httpsite_", this.site_http_address);
    169173        // read the site configuration file
    170174        File config_file = new File(GSFile.siteConfigFile(this.site_home));
     
    187191        Element sc = GSXML.getNamedElement(cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, this.cluster_name);
    188192
     193        // this is probably a reconfigure, so clear all previous info
     194        clearServices();
     195        clearLocalData();
    189196        return this.configure(sc);
    190197    }
    191198
     199  /** this is called by configure(), but also by MR when it is loading up all the service clusters */
    192200    public boolean configure(Element service_cluster_info)
    193201    {
    194 
     202      configureLocalData(service_cluster_info);
     203        // //get the plugin info
     204        // Element import_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.IMPORT_ELEM);
     205        // if (import_list != null)
     206        // {
     207        //  Element plugin_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
     208        //  if (plugin_list != null)
     209        //  {
     210        //      if (!addPlugins(plugin_list))
     211        //      {
     212
     213        //          logger.error("couldn't configure the plugins");
     214        //      }
     215        //  }
     216        // }
     217
     218        // do the service racks
     219        // empty the service map in case this is a reconfigure
     220        //clearServices();
     221        Element service_rack_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
     222        logger.error("cluster service rack list =");
     223        logger.error(GSXML.xmlNodeToString(service_rack_list));
     224        if (service_rack_list == null)
     225        {
     226            // is this an error? could you ever have a service cluster
     227            // without service racks???
     228            logger.error(cluster_name+" has no service racks!!");
     229        }
     230        else
     231        {
     232
     233            if (!configureServiceRackList(service_rack_list, null))
     234            {
     235                logger.error("couldn't configure "+cluster_name+" service racks!!");
     236                return false;
     237            }
     238        }
     239
     240        return true;
     241    }
     242
     243    protected void configureLocalData(Element service_cluster_info) {
    195244        // get the metadata - for now just add it to the list
    196245        Element meta_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     
    208257        if (display_list != null)
    209258        {
     259          resolveMacros(display_list);
    210260            if (!addDisplayItems(display_list))
    211261            {
     
    215265        }
    216266
    217         //get the plugin info
    218         Element import_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.IMPORT_ELEM);
    219         if (import_list != null)
    220         {
    221             Element plugin_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
    222             if (plugin_list != null)
    223             {
    224                 if (!addPlugins(plugin_list))
    225                 {
    226 
    227                     logger.error("couldn't configure the plugins");
    228                 }
    229             }
    230         }
    231 
    232         // do the service racks
    233         // empty the service map in case this is a reconfigure
    234         clearServices();
    235         Element service_rack_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
    236         if (service_rack_list == null)
    237         {
    238             // is this an error? could you ever have a service cluster
    239             // without service racks???
    240             logger.error("cluster has no service racks!!");
    241         }
    242         else
    243         {
    244 
    245             if (!configureServiceRackList(service_rack_list, null))
    246             {
    247                 logger.error("couldn't configure the  service racks!!");
    248                 return false;
    249             }
    250         }
    251 
    252         return true;
    253     }
    254 
     267        // get the servlet params
     268        Element param_list = (Element) GSXML.getChildByTagName(service_cluster_info, "libraryParamList");
     269        if (param_list != null) {
     270          if (!addLibraryParams(param_list)) {
     271            logger.error("couldn't configure the library param list");
     272          }
     273        }
     274
     275  }
    255276    /**
    256277     * adds metadata from a metadataList into the metadata_list xml
     
    271292        return true;
    272293    }
    273 
     294  /** adds an individual metadata element into the list */
     295  protected boolean addMetadata(String name, String value) {
     296    return GSXML.addMetadata(this.doc, this.metadata_list, name, value);
     297  }
     298 
     299  /** in displayItemList, end up with the following for each named displayItem
     300      <displayItem name="">
     301        <displayItem name="" lang="">value</displayItem>
     302        <displayItem name="" lang="">value</displayItem>
     303      </displayItem>
     304  */
    274305    protected boolean addDisplayItems(Element display_list)
    275306    {
     
    305336    }
    306337
    307     protected boolean addPlugins(Element plugin_list)
    308     {
    309         if (plugin_list == null)
    310             return false;
    311         NodeList pluginNodes = plugin_list.getElementsByTagName(GSXML.PLUGIN_ELEM);
    312         if (pluginNodes.getLength() > 0)
    313         {
    314             for (int k = 0; k < pluginNodes.getLength(); k++)
    315             {
    316                 this.plugin_item_list.appendChild(this.doc.importNode(pluginNodes.item(k), true));
    317             }
    318         }
    319 
     338    // protected boolean addPlugins(Element plugin_list)
     339    // {
     340    //  if (plugin_list == null)
     341    //      return false;
     342    //  NodeList pluginNodes = plugin_list.getElementsByTagName(GSXML.PLUGIN_ELEM);
     343    //  if (pluginNodes.getLength() > 0)
     344    //  {
     345    //      for (int k = 0; k < pluginNodes.getLength(); k++)
     346    //      {
     347    //          this.plugin_item_list.appendChild(this.doc.importNode(pluginNodes.item(k), true));
     348    //      }
     349    //  }
     350
     351    //  return true;
     352    // }
     353    protected boolean resolveMacros(Element display_list)
     354    {
     355        if (display_list == null)
     356            return false;
     357        NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
     358        if (displaynodes.getLength() > 0)
     359        {
     360          //String http_site = this.site_http_address;
     361          //String http_collection = this.site_http_address + "/collect/" + this.cluster_name;
     362            for (int k = 0; k < displaynodes.getLength(); k++)
     363            {
     364                Element d = (Element) displaynodes.item(k);
     365                String text = GSXML.getNodeText(d);
     366                text= macro_resolver.resolve(text);
     367                //text = StringUtils.replace(text, "_httpsite_", http_site);
     368                //text = StringUtils.replace(text, "_httpcollection_", http_collection);
     369                GSXML.setNodeText(d, text);
     370            }
     371        }
    320372        return true;
    321373    }
    322374
     375    /**
     376     * adds library params from libraryParamList into library_param_list xml
     377     */
     378    protected boolean addLibraryParams(Element param_list)
     379    {
     380        if (param_list == null)
     381            return false;
     382        NodeList paramnodes = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
     383        if (paramnodes.getLength() > 0)
     384        {
     385            for (int k = 0; k < paramnodes.getLength(); k++)
     386            {
     387                this.library_param_list.appendChild(this.doc.importNode(paramnodes.item(k), true));
     388            }
     389        }
     390
     391        return true;
     392    }
    323393    protected void clearServices()
    324394    {
     395      cleanUp();
    325396        service_map.clear();
     397        service_name_map.clear();
    326398        this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
    327399    }
    328400
     401  protected void clearLocalData() {
     402    this.description = this.doc.createElement(GSXML.CLUSTER_ELEM);
     403    this.display_item_list = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
     404    this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     405    this.library_param_list = this.doc.createElement("libraryParamList");
     406 
     407  }
    329408    /**
    330409     * creates and configures all the services - extra_info is some more xml
     
    555634            Element description = (Element) this.description.cloneNode(false);
    556635            // set collection type : mg, mgpp, lucene or solr
    557             description.setAttribute(GSXML.TYPE_ATT, col_type);
    558             description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
     636            //description.setAttribute(GSXML.TYPE_ATT, col_type);
     637            //description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
    559638
    560639            response.appendChild(description);
     
    566645                description.appendChild(this.service_list);
    567646                description.appendChild(this.metadata_list);
    568                 description.appendChild(this.plugin_item_list);
     647                description.appendChild(this.library_param_list);
     648                //description.appendChild(this.plugin_item_list);
    569649                return response;
    570650            }
     
    586666                    else if (info.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER))
    587667                    {
    588                         description.appendChild(metadata_list);
     668                        description.appendChild(this.metadata_list);
    589669                    }
    590670                    else if (info.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
     
    592672                        addAllDisplayInfo(description, lang);
    593673                    }
    594                     else if (info.equals(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER))
     674                    else if (info.equals("libraryParamlist"))
    595675                    {
    596                         description.appendChild(plugin_item_list);
     676                      description.appendChild(this.library_param_list);
    597677                    }
    598678                }
     
    820900            return addMetadata(metadata_list);
    821901        }
    822         else if (subset.equals(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER))
    823         {
    824             this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
    825             Element import_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.IMPORT_ELEM);
    826             if (import_list != null)
    827             {
    828                 Element plugin_item_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
    829                 return addPlugins(plugin_item_list);
    830             }
    831             else
    832                 return false;
    833         }
     902        // else if (subset.equals(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER))
     903        // {
     904        // this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
     905        // Element import_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.IMPORT_ELEM);
     906        // if (import_list != null)
     907        // {
     908        //      Element plugin_item_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
     909        //      return addPlugins(plugin_item_list);
     910        // }
     911        // else
     912        //      return false;
     913        // }
    834914        else
    835915        {
     
    847927        { // for each key
    848928            Element m = (Element) items.item(i);
    849             // find the child with the correct language
     929            // findthe child with the correct language
    850930            Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
    851931            if (new_m == null && lang != DEFAULT_LANG)
Note: See TracChangeset for help on using the changeset viewer.