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

Last change on this file since 28966 was 28966, checked in by kjdon, 10 years ago

Lots of changes. Mainly to do with removing this.doc from everywhere. Document is not thread safe. Now we tend to create a new Document everytime we are starting a new page/message etc. in service this.desc_doc is available as teh document to create service info stuff. But it should only be used for this and not for other messages. newDOM is now static for XMLConverter. method param changes for some GSXML methods.

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