- Timestamp:
- 2002-10-16T09:30:01+13:00 (22 years ago)
- 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 34 34 35 35 /** 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. 39 38 * 40 39 * @author <a href="mailto:[email protected]">Katherine Don</a> … … 43 42 */ 44 43 public class Collection 45 implements ModuleInterface{44 extends ServiceCluster { 46 45 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 */ 78 47 public void setCollectionName(String name) { 79 collection_name_ = name; 80 description_.setAttribute("name", name); 48 setClusterName(name); 81 49 } 82 50 … … 88 56 service_info_ = doc_.createElement("serviceList"); 89 57 meta_info_ = doc_.createElement("metadataList"); 90 91 58 92 59 } … … 104 71 public boolean configure() { 105 72 106 if (site_home_ == null || c ollection_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!"); 108 75 return false; 109 76 } 110 77 // read the collection build configuration file 111 File config_file_ = new File(GSFile.collectionBuildConfigFile(site_home_, c ollection_name_));78 File config_file_ = new File(GSFile.collectionBuildConfigFile(site_home_, cluster_name_)); 112 79 113 80 if (!config_file_.exists()) { 114 81 System.err.println(config_file_+" does not exist"); 115 System.err.println("couldn't configure collection: "+c ollection_name_);82 System.err.println("couldn't configure collection: "+cluster_name_); 116 83 return false; 117 84 } 118 85 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 180 91 } 181 92 182 93 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 } 192 95 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 Node202 *203 */204 public Node process(Node xml_in) {205 96 206 // xml_in should always be an Element - should check this207 Element xml = (Element)xml_in;208 String to = xml.getAttribute("to");209 97 210 // the collection name should be first, check, then remove211 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 }219 98 220 if (to.equals("")) { // this command is for me221 String type = xml.getAttribute("type");222 if (type.equals("describe")) {223 // create a response with the description inside it224 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 services236 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.