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

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

Adding in a variable to store any global formatting information

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