Changeset 3468 for trunk


Ignore:
Timestamp:
2002-10-16T09:30:01+13:00 (22 years ago)
Author:
kjdon
Message:

new class - ServiceCluster - a group of related services. Collection now extends this.

Location:
trunk/gsdl3/src/java/org/greenstone/gsdl3/collection
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/collection/Collection.java

    r3441 r3468  
    3434
    3535/**
    36  * Represents a collection in Greenstone.
    37  *
    38  * Implements ModuleInterface. Contains a list of services supported by the collection, as well as collection metadata
     36 * Represents a collection in Greenstone. A collection is an extension of
     37 * a ServiceCluster - it has local data that the services use.
    3938 *
    4039 * @author <a href="mailto:[email protected]">Katherine Don</a>
     
    4342 */
    4443public class Collection
    45     implements ModuleInterface {
     44    extends ServiceCluster {
    4645
    47     /** base directory for the site */
    48     protected String site_home_ = null;
    49 
    50     /** The name of the collection - this must be the name of the collection
    51      * directory inside collect */
    52     protected String collection_name_ = null;
    53 
    54     /** The map of services.
    55      *
    56      * Maps Services to ServiceModule objects
    57      * @see ServiceModule
    58      *
    59      */
    60     protected HashMap service_map_=null;
    61 
    62     /** XML converter for String to DOM and vice versa */
    63     protected XMLConverter converter_=null;
    64 
    65     /** container doc for description elements */
    66     protected Document doc_ = null;
    67     /** list of services */
    68     protected Element service_info_ = null;
    69     /** list of Metadata */
    70     protected Element meta_info_ = null;
    71     /** full description */
    72     protected Element description_ = null;
    73 
    74     public void setSiteHome(String home) {
    75     site_home_ = home;
    76     }
    77 
     46    /** same as setClusterName */
    7847    public void setCollectionName(String name) {
    79     collection_name_ = name;
    80     description_.setAttribute("name", name);
     48    setClusterName(name);
    8149    }
    8250
     
    8856    service_info_ = doc_.createElement("serviceList");
    8957    meta_info_ = doc_.createElement("metadataList");
    90    
    9158   
    9259    }
     
    10471    public boolean configure() {
    10572   
    106     if (site_home_ == null || collection_name_== null) {
    107         System.err.println("site_home_ and collection_name_ must be set before configure called!");
     73    if (site_home_ == null || cluster_name_== null) {
     74        System.err.println("site_home and collection_name must be set before configure called!");
    10875        return false;
    10976    }
    11077    // read the collection build configuration file
    111     File config_file_ = new File(GSFile.collectionBuildConfigFile(site_home_, collection_name_));
     78    File config_file_ = new File(GSFile.collectionBuildConfigFile(site_home_, cluster_name_));
    11279
    11380    if (!config_file_.exists()) {
    11481        System.err.println(config_file_+" does not exist");
    115         System.err.println("couldn't configure collection: "+collection_name_);
     82        System.err.println("couldn't configure collection: "+cluster_name_);
    11683        return false;
    11784    }
    11885
    119     try {
    120        
    121         Document doc = converter_.getDOM(config_file_);
    122        
    123         // create all the services
    124         NodeList nodes = doc.getElementsByTagName("serviceModule");
    125 
    126         // the xml request to send to each serviceModule to query what
    127         // services it provides
    128         Element request = doc_.createElement("request");
    129         request.setAttribute("type", "describe");
    130         request.setAttribute("info", "serviceList");
    131 
    132         if (nodes.getLength()==0) {
    133         System.err.println("Collection configuration error: collection "+collection_name_+" has no services!");
    134         return false;
    135         }
    136 
    137         for(int i=0; i<nodes.getLength(); i++) {
    138         Element n = (Element)nodes.item(i);
    139         String servicetype = n.getAttribute("name");
    140        
    141         ServiceModule s = (ServiceModule)Class.forName("org.greenstone.gsdl3.service."+servicetype).newInstance();
    142         s.setSiteHome(site_home_);
    143         s.setCollectionName(collection_name_);
    144         // pass the xml node to the service for configuration
    145         s.configure(n);
    146 
    147         // find out the supported service types for this service module
    148         Node types = s.process(request);
    149         NodeList typenodes = ((Element)types).getElementsByTagName("service");     
    150 
    151         for (int j=0; j<typenodes.getLength();j++) {
    152             String service = ((Element) typenodes.item(j)).getAttribute("name");       
    153             service_map_.put(service, s);
    154 
    155             // also add info to the ServiceInfo XML element
    156             service_info_.appendChild(doc_.importNode(typenodes.item(j), true));
    157         }
    158        
    159         }
    160 
    161         // get the metadata - for now just add it to the list
    162         NodeList metanodes = doc.getElementsByTagName("metadata");
    163 
    164         if (metanodes.getLength()>0) { 
    165         for(int k=0; k<metanodes.getLength(); k++) {
    166             meta_info_.appendChild(doc_.importNode(metanodes.item(k),true));
    167         }
    168         }       
    169 
    170     } catch (Exception e) {
    171         System.out.println("Collection configure exception: "+e.getMessage() + e.getClass() );
    172         e.printStackTrace();
    173         return false;
    174     }
    175 
    176     // add elements to description
    177     description_.appendChild(service_info_);
    178     description_.appendChild(meta_info_);
    179     return true;
     86    Document doc = converter_.getDOM(config_file_);
     87    Element e = doc.getDocumentElement();
     88   
     89    return configure(e);
     90 
    18091    }
    18192
    18293
    183     /**
    184      * Process an XML document - uses Strings
    185      *  just calls process(Node).
    186      *
    187      * @param in the Document to process - a string
    188      * @return the resultant document as a string - contains any error messages
    189      * @see String
    190      */
    191     public String process(String in) {
     94}
    19295
    193     Document doc = converter_.getDOM(in);
    194     Element e = doc.getDocumentElement();
    195    
    196     Node res = process(e);
    197     return converter_.getString(res);
    198    
    199     }
    200    
    201     /** process XML as Node
    202      *
    203      */
    204     public Node process(Node xml_in) {
    20596
    206     // xml_in should always be an Element - should check this
    207     Element xml = (Element)xml_in;
    208     String to = xml.getAttribute("to");
    20997
    210     // the collection name should be first, check, then remove
    211     String col = GSPath.getFirstLink(to);
    212     if (col.equals(collection_name_)){
    213         to = GSPath.removeFirstLink(to);
    214         xml.setAttribute("to", to);
    215     } else {
    216         System.out.println("Collection error: collection name wrong! was "+col+" should have been "+collection_name_);
    217         return null;
    218     }
    21998
    220     if (to.equals("")) { // this command is for me
    221         String type = xml.getAttribute("type");
    222         if (type.equals("describe")) {
    223         // create a response with the description inside it
    224         Element response = doc_.createElement("response");
    225         response.setAttribute("from", collection_name_);
    226         response.setAttribute("type", "describe");
    227         response.appendChild(description_);
    228         return response;
    229         } else {
    230         System.out.println("Collection: error, cant handle request:");
    231         System.out.println(converter_.getString(xml));
    232         return null;
    233         }
    234        
    235     } else { // the request is for one of my services
    236         String service = GSPath.getFirstLink(to);
    237        
    238         if (service_map_.containsKey(service)) {
    239         return ((ServiceModule)service_map_.get(service)).process(xml);
    240         } else {
    241         System.err.println("Collection "+collection_name_+": non-existant service in:\n"+converter_.getString(xml));
    242         return null;
    243         }
    244 
    245     }
    246     }
    247      
    248 }
Note: See TracChangeset for help on using the changeset viewer.