source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/ServiceCluster.java@ 23489

Last change on this file since 23489 was 23489, checked in by sjb48, 13 years ago

Working on saving a format statement to the config file. This looks like it should work but changes in FormatAction have caused major issues with Document/Node violations. This is because saving required the format statement as a Document rather than a string (which did work). Added a check to GSXML for xmlNodeToString to check that attrs is not null (attrs will be null unless the node is an element).

  • Property svn:keywords set to Author Date Id Revision
File size: 28.4 KB
Line 
1/*
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// leave the package name as is for now - should be changed to something better
20// cluster? groups?
21package org.greenstone.gsdl3.collection;
22
23
24import org.greenstone.gsdl3.util.*;
25import org.greenstone.gsdl3.core.*;
26import org.greenstone.gsdl3.service.*;
27
28// java XML classes we're using
29import org.w3c.dom.Document;
30import org.w3c.dom.Node;
31import org.w3c.dom.Element;
32import org.w3c.dom.NodeList;
33
34import java.io.*;
35import java.io.File;
36import java.io.RandomAccessFile;
37import java.util.HashMap;
38import java.util.Iterator;
39
40import org.apache.log4j.*;
41
42/* 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: 23489 $
47 * @see ModuleInterface
48 */
49public 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++)
273 {
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
287that 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 }
360 }
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 }
777}
778
779
780
781
782
783
Note: See TracBrowser for help on using the repository browser.