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 | */
|
---|
19 | package org.greenstone.gsdl3.core;
|
---|
20 |
|
---|
21 | import org.greenstone.gsdl3.util.*;
|
---|
22 | import org.greenstone.gsdl3.service.*;
|
---|
23 | import org.greenstone.gsdl3.comms.*;
|
---|
24 | import org.greenstone.gsdl3.collection.*;
|
---|
25 |
|
---|
26 | // XML classes
|
---|
27 | import org.w3c.dom.Node;
|
---|
28 | import org.w3c.dom.NodeList;
|
---|
29 | import org.w3c.dom.Document;
|
---|
30 | import org.w3c.dom.Element;
|
---|
31 | import org.xml.sax.InputSource;
|
---|
32 | import javax.xml.parsers.*;
|
---|
33 |
|
---|
34 | // other java classes
|
---|
35 | import java.io.File;
|
---|
36 | import java.util.HashMap;
|
---|
37 | import java.util.Iterator;
|
---|
38 | import java.io.Reader;
|
---|
39 | import java.io.StringReader;
|
---|
40 | import java.net.Authenticator;
|
---|
41 | import 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: 5402 $
|
---|
55 | * @see ModuleInterface
|
---|
56 | * @see Collection
|
---|
57 | * @see ServiceRack
|
---|
58 | * @see Communicator
|
---|
59 | *
|
---|
60 | */
|
---|
61 | public 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 | // the old way, should all be using system now
|
---|
563 | if (type.equals(GSXML.REQUEST_TYPE_CONFIGURE)) {
|
---|
564 |
|
---|
565 | // a list of configure requests - should put any error messages
|
---|
566 | // or success messages into response
|
---|
567 | NodeList commands = req.getElementsByTagName(GSXML.CONFIGURE_ELEM);
|
---|
568 | for (int i=0; i<commands.getLength(); i++) {
|
---|
569 | // all the commands should be Elements
|
---|
570 | Element elem = (Element)commands.item(i);
|
---|
571 | String action = elem.getAttribute(GSXML.ACTION_ATT);
|
---|
572 | String module_name = elem.getAttribute(GSXML.NAME_ATT);
|
---|
573 | String module_type = elem.getAttribute(GSXML.TYPE_ATT);
|
---|
574 | if (action.equals(GSXML.CONFIG_ACTION_ACTIVATE)) {
|
---|
575 | if (module_type.equals(GSXML.COLLECTION_ELEM)) {
|
---|
576 | activateCollection(module_name);
|
---|
577 | } else if (module_type.equals(GSXML.SERVICE_CLASS_ELEM)) {
|
---|
578 | activateServiceRack(module_name);
|
---|
579 | } else if (module_type.equals(GSXML.CLUSTER_ELEM)) {
|
---|
580 | activateServiceCluster(module_name);
|
---|
581 | } else {
|
---|
582 | // cant process the activation
|
---|
583 | // send an error
|
---|
584 | }
|
---|
585 | } else if (action.equals(GSXML.CONFIG_ACTION_DEACTIVATE)) {
|
---|
586 | deactivateModule(module_type, module_name);
|
---|
587 |
|
---|
588 | }
|
---|
589 | }
|
---|
590 |
|
---|
591 | return response;
|
---|
592 |
|
---|
593 | }
|
---|
594 |
|
---|
595 | if (type.equals(GSXML.REQUEST_TYPE_SYSTEM)) {
|
---|
596 |
|
---|
597 | // a list of system requests - should put any error messages
|
---|
598 | // or success messages into response
|
---|
599 | NodeList commands = req.getElementsByTagName(GSXML.SYSTEM_ELEM);
|
---|
600 | Element site_config_elem = null;
|
---|
601 | boolean success = false;
|
---|
602 |
|
---|
603 | for (int i=0; i<commands.getLength(); i++) {
|
---|
604 | // all the commands should be Elements
|
---|
605 | Element elem = (Element)commands.item(i);
|
---|
606 | String action = elem.getAttribute(GSXML.TYPE_ATT);
|
---|
607 | if (action.equals("configure")) {
|
---|
608 | String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
|
---|
609 | if (subset.equals("")) {
|
---|
610 | // need to reconfigure the MR
|
---|
611 | this.configure();
|
---|
612 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "mr reconfigured");
|
---|
613 | response.appendChild(s);
|
---|
614 |
|
---|
615 | } else {
|
---|
616 | // else it a specific request
|
---|
617 | if (subset.equals(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
618 | success = configureCollections();
|
---|
619 | } else {
|
---|
620 |
|
---|
621 | // need the site config file
|
---|
622 | if (site_config_elem==null) {
|
---|
623 |
|
---|
624 | File configFile = new File(GSFile.siteConfigFile(this.site_home));
|
---|
625 | if (!configFile.exists() ) {
|
---|
626 | System.err.println("MessageRouter: site config file: "+configFile.getPath()+" not found!");
|
---|
627 | continue;
|
---|
628 | }
|
---|
629 | site_config_elem = this.converter.getDOM(configFile).getDocumentElement();
|
---|
630 | }
|
---|
631 | if (subset.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
632 | Element service_rack_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SERVICE_CLASS_ELEM+GSXML.LIST_MODIFIER);
|
---|
633 |
|
---|
634 | success = configureServices(service_rack_list);
|
---|
635 | } else if (subset.equals(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
636 | Element cluster_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
|
---|
637 |
|
---|
638 | success = configureClusters(cluster_list);
|
---|
639 | } else if (subset.equals(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
640 | Element site_list = (Element)GSXML.getChildByTagName(site_config_elem, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
|
---|
641 | success = configureSites(site_list);
|
---|
642 | }
|
---|
643 | }
|
---|
644 | String message=null;
|
---|
645 | if (success) {
|
---|
646 | message = subset + "reconfigured successfully";
|
---|
647 | } else {
|
---|
648 | message = "Error in reconfiguring "+subset;
|
---|
649 | }
|
---|
650 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
|
---|
651 | response.appendChild(s);
|
---|
652 | }
|
---|
653 |
|
---|
654 |
|
---|
655 | } else {
|
---|
656 | String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
|
---|
657 | String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
|
---|
658 |
|
---|
659 | if (action.equals("deactivate")) {
|
---|
660 | deactivateModule(module_type, module_name);
|
---|
661 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" deactivated");
|
---|
662 | response.appendChild(s);
|
---|
663 | } else if (action.equals("activate")) {
|
---|
664 | if (module_type.equals(GSXML.COLLECTION_ELEM)) {
|
---|
665 | success = activateCollection(module_name);
|
---|
666 | } else if (module_type.equals(GSXML.SITE_ELEM)) {
|
---|
667 | success = activateSite(module_name);
|
---|
668 | } else if (module_type.equals(GSXML.CLUSTER_ELEM)) {
|
---|
669 | success = activateServiceCluster(module_name);
|
---|
670 | }
|
---|
671 | if (success) {
|
---|
672 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" activated");
|
---|
673 | response.appendChild(s);
|
---|
674 | } else {
|
---|
675 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, module_type+": "+module_name+" could not be activated");
|
---|
676 | response.appendChild(s);
|
---|
677 | }
|
---|
678 | }
|
---|
679 | } // else not a configure action
|
---|
680 | } // for all commands
|
---|
681 | return response;
|
---|
682 |
|
---|
683 |
|
---|
684 | } // system type request
|
---|
685 |
|
---|
686 | // if get here something has gone wrong
|
---|
687 | System.err.println("MessageRouter: cant process request:");
|
---|
688 | System.err.println(this.converter.getString(req));
|
---|
689 | return null;
|
---|
690 |
|
---|
691 | }
|
---|
692 |
|
---|
693 | // ****************************************************
|
---|
694 | // other methods
|
---|
695 | // ****************************************************
|
---|
696 |
|
---|
697 | /** creates and configures a new collection
|
---|
698 | *
|
---|
699 | *@param col_name the name of the collection
|
---|
700 | *@return true if collection created ok
|
---|
701 | */
|
---|
702 | protected boolean activateCollection(String col_name) {
|
---|
703 |
|
---|
704 | System.out.println("MessageRouter:Activating collection: "+col_name+".");
|
---|
705 |
|
---|
706 | // now we need to look for the etc/collectionInit.xml file, and see what sort of Collection to load
|
---|
707 | Collection c = null;
|
---|
708 | File init_file = new File(GSFile.collectionInitFile(this.site_home, col_name));
|
---|
709 | if (init_file.exists()) {
|
---|
710 | Element init_elem = this.converter.getDOM(init_file).getDocumentElement();
|
---|
711 | String coll_class_name = init_elem.getAttribute("class");
|
---|
712 | if (coll_class_name.equals("")) {
|
---|
713 | c = new Collection();
|
---|
714 | } else {
|
---|
715 | try {
|
---|
716 | c = (Collection)Class.forName("org.greenstone.gsdl3.collection."+coll_class_name).newInstance();
|
---|
717 | } catch (Exception e) {
|
---|
718 | System.out.println("MessageRouter: couldn't create a new collection, type "+coll_class_name+", defaulting to class Collection");
|
---|
719 | c = new Collection();
|
---|
720 | }
|
---|
721 | }
|
---|
722 | } else {
|
---|
723 | // use the defualt collection
|
---|
724 | c = new Collection();
|
---|
725 | }
|
---|
726 | c.setCollectionName(col_name);
|
---|
727 | c.setSiteHome(this.site_home);
|
---|
728 | c.setSiteAddress(this.site_http_address);
|
---|
729 | if (c.configure()) {
|
---|
730 | // this could be a reactivation, so delete the old version
|
---|
731 | deactivateModule(GSXML.COLLECTION_ELEM, col_name);
|
---|
732 | // add to list of collections
|
---|
733 | this.module_map.put(col_name, c);
|
---|
734 | //add short description_ to collection_list_
|
---|
735 | Element e = this.doc.createElement(GSXML.COLLECTION_ELEM);
|
---|
736 | e.setAttribute(GSXML.NAME_ATT, col_name);
|
---|
737 | this.collection_list.appendChild(e);
|
---|
738 | return true;
|
---|
739 | } else {
|
---|
740 | System.err.println("MessageRouter:Couldn't configure collection: "+
|
---|
741 | col_name+".");
|
---|
742 | return false;
|
---|
743 | }
|
---|
744 |
|
---|
745 | }
|
---|
746 |
|
---|
747 | protected boolean activateSite(String site_name) {
|
---|
748 | System.out.println("MessageRouter:Activating site: "+site_name+".");
|
---|
749 |
|
---|
750 | // just in case this is a reactivation, deactivate this site first
|
---|
751 | deactivateModule(GSXML.SITE_ELEM, site_name);
|
---|
752 | File configFile = new File(GSFile.siteConfigFile(this.site_home));
|
---|
753 |
|
---|
754 | if (!configFile.exists() ) {
|
---|
755 | System.err.println("MessageRouter: site config file: "+configFile.getPath()+" not found!");
|
---|
756 | return false;
|
---|
757 | }
|
---|
758 | Element config = this.converter.getDOM(configFile).getDocumentElement();
|
---|
759 |
|
---|
760 | Element site_list = (Element)GSXML.getChildByTagName(config, 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 | this.module_map.remove(name);
|
---|
816 |
|
---|
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 |
|
---|