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

Last change on this file since 26446 was 26446, checked in by kjdon, 11 years ago

trying to tidy up the collection classes. Move some general stuff to servicecluster and reuse the code a bit more

  • Property svn:keywords set to Author Date Id Revision
File size: 31.8 KB
RevLine 
[3468]1/*
[24975]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 */
[3468]19// leave the package name as is for now - should be changed to something better
20// cluster? groups?
21package org.greenstone.gsdl3.collection;
22
[25750]23import java.io.File;
24import java.util.HashMap;
25import java.util.Iterator;
[3468]26
[25750]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;
[26446]34import org.greenstone.gsdl3.util.SimpleMacroResolver;
[25750]35import org.greenstone.gsdl3.util.UserContext;
36import org.greenstone.gsdl3.util.XMLConverter;
[24975]37import org.w3c.dom.Document;
[25750]38import org.w3c.dom.Element;
[24975]39import org.w3c.dom.Node;
40import org.w3c.dom.NodeList;
[3468]41
42/* ServiceCluster - a groups of services that are related in some way
[24975]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{
[3468]49
[24975]50 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.ServiceCluster.class.getName());
[13123]51
[24221]52 protected static final String CONFIG_ENCODING = "utf-8";
[3468]53
[24221]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
[24975]55
56 /** base directory for the site that this cluster belongs to */
[24221]57 protected String site_home = null;
58 /** http address of the site that this cluster belongs to */
59 protected String site_http_address = null;
[24975]60 /** The name of the cluster - for a collection, this is the collection name */
[24221]61 protected String cluster_name = null;
[26446]62
[24221]63 /** a reference to the message router */
64 protected MessageRouter router = null;
[24975]65 /**
66 * The map of services.
67 *
68 * Maps Services to ServiceRack objects
69 *
70 * @see ServiceRack
71 *
72 */
[25635]73 protected HashMap<String, ServiceRack> service_map = null;
[24975]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 */
[25635]78 protected HashMap<String, String> service_name_map = null;
[24975]79
[24221]80 /** XML converter for String to DOM and vice versa */
[24975]81 protected XMLConverter converter = null;
[26446]82 /** a MacroResolver for resolving macros in displayItems */
83 protected SimpleMacroResolver macro_resolver = null;
[3468]84
[24221]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;
[26446]91 /** language specific display items */
92
[24221]93 protected Element display_item_list = null;
[26446]94 /** default values for servlet params */
95 protected Element library_param_list = null;
[24221]96 /** the element that will have any descriptions passed back in */
97 protected Element description = null;
[3468]98
[24221]99 /** list of plugin */
[26446]100 //protected Element plugin_item_list = null;
[24975]101
[25973]102 protected Element _globalFormat = null;
103
[24975]104 public void setSiteHome(String home)
105 {
[24221]106 this.site_home = home;
107 }
[3468]108
[24975]109 public void setSiteAddress(String address)
110 {
[24221]111 this.site_http_address = address;
112 }
[3468]113
[24975]114 public void cleanUp()
115 {
[25635]116 Iterator<ServiceRack> i = this.service_map.values().iterator();
[24975]117 while (i.hasNext())
118 {
[25635]119 ServiceRack s = i.next();
[24221]120 s.cleanUp();
121 }
122 }
[24975]123
124 public void setClusterName(String name)
125 {
126 this.cluster_name = name;
[24221]127 this.description.setAttribute(GSXML.NAME_ATT, name);
128 }
[3992]129
[24975]130 public void setMessageRouter(MessageRouter m)
131 {
[24221]132 this.router = m;
[9874]133 }
[3468]134
[24975]135 public ServiceCluster()
136 {
[25635]137 this.service_map = new HashMap<String, ServiceRack>();
138 this.service_name_map = new HashMap<String, String>();
[24221]139 this.converter = new XMLConverter();
[26446]140 this.macro_resolver = new SimpleMacroResolver();
[24221]141 this.doc = this.converter.newDOM();
142 this.description = this.doc.createElement(GSXML.CLUSTER_ELEM);
[24975]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);
[26446]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);
[24221]148 }
[3490]149
[24221]150 /**
[24975]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 *
[26446]160 * This is called by ServiceCluster itself when asked to do a reconfigure
[24975]161 * @return true if configure successful, false otherwise.
162 */
163 public boolean configure()
164 {
[3468]165
[24975]166 if (this.site_home == null || this.cluster_name == null)
167 {
[24221]168 logger.error("site_home and cluster_name must be set before configure called!");
169 return false;
170 }
171 logger.info("configuring service cluster");
[26446]172 macro_resolver.addMacro("_httpsite_", this.site_http_address);
[24221]173 // read the site configuration file
174 File config_file = new File(GSFile.siteConfigFile(this.site_home));
[3468]175
[24975]176 if (!config_file.exists())
177 {
178 logger.error("couldn't configure cluster: " + this.cluster_name + ", " + config_file + " does not exist");
[24221]179 return false;
180 }
[3468]181
[24221]182 Document doc = this.converter.getDOM(config_file, CONFIG_ENCODING);
[24975]183 if (doc == null)
184 {
185 logger.error("couldn't parse config file " + config_file.getPath());
[24221]186 return false;
187 }
[24975]188
[24221]189 // get the appropriate service cluster element
[24975]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
[26446]193 // this is probably a reconfigure, so clear all previous info
194 clearServices();
195 clearLocalData();
[24221]196 return this.configure(sc);
[3468]197 }
[24975]198
[26446]199 /** this is called by configure(), but also by MR when it is loading up all the service clusters */
[24975]200 public boolean configure(Element service_cluster_info)
201 {
[26446]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 // {
[24975]212
[26446]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) {
[24221]244 // get the metadata - for now just add it to the list
[24975]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
[24221]251 logger.error(" couldn't configure the metadata");
252 }
253 }
[24975]254
[24221]255 // get the display info
[24975]256 Element display_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
257 if (display_list != null)
258 {
[26446]259 resolveMacros(display_list);
[24975]260 if (!addDisplayItems(display_list))
261 {
262
[24221]263 logger.error("couldn't configure the display items");
264 }
265 }
[24975]266
[26446]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 }
[24975]273 }
274
[26446]275 }
[24975]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;
[24221]283 NodeList metanodes = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
[24975]284 if (metanodes.getLength() > 0)
285 {
286 for (int k = 0; k < metanodes.getLength(); k++)
287 {
[24221]288 this.metadata_list.appendChild(this.doc.importNode(metanodes.item(k), true));
289 }
290 }
[24975]291
[24221]292 return true;
293 }
[26446]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 */
[24975]305 protected boolean addDisplayItems(Element display_list)
306 {
307
308 if (display_list == null)
309 return false;
[24221]310 NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
[24975]311 if (displaynodes.getLength() > 0)
312 {
313 for (int k = 0; k < displaynodes.getLength(); k++)
314 {
[24221]315 Element d = (Element) displaynodes.item(k);
316 String lang = d.getAttribute(GSXML.LANG_ATT);
[24975]317 if (lang == null || lang.equals(""))
318 {
[24221]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);
[24975]324 if (this_item == null)
325 {
[24221]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 }
[24975]330
[24221]331 this_item.appendChild(this.doc.importNode(d, true));
332 }
[4942]333 }
[24975]334
[24221]335 return true;
[4942]336 }
[24975]337
[26446]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)
[24975]354 {
[26446]355 if (display_list == null)
[24975]356 return false;
[26446]357 NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
358 if (displaynodes.getLength() > 0)
[24975]359 {
[26446]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++)
[24221]363 {
[26446]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);
[24221]370 }
[13994]371 }
[26446]372 return true;
373 }
[24975]374
[26446]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
[24221]391 return true;
[13994]392 }
[24975]393 protected void clearServices()
394 {
[26446]395 cleanUp();
[24221]396 service_map.clear();
[26446]397 service_name_map.clear();
[24975]398 this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
[24221]399 }
[4097]400
[26446]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 }
[24975]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
[24221]416 // create all the services
417 NodeList nodes = service_rack_list.getElementsByTagName(GSXML.SERVICE_CLASS_ELEM);
[24975]418 if (nodes.getLength() == 0)
419 {
420 logger.error("ServiceCluster configuration error: cluster " + this.cluster_name + " has no service modules!");
[24221]421 return false;
422 }
[3468]423
[24975]424 for (int i = 0; i < nodes.getLength(); i++)
425 {
426
[24221]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);
[24993]430 Element request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
[24221]431 message.appendChild(request);
[3512]432
[24975]433 Element n = (Element) nodes.item(i);
[24221]434 String servicetype = n.getAttribute(GSXML.NAME_ATT);
[24975]435
[24221]436 ServiceRack s = null;
[24975]437
438 try
439 {
[24221]440 // try for a default service in standard package
[24975]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 {
[24221]450 // name as is, in case package is already specified
[24975]451 s = (ServiceRack) Class.forName(servicetype).newInstance();
452 }
453 catch (Exception e)
454 {
455 }
[24221]456 }
[3468]457
[24975]458 if (s == null)
459 {
460 logger.error("Couldn't get an instance of class " + servicetype + ", or org.greenstone.gsdl3.service." + servicetype);
[24221]461 continue;
462 }
[9433]463
[25973]464 if (_globalFormat != null)
465 {
466 s.setGlobalFormat(_globalFormat);
467 }
468
[24221]469 s.setSiteHome(this.site_home);
470 s.setSiteAddress(this.site_http_address);
471 s.setClusterName(this.cluster_name);
472 s.setMessageRouter(this.router);
473 // pass the xml node to the service for configuration
[24975]474 if (s.configure(n, extra_info))
475 {
[24221]476 // find out the supported service types for this service module
477 Node types = s.process(message);
[24975]478 NodeList typenodes = ((Element) types).getElementsByTagName(GSXML.SERVICE_ELEM);
479
480 for (int j = 0; j < typenodes.getLength(); j++)
481 {
482 String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);
[25750]483
[24975]484 if (service_map.get(service) != null)
485 {
[24221]486 char extra = '0';
[24975]487 String new_service = service + extra;
488
489 while (service_map.get(new_service) != null)
490 {
[24221]491 extra++;
[24975]492 new_service = service + extra;
[24221]493 }
494 this.service_name_map.put(new_service, service);
[24975]495 service = new_service;
[24221]496 ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
497 }
498 this.service_map.put(service, s);
499 // also add info to the ServiceInfo XML element
500 this.service_list.appendChild(this.doc.importNode(typenodes.item(j), true));
501 }
502 }
[24975]503 }
504
[24221]505 return true;
[24975]506
[24221]507 }
[3468]508
[24221]509 /**
[24975]510 * Process an XML document - uses Strings just calls process(Node).
511 *
512 * @param in
513 * the Document to process - a string
514 * @return the resultant document as a string - contains any error messages
515 * @see String
516 */
517 public String process(String in)
518 {
[3468]519
[24221]520 Document doc = this.converter.getDOM(in);
[24975]521
[24221]522 Node res = process(doc);
523 return this.converter.getString(res);
[24975]524
[24221]525 }
[3468]526
[24975]527 /**
528 * process XML as Node
529 *
530 */
531 public Node process(Node message_node)
532 {
[24221]533 Element message = this.converter.nodeToElement(message_node);
[16688]534
[24221]535 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
536 Document mess_doc = message.getOwnerDocument();
537 Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
[24975]538 if (requests.getLength() == 0)
539 {
540 logger.error("no requests for cluster:" + this.cluster_name);
[24221]541 // no requests
542 return mainResult; // for now
543 }
[24975]544 for (int i = 0; i < requests.getLength(); i++)
545 {
546 Element request = (Element) requests.item(i);
[24221]547 String to = request.getAttribute(GSXML.TO_ATT);
[3468]548
[24221]549 // the cluster name should be first, check, then remove
550 String clustername = GSPath.getFirstLink(to);
[24975]551 if (!clustername.equals(this.cluster_name))
552 {
553 logger.error("cluster name wrong! was " + clustername + " should have been " + this.cluster_name);
[24221]554 continue; // ignore this request
[9433]555 }
[24221]556 to = GSPath.removeFirstLink(to);
557 request.setAttribute(GSXML.TO_ATT, to);
[24975]558
559 if (to.equals(""))
560 { // this command is for me
[24221]561 Element response = processMessage(request);
562 mainResult.appendChild(response);
[24975]563
564 }
565 else
566 { // the request is for one of my services
[24221]567 String service = GSPath.getFirstLink(to);
[24975]568
569 if (!this.service_map.containsKey(service))
570 {
571 logger.error("non-existant service, " + service + ", specified!");
[24221]572 continue;
573 }
574 String real_service = service;
[24975]575 if (this.service_name_map.containsKey(service))
576 {
[25635]577 real_service = this.service_name_map.get(service);
[24221]578 // need to change the to att in the request - give the real service name
579 to = request.getAttribute(GSXML.TO_ATT);
580 String old_to = to;
581 to = GSPath.replaceFirstLink(to, real_service);
582 request.setAttribute(GSXML.TO_ATT, to);
583 }
584 // have to pass the request to the service
585 Element single_message = mess_doc.createElement(GSXML.MESSAGE_ELEM);
586 single_message.appendChild(request);
[25750]587
[25635]588 Node response_message = this.service_map.get(service).process(single_message);
[24975]589 if (response_message != null)
590 {
[24221]591 Element response = (Element) GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM);
592 String from = response.getAttribute(GSXML.FROM_ATT);
[24975]593 if (!real_service.equals(service))
594 {
[24221]595 // replace the real service name with the pseudo service name
596 from = GSPath.replaceFirstLink(from, service);
597 // also need to do it in the service itself
598 // shoudl this be done here??
599 Element service_elem = (Element) GSXML.getChildByTagName(response, GSXML.SERVICE_ELEM);
[24975]600 if (service_elem != null)
601 {
[24221]602 service_elem.setAttribute(GSXML.NAME_ATT, service);
603 }
604 }
605 from = GSPath.prependLink(from, this.cluster_name);
606 response.setAttribute(GSXML.FROM_ATT, from);
607 mainResult.appendChild(this.doc.importNode(response, true));
608 }
[24975]609
[24221]610 } // else
[24975]611
[24221]612 } // for each request
613 return mainResult;
614 }
[4097]615
[24975]616 /**
617 * handles requests made to the ServiceCluster itself
618 *
619 * @param req
620 * - the request Element- <request>
621 * @return the result Element - should be <response>
622 */
623 protected Element processMessage(Element request)
624 {
[24221]625 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
626 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
627 String type = request.getAttribute(GSXML.TYPE_ATT);
628 String lang = request.getAttribute(GSXML.LANG_ATT);
629 response.setAttribute(GSXML.TYPE_ATT, type);
[24975]630
631 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE))
632 {
[24221]633 // create the collection element
[24975]634 Element description = (Element) this.description.cloneNode(false);
[25750]635 // set collection type : mg, mgpp, lucene or solr
[26446]636 //description.setAttribute(GSXML.TYPE_ATT, col_type);
637 //description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
[14640]638
[24221]639 response.appendChild(description);
640 // check the param list
[24975]641 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
642 if (param_list == null)
643 {
644 addAllDisplayInfo(description, lang);
[24221]645 description.appendChild(this.service_list);
646 description.appendChild(this.metadata_list);
[26446]647 description.appendChild(this.library_param_list);
648 //description.appendChild(this.plugin_item_list);
[24221]649 return response;
650 }
[24975]651
[24221]652 // go through the param list and see what components are wanted
653 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
[24975]654 for (int i = 0; i < params.getLength(); i++)
655 {
656
657 Element param = (Element) params.item(i);
[24221]658 // Identify the structure information desired
[24975]659 if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM))
660 {
[24221]661 String info = param.getAttribute(GSXML.VALUE_ATT);
[24975]662 if (info.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
663 {
[24221]664 description.appendChild(this.service_list);
[24975]665 }
666 else if (info.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER))
667 {
[26446]668 description.appendChild(this.metadata_list);
[24975]669 }
670 else if (info.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
671 {
[24221]672 addAllDisplayInfo(description, lang);
[24975]673 }
[26446]674 else if (info.equals("libraryParamlist"))
[24975]675 {
[26446]676 description.appendChild(this.library_param_list);
[24221]677 }
678 }
679 }
680 return response;
[4097]681 }
[24221]682 /*
[24975]683 * if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING)) {
684 * logger.error("Received format string request"); String service =
685 * request.getAttribute("service"); logger.error("Service is " +
686 * service); String classifier = null;
687 * if(service.equals("ClassifierBrowse")) { classifier =
688 * request.getAttribute("classifier"); logger.error("Classifier is " +
689 * classifier); } Element format_element = (Element)
690 * GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM); String
691 * format_string = GSXML.getNodeText(format_element);
692 * logger.error("Format string: " + format_string);
693 * logger.error("Config file location = " +
694 * GSFile.collectionConfigFile(this.site_home, this.cluster_name));
695 *
696 * // check for version file
697 *
698 * String directory = new
699 * File(GSFile.collectionConfigFile(this.site_home,
700 * this.cluster_name)).getParent() + File.pathSeparator;
701 * logger.error("Directory is " + directory);
702 *
703 * String version_filename = "";
704 *
705 * if(service.equals("ClassifierBrowse")) version_filename = directory +
706 * "browse_"+classifier+"_format_statement_version.txt"; else
707 * version_filename = directory + "query_format_statement_version.txt";
708 *
709 * File version_file = new File(version_filename);
710 * logger.error("Version filename is " + version_filename);
711 *
712 * String version_number = "1"; BufferedWriter writer; // = new
713 * BufferedWriter(new FileWriter(version_filename)); //RandomAccessFile
714 * version_file_random_access;
715 *
716 * try{
717 *
718 * if(version_file.exists()) { // Read version BufferedReader reader =
719 * new BufferedReader(new FileReader(version_filename));
720 * //version_file_random_access = new RandomAccessFile(version_file,
721 * "r"); //logger.error(" //version_number =
722 * version_file_random_access.readInt(); version_number =
723 * reader.readLine(); int aInt = Integer.parseInt(version_number) + 1;
724 * version_number = Integer.toString(aInt); reader.close();
725 * //version_file_random_access.close(); } else{ // Create
726 * version_file.createNewFile(); // write 1 to file writer = new
727 * BufferedWriter(new FileWriter(version_filename));
728 * //version_file_random_access = new RandomAccessFile(version_file,
729 * "w"); //version_file_random_access.writeInt(version_number);
730 * writer.write(version_number); writer.close();
731 * //version_file_random_access.close(); }
732 *
733 * // Write version file String format_statement_filename = "";
734 *
735 * if(service.equals("ClassifierBrowse")) format_statement_filename =
736 * directory + "browse_"+classifier+"_format_statement_v" +
737 * version_number + ".txt"; else format_statement_filename = directory +
738 * "query_format_statement_v" + version_number + ".txt";
739 *
740 * logger.error("Format statement filename is " +
741 * format_statement_filename);
742 *
743 * writer = new BufferedWriter(new
744 * FileWriter(format_statement_filename)); writer.write(format_string);
745 * writer.close();
746 *
747 * // Update version number //version_file_random_access = new
748 * RandomAccessFile(version_file, "w");
749 * //version_file_random_access.writeInt(version_number);
750 * //version_file_random_access.close();
751 *
752 * writer = new BufferedWriter(new FileWriter(version_filename));
753 * //version_file_random_access = new RandomAccessFile(version_file,
754 * "w"); //version_file_random_access.writeInt(version_number);
755 * writer.write(version_number); writer.close();
756 *
757 *
758 *
759 * } catch (IOException e) { logger.error("IO Exception "+e);
760 * //System.exit(1); }
761 *
762 *
763 * }
764 */
765 if (type.equals(GSXML.REQUEST_TYPE_SYSTEM))
[24221]766 {
[24975]767 response = processSystemRequest(request);
[24221]768 }
769 else
[24975]770 { // unknown type
771 logger.error("cant handle request of type " + type);
[23417]772
[24221]773 }
774 return response;
[4097]775 }
776
[24975]777 protected Element processSystemRequest(Element request)
778 {
[4097]779
[24221]780 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
781 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
782 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SYSTEM);
[4097]783
[24221]784 // a list of system requests - should put any error messages
785 // or success messages into response
786 NodeList commands = request.getElementsByTagName(GSXML.SYSTEM_ELEM);
[24975]787 String message = null;
788 for (int i = 0; i < commands.getLength(); i++)
789 {
[24221]790 // all the commands should be Elements
[24975]791 Element elem = (Element) commands.item(i);
[24221]792 String action = elem.getAttribute(GSXML.TYPE_ATT);
[24975]793 if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE))
794 {
[24221]795 String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
[24975]796 if (subset.equals(""))
797 {
[24221]798 // need to reconfigure the service cluster
[24975]799
800 if (this.configure())
801 {
[24221]802 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " reconfigured");
803 response.appendChild(s);
[24975]804
805 }
806 else
807 {
[24221]808 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " could not be reconfigured");
[24975]809 response.appendChild(s);
[24221]810 }
[24975]811 }
812 else if (this.configureSubset(subset))
813 {
814 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " " + subset + " reconfigured");
[24221]815 response.appendChild(s);
816 }
[24975]817 else
818 {
819 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " " + subset + " could not be reconfigured");
820 response.appendChild(s);
821 }
[24221]822 continue;
823 } // configure action
[24975]824
[24221]825 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
826 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
[24975]827 if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE))
828 {
[24221]829 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "activate action not yet implemented - does it even make sense in this context??");
830 response.appendChild(s);
[24975]831 }
832 else if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE))
833 {
834 if (module_type.equals(GSXML.SERVICE_ELEM))
835 {
[24221]836 // deactivate the service
837 // remove from service_map
838 this.service_map.remove(module_name);
839 Element service_elem = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, module_name);
[24975]840 service_list.removeChild(service_elem);
841 message = module_type + ": " + module_name + " deactivated";
842 }
843 else
844 {
845 message = "can't deactivate " + module_type + " type modules!";
846 }
[24221]847 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
848 response.appendChild(s);
[24975]849 }
850 else
851 {
852 logger.error("cant process system request, action " + action);
[24221]853 continue;
854 }
855 } // for each command
856 return response;
857 }
858
[24975]859 /**
860 * do a configure on only part of the collection
861 */
862 protected boolean configureSubset(String subset)
863 {
864
[24221]865 File configFile = new File(GSFile.siteConfigFile(this.site_home));
[24975]866 if (!configFile.exists())
867 {
868 logger.error("site config file: " + configFile.getPath() + " not found!");
[24221]869 // wont be able to do any of the requests
870 return false;
[24975]871
[4097]872 }
[24975]873
874 Document site_config_doc = this.converter.getDOM(configFile);
875 if (site_config_doc == null)
876 {
877 logger.error("could not read in site config file: " + configFile.getPath());
[24221]878 return false;
879 }
[24975]880
[24221]881 Element site_config_elem = site_config_doc.getDocumentElement();
[24975]882 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);
883 if (cluster_config_elem == null)
884 {
885 logger.error("site config file: " + configFile.getPath() + " has no element for cluster " + this.cluster_name);
[24221]886 // wont be able to do any of teh requests
887 return false;
[24975]888
[24221]889 }
[24975]890 if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
891 {
892 Element service_rack_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
[24221]893 clearServices();
894 return configureServiceRackList(service_rack_list, null);
[24975]895 }
896 else if (subset.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER))
897 {
898 this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
899 Element metadata_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
[24221]900 return addMetadata(metadata_list);
[24975]901 }
[26446]902 // else if (subset.equals(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER))
903 // {
904 // this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
905 // Element import_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.IMPORT_ELEM);
906 // if (import_list != null)
907 // {
908 // Element plugin_item_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
909 // return addPlugins(plugin_item_list);
910 // }
911 // else
912 // return false;
913 // }
[24975]914 else
915 {
916 logger.error("cannot process system request, configure " + subset);
[24221]917 return false;
[24975]918 }
[4105]919
[24975]920 }
[4105]921
[24975]922 protected boolean addAllDisplayInfo(Element description, String lang)
923 {
[4267]924
[24221]925 NodeList items = this.display_item_list.getChildNodes();
[24975]926 for (int i = 0; i < items.getLength(); i++)
927 { // for each key
[24221]928 Element m = (Element) items.item(i);
[26446]929 // findthe child with the correct language
[24221]930 Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
[24975]931 if (new_m == null && lang != DEFAULT_LANG)
932 {
[24221]933 // use the default lang
934 new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
935 }
[24975]936 if (new_m == null)
937 {
[24221]938 // just get the first one
[24975]939 new_m = (Element) GSXML.getChildByTagName(m, GSXML.DISPLAY_TEXT_ELEM);
[24221]940 }
941 description.appendChild(new_m.cloneNode(true));
942 }
943 return true;
[24975]944
[4267]945 }
[4942]946
[24975]947 protected Element getDisplayTextElement(String key, String lang)
948 {
949
[24221]950 Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, key);
[24975]951 if (this_item == null)
952 {
[24221]953 return null;
954 }
[4942]955
[24221]956 Element this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
[24975]957 if (this_lang == null && lang != DEFAULT_LANG)
958 {
[24221]959 // try the default
960 this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
961 }
[24975]962 if (this_lang == null)
963 {
[24221]964 // just return the first one
965 return GSXML.getFirstElementChild(this_item);//(Element)this_item.getFirstChild().cloneNode(true);
966 }
[24975]967 return (Element) this_lang.cloneNode(true);
968
[4942]969 }
[24975]970
[25635]971 public HashMap<String, ServiceRack> getServiceMap()
[24975]972 {
[24221]973 return service_map;
[4942]974 }
[3468]975}
Note: See TracBrowser for help on using the repository browser.