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