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

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

... without the key map lookup debug block

  • Property svn:keywords set to Author Date Id Revision
File size: 42.7 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: 26909 $
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 //System.err.println("**** in MessageRouter, cleanUpModuleMapEntire");
385
386 if (this.module_map != null)
387 {
388 Iterator<ModuleInterface> i = this.module_map.values().iterator();
389 while (i.hasNext())
390 {
391 ModuleInterface i_next = i.next();
392 // System.err.println("**** in MessageRouter, cleanUpModuleMapEntire, away to call cleanUp on " + i_next.toString());
393
394 i_next.cleanUp();
395 i.remove();
396 }
397 }
398 }
399
400 /**
401 * Goes through the children of list, and for each local/site-specific name
402 * attribute, calls cleanUp on the module and removes it from the module_map
403 * and removes it from the list
404 */
405 protected void cleanUpModuleMapSubset(Element list, String remote_site)
406 {
407 logger.error(this.converter.getString(list));
408 NodeList elements = list.getChildNodes(); // we are assuming no extraneous nodes
409 for (int i = elements.getLength() - 1; i >= 0; i--)
410 {
411 Element item = (Element) elements.item(i);
412 String name = item.getAttribute(GSXML.NAME_ATT);
413 String potential_site_name = GSPath.getFirstLink(name);
414 if (remote_site != null)
415 {
416 if (remote_site.equals(potential_site_name))
417 {
418 list.removeChild(item);
419 }
420 }
421 else
422 {
423 if (name.equals(potential_site_name))
424 {// there was no site
425 list.removeChild(item);
426 ModuleInterface m = this.module_map.remove(name);
427 m.cleanUp(); // clean up any open files/connections etc
428 m = null;
429 }
430 }
431 }
432 logger.error(this.converter.getString(list));
433 }
434
435 /**
436 * removes all site modules from module_map, and any stored info about this
437 * sites collections and services
438 */
439 protected void cleanUpAllExternalSiteInfo()
440 {
441
442 NodeList site_nodes = this.site_list.getChildNodes();
443 for (int i = site_nodes.getLength() - 1; i >= 0; i--)
444 {
445 Element item = (Element) site_nodes.item(i);
446 String name = item.getAttribute(GSXML.NAME_ATT);
447 // will remove the node from site_list
448 deactivateModule(GSXML.SITE_ELEM, name);
449 }
450
451 }
452
453 /**
454 * read thru own site config file - create services and connect to sites
455 */
456 protected boolean configureLocalSite()
457 {
458
459 // this may be a reconfigure, so clean up the old moduleMap
460 cleanUpModuleMapEntire();
461
462 File configFile = new File(GSFile.siteConfigFile(this.site_home));
463
464 if (!configFile.exists())
465 {
466 logger.error(" site config file: " + configFile.getPath() + " not found!");
467 return false;
468 }
469
470 Document config_doc = this.converter.getDOM(configFile);
471 if (config_doc == null)
472 {
473 logger.error(" couldn't parse site config file: " + configFile.getPath());
474 return false;
475 }
476
477 this.config_info = config_doc.getDocumentElement();
478
479 // load up the services: serviceRackList
480 this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
481 Element service_rack_list_elem = (Element) GSXML.getChildByTagName(config_info, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
482 configureServices(service_rack_list_elem);
483
484 // load up the service clusters
485 this.cluster_list = this.doc.createElement(GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
486 Element cluster_list_elem = (Element) GSXML.getChildByTagName(config_info, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
487 configureClusters(cluster_list_elem);
488
489 // load up the collections
490 this.collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
491 this.private_collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
492 this.oai_collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
493 configureCollections();
494
495 // load up the external sites - this also adds their services/clusters/collections to the other lists - so must be done last
496 this.site_list = this.doc.createElement(GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
497 Element site_list_elem = (Element) GSXML.getChildByTagName(config_info, GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
498 configureExternalSites(site_list_elem);
499
500 // load up the site metadata
501 this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
502 Element metadata_list_elem = (Element) GSXML.getChildByTagName(config_info, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
503 loadMetadata(metadata_list_elem);
504
505 return true;
506
507 }
508
509 protected boolean configureServices(Element service_rack_list)
510 {
511
512 // load up the individual services
513 logger.info("loading service modules...");
514
515 if (service_rack_list == null)
516 {
517 logger.info("... none to be loaded");
518 return true;
519 }
520
521 NodeList service_racks = service_rack_list.getElementsByTagName(GSXML.SERVICE_CLASS_ELEM);
522 if (service_racks.getLength() == 0)
523 {
524 logger.info("... none to be loaded");
525 return true;
526 }
527
528 Element service_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
529 Element service_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
530 service_message.appendChild(service_request);
531
532 for (int i = 0; i < service_racks.getLength(); i++)
533 {
534 Element n = (Element) service_racks.item(i);
535 String service_name = n.getAttribute(GSXML.NAME_ATT);
536 logger.info("..." + service_name);
537
538 Class service_class = null;
539 try
540 {
541 service_class = Class.forName("org.greenstone.gsdl3.service." + service_name);
542 }
543 catch (ClassNotFoundException e)
544 {
545 try
546 {
547 //try the service_name alone in case the package name is already specified
548 service_class = Class.forName(service_name);
549 }
550 catch (ClassNotFoundException ae)
551 {
552 logger.info(ae.getMessage());
553 }
554 }
555 try
556 {
557 ServiceRack s = (ServiceRack) service_class.newInstance();
558 s.setSiteHome(this.site_home);
559 s.setSiteAddress(this.site_http_address);
560 s.setLibraryName(this.library_name);
561 s.setMessageRouter(this);
562 // pass the XML node to the service for service configuration
563 if (!s.configure(n, null))
564 {
565 logger.error("couldn't configure ServiceRack " + service_name);
566 continue;
567 }
568
569 // find out the supported services for this service module
570 Element service_response = (Element) s.process(service_message);
571 NodeList services = service_response.getElementsByTagName(GSXML.SERVICE_ELEM);
572 if (services.getLength() == 0)
573 {
574 logger.error("MessageRouter configure error: serviceRack " + service_name + " has no services!");
575 }
576 else
577 {
578 for (int j = 0; j < services.getLength(); j++)
579 {
580 String service = ((Element) services.item(j)).getAttribute(GSXML.NAME_ATT);
581
582 this.module_map.put(service, s);
583
584 // add short info to service_list_ XML
585 this.service_list.appendChild(this.doc.importNode(services.item(j), true));
586 }
587 }
588 }
589 catch (Exception e)
590 {
591 logger.error("MessageRouter configure exception: in ServiceRack class specification: " + e.getMessage());
592 e.printStackTrace();
593 }
594 } // for each service module
595 return true;
596 }
597
598 protected boolean configureClusters(Element config_cluster_list)
599 {
600
601 // load up the service clusters
602 logger.info("loading service clusters ...");
603 if (config_cluster_list == null)
604 {
605 logger.info("... none to be loaded");
606 return true;
607 }
608 NodeList service_clusters = config_cluster_list.getElementsByTagName(GSXML.CLUSTER_ELEM);
609 if (service_clusters.getLength() == 0)
610 {
611 logger.info("... none to be loaded");
612 return true;
613 }
614
615 for (int i = 0; i < service_clusters.getLength(); i++)
616 {
617 Element cluster = (Element) service_clusters.item(i);
618 String name = cluster.getAttribute(GSXML.NAME_ATT);
619 logger.info("..." + name);
620 ServiceCluster sc = new ServiceCluster();
621 sc.setSiteHome(this.site_home);
622 sc.setSiteAddress(this.site_http_address);
623 sc.setClusterName(name);
624 sc.setMessageRouter(this);
625 if (!sc.configure(cluster))
626 {
627 logger.error("couldn't configure ServiceCluster " + name);
628 continue;
629 }
630
631 this.module_map.put(name, sc); // this replaces the old one if there was one already present
632 //add short info to cluster list
633 Element e = this.doc.createElement(GSXML.CLUSTER_ELEM);
634 e.setAttribute(GSXML.NAME_ATT, name);
635 this.cluster_list.appendChild(e);
636
637 }
638 return true;
639 }
640
641 /**
642 * looks through the collect directory and activates any collections it
643 * finds. If this is a reconfigure, clean up must be done first before
644 * calling this
645 */
646 protected boolean configureCollections()
647 {
648
649 // read thru the collect directory and activate all the valid collections
650 File collectDir = new File(GSFile.collectDir(this.site_home));
651 if (collectDir.exists())
652 {
653 logger.info("Reading thru directory " + collectDir.getPath() + " to find collections.");
654 File[] contents = collectDir.listFiles();
655 for (int i = 0; i < contents.length; i++)
656 {
657 if (contents[i].isDirectory())
658 {
659
660 String colName = contents[i].getName();
661 if (!colName.startsWith("CVS") && !colName.startsWith(".svn"))
662 {
663 activateCollectionByName(colName);
664 }
665 }
666 }
667 } // collectDir
668 return true;
669 }
670
671 // testing whether a collection (or more generally, a module) is active
672 protected boolean pingModule(String name)
673 {
674 // module (including collection) would have been added to module_map
675 // if activated, and removed from map if deactivated.
676 // The this.collection_list Element would contain the collection too,
677 // but the following check seems to be more generally useful
678 return this.module_map.containsKey(name);
679 }
680
681 /**
682 * creates and configures a new collection if this is done for a
683 * reconfigure, the collection should be deactivated first.
684 *
685 * @param col_name
686 * the name of the collection
687 * @return true if collection created ok
688 */
689 protected boolean activateCollectionByName(String col_name)
690 {
691
692 logger.info("Activating collection: " + col_name + ".");
693
694 // Look for the etc/collectionInit.xml file, and see what sort of Collection to load
695 Collection c = null;
696 File init_file = new File(GSFile.collectionInitFile(this.site_home, col_name));
697
698 if (init_file.exists())
699 {
700 Document init_doc = this.converter.getDOM(init_file);
701 if (init_doc != null)
702 {
703 Element init_elem = init_doc.getDocumentElement();
704 if (init_elem != null)
705 {
706 String coll_class_name = init_elem.getAttribute("class");
707 if (!coll_class_name.equals(""))
708 {
709 try
710 {
711 c = (Collection) Class.forName("org.greenstone.gsdl3.collection." + coll_class_name).newInstance();
712 }
713 catch (Exception e)
714 {
715 logger.info(" couldn't create a new collection, type " + coll_class_name + ", defaulting to class Collection");
716 }
717 }
718 }
719 }
720 }
721 if (c == null)
722 { // we haven't found another classname to use
723 c = new Collection();
724 }
725
726 c.setCollectionName(col_name);
727 c.setSiteHome(this.site_home);
728 c.setSiteAddress(this.site_http_address);
729 c.setMessageRouter(this);
730 if (c.configure())
731 {
732 logger.info("have just configured collection " + col_name);
733 // add to list of collections
734 this.module_map.put(col_name, c);
735 Element e = this.doc.createElement(GSXML.COLLECTION_ELEM);
736 e.setAttribute(GSXML.NAME_ATT, col_name);
737
738 if (c.isPublic())
739 {
740 // only public collections will appear on the home page
741 // add short description_ to collection_list_
742 this.collection_list.appendChild(e);
743
744 if (c.hasOAI())
745 {
746 Element ane = this.doc.createElement(GSXML.COLLECTION_ELEM);
747 //The collection name is returned as site_name:coll_name, which is in fact the set specification
748 ane.setAttribute(GSXML.NAME_ATT, site_name + ":" + col_name);
749 ane.setAttribute(OAIXML.LASTMODIFIED, "" + c.getLastmodified());
750 // lastmodified not of use anymore for OAI, perhaps useful as general information
751 ane.setAttribute(OAIXML.EARLIEST_DATESTAMP, "" + c.getEarliestDatestamp()); // for OAI
752
753 this.oai_collection_list.appendChild(ane);
754 //logger.info(GSXML.xmlNodeToString(oai_collection_list));
755 }
756
757 }
758 else
759 {
760 this.private_collection_list.appendChild(e);
761 }
762 return true;
763 }
764 else
765 {
766 logger.error("Couldn't configure collection: " + col_name + ".");
767 return false;
768 }
769 }
770
771 /**
772 * Goes through the siteList and activates each site found. If this is done
773 * for a reconfigure, a clean up must be done first ****HOW???
774 */
775 protected boolean configureExternalSites(Element config_site_list)
776 {
777
778 // load up the sites
779 logger.info("loading external sites...");
780 if (config_site_list == null)
781 {
782 logger.info("...none found");
783 return true;
784 }
785
786 NodeList sites = config_site_list.getElementsByTagName(GSXML.SITE_ELEM);
787 if (sites.getLength() == 0)
788 {
789 logger.info("...none found");
790 return true;
791 }
792
793 // this is a name to identify the current site in the Communicator
794 String local_site_name = config_site_list.getAttribute(GSXML.LOCAL_SITE_NAME_ATT);
795 if (local_site_name.equals(""))
796 {
797 local_site_name = site_name;
798 }
799
800 for (int i = 0; i < sites.getLength(); i++)
801 {
802 Element s = (Element) sites.item(i);
803 activateSite(s, local_site_name);
804 }
805 return true;
806 }
807
808 protected boolean activateSiteByName(String site_name)
809 {
810 logger.info("Activating site: " + site_name + ".");
811
812 File configFile = new File(GSFile.siteConfigFile(this.site_home));
813
814 if (!configFile.exists())
815 {
816 logger.error(" site config file: " + configFile.getPath() + " not found!");
817 return false;
818 }
819 Document config_doc = this.converter.getDOM(configFile);
820 if (config_doc == null)
821 {
822 logger.error(" couldn't parse site config file: " + configFile.getPath());
823 return false;
824 }
825 Element config_elem = config_doc.getDocumentElement();
826
827 Element config_site_list = (Element) GSXML.getChildByTagName(config_elem, GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
828 if (config_site_list == null)
829 {
830 logger.error("activateSite, no sites found");
831 return false;
832 }
833 // this is a name to identify the current site in the Communicator
834 String local_site_name = config_site_list.getAttribute("localSiteName");
835 if (local_site_name.equals(""))
836 {
837 local_site_name = site_name;
838 }
839
840 Element this_site_elem = GSXML.getNamedElement(config_site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, site_name);
841 if (this_site_elem == null)
842 {
843 logger.error("activateSite, site " + site_name + " not found");
844 return false;
845 }
846
847 return activateSite(this_site_elem, local_site_name);
848 }
849
850 protected boolean activateSite(Element site_elem, String local_site_name)
851 {
852
853 Communicator comm = null;
854 String type = site_elem.getAttribute(GSXML.TYPE_ATT);
855 String name = site_elem.getAttribute(GSXML.NAME_ATT);
856 if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA))
857 {
858 logger.info("activating SOAP site " + name);
859 comm = new SOAPCommunicator();
860 if (comm.configure(site_elem))
861 {
862 comm.setLocalSiteName(local_site_name);
863
864 // add to map of modules
865 this.module_map.put(name, comm);
866 this.site_list.appendChild(this.doc.importNode(site_elem, true));
867 // need to get collection list and service
868 // list from here- if the site isn't up yet, the site will
869 // have to be added later
870 if (!getRemoteSiteInfo(comm, name))
871 {
872 logger.error(" couldn't get info from site");
873 }
874 }
875 else
876 {
877 logger.error(" couldn't configure site");
878 return false;
879 }
880
881 }
882 else
883 {
884 logger.error(" cant talk to server of type:" + type + ", so not making a connection to " + name);
885 return false;
886 }
887 return true;
888 }
889
890 /** Goes through the metadataList and loads each metadatum found */
891 protected boolean loadMetadata(Element config_metadata_list)
892 {
893
894 // load up the sites
895 logger.info("loading site metadata...");
896 if (config_metadata_list == null)
897 {
898 logger.info("...none found");
899 return true;
900 }
901
902 NodeList metadata = config_metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
903 if (metadata.getLength() == 0)
904 {
905 logger.info("...none found");
906 return true;
907 }
908
909 for (int i = 0; i < metadata.getLength(); i++)
910 {
911 Element s = (Element) metadata.item(i);
912 this.metadata_list.appendChild(this.doc.importNode(s, true));
913 }
914 return true;
915 }
916
917 /**
918 * get site info from external site
919 *
920 * @param comm
921 * - the communicator object for the external site
922 * @param site_name
923 * - the name of the external site
924 * @return true if successful
925 */
926 protected boolean getRemoteSiteInfo(Communicator comm, String site_name)
927 {
928
929 logger.info(" getting info from site:" + site_name);
930
931 Element info_request = this.doc.createElement(GSXML.MESSAGE_ELEM);
932 Element req = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
933 info_request.appendChild(req);
934
935 // process the message
936 Node info_response_node = comm.process(info_request);
937 Element info_response = converter.nodeToElement(info_response_node);
938
939 if (info_response == null)
940 {
941 return false;
942 }
943 // collection info
944 NodeList colls = info_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
945 if (colls.getLength() > 0)
946 {
947 for (int i = 0; i < colls.getLength(); i++)
948 {
949 Element e = (Element) colls.item(i);
950 String col_name = e.getAttribute(GSXML.NAME_ATT);
951 // add the info to own coll list - may want to keep
952 // this separate in future - so can distinguish own and
953 // other collections ??
954 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(col_name, site_name));
955 this.collection_list.appendChild(this.doc.importNode(e, true));
956 }
957 }
958
959 // service info
960 NodeList services = info_response.getElementsByTagName(GSXML.SERVICE_ELEM);
961 if (services.getLength() > 0)
962 {
963 for (int i = 0; i < services.getLength(); i++)
964 {
965 Element e = (Element) services.item(i);
966 String serv_name = e.getAttribute(GSXML.NAME_ATT);
967 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(serv_name, site_name));
968 this.service_list.appendChild(this.doc.importNode(e, true));
969 }
970 }
971
972 // serviceCluster info
973 NodeList clusters = info_response.getElementsByTagName(GSXML.CLUSTER_ELEM);
974 if (clusters.getLength() > 0)
975 {
976 for (int i = 0; i < clusters.getLength(); i++)
977 {
978 Element e = (Element) clusters.item(i);
979 String clus_name = e.getAttribute(GSXML.NAME_ATT);
980 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(clus_name, site_name));
981 this.cluster_list.appendChild(this.doc.importNode(e, true));
982 }
983 }
984 return true;
985 }
986
987 protected boolean activateServiceClusterByName(String cluster_name)
988 {
989 return false;
990
991 }
992
993 protected boolean activateServiceRackByName(String module_name)
994 {
995 return false;
996 }
997
998 protected boolean deactivateModule(String type, String name)
999 {
1000
1001 logger.info("deactivating " + type + " module: " + name);
1002 if (this.module_map.containsKey(name))
1003 {
1004
1005 logger.info("found the module");
1006 ModuleInterface m = this.module_map.remove(name);
1007 // also remove the xml bit from description list
1008 if (type.equals(GSXML.COLLECTION_ELEM))
1009 {
1010 if (((Collection) m).isPublic())
1011 {
1012 Element this_col = GSXML.getNamedElement(this.collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
1013 if (this_col != null)
1014 {
1015 this.collection_list.removeChild(this_col);
1016 }
1017 if (((Collection) m).hasOAI())
1018 {
1019 this_col = GSXML.getNamedElement(this.oai_collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
1020 if (this_col != null)
1021 {
1022 this.oai_collection_list.removeChild(this_col);
1023 }
1024 }
1025 }
1026 else
1027 {
1028 // a private collection
1029 Element this_col = GSXML.getNamedElement(this.private_collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
1030 if (this_col != null)
1031 {
1032 this.private_collection_list.removeChild(this_col);
1033 }
1034 }
1035 }
1036 else if (type.equals(GSXML.SERVICE_ELEM))
1037 {
1038 Element this_service = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, name);
1039 if (this_service != null)
1040 {
1041 this.service_list.removeChild(this_service);
1042 }
1043 }
1044 else if (type.equals(GSXML.CLUSTER_ELEM))
1045 {
1046 Element this_cluster = GSXML.getNamedElement(this.cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, name);
1047 if (this_cluster != null)
1048 {
1049 this.cluster_list.removeChild(this_cluster);
1050 }
1051 }
1052 else if (type.equals(GSXML.SITE_ELEM))
1053 {
1054 Element this_site = GSXML.getNamedElement(this.site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, name);
1055 if (this_site != null)
1056 {
1057 this.site_list.removeChild(this_site);
1058
1059 // also remove this sites colls, services, clusters etc
1060 cleanUpModuleMapSubset(this.collection_list, name);
1061 cleanUpModuleMapSubset(this.cluster_list, name);
1062 cleanUpModuleMapSubset(this.service_list, name);
1063
1064 // can remote collections be in the oai_coll list, or private coll list ??
1065 }
1066 }
1067 else
1068 {
1069 logger.error("invalid module type: " + type + ", can't remove info about this module");
1070 }
1071
1072 m.cleanUp(); // clean up any open files/connections etc - can cause trouble on windows
1073 m = null;
1074 return true;
1075 }
1076 // else not deactivated
1077 logger.error(name + " module not found");
1078 return false;
1079
1080 }
1081
1082 //*****************************************************************
1083 // auxiliary process methods
1084 //*****************************************************************
1085
1086 /**
1087 * handles requests made to the MessageRouter itself
1088 *
1089 * @param req
1090 * - the request Element- <request>
1091 * @return the result Element - should be <response>
1092 */
1093 protected Element processMessage(Element req)
1094 {
1095
1096 // message for self, should be type=describe/configure at this stage
1097 String type = req.getAttribute(GSXML.TYPE_ATT);
1098 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
1099 response.setAttribute(GSXML.FROM_ATT, "");
1100 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE))
1101 {
1102 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
1103 // check the param list
1104 Element param_list = (Element) GSXML.getChildByTagName(req, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
1105 if (param_list == null)
1106 {
1107 response.appendChild(this.collection_list);
1108 response.appendChild(this.cluster_list);
1109 response.appendChild(this.site_list);
1110 response.appendChild(this.service_list);
1111 response.appendChild(this.metadata_list);
1112 return response;
1113 }
1114
1115 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
1116
1117 // go through the param list and see what components are wanted
1118 for (int i = 0; i < params.getLength(); i++)
1119 {
1120
1121 Element param = (Element) params.item(i);
1122 // Identify the structure information desired
1123 if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM))
1124 {
1125 String info = param.getAttribute(GSXML.VALUE_ATT);
1126 if (info.equals(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER))
1127 {
1128 response.appendChild(this.collection_list);
1129 }
1130 else if (info.equals(GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER))
1131 {
1132 response.appendChild(this.cluster_list);
1133 }
1134 else if (info.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
1135 {
1136 response.appendChild(this.service_list);
1137 }
1138 else if (info.equals(GSXML.SITE_ELEM + GSXML.LIST_MODIFIER))
1139 {
1140 response.appendChild(this.site_list);
1141 }
1142 else if (info.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER))
1143 {
1144 response.appendChild(this.metadata_list);
1145 }
1146 }
1147 }
1148 return response;
1149
1150 }
1151
1152 if (type.equals(OAIXML.OAI_SET_LIST))
1153 {
1154 logger.info("oaiSetList request received");
1155 //this is the oai receptionist asking for a list of oai-support collections
1156 response.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SET_LIST);
1157 response.appendChild(this.oai_collection_list);
1158 return response;
1159 }
1160
1161 if (type.equals(GSXML.REQUEST_TYPE_SYSTEM))
1162 {
1163
1164 // a list of system requests - should put any error messages
1165 // or success messages into response
1166 NodeList commands = req.getElementsByTagName(GSXML.SYSTEM_ELEM);
1167 Element site_config_elem = null;
1168 boolean success = false;
1169
1170 for (int i = 0; i < commands.getLength(); i++)
1171 {
1172 // all the commands should be Elements
1173 Element elem = (Element) commands.item(i);
1174 String action = elem.getAttribute(GSXML.TYPE_ATT);
1175
1176 if (action.equals(GSXML.SYSTEM_TYPE_PING))
1177 { // ?a=s&sa=ping or ?a=s&sa=ping(&st=collection)&sn=colname
1178
1179 String message = ""; // will be creating the same messages as in GS2's recept/pingaction.cpp
1180 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
1181 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
1182
1183 if (module_name.equals(""))
1184 { // server-level ping
1185 message = "Ping succeeded.";
1186 }
1187 else
1188 { // ping at collection level
1189 if (pingModule(module_name))
1190 {
1191 message = "Ping for " + module_name + " succeeded.";
1192 }
1193 else
1194 {
1195 message = "Ping for " + module_name + " did not succeed.";
1196 }
1197 }
1198 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
1199 response.appendChild(s);
1200 }
1201 //else if (action.equals(GSXML.SYSTEM_TYPE_ISPERSISTENT)) {
1202 // Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "Persistent: true.");
1203 // response.appendChild(s);
1204 //}
1205 else if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE))
1206 {
1207 String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
1208 if (subset.equals(""))
1209 {
1210 // need to reconfigure the MR
1211 this.configureLocalSite();
1212 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "MessageRouter reconfigured successfully");
1213 response.appendChild(s);
1214
1215 }
1216 else
1217 {
1218 // else it a specific request
1219 if (subset.equals(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER))
1220 {
1221 // get rid of all the old collection stuff (not counting remote ones) before activating all the new ones
1222 cleanUpModuleMapSubset(this.collection_list, null);
1223 cleanUpModuleMapSubset(this.private_collection_list, null);
1224 success = configureCollections();
1225 }
1226 else
1227 {
1228
1229 // need the site config file
1230 if (site_config_elem == null)
1231 {
1232
1233 File configFile = new File(GSFile.siteConfigFile(this.site_home));
1234 if (!configFile.exists())
1235 {
1236 logger.error(" site config file: " + configFile.getPath() + " not found!");
1237 continue;
1238 }
1239 Document site_config_doc = this.converter.getDOM(configFile);
1240 if (site_config_doc == null)
1241 {
1242 logger.error(" couldn't parse site config file: " + configFile.getPath());
1243 continue;
1244 }
1245 site_config_elem = site_config_doc.getDocumentElement();
1246 }
1247 if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
1248 {
1249 Element service_rack_list = (Element) GSXML.getChildByTagName(site_config_elem, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
1250 cleanUpModuleMapSubset(this.service_list, null);
1251 success = configureServices(service_rack_list);
1252 }
1253 else if (subset.equals(GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER))
1254 {
1255 Element cluster_list = (Element) GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
1256 cleanUpModuleMapSubset(this.cluster_list, null);
1257 success = configureClusters(cluster_list);
1258 }
1259 else if (subset.equals(GSXML.SITE_ELEM + GSXML.LIST_MODIFIER))
1260 {
1261 Element site_list = (Element) GSXML.getChildByTagName(site_config_elem, GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
1262 cleanUpAllExternalSiteInfo();
1263 success = configureExternalSites(site_list);
1264 }
1265 }
1266 String message = null;
1267 if (success)
1268 {
1269 message = subset + "reconfigured successfully";
1270 }
1271 else
1272 {
1273 message = "Error in reconfiguring " + subset;
1274 }
1275 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
1276 response.appendChild(s);
1277 }
1278
1279 }
1280 else
1281 {
1282 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
1283 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
1284
1285 if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE))
1286 {
1287 success = deactivateModule(module_type, module_name);
1288 if (success)
1289 {
1290 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " deactivated", GSXML.SYSTEM_TYPE_DEACTIVATE, GSXML.SUCCESS);
1291 response.appendChild(s);
1292 }
1293 else
1294 {
1295 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " could not be deactivated", GSXML.SYSTEM_TYPE_DEACTIVATE, GSXML.ERROR);
1296 response.appendChild(s);
1297 }
1298
1299 }
1300 else if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE))
1301 {
1302 // we need to deactivate the module first, in case this is a
1303 // reconfigure
1304 deactivateModule(module_type, module_name);
1305 if (module_type.equals(GSXML.COLLECTION_ELEM))
1306 {
1307 success = activateCollectionByName(module_name);
1308 }
1309 else if (module_type.equals(GSXML.SITE_ELEM))
1310 {
1311 success = activateSiteByName(module_name);
1312 }
1313 else if (module_type.equals(GSXML.CLUSTER_ELEM))
1314 {
1315 success = activateServiceClusterByName(module_name);
1316 }
1317 if (success)
1318 {
1319 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " activated", GSXML.SYSTEM_TYPE_ACTIVATE, GSXML.SUCCESS);
1320 response.appendChild(s);
1321 }
1322 else
1323 {
1324 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type + ": " + module_name + " could not be activated", GSXML.SYSTEM_TYPE_ACTIVATE, GSXML.ERROR);
1325 response.appendChild(s);
1326 }
1327 }
1328 } // else not a configure action
1329 } // for all commands
1330 return response;
1331
1332 } // system type request
1333
1334 // if get here something has gone wrong
1335 logger.error(" cant process request:");
1336 logger.error(this.converter.getString(req));
1337 return null;
1338
1339 }
1340
1341 //* 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... */
1342
1343 protected Element modifyMessages(Element request, Element message, Element result)
1344 {
1345 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
1346 response.setAttribute(GSXML.FROM_ATT, "");
1347 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_MESSAGING);
1348
1349 NodeList commands = request.getElementsByTagName("command");
1350 if (commands == null)
1351 {
1352 logger.error("no commands, " + converter.getPrettyString(request));
1353 return response;
1354 }
1355 for (int i = 0; i < commands.getLength(); i++)
1356 {
1357 Element action = (Element) commands.item(i);
1358 String type = action.getAttribute(GSXML.TYPE_ATT);
1359 if (type.equals("copyNode"))
1360 {
1361 // copies the from node as a child of to node
1362 String from_path = action.getAttribute("from");
1363 String to_path = action.getAttribute("to");
1364 Element from_node = null;
1365 String from_node_root = GSPath.getFirstLink(from_path);
1366 if (from_node_root.startsWith(GSXML.REQUEST_ELEM))
1367 {
1368 from_node = message;
1369 }
1370 else if (from_node_root.startsWith(GSXML.RESPONSE_ELEM))
1371 {
1372 from_node = result;
1373 }
1374 if (from_node == null)
1375 {
1376 continue;
1377 }
1378 Element to_node = null;
1379 String to_node_root = GSPath.getFirstLink(to_path);
1380 if (to_node_root.startsWith(GSXML.REQUEST_ELEM))
1381 {
1382 to_node = message;
1383 }
1384 else if (to_node_root.startsWith(GSXML.RESPONSE_ELEM))
1385 {
1386 to_node = result;
1387 }
1388 if (to_node == null)
1389 {
1390 continue;
1391 }
1392 // now we know what node to copy where
1393 Node orig_node = GSXML.getNodeByPathIndexed(from_node, from_path);
1394 if (orig_node == null)
1395 {
1396 continue;
1397 }
1398 Node new_parent = GSXML.getNodeByPathIndexed(to_node, to_path);
1399 if (new_parent == null)
1400 {
1401 continue;
1402
1403 }
1404 new_parent.appendChild(to_node.getOwnerDocument().importNode(orig_node, true));
1405 }
1406
1407 else if (type.equals("copyChildren"))
1408 {
1409
1410 }
1411 } // for each command
1412 return response;
1413 }
1414
1415 // ****************************************************
1416 // other methods
1417 // ****************************************************
1418
1419}
Note: See TracBrowser for help on using the repository browser.