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

Last change on this file since 24221 was 24221, checked in by sjm84, 13 years ago

Added more functionality to ArchiveRetrieve, added an additonal parameter to printXMLNode in GSXML that controls whether text nodes are to be printed or not and also made the infodb type of collections more easily accessible

  • Property svn:keywords set to Author Date Id Revision
File size: 27.5 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: 24221 $
47* @see ModuleInterface
48*/
49public class ServiceCluster
50implements 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, mgpp or lucene */
65 protected String col_type = "";
66 /** database type : gdbm, jdbm or sqlite*/
67 protected String db_type = "";
68
69 /** a reference to the message router */
70 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
81 /** XML converter for String to DOM and vice versa */
82 protected XMLConverter converter=null;
83
84 /** container doc for description elements */
85 protected Document doc = null;
86 /** list of services */
87 protected Element service_list = null;
88 /** list of metadata - all metadata, regardless of language goes in here */
89 protected Element metadata_list = null;
90 /** language specific stuff */
91 //protected Element lang_specific_metadata_list = null;
92 protected Element display_item_list = null;
93 /** the element that will have any descriptions passed back in */
94 protected Element description = null;
95
96 /** list of plugin */
97 protected Element plugin_item_list = null;
98
99 public void setSiteHome(String home) {
100 this.site_home = home;
101 }
102
103 public void setSiteAddress(String address) {
104 this.site_http_address = address;
105 }
106
107 public void cleanUp() {
108 Iterator i = this.service_map.values().iterator();
109 while (i.hasNext()) {
110 ServiceRack s = (ServiceRack)i.next();
111 s.cleanUp();
112 }
113 }
114 public void setClusterName(String name) {
115 this.cluster_name = name;
116 this.description.setAttribute(GSXML.NAME_ATT, name);
117 }
118
119 public void setMessageRouter(MessageRouter m) {
120 this.router = m;
121 }
122
123 public ServiceCluster() {
124 this.service_map = new HashMap();
125 this.service_name_map = new HashMap();
126 this.converter = new XMLConverter();
127 this.doc = this.converter.newDOM();
128 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);
132 }
133
134 /**
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) {
150 logger.error("site_home and cluster_name must be set before configure called!");
151 return false;
152 }
153 logger.info("configuring service cluster");
154 // read the site configuration file
155 File config_file = new File(GSFile.siteConfigFile(this.site_home));
156
157 if (!config_file.exists()) {
158 logger.error("couldn't configure cluster: "+this.cluster_name +", "+config_file+" does not exist");
159 return false;
160 }
161
162 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
168 // 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
173 return this.configure(sc);
174 }
175
176
177 public boolean configure(Element service_cluster_info) {
178
179 // 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
184 logger.error(" couldn't configure the metadata");
185 }
186 }
187
188 // 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
193 logger.error("couldn't configure the display items");
194 }
195 }
196
197 //get the plugin info
198 Element import_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.IMPORT_ELEM);
199 if (import_list != null)
200 {
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
205 logger.error("couldn't configure the plugins");
206 }
207 }
208 }
209
210 // do the service racks
211 // empty the service map in case this is a reconfigure
212 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) {
215 // is this an error? could you ever have a service cluster
216 // without service racks???
217 logger.error("cluster has no service racks!!");
218 } else {
219
220 if (!configureServiceRackList(service_rack_list, null)) {
221 logger.error("couldn't configure the service racks!!");
222 return false;
223 }
224 }
225
226 return true;
227 }
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;
233 NodeList metanodes = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
234 if (metanodes.getLength()>0) {
235 for(int k=0; k<metanodes.getLength(); k++) {
236 this.metadata_list.appendChild(this.doc.importNode(metanodes.item(k), true));
237 }
238 }
239
240 return true;
241 }
242
243 protected boolean addDisplayItems(Element display_list) {
244
245 if (display_list==null) return false;
246 NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
247 if (displaynodes.getLength()>0) {
248 for(int k=0; k<displaynodes.getLength(); k++) {
249 Element d = (Element) displaynodes.item(k);
250 String lang = d.getAttribute(GSXML.LANG_ATT);
251 if (lang==null||lang.equals("")) {
252 //set the lang to teh default
253 d.setAttribute(GSXML.LANG_ATT, DEFAULT_LANG);
254 }
255 String name = d.getAttribute(GSXML.NAME_ATT);
256 Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, name);
257 if (this_item==null) {
258 this_item = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM);
259 this_item.setAttribute(GSXML.NAME_ATT, name);
260 this.display_item_list.appendChild(this_item);
261 }
262
263 this_item.appendChild(this.doc.importNode(d, true));
264 }
265 }
266
267 return true;
268 }
269
270 protected boolean addPlugins(Element plugin_list) {
271 if (plugin_list == null) return false;
272 NodeList pluginNodes = plugin_list.getElementsByTagName(GSXML.PLUGIN_ELEM);
273 if (pluginNodes.getLength() > 0) {
274 for (int k = 0; k < pluginNodes.getLength(); k++)
275 {
276 this.plugin_item_list.appendChild(this.doc.importNode(pluginNodes.item(k), true));
277 }
278 }
279
280 return true;
281 }
282
283
284 protected void clearServices() {
285 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
289that 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) {
293
294 // create all the services
295 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
303 // the xml request to send to the serviceRack to query what
304 // services it provides
305 Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
306 Element request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", "", "");
307 message.appendChild(request);
308
309 Element n = (Element)nodes.item(i);
310 String servicetype = n.getAttribute(GSXML.NAME_ATT);
311
312 ServiceRack s = null;
313
314 try {
315 // 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 {
321 // 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);
328 continue;
329 }
330
331
332 s.setSiteHome(this.site_home);
333 s.setSiteAddress(this.site_http_address);
334 s.setClusterName(this.cluster_name);
335 s.setMessageRouter(this.router);
336 // pass the xml node to the service for configuration
337 if (s.configure(n, extra_info)) {
338
339 // find out the supported service types for this service module
340 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) {
346 char extra = '0';
347 String new_service = service+extra;
348
349 while (service_map.get(new_service)!=null) {
350 extra++;
351 new_service = service+extra;
352 }
353 this.service_name_map.put(new_service, service);
354 service=new_service;
355 ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
356 }
357 this.service_map.put(service, s);
358 // also add info to the ServiceInfo XML element
359 this.service_list.appendChild(this.doc.importNode(typenodes.item(j), true));
360 }
361 }
362 }
363
364 return true;
365
366
367 }
368
369
370 /**
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) {
379
380 Document doc = this.converter.getDOM(in);
381
382 Node res = process(doc);
383 return this.converter.getString(res);
384
385 }
386
387 /** process XML as Node
388 *
389 */
390 public Node process(Node message_node) {
391
392 Element message = this.converter.nodeToElement(message_node);
393
394 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
395 Document mess_doc = message.getOwnerDocument();
396 Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
397 if (requests.getLength()==0) {
398 logger.error("no requests for cluster:"+this.cluster_name);
399 // no requests
400 return mainResult; // for now
401 }
402 for (int i=0; i<requests.getLength(); i++) {
403 Element request = (Element)requests.item(i);
404 String to = request.getAttribute(GSXML.TO_ATT);
405
406 // the cluster name should be first, check, then remove
407 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);
410 continue; // ignore this request
411 }
412 to = GSPath.removeFirstLink(to);
413 request.setAttribute(GSXML.TO_ATT, to);
414
415 if (to.equals("")) { // this command is for me
416 Element response = processMessage(request);
417 mainResult.appendChild(response);
418
419 } else { // the request is for one of my services
420 String service = GSPath.getFirstLink(to);
421
422 if (!this.service_map.containsKey(service)) {
423 logger.error("non-existant service, "+service+", specified!");
424 continue;
425 }
426 String real_service = service;
427 if (this.service_name_map.containsKey(service)) {
428 real_service = (String)this.service_name_map.get(service);
429 // need to change the to att in the request - give the real service name
430 to = request.getAttribute(GSXML.TO_ATT);
431 String old_to = to;
432 to = GSPath.replaceFirstLink(to, real_service);
433 request.setAttribute(GSXML.TO_ATT, to);
434 }
435 // have to pass the request to the service
436 Element single_message = mess_doc.createElement(GSXML.MESSAGE_ELEM);
437 single_message.appendChild(request);
438 Node response_message = ((ModuleInterface)this.service_map.get(service)).process(single_message);
439 if (response_message != null) {
440 Element response = (Element) GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM);
441 String from = response.getAttribute(GSXML.FROM_ATT);
442 if (!real_service.equals(service)) {
443 // replace the real service name with the pseudo service name
444 from = GSPath.replaceFirstLink(from, service);
445 // also need to do it in the service itself
446 // shoudl this be done here??
447 Element service_elem = (Element) GSXML.getChildByTagName(response, GSXML.SERVICE_ELEM);
448 if (service_elem!= null) {
449 service_elem.setAttribute(GSXML.NAME_ATT, service);
450 }
451 }
452 from = GSPath.prependLink(from, this.cluster_name);
453 response.setAttribute(GSXML.FROM_ATT, from);
454 mainResult.appendChild(this.doc.importNode(response, true));
455 }
456
457 } // else
458
459
460 } // for each request
461 return mainResult;
462 }
463
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) {
470
471 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
472 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
473 String type = request.getAttribute(GSXML.TYPE_ATT);
474 String lang = request.getAttribute(GSXML.LANG_ATT);
475 response.setAttribute(GSXML.TYPE_ATT, type);
476
477 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
478 // create the collection element
479 Element description = (Element)this.description.cloneNode(false);
480 // set collection type : mg or mgpp
481 description.setAttribute(GSXML.TYPE_ATT, col_type);
482 description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
483
484 response.appendChild(description);
485 // 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);
489 description.appendChild(this.service_list);
490 description.appendChild(this.metadata_list);
491 description.appendChild(this.plugin_item_list);
492 return response;
493 }
494
495 // go through the param list and see what components are wanted
496 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);
500 // Identify the structure information desired
501 if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM)) {
502 String info = param.getAttribute(GSXML.VALUE_ATT);
503 if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
504 description.appendChild(this.service_list);
505 } else if (info.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER)) {
506 description.appendChild(metadata_list);
507 } else if (info.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
508 addAllDisplayInfo(description, lang);
509 } else if (info.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
510 description.appendChild(plugin_item_list);
511 }
512 }
513 }
514 return response;
515 }
516 /*
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";
541 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
617 }
618 return response;
619 }
620
621 protected Element processSystemRequest(Element request) {
622
623 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
624 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
625 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SYSTEM);
626
627 // a list of system requests - should put any error messages
628 // or success messages into response
629 NodeList commands = request.getElementsByTagName(GSXML.SYSTEM_ELEM);
630 String message=null;
631 for (int i=0; i<commands.getLength(); i++) {
632 // all the commands should be Elements
633 Element elem = (Element)commands.item(i);
634 String action = elem.getAttribute(GSXML.TYPE_ATT);
635 if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE)) {
636 String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
637 if (subset.equals("")) {
638 // need to reconfigure the service cluster
639
640 if (this.configure()) {
641 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " reconfigured");
642 response.appendChild(s);
643
644 } else {
645 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " could not be reconfigured");
646 response.appendChild(s);
647 }
648 } else if (this.configureSubset(subset)) {
649 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " "+subset+" reconfigured");
650 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);
654 }
655 continue;
656 } // configure action
657
658 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
659 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
660 if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE)) {
661 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "activate action not yet implemented - does it even make sense in this context??");
662 response.appendChild(s);
663 } else if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE)) {
664 if (module_type.equals(GSXML.SERVICE_ELEM)) {
665 // deactivate the service
666 // remove from service_map
667 this.service_map.remove(module_name);
668 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!";}
673 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
674 response.appendChild(s);
675 } else {
676 logger.error("cant process system request, action "+action);
677 continue;
678 }
679 } // for each command
680 return response;
681 }
682
683 /**
684 * do a configure on only part of the collection
685 */
686 protected boolean configureSubset(String subset) {
687
688 File configFile = new File(GSFile.siteConfigFile(this.site_home));
689 if (!configFile.exists() ) {
690 logger.error("site config file: "+configFile.getPath()+" not found!");
691 // wont be able to do any of the requests
692 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
702 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);
706 // wont be able to do any of teh requests
707 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);
712 clearServices();
713 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);
717 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);
721 if (import_list != null)
722 {
723 Element plugin_item_list = (Element)GSXML.getChildByTagName(cluster_config_elem,GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
724 return addPlugins(plugin_item_list);
725 }
726 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
738 NodeList items = this.display_item_list.getChildNodes();
739 for (int i=0; i<items.getLength(); i++) { // for each key
740 Element m = (Element) items.item(i);
741 // find the child with the correct language
742 Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
743 if (new_m==null && lang != DEFAULT_LANG) {
744 // use the default lang
745 new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
746 }
747 if (new_m==null) {
748 // just get the first one
749 new_m = (Element)GSXML.getChildByTagName(m, GSXML.DISPLAY_TEXT_ELEM);
750 }
751 description.appendChild(new_m.cloneNode(true));
752 }
753 return true;
754
755 }
756
757
758 protected Element getDisplayTextElement(String key, String lang) {
759
760 Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, key);
761 if (this_item == null) {
762 return null;
763 }
764
765 Element this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
766 if (this_lang == null && lang != DEFAULT_LANG) {
767 // try the default
768 this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
769 }
770 if (this_lang == null) {
771 // just return the first one
772 return GSXML.getFirstElementChild(this_item);//(Element)this_item.getFirstChild().cloneNode(true);
773 }
774 return (Element)this_lang.cloneNode(true);
775
776 }
777 public HashMap getServiceMap() {
778 return service_map;
779 }
780}
781
782
783
784
785
786
Note: See TracBrowser for help on using the repository browser.