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

Last change on this file since 27940 was 27940, checked in by davidb, 11 years ago

Tidy up on error messages generated in this file

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