source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/MessageRouter.java@ 26910

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

Debug tidy up

  • Property svn:keywords set to Author Date Id Revision
File size: 42.5 KB
Line 
1/*
2 * MessageRouter.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 */
19package org.greenstone.gsdl3.core;
20
21import java.io.File;
22import java.net.Authenticator;
23import java.net.PasswordAuthentication;
24import java.util.HashMap;
25import java.util.Map;
26import java.util.Iterator;
27
28import org.apache.commons.lang3.StringUtils;
29import org.apache.log4j.Logger;
30import org.greenstone.gsdl3.collection.Collection;
31import org.greenstone.gsdl3.collection.ServiceCluster;
32import org.greenstone.gsdl3.comms.Communicator;
33import org.greenstone.gsdl3.comms.SOAPCommunicator;
34import org.greenstone.gsdl3.service.ServiceRack;
35import org.greenstone.gsdl3.util.GSFile;
36import org.greenstone.gsdl3.util.GSPath;
37import org.greenstone.gsdl3.util.GSXML;
38import org.greenstone.gsdl3.util.OAIXML;
39import org.greenstone.gsdl3.util.UserContext;
40import org.greenstone.gsdl3.util.XMLConverter;
41import org.greenstone.util.GlobalProperties;
42import org.w3c.dom.Document;
43import org.w3c.dom.Element;
44import org.w3c.dom.Node;
45import org.w3c.dom.NodeList;
46
47/**
48 * The hub of a Greenstone system.
49 *
50 * Accepts XML requests (via process method of ModuleInterface) and routes them
51 * to the appropriate collection or service or external entity.
52 *
53 * contains a map of module objects - may be services, collections, comms
54 * objects talking to other MessageRouters etc.
55 *
56 *
57 * @author Katherine Don
58 * @version $Revision: 26910 $
59 * @see ModuleInterface
60 * @see Collection
61 * @see ServiceRack
62 * @see Communicator
63 *
64 * Since some service classes are moved into a separate directory in order
65 * for them to be checked out from a different repository, we modify the
66 * configureServices method to search some of the classes in other place if
67 * they are not found in the service directory.
68 */
69public class MessageRouter implements ModuleInterface
70{
71
72 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.MessageRouter.class.getName());
73
74 /** the (directory) name of the site */
75 protected String site_name = null;
76 /** site home - the home directory for the site */
77 protected String site_home = null;
78 /** the http address for this site */
79 protected String site_http_address = null;
80
81 protected String library_name = null;
82
83 /** map of names to Module objects */
84 protected HashMap<String, ModuleInterface> module_map = null;
85
86 /** container Document to create XML Nodes */
87 protected Document doc = null;
88 /** the full description of this site */
89
90 // should these things be separated into local and remote??
91
92 /** the original xml config element */
93 public Element config_info = null;
94
95 /** list of collections that can be reached */
96 protected Element collection_list = null;
97 /** list of collections that are loaded but are private */
98 protected Element private_collection_list = null;
99
100 /** list of collections that are public and OAI-supportive */
101 protected Element oai_collection_list = null;
102
103 /** list of service clusters that can be reached */
104 protected Element cluster_list = null;
105 /** list of single services that can be reached */
106 protected Element service_list = null;
107 /** list of sites that can be reached */
108 protected Element site_list = null;
109 /** list of metadata for the site */
110 protected Element metadata_list = null;
111
112 /** a converter class to parse XML and create Docs */
113 protected XMLConverter converter = null;
114
115 //***************************************************************
116 // public methods
117 //***************************************************************
118
119 /** constructor */
120 public MessageRouter()
121 {
122 this.converter = new XMLConverter();
123 this.doc = this.converter.newDOM();
124 }
125
126 public void cleanUp()
127 {
128 cleanUpModuleMapEntire();
129 }
130
131 /** site_name must be set before configure is called */
132 public void setSiteName(String site_name)
133 {
134 this.site_name = site_name;
135 }
136
137 public String getSiteName()
138 {
139 return this.site_name;
140 }
141
142 /** library_name must be set before configure is called */
143 public void setLibraryName(String library_name)
144 {
145 this.library_name = library_name;
146 }
147
148 public String getLibraryName()
149 {
150 return this.library_name;
151 }
152
153 /**
154 * configures the system
155 *
156 * looks in site_home/collect for collections, reads config file
157 * site_home/siteConfig.xml
158 *
159 */
160 public boolean configure()
161 {
162
163 logger.info("configuring the Message Router");
164
165 if (this.site_name == null)
166 {
167 logger.error(" You must set site_name before calling configure");
168 return false;
169 }
170 this.site_home = GSFile.siteHome(GlobalProperties.getGSDL3Home(), this.site_name);
171 String web_address = GlobalProperties.getGSDL3WebAddress();
172 if (web_address.equals("")) {
173 this.site_http_address = "sites/"+this.site_name;
174 } else {
175 this.site_http_address = web_address + "/sites/" + this.site_name;
176 }
177 // are we behind a firewall?? - is there a better place to set up the proxy?
178 String host = GlobalProperties.getProperty("proxy.host");
179 String port = GlobalProperties.getProperty("proxy.port");
180 final String user = GlobalProperties.getProperty("proxy.user");
181 final String passwd = GlobalProperties.getProperty("proxy.password");
182
183 if (host != null && !host.equals("") && port != null && !port.equals(""))
184 {
185 System.setProperty("http.proxyType", "4");
186 System.setProperty("http.proxyHost", host);
187 System.setProperty("http.proxyPort", port);
188 System.setProperty("http.proxySet", "true");
189 // have we got a user/password?
190 if (user != null && !user.equals("") && passwd != null && !passwd.equals(""))
191 {
192 try
193 {
194 // set up the authenticator
195 Authenticator.setDefault(new Authenticator()
196 {
197 protected PasswordAuthentication getPasswordAuthentication()
198 {
199 return new PasswordAuthentication(user, new String(passwd).toCharArray());
200 }
201 });
202
203 }
204 catch (Exception e)
205 {
206 logger.error("MessageRouter Error: couldn't set up an authenticator the proxy");
207
208 }
209 }
210 }
211
212 this.module_map = new HashMap<String, ModuleInterface>();
213
214 // This stuff may be done at a reconfigure also
215 return configureLocalSite();
216
217 }
218
219 /**
220 * Process an XML request - as a String
221 *
222 * @param xml_in
223 * the request to process
224 * @return the response - contains any error messages
225 * @see String
226 */
227 public String process(String xml_in)
228 {
229
230 Document doc = this.converter.getDOM(xml_in);
231
232 Node result = process(doc);
233 return this.converter.getString(result);
234 }
235
236 /**
237 * Process an XML request - as a DOM Element
238 *
239 * @param xml_in
240 * the message to process - should be <message>
241 * @return the response - contains any error messages
242 * @see Element
243 */
244 public Node process(Node message_node)
245 {
246
247 Element message = this.converter.nodeToElement(message_node);
248
249 // check that its a correct message tag
250 if (!message.getTagName().equals(GSXML.MESSAGE_ELEM))
251 {
252 logger.error(" Invalid message. GSDL message should start with <" + GSXML.MESSAGE_ELEM + ">, instead it starts with:" + message.getTagName() + ".");
253 return null;
254 }
255
256 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
257
258 Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
259
260 // empty request
261 if (requests.getLength() == 0)
262 {
263 logger.error("empty request");
264 return mainResult;
265 }
266
267 Document message_doc = message.getOwnerDocument();
268
269 // for now, just process each request one by one, and append the results to mainResult
270 // Note: if you add an element to another node in the same document, it
271 // gets removed from where it was. This changes the node list - you cant iterate over the node list in a normal manner if you are moving elements out of it
272 int num_requests = requests.getLength();
273 for (int i = 0; i < num_requests; i++)
274 {
275 Node result = null;
276 Element req = (Element) requests.item(i);
277 if (req == null)
278 {
279 logger.error("request " + i + " is null");
280 continue;
281 }
282 String path = req.getAttribute(GSXML.TO_ATT); // returns "" if no att of this name
283 if (path.equals(""))
284 {
285 // its a message for the message router
286 String type_att = req.getAttribute(GSXML.TYPE_ATT);
287 if (type_att.equals(GSXML.REQUEST_TYPE_MESSAGING))
288 {
289 // its a messaging request - modifies the requests/responses
290 result = modifyMessages(req, message, mainResult);
291 }
292 else
293 {
294 // standard request
295 result = processMessage(req);
296 }
297
298 if (result != null)
299 {
300 mainResult.appendChild(this.doc.importNode(result, true));
301 }
302 }
303 else
304 {
305 // The message needs to go to another module. The same message can
306 // be passed to multiple modules - they will be in a comma
307 // separated list in the 'to' attribute
308 String[] modules = StringUtils.split(path, ",");
309
310 for (String this_mod : modules)
311 {
312 // why can't we do this outside the loop??
313 Element mess = this.doc.createElement(GSXML.MESSAGE_ELEM);
314 Element copied_request = (Element) this.doc.importNode(req, true);
315 mess.appendChild(copied_request);
316
317 // find the module to pass it on to
318 // need to put the request into a message element
319 String obj = GSPath.getFirstLink(this_mod);
320
321 if (this.module_map.containsKey(obj))
322 {
323 copied_request.setAttribute(GSXML.TO_ATT, this_mod);
324 result = this.module_map.get(obj).process(mess);
325
326 if (result != null)
327 {
328 // append the contents of the message to the mainResult - there will only be one response at this stage
329 Node res = GSXML.getChildByTagName(result, GSXML.RESPONSE_ELEM);
330 if (res != null)
331 {
332 mainResult.appendChild(this.doc.importNode(res, true));
333 }
334 }
335 else
336 {
337 // add in a place holder response
338 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
339 response.setAttribute(GSXML.FROM_ATT, this_mod);
340 mainResult.appendChild(response);
341 logger.error("MessageRouter Error: request had null result!");
342 }
343 }
344 else
345 {
346 logger.error("MessageRouter Error: request has illegal module name in:\n" + this.converter.getString(req));
347 }
348 }
349 }
350
351 } // for each request
352
353 logger.debug("MR returned response");
354 logger.debug(this.converter.getString(mainResult));
355
356 return mainResult;
357 }
358
359 public Element getCollectionList()
360 {
361 return collection_list;
362 }
363
364 public Element getPrivateCollectionList()
365 {
366 return private_collection_list;
367 }
368
369 public HashMap<String, ModuleInterface> getModuleMap()
370 {
371 return module_map;
372 }
373
374 // ********************************************************************
375 // auxiliary configure and cleanup methods
376 // *******************************************************************
377
378 /**
379 * Calls clean up on all modules referenced in the module_map and removes
380 * them .
381 */
382 protected void cleanUpModuleMapEntire()
383 {
384 if (this.module_map != null)
385 {
386 Iterator<ModuleInterface> i = this.module_map.values().iterator();
387 while (i.hasNext())
388 {
389 ModuleInterface i_next = i.next();
390
391 i_next.cleanUp();
392 i.remove();
393 }
394 }
395 }
396
397 /**
398 * Goes through the children of list, and for each local/site-specific name
399 * attribute, calls cleanUp on the module and removes it from the module_map
400 * and removes it from the list
401 */
402 protected void cleanUpModuleMapSubset(Element list, String remote_site)
403 {
404 logger.error(this.converter.getString(list));
405 NodeList elements = list.getChildNodes(); // we are assuming no extraneous nodes
406 for (int i = elements.getLength() - 1; i >= 0; i--)
407 {
408 Element item = (Element) elements.item(i);
409 String name = item.getAttribute(GSXML.NAME_ATT);
410 String potential_site_name = GSPath.getFirstLink(name);
411 if (remote_site != null)
412 {
413 if (remote_site.equals(potential_site_name))
414 {
415 list.removeChild(item);
416 }
417 }
418 else
419 {
420 if (name.equals(potential_site_name))
421 {// there was no site
422 list.removeChild(item);
423 ModuleInterface m = this.module_map.remove(name);
424 m.cleanUp(); // clean up any open files/connections etc
425 m = null;
426 }
427 }
428 }
429 logger.error(this.converter.getString(list));
430 }
431
432 /**
433 * removes all site modules from module_map, and any stored info about this
434 * sites collections and services
435 */
436 protected void cleanUpAllExternalSiteInfo()
437 {
438
439 NodeList site_nodes = this.site_list.getChildNodes();
440 for (int i = site_nodes.getLength() - 1; i >= 0; i--)
441 {
442 Element item = (Element) site_nodes.item(i);
443 String name = item.getAttribute(GSXML.NAME_ATT);
444 // will remove the node from site_list
445 deactivateModule(GSXML.SITE_ELEM, name);
446 }
447
448 }
449
450 /**
451 * read thru own site config file - create services and connect to sites
452 */
453 protected boolean configureLocalSite()
454 {
455
456 // this may be a reconfigure, so clean up the old moduleMap
457 cleanUpModuleMapEntire();
458
459 File configFile = new File(GSFile.siteConfigFile(this.site_home));
460
461 if (!configFile.exists())
462 {
463 logger.error(" site config file: " + configFile.getPath() + " not found!");
464 return false;
465 }
466
467 Document config_doc = this.converter.getDOM(configFile);
468 if (config_doc == null)
469 {
470 logger.error(" couldn't parse site config file: " + configFile.getPath());
471 return false;
472 }
473
474 this.config_info = config_doc.getDocumentElement();
475
476 // load up the services: serviceRackList
477 this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
478 Element service_rack_list_elem = (Element) GSXML.getChildByTagName(config_info, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
479 configureServices(service_rack_list_elem);
480
481 // load up the service clusters
482 this.cluster_list = this.doc.createElement(GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
483 Element cluster_list_elem = (Element) GSXML.getChildByTagName(config_info, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
484 configureClusters(cluster_list_elem);
485
486 // load up the collections
487 this.collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
488 this.private_collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
489 this.oai_collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
490 configureCollections();
491
492 // load up the external sites - this also adds their services/clusters/collections to the other lists - so must be done last
493 this.site_list = this.doc.createElement(GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
494 Element site_list_elem = (Element) GSXML.getChildByTagName(config_info, GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
495 configureExternalSites(site_list_elem);
496
497 // load up the site metadata
498 this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
499 Element metadata_list_elem = (Element) GSXML.getChildByTagName(config_info, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
500 loadMetadata(metadata_list_elem);
501
502 return true;
503
504 }
505
506 protected boolean configureServices(Element service_rack_list)
507 {
508
509 // load up the individual services
510 logger.info("loading service modules...");
511
512 if (service_rack_list == null)
513 {
514 logger.info("... none to be loaded");
515 return true;
516 }
517
518 NodeList service_racks = service_rack_list.getElementsByTagName(GSXML.SERVICE_CLASS_ELEM);
519 if (service_racks.getLength() == 0)
520 {
521 logger.info("... none to be loaded");
522 return true;
523 }
524
525 Element service_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
526 Element service_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
527 service_message.appendChild(service_request);
528
529 for (int i = 0; i < service_racks.getLength(); i++)
530 {
531 Element n = (Element) service_racks.item(i);
532 String service_name = n.getAttribute(GSXML.NAME_ATT);
533 logger.info("..." + service_name);
534
535 Class service_class = null;
536 try
537 {
538 service_class = Class.forName("org.greenstone.gsdl3.service." + service_name);
539 }
540 catch (ClassNotFoundException e)
541 {
542 try
543 {
544 //try the service_name alone in case the package name is already specified
545 service_class = Class.forName(service_name);
546 }
547 catch (ClassNotFoundException ae)
548 {
549 logger.info(ae.getMessage());
550 }
551 }
552 try
553 {
554 ServiceRack s = (ServiceRack) service_class.newInstance();
555 s.setSiteHome(this.site_home);
556 s.setSiteAddress(this.site_http_address);
557 s.setLibraryName(this.library_name);
558 s.setMessageRouter(this);
559 // pass the XML node to the service for service configuration
560 if (!s.configure(n, null))
561 {
562 logger.error("couldn't configure ServiceRack " + service_name);
563 continue;
564 }
565
566 // find out the supported services for this service module
567 Element service_response = (Element) s.process(service_message);
568 NodeList services = service_response.getElementsByTagName(GSXML.SERVICE_ELEM);
569 if (services.getLength() == 0)
570 {
571 logger.error("MessageRouter configure error: serviceRack " + service_name + " has no services!");
572 }
573 else
574 {
575 for (int j = 0; j < services.getLength(); j++)
576 {
577 String service = ((Element) services.item(j)).getAttribute(GSXML.NAME_ATT);
578
579 this.module_map.put(service, s);
580
581 // add short info to service_list_ XML
582 this.service_list.appendChild(this.doc.importNode(services.item(j), true));
583 }
584 }
585 }
586 catch (Exception e)
587 {
588 logger.error("MessageRouter configure exception: in ServiceRack class specification: " + e.getMessage());
589 e.printStackTrace();
590 }
591 } // for each service module
592 return true;
593 }
594
595 protected boolean configureClusters(Element config_cluster_list)
596 {
597
598 // load up the service clusters
599 logger.info("loading service clusters ...");
600 if (config_cluster_list == null)
601 {
602 logger.info("... none to be loaded");
603 return true;
604 }
605 NodeList service_clusters = config_cluster_list.getElementsByTagName(GSXML.CLUSTER_ELEM);
606 if (service_clusters.getLength() == 0)
607 {
608 logger.info("... none to be loaded");
609 return true;
610 }
611
612 for (int i = 0; i < service_clusters.getLength(); i++)
613 {
614 Element cluster = (Element) service_clusters.item(i);
615 String name = cluster.getAttribute(GSXML.NAME_ATT);
616 logger.info("..." + name);
617 ServiceCluster sc = new ServiceCluster();
618 sc.setSiteHome(this.site_home);
619 sc.setSiteAddress(this.site_http_address);
620 sc.setClusterName(name);
621 sc.setMessageRouter(this);
622 if (!sc.configure(cluster))
623 {
624 logger.error("couldn't configure ServiceCluster " + name);
625 continue;
626 }
627
628 this.module_map.put(name, sc); // this replaces the old one if there was one already present
629 //add short info to cluster list
630 Element e = this.doc.createElement(GSXML.CLUSTER_ELEM);
631 e.setAttribute(GSXML.NAME_ATT, name);
632 this.cluster_list.appendChild(e);
633
634 }
635 return true;
636 }
637
638 /**
639 * looks through the collect directory and activates any collections it
640 * finds. If this is a reconfigure, clean up must be done first before
641 * calling this
642 */
643 protected boolean configureCollections()
644 {
645
646 // read thru the collect directory and activate all the valid collections
647 File collectDir = new File(GSFile.collectDir(this.site_home));
648 if (collectDir.exists())
649 {
650 logger.info("Reading thru directory " + collectDir.getPath() + " to find collections.");
651 File[] contents = collectDir.listFiles();
652 for (int i = 0; i < contents.length; i++)
653 {
654 if (contents[i].isDirectory())
655 {
656
657 String colName = contents[i].getName();
658 if (!colName.startsWith("CVS") && !colName.startsWith(".svn"))
659 {
660 activateCollectionByName(colName);
661 }
662 }
663 }
664 } // collectDir
665 return true;
666 }
667
668 // testing whether a collection (or more generally, a module) is active
669 protected boolean pingModule(String name)
670 {
671 // module (including collection) would have been added to module_map
672 // if activated, and removed from map if deactivated.
673 // The this.collection_list Element would contain the collection too,
674 // but the following check seems to be more generally useful
675 return this.module_map.containsKey(name);
676 }
677
678 /**
679 * creates and configures a new collection if this is done for a
680 * reconfigure, the collection should be deactivated first.
681 *
682 * @param col_name
683 * the name of the collection
684 * @return true if collection created ok
685 */
686 protected boolean activateCollectionByName(String col_name)
687 {
688
689 logger.info("Activating collection: " + col_name + ".");
690
691 // Look for the etc/collectionInit.xml file, and see what sort of Collection to load
692 Collection c = null;
693 File init_file = new File(GSFile.collectionInitFile(this.site_home, col_name));
694
695 if (init_file.exists())
696 {
697 Document init_doc = this.converter.getDOM(init_file);
698 if (init_doc != null)
699 {
700 Element init_elem = init_doc.getDocumentElement();
701 if (init_elem != null)
702 {
703 String coll_class_name = init_elem.getAttribute("class");
704 if (!coll_class_name.equals(""))
705 {
706 try
707 {
708 c = (Collection) Class.forName("org.greenstone.gsdl3.collection." + coll_class_name).newInstance();
709 }
710 catch (Exception e)
711 {
712 logger.info(" couldn't create a new collection, type " + coll_class_name + ", defaulting to class Collection");
713 }
714 }
715 }
716 }
717 }
718 if (c == null)
719 { // we haven't found another classname to use
720 c = new Collection();
721 }
722
723 c.setCollectionName(col_name);
724 c.setSiteHome(this.site_home);
725 c.setSiteAddress(this.site_http_address);
726 c.setMessageRouter(this);
727 if (c.configure())
728 {
729 logger.info("have just configured collection " + col_name);
730 // add to list of collections
731 this.module_map.put(col_name, c);
732 Element e = this.doc.createElement(GSXML.COLLECTION_ELEM);
733 e.setAttribute(GSXML.NAME_ATT, col_name);
734
735 if (c.isPublic())
736 {
737 // only public collections will appear on the home page
738 // add short description_ to collection_list_
739 this.collection_list.appendChild(e);
740
741 if (c.hasOAI())
742 {
743 Element ane = this.doc.createElement(GSXML.COLLECTION_ELEM);
744 //The collection name is returned as site_name:coll_name, which is in fact the set specification
745 ane.setAttribute(GSXML.NAME_ATT, site_name + ":" + col_name);
746 ane.setAttribute(OAIXML.LASTMODIFIED, "" + c.getLastmodified());
747 // lastmodified not of use anymore for OAI, perhaps useful as general information
748 ane.setAttribute(OAIXML.EARLIEST_DATESTAMP, "" + c.getEarliestDatestamp()); // for OAI
749
750 this.oai_collection_list.appendChild(ane);
751 //logger.info(GSXML.xmlNodeToString(oai_collection_list));
752 }
753
754 }
755 else
756 {
757 this.private_collection_list.appendChild(e);
758 }
759 return true;
760 }
761 else
762 {
763 logger.error("Couldn't configure collection: " + col_name + ".");
764 return false;
765 }
766 }
767
768 /**
769 * Goes through the siteList and activates each site found. If this is done
770 * for a reconfigure, a clean up must be done first ****HOW???
771 */
772 protected boolean configureExternalSites(Element config_site_list)
773 {
774
775 // load up the sites
776 logger.info("loading external sites...");
777 if (config_site_list == null)
778 {
779 logger.info("...none found");
780 return true;
781 }
782
783 NodeList sites = config_site_list.getElementsByTagName(GSXML.SITE_ELEM);
784 if (sites.getLength() == 0)
785 {
786 logger.info("...none found");
787 return true;
788 }
789
790 // this is a name to identify the current site in the Communicator
791 String local_site_name = config_site_list.getAttribute(GSXML.LOCAL_SITE_NAME_ATT);
792 if (local_site_name.equals(""))
793 {
794 local_site_name = site_name;
795 }
796
797 for (int i = 0; i < sites.getLength(); i++)
798 {
799 Element s = (Element) sites.item(i);
800 activateSite(s, local_site_name);
801 }
802 return true;
803 }
804
805 protected boolean activateSiteByName(String site_name)
806 {
807 logger.info("Activating site: " + site_name + ".");
808
809 File configFile = new File(GSFile.siteConfigFile(this.site_home));
810
811 if (!configFile.exists())
812 {
813 logger.error(" site config file: " + configFile.getPath() + " not found!");
814 return false;
815 }
816 Document config_doc = this.converter.getDOM(configFile);
817 if (config_doc == null)
818 {
819 logger.error(" couldn't parse site config file: " + configFile.getPath());
820 return false;
821 }
822 Element config_elem = config_doc.getDocumentElement();
823
824 Element config_site_list = (Element) GSXML.getChildByTagName(config_elem, GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
825 if (config_site_list == null)
826 {
827 logger.error("activateSite, no sites found");
828 return false;
829 }
830 // this is a name to identify the current site in the Communicator
831 String local_site_name = config_site_list.getAttribute("localSiteName");
832 if (local_site_name.equals(""))
833 {
834 local_site_name = site_name;
835 }
836
837 Element this_site_elem = GSXML.getNamedElement(config_site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, site_name);
838 if (this_site_elem == null)
839 {
840 logger.error("activateSite, site " + site_name + " not found");
841 return false;
842 }
843
844 return activateSite(this_site_elem, local_site_name);
845 }
846
847 protected boolean activateSite(Element site_elem, String local_site_name)
848 {
849
850 Communicator comm = null;
851 String type = site_elem.getAttribute(GSXML.TYPE_ATT);
852 String name = site_elem.getAttribute(GSXML.NAME_ATT);
853 if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA))
854 {
855 logger.info("activating SOAP site " + name);
856 comm = new SOAPCommunicator();
857 if (comm.configure(site_elem))
858 {
859 comm.setLocalSiteName(local_site_name);
860
861 // add to map of modules
862 this.module_map.put(name, comm);
863 this.site_list.appendChild(this.doc.importNode(site_elem, true));
864 // need to get collection list and service
865 // list from here- if the site isn't up yet, the site will
866 // have to be added later
867 if (!getRemoteSiteInfo(comm, name))
868 {
869 logger.error(" couldn't get info from site");
870 }
871 }
872 else
873 {
874 logger.error(" couldn't configure site");
875 return false;
876 }
877
878 }
879 else
880 {
881 logger.error(" cant talk to server of type:" + type + ", so not making a connection to " + name);
882 return false;
883 }
884 return true;
885 }
886
887 /** Goes through the metadataList and loads each metadatum found */
888 protected boolean loadMetadata(Element config_metadata_list)
889 {
890
891 // load up the sites
892 logger.info("loading site metadata...");
893 if (config_metadata_list == null)
894 {
895 logger.info("...none found");
896 return true;
897 }
898
899 NodeList metadata = config_metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
900 if (metadata.getLength() == 0)
901 {
902 logger.info("...none found");
903 return true;
904 }
905
906 for (int i = 0; i < metadata.getLength(); i++)
907 {
908 Element s = (Element) metadata.item(i);
909 this.metadata_list.appendChild(this.doc.importNode(s, true));
910 }
911 return true;
912 }
913
914 /**
915 * get site info from external site
916 *
917 * @param comm
918 * - the communicator object for the external site
919 * @param site_name
920 * - the name of the external site
921 * @return true if successful
922 */
923 protected boolean getRemoteSiteInfo(Communicator comm, String site_name)
924 {
925
926 logger.info(" getting info from site:" + site_name);
927
928 Element info_request = this.doc.createElement(GSXML.MESSAGE_ELEM);
929 Element req = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
930 info_request.appendChild(req);
931
932 // process the message
933 Node info_response_node = comm.process(info_request);
934 Element info_response = converter.nodeToElement(info_response_node);
935
936 if (info_response == null)
937 {
938 return false;
939 }
940 // collection info
941 NodeList colls = info_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
942 if (colls.getLength() > 0)
943 {
944 for (int i = 0; i < colls.getLength(); i++)
945 {
946 Element e = (Element) colls.item(i);
947 String col_name = e.getAttribute(GSXML.NAME_ATT);
948 // add the info to own coll list - may want to keep
949 // this separate in future - so can distinguish own and
950 // other collections ??
951 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(col_name, site_name));
952 this.collection_list.appendChild(this.doc.importNode(e, true));
953 }
954 }
955
956 // service info
957 NodeList services = info_response.getElementsByTagName(GSXML.SERVICE_ELEM);
958 if (services.getLength() > 0)
959 {
960 for (int i = 0; i < services.getLength(); i++)
961 {
962 Element e = (Element) services.item(i);
963 String serv_name = e.getAttribute(GSXML.NAME_ATT);
964 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(serv_name, site_name));
965 this.service_list.appendChild(this.doc.importNode(e, true));
966 }
967 }
968
969 // serviceCluster info
970 NodeList clusters = info_response.getElementsByTagName(GSXML.CLUSTER_ELEM);
971 if (clusters.getLength() > 0)
972 {
973 for (int i = 0; i < clusters.getLength(); i++)
974 {
975 Element e = (Element) clusters.item(i);
976 String clus_name = e.getAttribute(GSXML.NAME_ATT);
977 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(clus_name, site_name));
978 this.cluster_list.appendChild(this.doc.importNode(e, true));
979 }
980 }
981 return true;
982 }
983
984 protected boolean activateServiceClusterByName(String cluster_name)
985 {
986 return false;
987
988 }
989
990 protected boolean activateServiceRackByName(String module_name)
991 {
992 return false;
993 }
994
995 protected boolean deactivateModule(String type, String name)
996 {
997
998 logger.info("deactivating " + type + " module: " + name);
999 if (this.module_map.containsKey(name))
1000 {
1001
1002 logger.info("found the module");
1003 ModuleInterface m = this.module_map.remove(name);
1004 // also remove the xml bit from description list
1005 if (type.equals(GSXML.COLLECTION_ELEM))
1006 {
1007 if (((Collection) m).isPublic())
1008 {
1009 Element this_col = GSXML.getNamedElement(this.collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
1010 if (this_col != null)
1011 {
1012 this.collection_list.removeChild(this_col);
1013 }
1014 if (((Collection) m).hasOAI())
1015 {
1016 this_col = GSXML.getNamedElement(this.oai_collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
1017 if (this_col != null)
1018 {
1019 this.oai_collection_list.removeChild(this_col);
1020 }
1021 }
1022 }
1023 else
1024 {
1025 // a private collection
1026 Element this_col = GSXML.getNamedElement(this.private_collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
1027 if (this_col != null)
1028 {
1029 this.private_collection_list.removeChild(this_col);
1030 }
1031 }
1032 }
1033 else if (type.equals(GSXML.SERVICE_ELEM))
1034 {
1035 Element this_service = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, name);
1036 if (this_service != null)
1037 {
1038 this.service_list.removeChild(this_service);
1039 }
1040 }
1041 else if (type.equals(GSXML.CLUSTER_ELEM))
1042 {
1043 Element this_cluster = GSXML.getNamedElement(this.cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, name);
1044 if (this_cluster != null)
1045 {
1046 this.cluster_list.removeChild(this_cluster);
1047 }
1048 }
1049 else if (type.equals(GSXML.SITE_ELEM))
1050 {
1051 Element this_site = GSXML.getNamedElement(this.site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, name);
1052 if (this_site != null)
1053 {
1054 this.site_list.removeChild(this_site);
1055
1056 // also remove this sites colls, services, clusters etc
1057 cleanUpModuleMapSubset(this.collection_list, name);
1058 cleanUpModuleMapSubset(this.cluster_list, name);
1059 cleanUpModuleMapSubset(this.service_list, name);
1060
1061 // can remote collections be in the oai_coll list, or private coll list ??
1062 }
1063 }
1064 else
1065 {
1066 logger.error("invalid module type: " + type + ", can't remove info about this module");
1067 }
1068
1069 m.cleanUp(); // clean up any open files/connections etc - can cause trouble on windows
1070 m = null;
1071 return true;
1072 }
1073 // else not deactivated
1074 logger.error(name + " module not found");
1075 return false;
1076
1077 }
1078
1079 //*****************************************************************
1080 // auxiliary process methods
1081 //*****************************************************************
1082
1083 /**
1084 * handles requests made to the MessageRouter itself
1085 *
1086 * @param req
1087 * - the request Element- <request>
1088 * @return the result Element - should be <response>
1089 */
1090 protected Element processMessage(Element req)
1091 {
1092
1093 // message for self, should be type=describe/configure at this stage
1094 String type = req.getAttribute(GSXML.TYPE_ATT);
1095 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
1096 response.setAttribute(GSXML.FROM_ATT, "");
1097 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE))
1098 {
1099 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
1100 // check the param list
1101 Element param_list = (Element) GSXML.getChildByTagName(req, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
1102 if (param_list == null)
1103 {
1104 response.appendChild(this.collection_list);
1105 response.appendChild(this.cluster_list);
1106 response.appendChild(this.site_list);
1107 response.appendChild(this.service_list);
1108 response.appendChild(this.metadata_list);
1109 return response;
1110 }
1111
1112 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
1113
1114 // go through the param list and see what components are wanted
1115 for (int i = 0; i < params.getLength(); i++)
1116 {
1117
1118 Element param = (Element) params.item(i);
1119 // Identify the structure information desired
1120 if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM))
1121 {
1122 String info = param.getAttribute(GSXML.VALUE_ATT);
1123 if (info.equals(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER))
1124 {
1125 response.appendChild(this.collection_list);
1126 }
1127 else if (info.equals(GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER))
1128 {
1129 response.appendChild(this.cluster_list);
1130 }
1131 else if (info.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
1132 {
1133 response.appendChild(this.service_list);
1134 }
1135 else if (info.equals(GSXML.SITE_ELEM + GSXML.LIST_MODIFIER))
1136 {
1137 response.appendChild(this.site_list);
1138 }
1139 else if (info.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER))
1140 {
1141 response.appendChild(this.metadata_list);
1142 }
1143 }
1144 }
1145 return response;
1146
1147 }
1148
1149 if (type.equals(OAIXML.OAI_SET_LIST))
1150 {
1151 logger.info("oaiSetList request received");
1152 //this is the oai receptionist asking for a list of oai-support collections
1153 response.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SET_LIST);
1154 response.appendChild(this.oai_collection_list);
1155 return response;
1156 }
1157
1158 if (type.equals(GSXML.REQUEST_TYPE_SYSTEM))
1159 {
1160
1161 // a list of system requests - should put any error messages
1162 // or success messages into response
1163 NodeList commands = req.getElementsByTagName(GSXML.SYSTEM_ELEM);
1164 Element site_config_elem = null;
1165 boolean success = false;
1166
1167 for (int i = 0; i < commands.getLength(); i++)
1168 {
1169 // all the commands should be Elements
1170 Element elem = (Element) commands.item(i);
1171 String action = elem.getAttribute(GSXML.TYPE_ATT);
1172
1173 if (action.equals(GSXML.SYSTEM_TYPE_PING))
1174 { // ?a=s&sa=ping or ?a=s&sa=ping(&st=collection)&sn=colname
1175
1176 String message = ""; // will be creating the same messages as in GS2's recept/pingaction.cpp
1177 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
1178 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
1179
1180 if (module_name.equals(""))
1181 { // server-level ping
1182 message = "Ping succeeded.";
1183 }
1184 else
1185 { // ping at collection level
1186 if (pingModule(module_name))
1187 {
1188 message = "Ping for " + module_name + " succeeded.";
1189 }
1190 else
1191 {
1192 message = "Ping for " + module_name + " did not succeed.";
1193 }
1194 }
1195 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
1196 response.appendChild(s);
1197 }
1198 //else if (action.equals(GSXML.SYSTEM_TYPE_ISPERSISTENT)) {
1199 // Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "Persistent: true.");
1200 // response.appendChild(s);
1201 //}
1202 else if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE))
1203 {
1204 String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
1205 if (subset.equals(""))
1206 {
1207 // need to reconfigure the MR
1208 this.configureLocalSite();
1209 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "MessageRouter reconfigured successfully");
1210 response.appendChild(s);
1211
1212 }
1213 else
1214 {
1215 // else it a specific request
1216 if (subset.equals(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER))
1217 {
1218 // get rid of all the old collection stuff (not counting remote ones) before activating all the new ones
1219 cleanUpModuleMapSubset(this.collection_list, null);
1220 cleanUpModuleMapSubset(this.private_collection_list, null);
1221 success = configureCollections();
1222 }
1223 else
1224 {
1225
1226 // need the site config file
1227 if (site_config_elem == null)
1228 {
1229
1230 File configFile = new File(GSFile.siteConfigFile(this.site_home));
1231 if (!configFile.exists())
1232 {
1233 logger.error(" site config file: " + configFile.getPath() + " not found!");
1234 continue;
1235 }
1236 Document site_config_doc = this.converter.getDOM(configFile);
1237 if (site_config_doc == null)
1238 {
1239 logger.error(" couldn't parse site config file: " + configFile.getPath());
1240 continue;
1241 }
1242 site_config_elem = site_config_doc.getDocumentElement();
1243 }
1244 if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
1245 {
1246 Element service_rack_list = (Element) GSXML.getChildByTagName(site_config_elem, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
1247 cleanUpModuleMapSubset(this.service_list, null);
1248 success = configureServices(service_rack_list);
1249 }
1250 else if (subset.equals(GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER))
1251 {
1252 Element cluster_list = (Element) GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
1253 cleanUpModuleMapSubset(this.cluster_list, null);
1254 success = configureClusters(cluster_list);
1255 }
1256 else if (subset.equals(GSXML.SITE_ELEM + GSXML.LIST_MODIFIER))
1257 {
1258 Element site_list = (Element) GSXML.getChildByTagName(site_config_elem, GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
1259 cleanUpAllExternalSiteInfo();
1260 success = configureExternalSites(site_list);
1261 }
1262 }
1263 String message = null;
1264 if (success)
1265 {
1266 message = subset + "reconfigured successfully";
1267 }
1268 else
1269 {
1270 message = "Error in reconfiguring " + subset;
1271 }
1272 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
1273 response.appendChild(s);
1274 }
1275
1276 }
1277 else
1278 {
1279 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
1280 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
1281
1282 if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE))
1283 {
1284 success = deactivateModule(module_type, module_name);
1285 if (success)
1286 {
1287 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " deactivated", GSXML.SYSTEM_TYPE_DEACTIVATE, GSXML.SUCCESS);
1288 response.appendChild(s);
1289 }
1290 else
1291 {
1292 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " could not be deactivated", GSXML.SYSTEM_TYPE_DEACTIVATE, GSXML.ERROR);
1293 response.appendChild(s);
1294 }
1295
1296 }
1297 else if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE))
1298 {
1299 // we need to deactivate the module first, in case this is a
1300 // reconfigure
1301 deactivateModule(module_type, module_name);
1302 if (module_type.equals(GSXML.COLLECTION_ELEM))
1303 {
1304 success = activateCollectionByName(module_name);
1305 }
1306 else if (module_type.equals(GSXML.SITE_ELEM))
1307 {
1308 success = activateSiteByName(module_name);
1309 }
1310 else if (module_type.equals(GSXML.CLUSTER_ELEM))
1311 {
1312 success = activateServiceClusterByName(module_name);
1313 }
1314 if (success)
1315 {
1316 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " activated", GSXML.SYSTEM_TYPE_ACTIVATE, GSXML.SUCCESS);
1317 response.appendChild(s);
1318 }
1319 else
1320 {
1321 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " could not be activated", GSXML.SYSTEM_TYPE_ACTIVATE, GSXML.ERROR);
1322 response.appendChild(s);
1323 }
1324 }
1325 } // else not a configure action
1326 } // for all commands
1327 return response;
1328
1329 } // system type request
1330
1331 // if get here something has gone wrong
1332 logger.error(" cant process request:");
1333 logger.error(this.converter.getString(req));
1334 return null;
1335
1336 }
1337
1338 //* Used to copy nodes from one message to another. E.g. copy a response node to the next request. Not sure if this is actually used anywhere yet... */
1339
1340 protected Element modifyMessages(Element request, Element message, Element result)
1341 {
1342 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
1343 response.setAttribute(GSXML.FROM_ATT, "");
1344 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_MESSAGING);
1345
1346 NodeList commands = request.getElementsByTagName("command");
1347 if (commands == null)
1348 {
1349 logger.error("no commands, " + converter.getPrettyString(request));
1350 return response;
1351 }
1352 for (int i = 0; i < commands.getLength(); i++)
1353 {
1354 Element action = (Element) commands.item(i);
1355 String type = action.getAttribute(GSXML.TYPE_ATT);
1356 if (type.equals("copyNode"))
1357 {
1358 // copies the from node as a child of to node
1359 String from_path = action.getAttribute("from");
1360 String to_path = action.getAttribute("to");
1361 Element from_node = null;
1362 String from_node_root = GSPath.getFirstLink(from_path);
1363 if (from_node_root.startsWith(GSXML.REQUEST_ELEM))
1364 {
1365 from_node = message;
1366 }
1367 else if (from_node_root.startsWith(GSXML.RESPONSE_ELEM))
1368 {
1369 from_node = result;
1370 }
1371 if (from_node == null)
1372 {
1373 continue;
1374 }
1375 Element to_node = null;
1376 String to_node_root = GSPath.getFirstLink(to_path);
1377 if (to_node_root.startsWith(GSXML.REQUEST_ELEM))
1378 {
1379 to_node = message;
1380 }
1381 else if (to_node_root.startsWith(GSXML.RESPONSE_ELEM))
1382 {
1383 to_node = result;
1384 }
1385 if (to_node == null)
1386 {
1387 continue;
1388 }
1389 // now we know what node to copy where
1390 Node orig_node = GSXML.getNodeByPathIndexed(from_node, from_path);
1391 if (orig_node == null)
1392 {
1393 continue;
1394 }
1395 Node new_parent = GSXML.getNodeByPathIndexed(to_node, to_path);
1396 if (new_parent == null)
1397 {
1398 continue;
1399
1400 }
1401 new_parent.appendChild(to_node.getOwnerDocument().importNode(orig_node, true));
1402 }
1403
1404 else if (type.equals("copyChildren"))
1405 {
1406
1407 }
1408 } // for each command
1409 return response;
1410 }
1411
1412 // ****************************************************
1413 // other methods
1414 // ****************************************************
1415
1416}
Note: See TracBrowser for help on using the repository browser.