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

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

I have got rid of site: from the oai set names. Maybe this needs to go back in if we have one MR talking to several remote sites. but really, will this ever happen???

  • Property svn:keywords set to Author Date Id Revision
File size: 42.4 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: 28853 $
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 external 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 ane.setAttribute(GSXML.NAME_ATT, col_name);
745 ane.setAttribute(OAIXML.LASTMODIFIED, "" + c.getLastmodified());
746 // lastmodified not of use anymore for OAI, perhaps useful as general information
747 ane.setAttribute(OAIXML.EARLIEST_DATESTAMP, "" + c.getEarliestDatestamp()); // for OAI
748
749 this.oai_collection_list.appendChild(ane);
750 }
751
752 }
753 else
754 {
755 this.private_collection_list.appendChild(e);
756 }
757 return true;
758 }
759 else
760 {
761 logger.error("Couldn't configure collection: " + col_name + ".");
762 return false;
763 }
764 }
765
766 /**
767 * Goes through the siteList and activates each site found. If this is done
768 * for a reconfigure, a clean up must be done first ****HOW???
769 */
770 protected boolean configureExternalSites(Element config_site_list)
771 {
772
773 // load up the sites
774 logger.info("loading external sites...");
775 if (config_site_list == null)
776 {
777 logger.info("...none found");
778 return true;
779 }
780
781 NodeList sites = config_site_list.getElementsByTagName(GSXML.SITE_ELEM);
782 if (sites.getLength() == 0)
783 {
784 logger.info("...none found");
785 return true;
786 }
787
788 // this is a name to identify the current site in the Communicator
789 String local_site_name = config_site_list.getAttribute(GSXML.LOCAL_SITE_NAME_ATT);
790 if (local_site_name.equals(""))
791 {
792 local_site_name = site_name;
793 }
794
795 for (int i = 0; i < sites.getLength(); i++)
796 {
797 Element s = (Element) sites.item(i);
798 activateSite(s, local_site_name);
799 }
800 return true;
801 }
802
803 protected boolean activateSiteByName(String site_name)
804 {
805 logger.info("Activating site: " + site_name + ".");
806
807 File configFile = new File(GSFile.siteConfigFile(this.site_home));
808
809 if (!configFile.exists())
810 {
811 logger.error(" site config file: " + configFile.getPath() + " not found!");
812 return false;
813 }
814 Document config_doc = this.converter.getDOM(configFile);
815 if (config_doc == null)
816 {
817 logger.error(" couldn't parse site config file: " + configFile.getPath());
818 return false;
819 }
820 Element config_elem = config_doc.getDocumentElement();
821
822 Element config_site_list = (Element) GSXML.getChildByTagName(config_elem, GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
823 if (config_site_list == null)
824 {
825 logger.error("activateSite, no sites found");
826 return false;
827 }
828 // this is a name to identify the current site in the Communicator
829 String local_site_name = config_site_list.getAttribute("localSiteName");
830 if (local_site_name.equals(""))
831 {
832 local_site_name = site_name;
833 }
834
835 Element this_site_elem = GSXML.getNamedElement(config_site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, site_name);
836 if (this_site_elem == null)
837 {
838 logger.error("activateSite, site " + site_name + " not found");
839 return false;
840 }
841
842 return activateSite(this_site_elem, local_site_name);
843 }
844
845 protected boolean activateSite(Element site_elem, String local_site_name)
846 {
847
848 Communicator comm = null;
849 String type = site_elem.getAttribute(GSXML.TYPE_ATT);
850 String name = site_elem.getAttribute(GSXML.NAME_ATT);
851 if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA))
852 {
853 logger.info("activating SOAP site " + name);
854 comm = new SOAPCommunicator();
855 if (comm.configure(site_elem))
856 {
857 comm.setLocalSiteName(local_site_name);
858
859 // add to map of modules
860 this.module_map.put(name, comm);
861 this.site_list.appendChild(this.doc.importNode(site_elem, true));
862 // need to get collection list and service
863 // list from here- if the site isn't up yet, the site will
864 // have to be added later
865 if (!getRemoteSiteInfo(comm, name))
866 {
867 logger.error(" couldn't get info from site");
868 }
869 }
870 else
871 {
872 logger.error(" couldn't configure site");
873 return false;
874 }
875
876 }
877 else
878 {
879 logger.error(" cant talk to server of type:" + type + ", so not making a connection to " + name);
880 return false;
881 }
882 return true;
883 }
884
885 /** Goes through the metadataList and loads each metadatum found */
886 protected boolean loadMetadata(Element config_metadata_list)
887 {
888
889 // load up the sites
890 logger.info("loading site metadata...");
891 if (config_metadata_list == null)
892 {
893 logger.info("...none found");
894 return true;
895 }
896
897 NodeList metadata = config_metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
898 if (metadata.getLength() == 0)
899 {
900 logger.info("...none found");
901 return true;
902 }
903
904 for (int i = 0; i < metadata.getLength(); i++)
905 {
906 Element s = (Element) metadata.item(i);
907 this.metadata_list.appendChild(this.doc.importNode(s, true));
908 }
909 return true;
910 }
911
912 /**
913 * get site info from external site
914 *
915 * @param comm
916 * - the communicator object for the external site
917 * @param site_name
918 * - the name of the external site
919 * @return true if successful
920 */
921 protected boolean getRemoteSiteInfo(Communicator comm, String site_name)
922 {
923
924 logger.info(" getting info from site:" + site_name);
925
926 Element info_request = this.doc.createElement(GSXML.MESSAGE_ELEM);
927 Element req = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
928 info_request.appendChild(req);
929
930 // process the message
931 Node info_response_node = comm.process(info_request);
932 Element info_response = converter.nodeToElement(info_response_node);
933
934 if (info_response == null)
935 {
936 return false;
937 }
938 // collection info
939 NodeList colls = info_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
940 if (colls.getLength() > 0)
941 {
942 for (int i = 0; i < colls.getLength(); i++)
943 {
944 Element e = (Element) colls.item(i);
945 String col_name = e.getAttribute(GSXML.NAME_ATT);
946 // add the info to own coll list - may want to keep
947 // this separate in future - so can distinguish own and
948 // other collections ??
949 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(col_name, site_name));
950 this.collection_list.appendChild(this.doc.importNode(e, true));
951 }
952 }
953
954 // service info
955 NodeList services = info_response.getElementsByTagName(GSXML.SERVICE_ELEM);
956 if (services.getLength() > 0)
957 {
958 for (int i = 0; i < services.getLength(); i++)
959 {
960 Element e = (Element) services.item(i);
961 String serv_name = e.getAttribute(GSXML.NAME_ATT);
962 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(serv_name, site_name));
963 this.service_list.appendChild(this.doc.importNode(e, true));
964 }
965 }
966
967 // serviceCluster info
968 NodeList clusters = info_response.getElementsByTagName(GSXML.CLUSTER_ELEM);
969 if (clusters.getLength() > 0)
970 {
971 for (int i = 0; i < clusters.getLength(); i++)
972 {
973 Element e = (Element) clusters.item(i);
974 String clus_name = e.getAttribute(GSXML.NAME_ATT);
975 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(clus_name, site_name));
976 this.cluster_list.appendChild(this.doc.importNode(e, true));
977 }
978 }
979 return true;
980 }
981
982 protected boolean activateServiceClusterByName(String cluster_name)
983 {
984 return false;
985
986 }
987
988 protected boolean activateServiceRackByName(String module_name)
989 {
990 return false;
991 }
992
993 protected boolean deactivateModule(String type, String name)
994 {
995
996 logger.info("deactivating " + type + " module: " + name);
997 if (this.module_map.containsKey(name))
998 {
999
1000 logger.info("found the module");
1001 ModuleInterface m = this.module_map.remove(name);
1002 // also remove the xml bit from description list
1003 if (type.equals(GSXML.COLLECTION_ELEM))
1004 {
1005 if (((Collection) m).isPublic())
1006 {
1007 Element this_col = GSXML.getNamedElement(this.collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
1008 if (this_col != null)
1009 {
1010 this.collection_list.removeChild(this_col);
1011 }
1012 if (((Collection) m).hasOAI())
1013 {
1014 this_col = GSXML.getNamedElement(this.oai_collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
1015 if (this_col != null)
1016 {
1017 this.oai_collection_list.removeChild(this_col);
1018 }
1019 }
1020 }
1021 else
1022 {
1023 // a private collection
1024 Element this_col = GSXML.getNamedElement(this.private_collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
1025 if (this_col != null)
1026 {
1027 this.private_collection_list.removeChild(this_col);
1028 }
1029 }
1030 }
1031 else if (type.equals(GSXML.SERVICE_ELEM))
1032 {
1033 Element this_service = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, name);
1034 if (this_service != null)
1035 {
1036 this.service_list.removeChild(this_service);
1037 }
1038 }
1039 else if (type.equals(GSXML.CLUSTER_ELEM))
1040 {
1041 Element this_cluster = GSXML.getNamedElement(this.cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, name);
1042 if (this_cluster != null)
1043 {
1044 this.cluster_list.removeChild(this_cluster);
1045 }
1046 }
1047 else if (type.equals(GSXML.SITE_ELEM))
1048 {
1049 Element this_site = GSXML.getNamedElement(this.site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, name);
1050 if (this_site != null)
1051 {
1052 this.site_list.removeChild(this_site);
1053
1054 // also remove this sites colls, services, clusters etc
1055 cleanUpModuleMapSubset(this.collection_list, name);
1056 cleanUpModuleMapSubset(this.cluster_list, name);
1057 cleanUpModuleMapSubset(this.service_list, name);
1058
1059 // can remote collections be in the oai_coll list, or private coll list ??
1060 }
1061 }
1062 else
1063 {
1064 logger.error("invalid module type: " + type + ", can't remove info about this module");
1065 }
1066
1067 m.cleanUp(); // clean up any open files/connections etc - can cause trouble on windows
1068 m = null;
1069 return true;
1070 }
1071 // else not deactivated
1072 logger.error(name + " module not found");
1073 return false;
1074
1075 }
1076
1077 //*****************************************************************
1078 // auxiliary process methods
1079 //*****************************************************************
1080
1081 /**
1082 * handles requests made to the MessageRouter itself
1083 *
1084 * @param req
1085 * - the request Element- <request>
1086 * @return the result Element - should be <response>
1087 */
1088 protected Element processMessage(Element req)
1089 {
1090
1091 // message for self, should be type=describe/configure at this stage
1092 String type = req.getAttribute(GSXML.TYPE_ATT);
1093 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
1094 response.setAttribute(GSXML.FROM_ATT, "");
1095 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE))
1096 {
1097 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
1098 // check the param list
1099 Element param_list = (Element) GSXML.getChildByTagName(req, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
1100 if (param_list == null)
1101 {
1102 response.appendChild(this.collection_list);
1103 response.appendChild(this.cluster_list);
1104 response.appendChild(this.site_list);
1105 response.appendChild(this.service_list);
1106 response.appendChild(this.metadata_list);
1107 return response;
1108 }
1109
1110 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
1111
1112 // go through the param list and see what components are wanted
1113 for (int i = 0; i < params.getLength(); i++)
1114 {
1115
1116 Element param = (Element) params.item(i);
1117 // Identify the structure information desired
1118 if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM))
1119 {
1120 String info = param.getAttribute(GSXML.VALUE_ATT);
1121 if (info.equals(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER))
1122 {
1123 response.appendChild(this.collection_list);
1124 }
1125 else if (info.equals(GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER))
1126 {
1127 response.appendChild(this.cluster_list);
1128 }
1129 else if (info.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
1130 {
1131 response.appendChild(this.service_list);
1132 }
1133 else if (info.equals(GSXML.SITE_ELEM + GSXML.LIST_MODIFIER))
1134 {
1135 response.appendChild(this.site_list);
1136 }
1137 else if (info.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER))
1138 {
1139 response.appendChild(this.metadata_list);
1140 }
1141 }
1142 }
1143 return response;
1144
1145 }
1146
1147 if (type.equals(OAIXML.OAI_SET_LIST))
1148 {
1149 logger.info("oaiSetList request received");
1150 //this is the oai receptionist asking for a list of oai-support collections
1151 response.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SET_LIST);
1152 response.appendChild(this.oai_collection_list);
1153 return response;
1154 }
1155
1156 if (type.equals(GSXML.REQUEST_TYPE_SYSTEM))
1157 {
1158
1159 // a list of system requests - should put any error messages
1160 // or success messages into response
1161 NodeList commands = req.getElementsByTagName(GSXML.SYSTEM_ELEM);
1162 Element site_config_elem = null;
1163 boolean success = false;
1164
1165 for (int i = 0; i < commands.getLength(); i++)
1166 {
1167 // all the commands should be Elements
1168 Element elem = (Element) commands.item(i);
1169 String action = elem.getAttribute(GSXML.TYPE_ATT);
1170
1171 if (action.equals(GSXML.SYSTEM_TYPE_PING))
1172 { // ?a=s&sa=ping or ?a=s&sa=ping(&st=collection)&sn=colname
1173
1174 String message = ""; // will be creating the same messages as in GS2's recept/pingaction.cpp
1175 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
1176 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
1177
1178 if (module_name.equals(""))
1179 { // server-level ping
1180 message = "Ping succeeded.";
1181 }
1182 else
1183 { // ping at collection level
1184 if (pingModule(module_name))
1185 {
1186 message = "Ping for " + module_name + " succeeded.";
1187 }
1188 else
1189 {
1190 message = "Ping for " + module_name + " did not succeed.";
1191 }
1192 }
1193 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
1194 response.appendChild(s);
1195 }
1196 //else if (action.equals(GSXML.SYSTEM_TYPE_ISPERSISTENT)) {
1197 // Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "Persistent: true.");
1198 // response.appendChild(s);
1199 //}
1200 else if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE))
1201 {
1202 String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
1203 if (subset.equals(""))
1204 {
1205 // need to reconfigure the MR
1206 this.configureLocalSite();
1207 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "MessageRouter reconfigured successfully");
1208 response.appendChild(s);
1209
1210 }
1211 else
1212 {
1213 // else it a specific request
1214 if (subset.equals(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER))
1215 {
1216 // get rid of all the old collection stuff (not counting remote ones) before activating all the new ones
1217 cleanUpModuleMapSubset(this.collection_list, null);
1218 cleanUpModuleMapSubset(this.private_collection_list, null);
1219 success = configureCollections();
1220 }
1221 else
1222 {
1223
1224 // need the site config file
1225 if (site_config_elem == null)
1226 {
1227
1228 File configFile = new File(GSFile.siteConfigFile(this.site_home));
1229 if (!configFile.exists())
1230 {
1231 logger.error(" site config file: " + configFile.getPath() + " not found!");
1232 continue;
1233 }
1234 Document site_config_doc = this.converter.getDOM(configFile);
1235 if (site_config_doc == null)
1236 {
1237 logger.error(" couldn't parse site config file: " + configFile.getPath());
1238 continue;
1239 }
1240 site_config_elem = site_config_doc.getDocumentElement();
1241 }
1242 if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
1243 {
1244 Element service_rack_list = (Element) GSXML.getChildByTagName(site_config_elem, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
1245 cleanUpModuleMapSubset(this.service_list, null);
1246 success = configureServices(service_rack_list);
1247 }
1248 else if (subset.equals(GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER))
1249 {
1250 Element cluster_list = (Element) GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
1251 cleanUpModuleMapSubset(this.cluster_list, null);
1252 success = configureClusters(cluster_list);
1253 }
1254 else if (subset.equals(GSXML.SITE_ELEM + GSXML.LIST_MODIFIER))
1255 {
1256 Element site_list = (Element) GSXML.getChildByTagName(site_config_elem, GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
1257 cleanUpAllExternalSiteInfo();
1258 success = configureExternalSites(site_list);
1259 }
1260 }
1261 String message = null;
1262 if (success)
1263 {
1264 message = subset + "reconfigured successfully";
1265 }
1266 else
1267 {
1268 message = "Error in reconfiguring " + subset;
1269 }
1270 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
1271 response.appendChild(s);
1272 }
1273
1274 }
1275 else
1276 {
1277 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
1278 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
1279
1280 if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE))
1281 {
1282 success = deactivateModule(module_type, module_name);
1283 if (success)
1284 {
1285 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " deactivated", GSXML.SYSTEM_TYPE_DEACTIVATE, GSXML.SUCCESS);
1286 response.appendChild(s);
1287 }
1288 else
1289 {
1290 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " could not be deactivated", GSXML.SYSTEM_TYPE_DEACTIVATE, GSXML.ERROR);
1291 response.appendChild(s);
1292 }
1293
1294 }
1295 else if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE))
1296 {
1297 // we need to deactivate the module first, in case this is a
1298 // reconfigure
1299 deactivateModule(module_type, module_name);
1300 if (module_type.equals(GSXML.COLLECTION_ELEM))
1301 {
1302 success = activateCollectionByName(module_name);
1303 }
1304 else if (module_type.equals(GSXML.SITE_ELEM))
1305 {
1306 success = activateSiteByName(module_name);
1307 }
1308 else if (module_type.equals(GSXML.CLUSTER_ELEM))
1309 {
1310 success = activateServiceClusterByName(module_name);
1311 }
1312 if (success)
1313 {
1314 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " activated", GSXML.SYSTEM_TYPE_ACTIVATE, GSXML.SUCCESS);
1315 response.appendChild(s);
1316 }
1317 else
1318 {
1319 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " could not be activated", GSXML.SYSTEM_TYPE_ACTIVATE, GSXML.ERROR);
1320 response.appendChild(s);
1321 }
1322 }
1323 } // else not a configure action
1324 } // for all commands
1325 return response;
1326
1327 } // system type request
1328
1329 // if get here something has gone wrong
1330 String eol = System.getProperty("line.separator");
1331
1332 String mess = "Can't process request:" + eol + " " + this.converter.getString(req);
1333 logger.error(mess);
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.