source: trunk/gsdl3/src/java/org/greenstone/gsdl3/core/MessageRouter.java@ 4987

Last change on this file since 4987 was 4859, checked in by kjdon, 21 years ago

commented out some printlns

  • Property svn:keywords set to Author Date Id Revision
File size: 28.6 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.gsdl3.util.*;
22import org.greenstone.gsdl3.service.*;
23import org.greenstone.gsdl3.comms.*;
24import org.greenstone.gsdl3.collection.*;
25
26// XML classes
27import org.w3c.dom.Node;
28import org.w3c.dom.NodeList;
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
31import org.xml.sax.InputSource;
32import javax.xml.parsers.*;
33
34// other java classes
35import java.io.File;
36import java.util.HashMap;
37import java.util.Iterator;
38import java.io.Reader;
39import java.io.StringReader;
40
41/**
42 * The hub of a Greenstone system.
43 *
44 * Accepts XML requests (via process method of ModuleInterface) and routes them to the appropriate collection or
45 * service or external entity.
46 *
47 * contains a map of module objects - may be services, collections, comms
48 * objects talking to other MessageRouters etc.
49 *
50 *
51 * @author <a href="mailto:[email protected]">Katherine Don</a>
52 * @version $Revision: 4859 $
53 * @see ModuleInterface
54 * @see Collection
55 * @see ServiceRack
56 * @see Communicator
57 *
58 */
59public class MessageRouter implements ModuleInterface {
60
61 /** site home - the home directory for the site */
62 protected String site_home=null;
63 /** the http address for this site */
64 protected String site_http_address=null;
65
66 /** the global name for this site */
67 protected String global_site_name=null;
68
69 /** map of names to Module objects */
70 protected HashMap module_map=null;
71
72 /** container Document to create XML Nodes */
73 protected Document doc=null;
74 /** the full description of this site */
75
76 // should these things be separated into local and remote??
77
78 /** list of collections that can be reached */
79 protected Element collection_list = null;
80 /** list of service clusters that can be reached */
81 protected Element cluster_list = null;
82 /** list of single services that can be reached */
83 protected Element service_list = null;
84 /** list of sites that can be reached */
85 protected Element site_list = null;
86
87 /** a converter class to parse XML and create Docs */
88 protected XMLConverter converter=null;
89
90 //***************************************************************
91 // public methods
92 //***************************************************************
93
94 /** constructor */
95 public MessageRouter() {
96 this.converter = new XMLConverter();
97 this.doc = this.converter.newDOM();
98
99
100 }
101
102 /** site_home must be set before configure called */
103 public void setSiteHome(String home) {
104 this.site_home = home;
105 }
106
107 /**
108 * configures the system
109 *
110 * looks in site_home/collect for collections, reads config file
111 * site_home/sitecfg.xml
112 *
113 */
114 public boolean configure() {
115
116 System.out.println("MessageRouter:configuring site");
117
118 if (this.site_home==null) {
119 System.err.println("You must set site_home before calling configure");
120 return false;
121 }
122
123 // read thru own config file - create services and connect to sites
124 File configFile = new File(GSFile.siteConfigFile(this.site_home));
125
126 if (!configFile.exists() ) {
127 System.err.println("MessageRouter: site config file: "+configFile.getPath()+" not found!");
128 return false;
129 }
130
131 this.module_map = new HashMap();
132
133 Element config = this.converter.getDOM(configFile).getDocumentElement();
134
135 Element local_site_name = (Element)GSXML.getChildByTagName(config, GSXML.SITE_NAME_ELEM);
136 if (local_site_name == null) {
137 System.err.println("MessageRouter configure error:no site name in config file");
138 return false;
139 } else {
140 this.global_site_name = local_site_name.getAttribute(GSXML.VALUE_ATT);
141 }
142
143 Element http_address = (Element)GSXML.getChildByTagName(config, GSXML.SITE_HTTP_ADDRESS_ELEM);
144 if (http_address == null) {
145 System.err.println("MessageRouter configure error: no http address in config file");
146 return false;
147 } else {
148 this.site_http_address = http_address.getAttribute(GSXML.VALUE_ATT);
149 }
150
151 // load up the services
152 Element service_rack_list = (Element)GSXML.getChildByTagName(config, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
153 configureServices(service_rack_list);
154
155 // load up the service clusters
156 Element cluster_list = (Element)GSXML.getChildByTagName(config, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
157 configureClusters(cluster_list);
158
159 // load up the collections
160 configureCollections();
161
162 // load up the external sites - this also adds their services/clusters/collections to the other lists - so must be done last
163 Element site_list = (Element)GSXML.getChildByTagName(config, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
164 configureSites(site_list);
165
166
167 return true;
168
169 }
170
171
172 /**
173 * Process an XML request - as a String
174 *
175 * @param xml_in the request to process
176 * @return the response - contains any error messages
177 * @see String
178 */
179 public String process(String xml_in) {
180
181 Document doc = this.converter.getDOM(xml_in);
182
183 Element result = process(doc.getDocumentElement());
184 return this.converter.getString(result);
185 }
186
187 /**
188 * Process an XML request - as a DOM Element
189 *
190 * @param xml_in the message to process - should be <message>
191 * @return the response - contains any error messages
192 * @see Element
193 */
194 public Element process(Element message) {
195
196 ///ystem.out.println("MR received request");
197 ///ystem.out.println(this.converter.getString(message));
198
199 // check that its a correct message tag
200 if (!message.getTagName().equals(GSXML.MESSAGE_ELEM)) {
201 System.err.println("Invalid message. GSDL message should start with <"+GSXML.MESSAGE_ELEM+">, instead it starts with:"+message.getTagName()+".");
202 return null;
203 }
204
205 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
206
207 Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
208
209 // empty request
210 if (requests.getLength()==0) {
211 return mainResult;
212 }
213
214 Document message_doc = message.getOwnerDocument();
215
216 // for now, just process each request one by one, and append the results to mainResult
217 // Note: if you add an element to another node in the same document, it
218 // 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
219 int num_requests = requests.getLength();
220 for (int i=0; i< num_requests; i++) {
221 Node result=null;
222 Element req = (Element)requests.item(0);
223 String path = req.getAttribute(GSXML.TO_ATT); // returns "" if no att of this name
224 if (path.equals("")) {
225 // its a message for the message router
226 result = processMessage(req);
227 mainResult.appendChild(this.doc.importNode(result, true));
228 } else {
229 // find the module to pass it on to
230 // need to put the request into a message element
231 Element mess = message_doc.createElement(GSXML.MESSAGE_ELEM);
232 mess.appendChild(req);
233 String obj = GSPath.getFirstLink(path);
234 if (this.module_map.containsKey(obj)) {
235 result = ((ModuleInterface)this.module_map.get(obj)).process(mess);
236 if (result !=null ) {
237 // append the contents of the message to the mainResult - there will only be one response at this stage
238 mainResult.appendChild(this.doc.importNode(GSXML.getChildByTagName(result, GSXML.RESPONSE_ELEM), true));
239 } else {
240 System.err.println("MessageRouter Error: request had null result!");
241 }
242 } else {
243 System.err.println("MessageRouter Error: request has illegal module name in:\n"+this.converter.getString(req));
244 }
245 }
246
247 } // for each request
248
249 ///ystem.out.println("MR returned response");
250 ///ystem.out.println(this.converter.getString(mainResult));
251
252 return mainResult;
253
254 }
255
256 // ********************************************************************
257 // auxiliary configure methods
258 // *******************************************************************
259
260
261 protected boolean configureServices(Element service_rack_list) {
262
263 this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
264
265 // load up the individual services
266 System.out.println("loading service modules...");
267
268 if (service_rack_list == null) {
269 System.out.println("... none to be loaded");
270 return true;
271 }
272
273 NodeList service_racks = service_rack_list.getElementsByTagName(GSXML.SERVICE_CLASS_ELEM);
274 if (service_racks.getLength()==0) {
275 System.out.println("... none to be loaded");
276 return true;
277 }
278
279 Element service_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
280 Element service_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", "");
281 service_message.appendChild(service_request);
282
283 for(int i=0; i<service_racks.getLength(); i++) {
284 Element n = (Element)service_racks.item(i);
285 String service_name = n.getAttribute(GSXML.NAME_ATT);
286 System.out.println("..."+service_name);
287 try {
288 ServiceRack s = (ServiceRack)Class.forName("org.greenstone.gsdl3.service."+service_name).newInstance();
289 s.setSiteHome(this.site_home);
290 s.setSiteAddress(this.site_http_address);
291 s.setMessageRouter(this);
292 // pass the XML node to the service for service configuration
293 s.configure(n, null);
294
295 // find out the supported services for this service module
296 Element service_response = (Element) s.process(service_message);
297 NodeList services = service_response.getElementsByTagName(GSXML.SERVICE_ELEM);
298 if (services.getLength()==0) {
299 System.err.println("MessageRouter configure error: serviceRack "+service_name+" has no services!");
300 } else {
301 for (int j=0; j<services.getLength();j++) {
302 String service = ((Element)services.item(j)).getAttribute(GSXML.NAME_ATT);
303 this.module_map.put(service, s);
304
305 // add short info to service_list_ XML
306 this.service_list.appendChild(this.doc.importNode(services.item(j), true));
307 }
308 }
309 } catch (Exception e ) {
310 System.err.println("MessageRouter configure exception: in ServiceRack class specification: "+ e.getMessage());
311 e.printStackTrace();
312 }
313 } // for each service module
314 return true;
315 }
316
317 protected boolean configureClusters(Element cluster_list) {
318
319 this.cluster_list = this.doc.createElement(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
320 // load up the service clusters
321 System.out.println("loading service clusters ...");
322 if (cluster_list == null) {
323 System.out.println("... none to be loaded");
324 return true;
325 }
326 NodeList service_clusters = cluster_list.getElementsByTagName(GSXML.CLUSTER_ELEM);
327 if (service_clusters.getLength()==0) {
328 System.out.println("... none to be loaded");
329 return true;
330 }
331
332 for (int i=0; i<service_clusters.getLength(); i++) {
333 Element cluster = (Element)service_clusters.item(i);
334 String name = cluster.getAttribute(GSXML.NAME_ATT);
335 System.out.println("..."+name);
336 ServiceCluster sc = new ServiceCluster();
337 sc.setSiteHome(this.site_home);
338 sc.setSiteAddress(this.site_http_address);
339 sc.setClusterName(name);
340 sc.setMessageRouter(this);
341 sc.configure(cluster);
342 this.module_map.put(name, sc); // this replaces the old one if there was one already present
343 //add short info to cluster list
344 Element e = this.doc.createElement(GSXML.CLUSTER_ELEM);
345 e.setAttribute(GSXML.NAME_ATT, name);
346 this.cluster_list.appendChild(e);
347
348 }
349 return true;
350 }
351
352 protected boolean configureCollections() {
353
354 this.collection_list = this.doc.createElement(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER);
355
356 // read thru the collect directory and activate all the valid collections
357 File collectDir = new File(GSFile.collectDir(this.site_home));
358 if (collectDir.exists()) {
359 System.out.println("Reading thru directory "+collectDir.getPath()+" to find collections.");
360 File[] contents = collectDir.listFiles();
361 for (int i=0; i<contents.length;i++) {
362 if(contents[i].isDirectory()) {
363
364 String colName = contents[i].getName();
365 if (!colName.startsWith("CVS")) {
366 activateCollection(colName);
367 }
368 }
369 }
370 } // collectDir
371 return true;
372 }
373
374 protected boolean configureSites(Element site_list) {
375
376 this.site_list = this.doc.createElement(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
377 // load up the sites
378 System.out.println("loading external sites...");
379 if (site_list ==null ) {
380 System.out.println("...none found");
381 return true;
382 }
383 NodeList sites = site_list.getElementsByTagName(GSXML.SITE_ELEM);
384 if (sites.getLength()==0) {
385 System.out.println("...none found");
386 return true;
387 }
388 for (int i=0; i<sites.getLength(); i++) {
389 Element s = (Element)sites.item(i);
390 Communicator comm=null;
391 String type = s.getAttribute(GSXML.TYPE_ATT);
392 String name = s.getAttribute(GSXML.NAME_ATT);
393 if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
394 comm = new SOAPCommunicator();
395 if (comm.configure(s)) {
396 comm.setLocalSiteName(this.global_site_name);
397
398 // add to map of modules
399 this.module_map.put(name, comm);
400 this.site_list.appendChild(this.doc.importNode(s, true));
401 // need to get collection list and service
402 // list from here- if the site isn't up yet, the site will
403 // have to be added later
404 if (!getRemoteSiteInfo(comm, name)) {
405 System.err.println("couldn't get info from site "+name);
406 }
407 } else {
408 System.err.println("couldn't configure soap site:"+name);
409 }
410
411 } else {
412 System.err.print("cant talk to server of type:"+type);
413 System.err.println(", so not making a connection to "+name);
414 }
415
416 }
417 return true;
418 }
419
420
421 /** get site info from external site
422 *
423 * @param comm - the communicator object for the external site
424 * @param site_name - the name of the external site
425 * @return true if successful
426 */
427 protected boolean getRemoteSiteInfo(Communicator comm, String site_name) {
428
429 System.out.println("MessageRouter: getting info from site:"+site_name);
430
431 Element info_request = this.doc.createElement(GSXML.MESSAGE_ELEM);
432 Element req = this.doc.createElement(GSXML.REQUEST_ELEM);
433 info_request.appendChild(req);
434 req.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
435
436 // process the message
437 Element info_response = comm.process(info_request);
438 if (info_response == null) {
439 return false;
440 }
441 // collection info
442 NodeList colls = info_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
443 if (colls.getLength()>0) {
444 for (int i=0; i<colls.getLength(); i++) {
445 Element e = (Element)colls.item(i);
446 String col_name = e.getAttribute(GSXML.NAME_ATT);
447 // add the info to own coll list - may want to keep
448 // this separate in future - so can distinguish own and
449 // other collections ??
450 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(col_name, site_name));
451 this.collection_list.appendChild(this.doc.importNode(e, true));
452 }
453 }
454
455 // service info
456 NodeList services = info_response.getElementsByTagName(GSXML.SERVICE_ELEM);
457 if (services.getLength()>0) {
458 for (int i=0; i<services.getLength(); i++) {
459 Element e = (Element)services.item(i);
460 String serv_name = e.getAttribute(GSXML.NAME_ATT);
461 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(serv_name, site_name));
462 this.service_list.appendChild(this.doc.importNode(e, true));
463 }
464 }
465
466 // serviceCluster info
467 NodeList clusters = info_response.getElementsByTagName(GSXML.CLUSTER_ELEM);
468 if (clusters.getLength()>0) {
469 for (int i=0; i<clusters.getLength(); i++) {
470 Element e = (Element)clusters.item(i);
471 String clus_name = e.getAttribute(GSXML.NAME_ATT);
472 e.setAttribute(GSXML.NAME_ATT, GSPath.prependLink(clus_name, site_name));
473 this.cluster_list.appendChild(this.doc.importNode(e, true));
474 }
475 }
476 return true;
477 }
478
479
480 //*****************************************************************
481 // auxiliary process methods
482 //*****************************************************************
483
484 /** handles requests made to the MessageRouter itself
485 *
486 * @param req - the request Element- <request>
487 * @return the result Element - should be <response>
488 */
489 protected Element processMessage(Element req) {
490
491 // message for self, should be type=describe/configure at this stage
492 String type = req.getAttribute(GSXML.TYPE_ATT);
493 Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
494 response.setAttribute(GSXML.FROM_ATT, "");
495 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
496 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
497 // check the param list
498 Element param_list = (Element) GSXML.getChildByTagName(req, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
499 if (param_list == null) {
500 response.appendChild(this.collection_list);
501 response.appendChild(this.cluster_list);
502 response.appendChild(this.site_list);
503 response.appendChild(this.service_list);
504 return response;
505 }
506 System.out.println("params found, getting subset");
507 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
508
509 // go through the param list and see what components are wanted
510 for (int i=0; i<params.getLength(); i++) {
511
512 Element param = (Element)params.item(i);
513 // Identify the structure information desired
514 if (param.getAttribute(GSXML.NAME_ATT) == GSXML.SUBSET_PARAM ) {
515 String info = param.getAttribute(GSXML.VALUE_ATT);
516 if (info.equals(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER)) {
517 response.appendChild(this.collection_list);
518
519 } else if (info.equals(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER)) {
520 response.appendChild(this.cluster_list);
521
522 } else if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
523 response.appendChild(this.service_list);
524 } else if (info.equals(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER)) {
525 response.appendChild(this.site_list);
526 }
527 }
528 }
529 return response;
530
531 }
532
533 // the old way, should all be using system now
534 if (type.equals(GSXML.REQUEST_TYPE_CONFIGURE)) {
535
536 // a list of configure requests - should put any error messages
537 // or success messages into response
538 NodeList commands = req.getElementsByTagName(GSXML.CONFIGURE_ELEM);
539 for (int i=0; i<commands.getLength(); i++) {
540 // all the commands should be Elements
541 Element elem = (Element)commands.item(i);
542 String action = elem.getAttribute(GSXML.ACTION_ATT);
543 String module_name = elem.getAttribute(GSXML.NAME_ATT);
544 String module_type = elem.getAttribute(GSXML.TYPE_ATT);
545 if (action.equals(GSXML.CONFIG_ACTION_ACTIVATE)) {
546 if (module_type.equals(GSXML.COLLECTION_ELEM)) {
547 activateCollection(module_name);
548 } else if (module_type.equals(GSXML.SERVICE_CLASS_ELEM)) {
549 activateServiceRack(module_name);
550 } else if (module_type.equals(GSXML.CLUSTER_ELEM)) {
551 activateServiceCluster(module_name);
552 } else {
553 // cant process the activation
554 // send an error
555 }
556 } else if (action.equals(GSXML.CONFIG_ACTION_DEACTIVATE)) {
557 deactivateModule(module_type, module_name);
558
559 }
560 }
561
562 return response;
563
564 }
565
566 if (type.equals(GSXML.REQUEST_TYPE_SYSTEM)) {
567
568 // a list of system requests - should put any error messages
569 // or success messages into response
570 NodeList commands = req.getElementsByTagName(GSXML.SYSTEM_ELEM);
571 Element site_config_elem = null;
572 boolean success = false;
573
574 for (int i=0; i<commands.getLength(); i++) {
575 // all the commands should be Elements
576 Element elem = (Element)commands.item(i);
577 String action = elem.getAttribute(GSXML.TYPE_ATT);
578 if (action.equals("configure")) {
579 String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
580 if (subset.equals("")) {
581 // need to reconfigure the MR
582 this.configure();
583 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "mr reconfigured");
584 response.appendChild(s);
585
586 } else {
587 // else it a specific request
588 if (subset.equals(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER)) {
589 success = configureCollections();
590 } else {
591
592 // need the site config file
593 if (site_config_elem==null) {
594
595 File configFile = new File(GSFile.siteConfigFile(this.site_home));
596 if (!configFile.exists() ) {
597 System.err.println("MessageRouter: site config file: "+configFile.getPath()+" not found!");
598 continue;
599 }
600 site_config_elem = this.converter.getDOM(configFile).getDocumentElement();
601 }
602 if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
603 Element service_rack_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
604
605 success = configureServices(service_rack_list);
606 } else if (subset.equals(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER)) {
607 Element cluster_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
608
609 success = configureClusters(cluster_list);
610 } else if (subset.equals(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER)) {
611 Element site_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
612 success = configureSites(site_list);
613 }
614 }
615 String message=null;
616 if (success) {
617 message = subset + "reconfigured successfully";
618 } else {
619 message = "Error in reconfiguring "+subset;
620 }
621 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
622 response.appendChild(s);
623 }
624
625
626 } else {
627 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
628 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
629
630 if (action.equals("deactivate")) {
631 deactivateModule(module_type, module_name);
632 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" deactivated");
633 response.appendChild(s);
634 } else if (action.equals("activate")) {
635 if (module_type.equals(GSXML.COLLECTION_ELEM)) {
636 success = activateCollection(module_name);
637 } else if (module_type.equals(GSXML.SITE_ELEM)) {
638 success = activateSite(module_name);
639 } else if (module_type.equals(GSXML.CLUSTER_ELEM)) {
640 success = activateServiceCluster(module_name);
641 }
642 if (success) {
643 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" activated");
644 response.appendChild(s);
645 } else {
646 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" could not be activated");
647 response.appendChild(s);
648 }
649 }
650 } // else not a configure action
651 } // for all commands
652 return response;
653
654
655 } // system type request
656
657 // if get here something has gone wrong
658 System.err.println("MessageRouter: cant process request:");
659 System.err.println(this.converter.getString(req));
660 return null;
661
662 }
663
664 // ****************************************************
665 // other methods
666 // ****************************************************
667
668 /** creates and configures a new collection
669 *
670 *@param col_name the name of the collection
671 *@return true if collection created ok
672 */
673 protected boolean activateCollection(String col_name) {
674
675 System.out.println("MessageRouter:Activating collection: "+col_name+".");
676
677 Collection c = new Collection();
678 c.setCollectionName(col_name);
679 c.setSiteHome(this.site_home);
680 c.setSiteAddress(this.site_http_address);
681 if (c.configure()) {
682 // this could be a reactivation, so delete the old version
683 deactivateModule(GSXML.COLLECTION_ELEM, col_name);
684 // add to list of collections
685 this.module_map.put(col_name, c);
686 //add short description_ to collection_list_
687 Element e = this.doc.createElement(GSXML.COLLECTION_ELEM);
688 e.setAttribute(GSXML.NAME_ATT, col_name);
689 this.collection_list.appendChild(e);
690 return true;
691 } else {
692 System.err.println("MessageRouter:Couldn't configure collection: "+
693 col_name+".");
694 return false;
695 }
696
697 }
698
699 protected boolean activateSite(String site_name) {
700 System.out.println("MessageRouter:Activating site: "+site_name+".");
701
702 // just in case this is a reactivation, deactivate this site first
703 deactivateModule(GSXML.SITE_ELEM, site_name);
704 File configFile = new File(GSFile.siteConfigFile(this.site_home));
705
706 if (!configFile.exists() ) {
707 System.err.println("MessageRouter: site config file: "+configFile.getPath()+" not found!");
708 return false;
709 }
710 Element config = this.converter.getDOM(configFile).getDocumentElement();
711
712 Element site_list = (Element)GSXML.getChildByTagName(config, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
713 if (site_list ==null ) {
714 System.out.println("MessageRouter:activateSite, no sites found");
715 return false;
716 }
717 Element this_site_elem = GSXML.getNamedElement(site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, site_name);
718 if (this_site_elem == null) {
719 System.out.println("MessageRouter:activateSite, site "+site_name+" not found");
720 return false;
721 }
722
723 Communicator comm=null;
724 String type = this_site_elem.getAttribute(GSXML.TYPE_ATT);
725 if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
726 comm = new SOAPCommunicator();
727 if (comm.configure(this_site_elem)) {
728 comm.setLocalSiteName(this.global_site_name);
729
730 // add to map of modules
731 this.module_map.put(site_name, comm);
732 this.site_list.appendChild(this.doc.importNode(this_site_elem, true));
733 // need to get collection list and service
734 // list from here- if the site isn't up yet, the site will
735 // have to be added later
736 if (!getRemoteSiteInfo(comm, site_name)) {
737 System.err.println("couldn't get info from site "+site_name);
738 }
739 } else {
740 System.err.println("couldn't configure soap site:"+site_name);
741 return false;
742 }
743
744 } else {
745 System.err.print("cant talk to server of type:"+type);
746 System.err.println(", so not making a connection to "+site_name);
747 return false;
748 }
749 return true;
750
751 }
752
753 protected boolean activateServiceCluster(String cluster_name) {
754 return false;
755
756 }
757
758 protected boolean activateServiceRack(String module_name) {
759 return false;
760 }
761
762 protected boolean deactivateModule(String type, String name) {
763
764 if (this.module_map.containsKey(name)) {
765
766 System.out.println("MessageRouter: deactivating "+name);
767 this.module_map.remove(name);
768
769 // also remove the xml bit from description list
770 if (type.equals(GSXML.COLLECTION_ELEM)) {
771 Element this_col = GSXML.getNamedElement(this.collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
772 if (this_col != null) {
773 this.collection_list.removeChild(this_col);
774 }
775 return true;
776 } else if (type.equals(GSXML.SERVICE_ELEM)) {
777 Element this_service = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, name);
778 if (this_service != null) {
779 this.service_list.removeChild(this_service);
780 }
781 return true;
782 } else if (type.equals(GSXML.CLUSTER_ELEM)) {
783 Element this_cluster = GSXML.getNamedElement(this.cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, name);
784 if (this_cluster != null) {
785 this.cluster_list.removeChild(this_cluster);
786 }
787 return true;
788 } else if (type.equals(GSXML.SITE_ELEM)) {
789 Element this_site = GSXML.getNamedElement(this.site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, name);
790 if (this_site != null) {
791 this.site_list.removeChild(this_site);
792
793 // also remove this sites colls, services, clusters etc
794 removeRemoteSiteInfo(this.collection_list, GSXML.COLLECTION_ELEM, name);
795 removeRemoteSiteInfo(this.cluster_list, GSXML.CLUSTER_ELEM, name);
796 removeRemoteSiteInfo(this.service_list, GSXML.SERVICE_ELEM, name);
797 }
798 } else {
799 System.err.println("couldn't deactivate coll");
800 // couldn't do it
801 return false;
802 }
803 }
804 // else not deactivated
805 return false;
806
807 }
808
809 /**
810 * this looks through a list (module_list) of elements named module_name,
811 * and removes any whose names start with site_name
812 */
813 protected void removeRemoteSiteInfo(Element module_list,
814 String module_name,
815 String site_name) {
816
817 NodeList modules = module_list.getElementsByTagName(module_name);
818 // will this work??
819 for (int i=0; i<modules.getLength(); i++) {
820
821 String name = ((Element)modules.item(i)).getAttribute(GSXML.NAME_ATT);
822 if (GSPath.getFirstLink(name).equals(site_name)) {
823 module_list.removeChild(modules.item(i));
824 i--; // move the pointer back
825 }
826 }
827 }
828
829
830}
831
832
Note: See TracBrowser for help on using the repository browser.