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

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

Working on document-level format editting

  • Property svn:keywords set to Author Date Id Revision
File size: 28.6 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: 24203 $
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 logger.error("ServiceCluster is processing a message");
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 logger.error(service+" specified!");
422
423 if (!this.service_map.containsKey(service)) {
424 logger.error("non-existant service, "+service+", specified!");
425 continue;
426 }
427 String real_service = service;
428 if (this.service_name_map.containsKey(service)) {
429 real_service = (String)this.service_name_map.get(service);
430 // need to change the to att in the request - give the real service name
431 to = request.getAttribute(GSXML.TO_ATT);
432 String old_to = to;
433 to = GSPath.replaceFirstLink(to, real_service);
434 request.setAttribute(GSXML.TO_ATT, to);
435 }
436 // have to pass the request to the service
437 Element single_message = mess_doc.createElement(GSXML.MESSAGE_ELEM);
438 single_message.appendChild(request);
439 Node response_message = ((ModuleInterface)this.service_map.get(service)).process(single_message);
440 if (response_message != null) {
441 Element response = (Element) GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM);
442 String from = response.getAttribute(GSXML.FROM_ATT);
443 if (!real_service.equals(service)) {
444 // replace the real service name with the pseudo service name
445 from = GSPath.replaceFirstLink(from, service);
446 // also need to do it in the service itself
447 // shoudl this be done here??
448 Element service_elem = (Element) GSXML.getChildByTagName(response, GSXML.SERVICE_ELEM);
449 if (service_elem!= null) {
450 service_elem.setAttribute(GSXML.NAME_ATT, service);
451 }
452 }
453 from = GSPath.prependLink(from, this.cluster_name);
454 response.setAttribute(GSXML.FROM_ATT, from);
455 mainResult.appendChild(this.doc.importNode(response, true));
456 }
457
458 } // else
459
460
461 } // for each request
462 return mainResult;
463 }
464
465 /** handles requests made to the ServiceCluster itself
466 *
467 * @param req - the request Element- <request>
468 * @return the result Element - should be <response>
469 */
470 protected Element processMessage(Element request) {
471
472 logger.error("ServiceCluster is processing an element request");
473 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
474 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
475 String type = request.getAttribute(GSXML.TYPE_ATT);
476 String lang = request.getAttribute(GSXML.LANG_ATT);
477 response.setAttribute(GSXML.TYPE_ATT, type);
478
479 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
480 // create the collection element
481 Element description = (Element)this.description.cloneNode(false);
482 // set collection type : mg or mgpp
483 description.setAttribute(GSXML.TYPE_ATT, col_type);
484
485 response.appendChild(description);
486 // check the param list
487 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
488 if (param_list == null) {
489 addAllDisplayInfo(description, lang);
490 description.appendChild(this.service_list);
491 description.appendChild(this.metadata_list);
492 description.appendChild(this.plugin_item_list);
493 return response;
494 }
495
496 // go through the param list and see what components are wanted
497 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
498 for (int i=0; i<params.getLength(); i++) {
499
500 Element param = (Element)params.item(i);
501 // Identify the structure information desired
502 if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM)) {
503 String info = param.getAttribute(GSXML.VALUE_ATT);
504 if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
505 description.appendChild(this.service_list);
506 } else if (info.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER)) {
507 description.appendChild(metadata_list);
508 } else if (info.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
509 addAllDisplayInfo(description, lang);
510 } else if (info.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
511 description.appendChild(plugin_item_list);
512 }
513 }
514 }
515 return response;
516 }
517 /*
518 if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING)) {
519 logger.error("Received format string request");
520 String service = request.getAttribute("service");
521 logger.error("Service is " + service);
522 String classifier = null;
523 if(service.equals("ClassifierBrowse"))
524 {
525 classifier = request.getAttribute("classifier");
526 logger.error("Classifier is " + classifier);
527 }
528 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
529 String format_string = GSXML.getNodeText(format_element);
530 logger.error("Format string: " + format_string);
531 logger.error("Config file location = " + GSFile.collectionConfigFile(this.site_home, this.cluster_name));
532
533 // check for version file
534
535 String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.pathSeparator;
536 logger.error("Directory is " + directory);
537
538 String version_filename = "";
539
540 if(service.equals("ClassifierBrowse"))
541 version_filename = directory + "browse_"+classifier+"_format_statement_version.txt";
542 else
543 version_filename = directory + "query_format_statement_version.txt";
544
545 File version_file = new File(version_filename);
546 logger.error("Version filename is " + version_filename);
547
548 String version_number = "1";
549 BufferedWriter writer; // = new BufferedWriter(new FileWriter(version_filename));
550 //RandomAccessFile version_file_random_access;
551
552 try{
553
554 if(version_file.exists())
555 {
556 // Read version
557 BufferedReader reader = new BufferedReader(new FileReader(version_filename));
558 //version_file_random_access = new RandomAccessFile(version_file, "r");
559 //logger.error("
560 //version_number = version_file_random_access.readInt();
561 version_number = reader.readLine();
562 int aInt = Integer.parseInt(version_number) + 1;
563 version_number = Integer.toString(aInt);
564 reader.close();
565 //version_file_random_access.close();
566 }
567 else{
568 // Create
569 version_file.createNewFile();
570 // write 1 to file
571 writer = new BufferedWriter(new FileWriter(version_filename));
572 //version_file_random_access = new RandomAccessFile(version_file, "w");
573 //version_file_random_access.writeInt(version_number);
574 writer.write(version_number);
575 writer.close();
576 //version_file_random_access.close();
577 }
578
579 // Write version file
580 String format_statement_filename = "";
581
582 if(service.equals("ClassifierBrowse"))
583 format_statement_filename = directory + "browse_"+classifier+"_format_statement_v" + version_number + ".txt";
584 else
585 format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
586
587 logger.error("Format statement filename is " + format_statement_filename);
588
589 writer = new BufferedWriter(new FileWriter(format_statement_filename));
590 writer.write(format_string);
591 writer.close();
592
593 // Update version number
594 //version_file_random_access = new RandomAccessFile(version_file, "w");
595 //version_file_random_access.writeInt(version_number);
596 //version_file_random_access.close();
597
598 writer = new BufferedWriter(new FileWriter(version_filename));
599 //version_file_random_access = new RandomAccessFile(version_file, "w");
600 //version_file_random_access.writeInt(version_number);
601 writer.write(version_number);
602 writer.close();
603
604
605
606 } catch (IOException e) {
607 logger.error("IO Exception "+e);
608 //System.exit(1);
609 }
610
611
612 }*/
613 if (type.equals(GSXML.REQUEST_TYPE_SYSTEM)) {
614 response = processSystemRequest(request);
615 } else { // unknown type
616 logger.error("cant handle request of type "+ type);
617
618 }
619 return response;
620 }
621
622 protected Element processSystemRequest(Element request) {
623
624 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
625 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
626 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SYSTEM);
627
628 // a list of system requests - should put any error messages
629 // or success messages into response
630 NodeList commands = request.getElementsByTagName(GSXML.SYSTEM_ELEM);
631 String message=null;
632 for (int i=0; i<commands.getLength(); i++) {
633 // all the commands should be Elements
634 Element elem = (Element)commands.item(i);
635 String action = elem.getAttribute(GSXML.TYPE_ATT);
636 if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE)) {
637 String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
638 if (subset.equals("")) {
639 // need to reconfigure the service cluster
640
641 if (this.configure()) {
642 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " reconfigured");
643 response.appendChild(s);
644
645 } else {
646 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " could not be reconfigured");
647 response.appendChild(s);
648 }
649 } else if (this.configureSubset(subset)) {
650 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " "+subset+" reconfigured");
651 response.appendChild(s);
652 } else {
653 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " "+subset + " could not be reconfigured");
654 response.appendChild(s);
655 }
656 continue;
657 } // configure action
658
659 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
660 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
661 if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE)) {
662 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "activate action not yet implemented - does it even make sense in this context??");
663 response.appendChild(s);
664 } else if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE)) {
665 if (module_type.equals(GSXML.SERVICE_ELEM)) {
666 // deactivate the service
667 // remove from service_map
668 this.service_map.remove(module_name);
669 Element service_elem = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, module_name);
670 service_list.removeChild(service_elem);
671 message = module_type+": "+module_name+" deactivated";
672 } else {
673 message = "can't deactivate "+module_type+" type modules!";}
674 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
675 response.appendChild(s);
676 } else {
677 logger.error("cant process system request, action "+action);
678 continue;
679 }
680 } // for each command
681 return response;
682 }
683
684 /**
685 * do a configure on only part of the collection
686 */
687 protected boolean configureSubset(String subset) {
688
689 File configFile = new File(GSFile.siteConfigFile(this.site_home));
690 if (!configFile.exists() ) {
691 logger.error("site config file: "+configFile.getPath()+" not found!");
692 // wont be able to do any of the requests
693 return false;
694
695 }
696
697 Document site_config_doc = this.converter.getDOM(configFile);
698 if (site_config_doc == null) {
699 logger.error("could not read in site config file: "+configFile.getPath());
700 return false;
701 }
702
703 Element site_config_elem = site_config_doc.getDocumentElement();
704 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);
705 if (cluster_config_elem == null) {
706 logger.error("site config file: "+configFile.getPath()+" has no element for cluster "+this.cluster_name);
707 // wont be able to do any of teh requests
708 return false;
709
710 }
711 if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
712 Element service_rack_list = (Element)GSXML.getChildByTagName(cluster_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
713 clearServices();
714 return configureServiceRackList(service_rack_list, null);
715 } else if (subset.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER)) {
716 this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
717 Element metadata_list = (Element)GSXML.getChildByTagName(cluster_config_elem, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
718 return addMetadata(metadata_list);
719 } else if (subset.equals(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER)) {
720 this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
721 Element import_list = (Element)GSXML.getChildByTagName(cluster_config_elem,GSXML.IMPORT_ELEM);
722 if (import_list != null)
723 {
724 Element plugin_item_list = (Element)GSXML.getChildByTagName(cluster_config_elem,GSXML.PLUGIN_ELEM+GSXML.LIST_MODIFIER);
725 return addPlugins(plugin_item_list);
726 }
727 else
728 return false;
729 } else {
730 logger.error("cannot process system request, configure "+subset);
731 return false;
732 }
733
734 }
735
736
737 protected boolean addAllDisplayInfo(Element description, String lang) {
738
739 NodeList items = this.display_item_list.getChildNodes();
740 for (int i=0; i<items.getLength(); i++) { // for each key
741 Element m = (Element) items.item(i);
742 // find the child with the correct language
743 Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
744 if (new_m==null && lang != DEFAULT_LANG) {
745 // use the default lang
746 new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
747 }
748 if (new_m==null) {
749 // just get the first one
750 new_m = (Element)GSXML.getChildByTagName(m, GSXML.DISPLAY_TEXT_ELEM);
751 }
752 description.appendChild(new_m.cloneNode(true));
753 }
754 return true;
755
756 }
757
758
759 protected Element getDisplayTextElement(String key, String lang) {
760
761 Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, key);
762 if (this_item == null) {
763 return null;
764 }
765
766 Element this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
767 if (this_lang == null && lang != DEFAULT_LANG) {
768 // try the default
769 this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
770 }
771 if (this_lang == null) {
772 // just return the first one
773 return GSXML.getFirstElementChild(this_item);//(Element)this_item.getFirstChild().cloneNode(true);
774 }
775 return (Element)this_lang.cloneNode(true);
776
777 }
778 public HashMap getServiceMap() {
779 return service_map;
780 }
781}
782
783
784
785
786
787
Note: See TracBrowser for help on using the repository browser.