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

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

removed old configure request handling - all should use new system stuff

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