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

Last change on this file since 25571 was 25571, checked in by ak19, 12 years ago

Added a ping SystemAction to mirror GS2's updated ability to ping the server as is needed for activate.pl. (GS2 also allows pinging a collection, but that's not been ported to GS3 yet).

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