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

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

now looks for proxy element in site config file - if its there, it sets the system proxyHost etc properties. this will enable http connections from teh servlet to the outside world - eg for InfomineProxy

  • Property svn:keywords set to Author Date Id Revision
File size: 30.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;
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: 5226 $
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("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 System.out.println("MR received request");
227 System.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("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 System.out.println("MR returned response");
280 System.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("couldn't get info from site "+name);
436 }
437 } else {
438 System.err.println("couldn't configure soap site:"+name);
439 }
440
441 } else {
442 System.err.print("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 System.out.println("params found, getting subset");
537 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
538
539 // go through the param list and see what components are wanted
540 for (int i=0; i<params.getLength(); i++) {
541
542 Element param = (Element)params.item(i);
543 // Identify the structure information desired
544 if (param.getAttribute(GSXML.NAME_ATT) == GSXML.SUBSET_PARAM ) {
545 String info = param.getAttribute(GSXML.VALUE_ATT);
546 if (info.equals(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER)) {
547 response.appendChild(this.collection_list);
548
549 } else if (info.equals(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER)) {
550 response.appendChild(this.cluster_list);
551
552 } else if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
553 response.appendChild(this.service_list);
554 } else if (info.equals(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER)) {
555 response.appendChild(this.site_list);
556 }
557 }
558 }
559 return response;
560
561 }
562
563 // the old way, should all be using system now
564 if (type.equals(GSXML.REQUEST_TYPE_CONFIGURE)) {
565
566 // a list of configure requests - should put any error messages
567 // or success messages into response
568 NodeList commands = req.getElementsByTagName(GSXML.CONFIGURE_ELEM);
569 for (int i=0; i<commands.getLength(); i++) {
570 // all the commands should be Elements
571 Element elem = (Element)commands.item(i);
572 String action = elem.getAttribute(GSXML.ACTION_ATT);
573 String module_name = elem.getAttribute(GSXML.NAME_ATT);
574 String module_type = elem.getAttribute(GSXML.TYPE_ATT);
575 if (action.equals(GSXML.CONFIG_ACTION_ACTIVATE)) {
576 if (module_type.equals(GSXML.COLLECTION_ELEM)) {
577 activateCollection(module_name);
578 } else if (module_type.equals(GSXML.SERVICE_CLASS_ELEM)) {
579 activateServiceRack(module_name);
580 } else if (module_type.equals(GSXML.CLUSTER_ELEM)) {
581 activateServiceCluster(module_name);
582 } else {
583 // cant process the activation
584 // send an error
585 }
586 } else if (action.equals(GSXML.CONFIG_ACTION_DEACTIVATE)) {
587 deactivateModule(module_type, module_name);
588
589 }
590 }
591
592 return response;
593
594 }
595
596 if (type.equals(GSXML.REQUEST_TYPE_SYSTEM)) {
597
598 // a list of system requests - should put any error messages
599 // or success messages into response
600 NodeList commands = req.getElementsByTagName(GSXML.SYSTEM_ELEM);
601 Element site_config_elem = null;
602 boolean success = false;
603
604 for (int i=0; i<commands.getLength(); i++) {
605 // all the commands should be Elements
606 Element elem = (Element)commands.item(i);
607 String action = elem.getAttribute(GSXML.TYPE_ATT);
608 if (action.equals("configure")) {
609 String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
610 if (subset.equals("")) {
611 // need to reconfigure the MR
612 this.configure();
613 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "mr reconfigured");
614 response.appendChild(s);
615
616 } else {
617 // else it a specific request
618 if (subset.equals(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER)) {
619 success = configureCollections();
620 } else {
621
622 // need the site config file
623 if (site_config_elem==null) {
624
625 File configFile = new File(GSFile.siteConfigFile(this.site_home));
626 if (!configFile.exists() ) {
627 System.err.println("MessageRouter: site config file: "+configFile.getPath()+" not found!");
628 continue;
629 }
630 site_config_elem = this.converter.getDOM(configFile).getDocumentElement();
631 }
632 if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
633 Element service_rack_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
634
635 success = configureServices(service_rack_list);
636 } else if (subset.equals(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER)) {
637 Element cluster_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
638
639 success = configureClusters(cluster_list);
640 } else if (subset.equals(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER)) {
641 Element site_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
642 success = configureSites(site_list);
643 }
644 }
645 String message=null;
646 if (success) {
647 message = subset + "reconfigured successfully";
648 } else {
649 message = "Error in reconfiguring "+subset;
650 }
651 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
652 response.appendChild(s);
653 }
654
655
656 } else {
657 String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
658 String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
659
660 if (action.equals("deactivate")) {
661 deactivateModule(module_type, module_name);
662 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" deactivated");
663 response.appendChild(s);
664 } else if (action.equals("activate")) {
665 if (module_type.equals(GSXML.COLLECTION_ELEM)) {
666 success = activateCollection(module_name);
667 } else if (module_type.equals(GSXML.SITE_ELEM)) {
668 success = activateSite(module_name);
669 } else if (module_type.equals(GSXML.CLUSTER_ELEM)) {
670 success = activateServiceCluster(module_name);
671 }
672 if (success) {
673 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" activated");
674 response.appendChild(s);
675 } else {
676 Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" could not be activated");
677 response.appendChild(s);
678 }
679 }
680 } // else not a configure action
681 } // for all commands
682 return response;
683
684
685 } // system type request
686
687 // if get here something has gone wrong
688 System.err.println("MessageRouter: cant process request:");
689 System.err.println(this.converter.getString(req));
690 return null;
691
692 }
693
694 // ****************************************************
695 // other methods
696 // ****************************************************
697
698 /** creates and configures a new collection
699 *
700 *@param col_name the name of the collection
701 *@return true if collection created ok
702 */
703 protected boolean activateCollection(String col_name) {
704
705 System.out.println("MessageRouter:Activating collection: "+col_name+".");
706
707 // now we need to look for the etc/collectionInit.xml file, and see what sort of Collection to load
708 Collection c = null;
709 File init_file = new File(GSFile.collectionInitFile(this.site_home, col_name));
710 if (init_file.exists()) {
711 Element init_elem = this.converter.getDOM(init_file).getDocumentElement();
712 String coll_class_name = init_elem.getAttribute("class");
713 if (coll_class_name.equals("")) {
714 c = new Collection();
715 } else {
716 try {
717 c = (Collection)Class.forName("org.greenstone.gsdl3.collection."+coll_class_name).newInstance();
718 } catch (Exception e) {
719 System.out.println("couldn't create a new collection, type "+coll_class_name+", defaulting to class Collection");
720 c = new Collection();
721 }
722 }
723 } else {
724 // use the defualt collection
725 c = new Collection();
726 }
727 c.setCollectionName(col_name);
728 c.setSiteHome(this.site_home);
729 c.setSiteAddress(this.site_http_address);
730 if (c.configure()) {
731 // this could be a reactivation, so delete the old version
732 deactivateModule(GSXML.COLLECTION_ELEM, col_name);
733 // add to list of collections
734 this.module_map.put(col_name, c);
735 //add short description_ to collection_list_
736 Element e = this.doc.createElement(GSXML.COLLECTION_ELEM);
737 e.setAttribute(GSXML.NAME_ATT, col_name);
738 this.collection_list.appendChild(e);
739 return true;
740 } else {
741 System.err.println("MessageRouter:Couldn't configure collection: "+
742 col_name+".");
743 return false;
744 }
745
746 }
747
748 protected boolean activateSite(String site_name) {
749 System.out.println("MessageRouter:Activating site: "+site_name+".");
750
751 // just in case this is a reactivation, deactivate this site first
752 deactivateModule(GSXML.SITE_ELEM, site_name);
753 File configFile = new File(GSFile.siteConfigFile(this.site_home));
754
755 if (!configFile.exists() ) {
756 System.err.println("MessageRouter: site config file: "+configFile.getPath()+" not found!");
757 return false;
758 }
759 Element config = this.converter.getDOM(configFile).getDocumentElement();
760
761 Element site_list = (Element)GSXML.getChildByTagName(config, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
762 if (site_list ==null ) {
763 System.out.println("MessageRouter:activateSite, no sites found");
764 return false;
765 }
766 Element this_site_elem = GSXML.getNamedElement(site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, site_name);
767 if (this_site_elem == null) {
768 System.out.println("MessageRouter:activateSite, site "+site_name+" not found");
769 return false;
770 }
771
772 Communicator comm=null;
773 String type = this_site_elem.getAttribute(GSXML.TYPE_ATT);
774 if (type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
775 comm = new SOAPCommunicator();
776 if (comm.configure(this_site_elem)) {
777 comm.setLocalSiteName(this.global_site_name);
778
779 // add to map of modules
780 this.module_map.put(site_name, comm);
781 this.site_list.appendChild(this.doc.importNode(this_site_elem, true));
782 // need to get collection list and service
783 // list from here- if the site isn't up yet, the site will
784 // have to be added later
785 if (!getRemoteSiteInfo(comm, site_name)) {
786 System.err.println("couldn't get info from site "+site_name);
787 }
788 } else {
789 System.err.println("couldn't configure soap site:"+site_name);
790 return false;
791 }
792
793 } else {
794 System.err.print("cant talk to server of type:"+type);
795 System.err.println(", so not making a connection to "+site_name);
796 return false;
797 }
798 return true;
799
800 }
801
802 protected boolean activateServiceCluster(String cluster_name) {
803 return false;
804
805 }
806
807 protected boolean activateServiceRack(String module_name) {
808 return false;
809 }
810
811 protected boolean deactivateModule(String type, String name) {
812
813 if (this.module_map.containsKey(name)) {
814
815 System.out.println("MessageRouter: deactivating "+name);
816 this.module_map.remove(name);
817
818 // also remove the xml bit from description list
819 if (type.equals(GSXML.COLLECTION_ELEM)) {
820 Element this_col = GSXML.getNamedElement(this.collection_list, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
821 if (this_col != null) {
822 this.collection_list.removeChild(this_col);
823 }
824 return true;
825 } else if (type.equals(GSXML.SERVICE_ELEM)) {
826 Element this_service = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, name);
827 if (this_service != null) {
828 this.service_list.removeChild(this_service);
829 }
830 return true;
831 } else if (type.equals(GSXML.CLUSTER_ELEM)) {
832 Element this_cluster = GSXML.getNamedElement(this.cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, name);
833 if (this_cluster != null) {
834 this.cluster_list.removeChild(this_cluster);
835 }
836 return true;
837 } else if (type.equals(GSXML.SITE_ELEM)) {
838 Element this_site = GSXML.getNamedElement(this.site_list, GSXML.SITE_ELEM, GSXML.NAME_ATT, name);
839 if (this_site != null) {
840 this.site_list.removeChild(this_site);
841
842 // also remove this sites colls, services, clusters etc
843 removeRemoteSiteInfo(this.collection_list, GSXML.COLLECTION_ELEM, name);
844 removeRemoteSiteInfo(this.cluster_list, GSXML.CLUSTER_ELEM, name);
845 removeRemoteSiteInfo(this.service_list, GSXML.SERVICE_ELEM, name);
846 }
847 } else {
848 System.err.println("couldn't deactivate coll");
849 // couldn't do it
850 return false;
851 }
852 }
853 // else not deactivated
854 return false;
855
856 }
857
858 /**
859 * this looks through a list (module_list) of elements named module_name,
860 * and removes any whose names start with site_name
861 */
862 protected void removeRemoteSiteInfo(Element module_list,
863 String module_name,
864 String site_name) {
865
866 NodeList modules = module_list.getElementsByTagName(module_name);
867 // will this work??
868 for (int i=0; i<modules.getLength(); i++) {
869
870 String name = ((Element)modules.item(i)).getAttribute(GSXML.NAME_ATT);
871 if (GSPath.getFirstLink(name).equals(site_name)) {
872 module_list.removeChild(modules.item(i));
873 i--; // move the pointer back
874 }
875 }
876 }
877
878}
879
880
Note: See TracBrowser for help on using the repository browser.