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

Last change on this file since 32449 was 32449, checked in by kjdon, 6 years ago

we now pass in the GSParams class to the service cluster, so all its services can add params to it that need saving.

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