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

Last change on this file since 25989 was 25973, checked in by sjm84, 12 years ago

Adding in a variable to store any global formatting information

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