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