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

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

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