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

Last change on this file since 24975 was 24975, checked in by sjm84, 12 years ago

Reformatting this file ahead of some changes

  • Property svn:keywords set to Author Date Id Revision
File size: 28.1 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
23import org.greenstone.gsdl3.util.*;
24import org.greenstone.gsdl3.core.*;
25import org.greenstone.gsdl3.service.*;
26
27// java XML classes we're using
28import org.w3c.dom.Document;
29import org.w3c.dom.Node;
30import org.w3c.dom.Element;
31import org.w3c.dom.NodeList;
32
33import java.io.*;
34import java.io.File;
35import java.io.RandomAccessFile;
36import java.util.HashMap;
37import java.util.Iterator;
38
39import org.apache.log4j.*;
40
41/* ServiceCluster - a groups of services that are related in some way
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: 24975 $
46 * @see ModuleInterface
47 */
48public class ServiceCluster implements ModuleInterface
49{
50
51 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.ServiceCluster.class.getName());
52
53 protected static final String CONFIG_ENCODING = "utf-8";
54
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
56
57 /** base directory for the site that this cluster belongs to */
58 protected String site_home = null;
59 /** http address of the site that this cluster belongs to */
60 protected String site_http_address = null;
61 /** The name of the cluster - for a collection, this is the collection name */
62 protected String cluster_name = null;
63 /** collection type : mg, mgpp or lucene */
64 protected String col_type = "";
65 /** database type : gdbm, jdbm or sqlite */
66 protected String db_type = "";
67
68 /** a reference to the message router */
69 protected MessageRouter router = null;
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
85 /** XML converter for String to DOM and vice versa */
86 protected XMLConverter converter = null;
87
88 /** container doc for description elements */
89 protected Document doc = null;
90 /** list of services */
91 protected Element service_list = null;
92 /** list of metadata - all metadata, regardless of language goes in here */
93 protected Element metadata_list = null;
94 /** language specific stuff */
95 //protected Element lang_specific_metadata_list = null;
96 protected Element display_item_list = null;
97 /** the element that will have any descriptions passed back in */
98 protected Element description = null;
99
100 /** list of plugin */
101 protected Element plugin_item_list = null;
102
103 public void setSiteHome(String home)
104 {
105 this.site_home = home;
106 }
107
108 public void setSiteAddress(String address)
109 {
110 this.site_http_address = address;
111 }
112
113 public void cleanUp()
114 {
115 Iterator i = this.service_map.values().iterator();
116 while (i.hasNext())
117 {
118 ServiceRack s = (ServiceRack) i.next();
119 s.cleanUp();
120 }
121 }
122
123 public void setClusterName(String name)
124 {
125 this.cluster_name = name;
126 this.description.setAttribute(GSXML.NAME_ATT, name);
127 }
128
129 public void setMessageRouter(MessageRouter m)
130 {
131 this.router = m;
132 }
133
134 public ServiceCluster()
135 {
136 this.service_map = new HashMap();
137 this.service_name_map = new HashMap();
138 this.converter = new XMLConverter();
139 this.doc = this.converter.newDOM();
140 this.description = this.doc.createElement(GSXML.CLUSTER_ELEM);
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);
144 }
145
146 /**
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 {
163 logger.error("site_home and cluster_name must be set before configure called!");
164 return false;
165 }
166 logger.info("configuring service cluster");
167 // read the site configuration file
168 File config_file = new File(GSFile.siteConfigFile(this.site_home));
169
170 if (!config_file.exists())
171 {
172 logger.error("couldn't configure cluster: " + this.cluster_name + ", " + config_file + " does not exist");
173 return false;
174 }
175
176 Document doc = this.converter.getDOM(config_file, CONFIG_ENCODING);
177 if (doc == null)
178 {
179 logger.error("couldn't parse config file " + config_file.getPath());
180 return false;
181 }
182
183 // get the appropriate service cluster element
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
187 return this.configure(sc);
188 }
189
190 public boolean configure(Element service_cluster_info)
191 {
192
193 // get the metadata - for now just add it to the list
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
200 logger.error(" couldn't configure the metadata");
201 }
202 }
203
204 // get the display info
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
211 logger.error("couldn't configure the display items");
212 }
213 }
214
215 //get the plugin info
216 Element import_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.IMPORT_ELEM);
217 if (import_list != null)
218 {
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
225 logger.error("couldn't configure the plugins");
226 }
227 }
228 }
229
230 // do the service racks
231 // empty the service map in case this is a reconfigure
232 clearServices();
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 {
236 // is this an error? could you ever have a service cluster
237 // without service racks???
238 logger.error("cluster has no service racks!!");
239 }
240 else
241 {
242
243 if (!configureServiceRackList(service_rack_list, null))
244 {
245 logger.error("couldn't configure the service racks!!");
246 return false;
247 }
248 }
249
250 return true;
251 }
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;
260 NodeList metanodes = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
261 if (metanodes.getLength() > 0)
262 {
263 for (int k = 0; k < metanodes.getLength(); k++)
264 {
265 this.metadata_list.appendChild(this.doc.importNode(metanodes.item(k), true));
266 }
267 }
268
269 return true;
270 }
271
272 protected boolean addDisplayItems(Element display_list)
273 {
274
275 if (display_list == null)
276 return false;
277 NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
278 if (displaynodes.getLength() > 0)
279 {
280 for (int k = 0; k < displaynodes.getLength(); k++)
281 {
282 Element d = (Element) displaynodes.item(k);
283 String lang = d.getAttribute(GSXML.LANG_ATT);
284 if (lang == null || lang.equals(""))
285 {
286 //set the lang to teh default
287 d.setAttribute(GSXML.LANG_ATT, DEFAULT_LANG);
288 }
289 String name = d.getAttribute(GSXML.NAME_ATT);
290 Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, name);
291 if (this_item == null)
292 {
293 this_item = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM);
294 this_item.setAttribute(GSXML.NAME_ATT, name);
295 this.display_item_list.appendChild(this_item);
296 }
297
298 this_item.appendChild(this.doc.importNode(d, true));
299 }
300 }
301
302 return true;
303 }
304
305 protected boolean addPlugins(Element plugin_list)
306 {
307 if (plugin_list == null)
308 return false;
309 NodeList pluginNodes = plugin_list.getElementsByTagName(GSXML.PLUGIN_ELEM);
310 if (pluginNodes.getLength() > 0)
311 {
312 for (int k = 0; k < pluginNodes.getLength(); k++)
313 {
314 this.plugin_item_list.appendChild(this.doc.importNode(pluginNodes.item(k), true));
315 }
316 }
317
318 return true;
319 }
320
321 protected void clearServices()
322 {
323 service_map.clear();
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 {
334
335 // create all the services
336 NodeList nodes = service_rack_list.getElementsByTagName(GSXML.SERVICE_CLASS_ELEM);
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
346 // the xml request to send to the serviceRack to query what
347 // services it provides
348 Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
349 Element request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", "", "");
350 message.appendChild(request);
351
352 Element n = (Element) nodes.item(i);
353 String servicetype = n.getAttribute(GSXML.NAME_ATT);
354
355 ServiceRack s = null;
356
357 try
358 {
359 // try for a default service in standard package
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 {
370 // name as is, in case package is already specified
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);
381 continue;
382 }
383
384 s.setSiteHome(this.site_home);
385 s.setSiteAddress(this.site_http_address);
386 s.setClusterName(this.cluster_name);
387 s.setMessageRouter(this.router);
388 // pass the xml node to the service for configuration
389 if (s.configure(n, extra_info))
390 {
391
392 // find out the supported service types for this service module
393 Node types = s.process(message);
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 {
401 char extra = '0';
402 String new_service = service + extra;
403
404 while (service_map.get(new_service) != null)
405 {
406 extra++;
407 new_service = service + extra;
408 }
409 this.service_name_map.put(new_service, service);
410 service = new_service;
411 ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
412 }
413 this.service_map.put(service, s);
414 // also add info to the ServiceInfo XML element
415 this.service_list.appendChild(this.doc.importNode(typenodes.item(j), true));
416 }
417 }
418 }
419
420 return true;
421
422 }
423
424 /**
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 {
434
435 Document doc = this.converter.getDOM(in);
436
437 Node res = process(doc);
438 return this.converter.getString(res);
439
440 }
441
442 /**
443 * process XML as Node
444 *
445 */
446 public Node process(Node message_node)
447 {
448
449 Element message = this.converter.nodeToElement(message_node);
450
451 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
452 Document mess_doc = message.getOwnerDocument();
453 Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
454 if (requests.getLength() == 0)
455 {
456 logger.error("no requests for cluster:" + this.cluster_name);
457 // no requests
458 return mainResult; // for now
459 }
460 for (int i = 0; i < requests.getLength(); i++)
461 {
462 Element request = (Element) requests.item(i);
463 String to = request.getAttribute(GSXML.TO_ATT);
464
465 // the cluster name should be first, check, then remove
466 String clustername = GSPath.getFirstLink(to);
467 if (!clustername.equals(this.cluster_name))
468 {
469 logger.error("cluster name wrong! was " + clustername + " should have been " + this.cluster_name);
470 continue; // ignore this request
471 }
472 to = GSPath.removeFirstLink(to);
473 request.setAttribute(GSXML.TO_ATT, to);
474
475 if (to.equals(""))
476 { // this command is for me
477 Element response = processMessage(request);
478 mainResult.appendChild(response);
479
480 }
481 else
482 { // the request is for one of my services
483 String service = GSPath.getFirstLink(to);
484
485 if (!this.service_map.containsKey(service))
486 {
487 logger.error("non-existant service, " + service + ", specified!");
488 continue;
489 }
490 String real_service = service;
491 if (this.service_name_map.containsKey(service))
492 {
493 real_service = (String) this.service_name_map.get(service);
494 // need to change the to att in the request - give the real service name
495 to = request.getAttribute(GSXML.TO_ATT);
496 String old_to = to;
497 to = GSPath.replaceFirstLink(to, real_service);
498 request.setAttribute(GSXML.TO_ATT, to);
499 }
500 // have to pass the request to the service
501 Element single_message = mess_doc.createElement(GSXML.MESSAGE_ELEM);
502 single_message.appendChild(request);
503 Node response_message = ((ModuleInterface) this.service_map.get(service)).process(single_message);
504 if (response_message != null)
505 {
506 Element response = (Element) GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM);
507 String from = response.getAttribute(GSXML.FROM_ATT);
508 if (!real_service.equals(service))
509 {
510 // replace the real service name with the pseudo service name
511 from = GSPath.replaceFirstLink(from, service);
512 // also need to do it in the service itself
513 // shoudl this be done here??
514 Element service_elem = (Element) GSXML.getChildByTagName(response, GSXML.SERVICE_ELEM);
515 if (service_elem != null)
516 {
517 service_elem.setAttribute(GSXML.NAME_ATT, service);
518 }
519 }
520 from = GSPath.prependLink(from, this.cluster_name);
521 response.setAttribute(GSXML.FROM_ATT, from);
522 mainResult.appendChild(this.doc.importNode(response, true));
523 }
524
525 } // else
526
527 } // for each request
528 return mainResult;
529 }
530
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 {
540
541 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
542 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
543 String type = request.getAttribute(GSXML.TYPE_ATT);
544 String lang = request.getAttribute(GSXML.LANG_ATT);
545 response.setAttribute(GSXML.TYPE_ATT, type);
546
547 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE))
548 {
549 // create the collection element
550 Element description = (Element) this.description.cloneNode(false);
551 // set collection type : mg or mgpp
552 description.setAttribute(GSXML.TYPE_ATT, col_type);
553 description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
554
555 response.appendChild(description);
556 // check the param list
557 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
558 if (param_list == null)
559 {
560 addAllDisplayInfo(description, lang);
561 description.appendChild(this.service_list);
562 description.appendChild(this.metadata_list);
563 description.appendChild(this.plugin_item_list);
564 return response;
565 }
566
567 // go through the param list and see what components are wanted
568 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
569 for (int i = 0; i < params.getLength(); i++)
570 {
571
572 Element param = (Element) params.item(i);
573 // Identify the structure information desired
574 if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM))
575 {
576 String info = param.getAttribute(GSXML.VALUE_ATT);
577 if (info.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
578 {
579 description.appendChild(this.service_list);
580 }
581 else if (info.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER))
582 {
583 description.appendChild(metadata_list);
584 }
585 else if (info.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
586 {
587 addAllDisplayInfo(description, lang);
588 }
589 else if (info.equals(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER))
590 {
591 description.appendChild(plugin_item_list);
592 }
593 }
594 }
595 return response;
596 }
597 /*
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 }
684 else
685 { // unknown type
686 logger.error("cant handle request of type " + type);
687
688 }
689 return response;
690 }
691
692 protected Element processSystemRequest(Element request)
693 {
694
695 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
696 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
697 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SYSTEM);
698
699 // a list of system requests - should put any error messages
700 // or success messages into response
701 NodeList commands = request.getElementsByTagName(GSXML.SYSTEM_ELEM);
702 String message = null;
703 for (int i = 0; i < commands.getLength(); i++)
704 {
705 // all the commands should be Elements
706 Element elem = (Element) commands.item(i);
707 String action = elem.getAttribute(GSXML.TYPE_ATT);
708 if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE))
709 {
710 String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
711 if (subset.equals(""))
712 {
713 // need to reconfigure the service cluster
714
715 if (this.configure())
716 {
717 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " reconfigured");
718 response.appendChild(s);
719
720 }
721 else
722 {
723 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " could not be reconfigured");
724 response.appendChild(s);
725 }
726 }
727 else if (this.configureSubset(subset))
728 {
729 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " " + subset + " reconfigured");
730 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);
736 }
737 continue;
738 } // configure action
739
740 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
741 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
742 if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE))
743 {
744 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "activate action not yet implemented - does it even make sense in this context??");
745 response.appendChild(s);
746 }
747 else if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE))
748 {
749 if (module_type.equals(GSXML.SERVICE_ELEM))
750 {
751 // deactivate the service
752 // remove from service_map
753 this.service_map.remove(module_name);
754 Element service_elem = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, module_name);
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 }
762 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
763 response.appendChild(s);
764 }
765 else
766 {
767 logger.error("cant process system request, action " + action);
768 continue;
769 }
770 } // for each command
771 return response;
772 }
773
774 /**
775 * do a configure on only part of the collection
776 */
777 protected boolean configureSubset(String subset)
778 {
779
780 File configFile = new File(GSFile.siteConfigFile(this.site_home));
781 if (!configFile.exists())
782 {
783 logger.error("site config file: " + configFile.getPath() + " not found!");
784 // wont be able to do any of the requests
785 return false;
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
796 Element site_config_elem = site_config_doc.getDocumentElement();
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);
801 // wont be able to do any of teh requests
802 return false;
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);
808 clearServices();
809 return configureServiceRackList(service_rack_list, null);
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);
815 return addMetadata(metadata_list);
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);
821 if (import_list != null)
822 {
823 Element plugin_item_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
824 return addPlugins(plugin_item_list);
825 }
826 else
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
840 NodeList items = this.display_item_list.getChildNodes();
841 for (int i = 0; i < items.getLength(); i++)
842 { // for each key
843 Element m = (Element) items.item(i);
844 // find the child with the correct language
845 Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
846 if (new_m == null && lang != DEFAULT_LANG)
847 {
848 // use the default lang
849 new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
850 }
851 if (new_m == null)
852 {
853 // just get the first one
854 new_m = (Element) GSXML.getChildByTagName(m, GSXML.DISPLAY_TEXT_ELEM);
855 }
856 description.appendChild(new_m.cloneNode(true));
857 }
858 return true;
859
860 }
861
862 protected Element getDisplayTextElement(String key, String lang)
863 {
864
865 Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, key);
866 if (this_item == null)
867 {
868 return null;
869 }
870
871 Element this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
872 if (this_lang == null && lang != DEFAULT_LANG)
873 {
874 // try the default
875 this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
876 }
877 if (this_lang == null)
878 {
879 // just return the first one
880 return GSXML.getFirstElementChild(this_item);//(Element)this_item.getFirstChild().cloneNode(true);
881 }
882 return (Element) this_lang.cloneNode(true);
883
884 }
885
886 public HashMap getServiceMap()
887 {
888 return service_map;
889 }
890}
Note: See TracBrowser for help on using the repository browser.