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

Last change on this file since 25727 was 25727, checked in by kjdon, 12 years ago

getting rid of my email address

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