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

Last change on this file since 30839 was 30839, checked in by kjdon, 8 years ago

displayItem handling moved to DisplayITemUtil class

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