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

Last change on this file since 32450 was 32450, checked in by kjdon, 6 years ago

now storing and passing round gsparams class so that service clusters and services can add params to it if needed

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