1 | /*
|
---|
2 | * ServiceCluster.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 | // leave the package name as is for now - should be changed to something better
|
---|
20 | // cluster? groups?
|
---|
21 | package org.greenstone.gsdl3.collection;
|
---|
22 |
|
---|
23 | import java.io.File;
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.Iterator;
|
---|
26 |
|
---|
27 | import org.apache.log4j.Logger;
|
---|
28 | import org.greenstone.gsdl3.core.MessageRouter;
|
---|
29 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
30 | import org.greenstone.gsdl3.service.ServiceRack;
|
---|
31 | import org.greenstone.gsdl3.util.GSFile;
|
---|
32 | import org.greenstone.gsdl3.util.GSPath;
|
---|
33 | import org.greenstone.gsdl3.util.GSXML;
|
---|
34 | import org.greenstone.gsdl3.util.UserContext;
|
---|
35 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
36 | import org.w3c.dom.Document;
|
---|
37 | import org.w3c.dom.Element;
|
---|
38 | import org.w3c.dom.Node;
|
---|
39 | import org.w3c.dom.NodeList;
|
---|
40 |
|
---|
41 | /* ServiceCluster - a groups of services that are related in some way
|
---|
42 | * Implements ModuleInterface. Contains a list of services provided by the cluster, along with metadata about the cluster itself.
|
---|
43 | * a collection is a special type of cluster
|
---|
44 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
45 | * @version $Revision: 25750 $
|
---|
46 | * @see ModuleInterface
|
---|
47 | */
|
---|
48 | public class ServiceCluster implements ModuleInterface
|
---|
49 | {
|
---|
50 |
|
---|
51 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.ServiceCluster.class.getName());
|
---|
52 |
|
---|
53 | protected static final String CONFIG_ENCODING = "utf-8";
|
---|
54 |
|
---|
55 | protected static final String DEFAULT_LANG = "en"; // hack for now, should be read from the coll cfg file? or site cfg file for cluster
|
---|
56 |
|
---|
57 | /** base directory for the site that this cluster belongs to */
|
---|
58 | protected String site_home = null;
|
---|
59 | /** http address of the site that this cluster belongs to */
|
---|
60 | protected String site_http_address = null;
|
---|
61 | /** The name of the cluster - for a collection, this is the collection name */
|
---|
62 | protected String cluster_name = null;
|
---|
63 | /** collection type : mg, mgpp or lucene */
|
---|
64 | protected String col_type = "";
|
---|
65 | /** database type : gdbm, jdbm or sqlite */
|
---|
66 | protected String db_type = "";
|
---|
67 |
|
---|
68 | /** a reference to the message router */
|
---|
69 | protected MessageRouter router = null;
|
---|
70 | /**
|
---|
71 | * The map of services.
|
---|
72 | *
|
---|
73 | * Maps Services to ServiceRack objects
|
---|
74 | *
|
---|
75 | * @see ServiceRack
|
---|
76 | *
|
---|
77 | */
|
---|
78 | protected HashMap<String, ServiceRack> service_map = null;
|
---|
79 | /**
|
---|
80 | * maps pseudo service names to real service names - needed if we have two
|
---|
81 | * services with the same name for one collection
|
---|
82 | */
|
---|
83 | protected HashMap<String, String> service_name_map = null;
|
---|
84 |
|
---|
85 | /** XML converter for String to DOM and vice versa */
|
---|
86 | protected XMLConverter converter = null;
|
---|
87 |
|
---|
88 | /** container doc for description elements */
|
---|
89 | protected Document doc = null;
|
---|
90 | /** list of services */
|
---|
91 | protected Element service_list = null;
|
---|
92 | /** list of metadata - all metadata, regardless of language goes in here */
|
---|
93 | protected Element metadata_list = null;
|
---|
94 | /** language specific stuff */
|
---|
95 | //protected Element lang_specific_metadata_list = null;
|
---|
96 | protected Element display_item_list = null;
|
---|
97 | /** the element that will have any descriptions passed back in */
|
---|
98 | protected Element description = null;
|
---|
99 |
|
---|
100 | /** list of plugin */
|
---|
101 | protected Element plugin_item_list = null;
|
---|
102 |
|
---|
103 | public void setSiteHome(String home)
|
---|
104 | {
|
---|
105 | this.site_home = home;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void setSiteAddress(String address)
|
---|
109 | {
|
---|
110 | this.site_http_address = address;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public void cleanUp()
|
---|
114 | {
|
---|
115 | Iterator<ServiceRack> i = this.service_map.values().iterator();
|
---|
116 | while (i.hasNext())
|
---|
117 | {
|
---|
118 | ServiceRack s = i.next();
|
---|
119 | s.cleanUp();
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void setClusterName(String name)
|
---|
124 | {
|
---|
125 | this.cluster_name = name;
|
---|
126 | this.description.setAttribute(GSXML.NAME_ATT, name);
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void setMessageRouter(MessageRouter m)
|
---|
130 | {
|
---|
131 | this.router = m;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public ServiceCluster()
|
---|
135 | {
|
---|
136 | this.service_map = new HashMap<String, ServiceRack>();
|
---|
137 | this.service_name_map = new HashMap<String, String>();
|
---|
138 | this.converter = new XMLConverter();
|
---|
139 | this.doc = this.converter.newDOM();
|
---|
140 | this.description = this.doc.createElement(GSXML.CLUSTER_ELEM);
|
---|
141 | this.display_item_list = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
|
---|
142 | this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
143 | this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Configures the cluster.
|
---|
148 | *
|
---|
149 | * gsdlHome and clusterName must be set before configure is called.
|
---|
150 | *
|
---|
151 | * reads the site configuration file, and configures itself this calls
|
---|
152 | * configure(Element) with the XML element node from the config file.
|
---|
153 | * configure(Element) should be used if the config file has already been
|
---|
154 | * parsed. This method will work with any subclass.
|
---|
155 | *
|
---|
156 | * @return true if configure successful, false otherwise.
|
---|
157 | */
|
---|
158 | public boolean configure()
|
---|
159 | {
|
---|
160 |
|
---|
161 | if (this.site_home == null || this.cluster_name == null)
|
---|
162 | {
|
---|
163 | logger.error("site_home and cluster_name must be set before configure called!");
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 | logger.info("configuring service cluster");
|
---|
167 | // read the site configuration file
|
---|
168 | File config_file = new File(GSFile.siteConfigFile(this.site_home));
|
---|
169 |
|
---|
170 | if (!config_file.exists())
|
---|
171 | {
|
---|
172 | logger.error("couldn't configure cluster: " + this.cluster_name + ", " + config_file + " does not exist");
|
---|
173 | return false;
|
---|
174 | }
|
---|
175 |
|
---|
176 | Document doc = this.converter.getDOM(config_file, CONFIG_ENCODING);
|
---|
177 | if (doc == null)
|
---|
178 | {
|
---|
179 | logger.error("couldn't parse config file " + config_file.getPath());
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 |
|
---|
183 | // get the appropriate service cluster element
|
---|
184 | Element cluster_list = (Element) GSXML.getChildByTagName(doc.getDocumentElement(), GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
|
---|
185 | Element sc = GSXML.getNamedElement(cluster_list, GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, this.cluster_name);
|
---|
186 |
|
---|
187 | return this.configure(sc);
|
---|
188 | }
|
---|
189 |
|
---|
190 | public boolean configure(Element service_cluster_info)
|
---|
191 | {
|
---|
192 |
|
---|
193 | // get the metadata - for now just add it to the list
|
---|
194 | Element meta_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
195 | if (meta_list != null)
|
---|
196 | {
|
---|
197 | if (!addMetadata(meta_list))
|
---|
198 | {
|
---|
199 |
|
---|
200 | logger.error(" couldn't configure the metadata");
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | // get the display info
|
---|
205 | Element display_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
|
---|
206 | if (display_list != null)
|
---|
207 | {
|
---|
208 | if (!addDisplayItems(display_list))
|
---|
209 | {
|
---|
210 |
|
---|
211 | logger.error("couldn't configure the display items");
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | //get the plugin info
|
---|
216 | Element import_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.IMPORT_ELEM);
|
---|
217 | if (import_list != null)
|
---|
218 | {
|
---|
219 | Element plugin_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
|
---|
220 | if (plugin_list != null)
|
---|
221 | {
|
---|
222 | if (!addPlugins(plugin_list))
|
---|
223 | {
|
---|
224 |
|
---|
225 | logger.error("couldn't configure the plugins");
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | // do the service racks
|
---|
231 | // empty the service map in case this is a reconfigure
|
---|
232 | clearServices();
|
---|
233 | Element service_rack_list = (Element) GSXML.getChildByTagName(service_cluster_info, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
|
---|
234 | if (service_rack_list == null)
|
---|
235 | {
|
---|
236 | // is this an error? could you ever have a service cluster
|
---|
237 | // without service racks???
|
---|
238 | logger.error("cluster has no service racks!!");
|
---|
239 | }
|
---|
240 | else
|
---|
241 | {
|
---|
242 |
|
---|
243 | if (!configureServiceRackList(service_rack_list, null))
|
---|
244 | {
|
---|
245 | logger.error("couldn't configure the service racks!!");
|
---|
246 | return false;
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | return true;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * adds metadata from a metadataList into the metadata_list xml
|
---|
255 | */
|
---|
256 | protected boolean addMetadata(Element metadata_list)
|
---|
257 | {
|
---|
258 | if (metadata_list == null)
|
---|
259 | return false;
|
---|
260 | NodeList metanodes = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
|
---|
261 | if (metanodes.getLength() > 0)
|
---|
262 | {
|
---|
263 | for (int k = 0; k < metanodes.getLength(); k++)
|
---|
264 | {
|
---|
265 | this.metadata_list.appendChild(this.doc.importNode(metanodes.item(k), true));
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | return true;
|
---|
270 | }
|
---|
271 |
|
---|
272 | protected boolean addDisplayItems(Element display_list)
|
---|
273 | {
|
---|
274 |
|
---|
275 | if (display_list == null)
|
---|
276 | return false;
|
---|
277 | NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
|
---|
278 | if (displaynodes.getLength() > 0)
|
---|
279 | {
|
---|
280 | for (int k = 0; k < displaynodes.getLength(); k++)
|
---|
281 | {
|
---|
282 | Element d = (Element) displaynodes.item(k);
|
---|
283 | String lang = d.getAttribute(GSXML.LANG_ATT);
|
---|
284 | if (lang == null || lang.equals(""))
|
---|
285 | {
|
---|
286 | //set the lang to teh default
|
---|
287 | d.setAttribute(GSXML.LANG_ATT, DEFAULT_LANG);
|
---|
288 | }
|
---|
289 | String name = d.getAttribute(GSXML.NAME_ATT);
|
---|
290 | Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, name);
|
---|
291 | if (this_item == null)
|
---|
292 | {
|
---|
293 | this_item = this.doc.createElement(GSXML.DISPLAY_TEXT_ELEM);
|
---|
294 | this_item.setAttribute(GSXML.NAME_ATT, name);
|
---|
295 | this.display_item_list.appendChild(this_item);
|
---|
296 | }
|
---|
297 |
|
---|
298 | this_item.appendChild(this.doc.importNode(d, true));
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | return true;
|
---|
303 | }
|
---|
304 |
|
---|
305 | protected boolean addPlugins(Element plugin_list)
|
---|
306 | {
|
---|
307 | if (plugin_list == null)
|
---|
308 | return false;
|
---|
309 | NodeList pluginNodes = plugin_list.getElementsByTagName(GSXML.PLUGIN_ELEM);
|
---|
310 | if (pluginNodes.getLength() > 0)
|
---|
311 | {
|
---|
312 | for (int k = 0; k < pluginNodes.getLength(); k++)
|
---|
313 | {
|
---|
314 | this.plugin_item_list.appendChild(this.doc.importNode(pluginNodes.item(k), true));
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | return true;
|
---|
319 | }
|
---|
320 |
|
---|
321 | protected void clearServices()
|
---|
322 | {
|
---|
323 | service_map.clear();
|
---|
324 | this.service_list = this.doc.createElement(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * creates and configures all the services - extra_info is some more xml
|
---|
329 | * that is passed to teh service - eg used for coll config files for
|
---|
330 | * Collection
|
---|
331 | */
|
---|
332 | protected boolean configureServiceRackList(Element service_rack_list, Element extra_info)
|
---|
333 | {
|
---|
334 |
|
---|
335 | // create all the services
|
---|
336 | NodeList nodes = service_rack_list.getElementsByTagName(GSXML.SERVICE_CLASS_ELEM);
|
---|
337 | if (nodes.getLength() == 0)
|
---|
338 | {
|
---|
339 | logger.error("ServiceCluster configuration error: cluster " + this.cluster_name + " has no service modules!");
|
---|
340 | return false;
|
---|
341 | }
|
---|
342 |
|
---|
343 | for (int i = 0; i < nodes.getLength(); i++)
|
---|
344 | {
|
---|
345 |
|
---|
346 | // the xml request to send to the serviceRack to query what
|
---|
347 | // services it provides
|
---|
348 | Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
349 | Element request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
|
---|
350 | message.appendChild(request);
|
---|
351 |
|
---|
352 | Element n = (Element) nodes.item(i);
|
---|
353 | String servicetype = n.getAttribute(GSXML.NAME_ATT);
|
---|
354 |
|
---|
355 | ServiceRack s = null;
|
---|
356 |
|
---|
357 | try
|
---|
358 | {
|
---|
359 | // try for a default service in standard package
|
---|
360 | s = (ServiceRack) Class.forName("org.greenstone.gsdl3.service." + servicetype).newInstance();
|
---|
361 | }
|
---|
362 | catch (Exception e)
|
---|
363 | {
|
---|
364 | }
|
---|
365 | if (s == null)
|
---|
366 | {
|
---|
367 | try
|
---|
368 | {
|
---|
369 | // name as is, in case package is already specified
|
---|
370 | s = (ServiceRack) Class.forName(servicetype).newInstance();
|
---|
371 | }
|
---|
372 | catch (Exception e)
|
---|
373 | {
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | if (s == null)
|
---|
378 | {
|
---|
379 | logger.error("Couldn't get an instance of class " + servicetype + ", or org.greenstone.gsdl3.service." + servicetype);
|
---|
380 | continue;
|
---|
381 | }
|
---|
382 |
|
---|
383 | s.setSiteHome(this.site_home);
|
---|
384 | s.setSiteAddress(this.site_http_address);
|
---|
385 | s.setClusterName(this.cluster_name);
|
---|
386 | s.setMessageRouter(this.router);
|
---|
387 | // pass the xml node to the service for configuration
|
---|
388 | if (s.configure(n, extra_info))
|
---|
389 | {
|
---|
390 | // find out the supported service types for this service module
|
---|
391 | Node types = s.process(message);
|
---|
392 | NodeList typenodes = ((Element) types).getElementsByTagName(GSXML.SERVICE_ELEM);
|
---|
393 |
|
---|
394 | for (int j = 0; j < typenodes.getLength(); j++)
|
---|
395 | {
|
---|
396 | String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);
|
---|
397 |
|
---|
398 | if (service_map.get(service) != null)
|
---|
399 | {
|
---|
400 | char extra = '0';
|
---|
401 | String new_service = service + extra;
|
---|
402 |
|
---|
403 | while (service_map.get(new_service) != null)
|
---|
404 | {
|
---|
405 | extra++;
|
---|
406 | new_service = service + extra;
|
---|
407 | }
|
---|
408 | this.service_name_map.put(new_service, service);
|
---|
409 | service = new_service;
|
---|
410 | ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
|
---|
411 | }
|
---|
412 | this.service_map.put(service, s);
|
---|
413 | // also add info to the ServiceInfo XML element
|
---|
414 | this.service_list.appendChild(this.doc.importNode(typenodes.item(j), true));
|
---|
415 | }
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | return true;
|
---|
420 |
|
---|
421 | }
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * Process an XML document - uses Strings just calls process(Node).
|
---|
425 | *
|
---|
426 | * @param in
|
---|
427 | * the Document to process - a string
|
---|
428 | * @return the resultant document as a string - contains any error messages
|
---|
429 | * @see String
|
---|
430 | */
|
---|
431 | public String process(String in)
|
---|
432 | {
|
---|
433 |
|
---|
434 | Document doc = this.converter.getDOM(in);
|
---|
435 |
|
---|
436 | Node res = process(doc);
|
---|
437 | return this.converter.getString(res);
|
---|
438 |
|
---|
439 | }
|
---|
440 |
|
---|
441 | /**
|
---|
442 | * process XML as Node
|
---|
443 | *
|
---|
444 | */
|
---|
445 | public Node process(Node message_node)
|
---|
446 | {
|
---|
447 | Element message = this.converter.nodeToElement(message_node);
|
---|
448 |
|
---|
449 | NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
|
---|
450 | Document mess_doc = message.getOwnerDocument();
|
---|
451 | Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
452 | if (requests.getLength() == 0)
|
---|
453 | {
|
---|
454 | logger.error("no requests for cluster:" + this.cluster_name);
|
---|
455 | // no requests
|
---|
456 | return mainResult; // for now
|
---|
457 | }
|
---|
458 | for (int i = 0; i < requests.getLength(); i++)
|
---|
459 | {
|
---|
460 | Element request = (Element) requests.item(i);
|
---|
461 | String to = request.getAttribute(GSXML.TO_ATT);
|
---|
462 |
|
---|
463 | // the cluster name should be first, check, then remove
|
---|
464 | String clustername = GSPath.getFirstLink(to);
|
---|
465 | if (!clustername.equals(this.cluster_name))
|
---|
466 | {
|
---|
467 | logger.error("cluster name wrong! was " + clustername + " should have been " + this.cluster_name);
|
---|
468 | continue; // ignore this request
|
---|
469 | }
|
---|
470 | to = GSPath.removeFirstLink(to);
|
---|
471 | request.setAttribute(GSXML.TO_ATT, to);
|
---|
472 |
|
---|
473 | if (to.equals(""))
|
---|
474 | { // this command is for me
|
---|
475 | Element response = processMessage(request);
|
---|
476 | mainResult.appendChild(response);
|
---|
477 |
|
---|
478 | }
|
---|
479 | else
|
---|
480 | { // the request is for one of my services
|
---|
481 | String service = GSPath.getFirstLink(to);
|
---|
482 |
|
---|
483 | if (!this.service_map.containsKey(service))
|
---|
484 | {
|
---|
485 | logger.error("non-existant service, " + service + ", specified!");
|
---|
486 | continue;
|
---|
487 | }
|
---|
488 | String real_service = service;
|
---|
489 | if (this.service_name_map.containsKey(service))
|
---|
490 | {
|
---|
491 | real_service = this.service_name_map.get(service);
|
---|
492 | // need to change the to att in the request - give the real service name
|
---|
493 | to = request.getAttribute(GSXML.TO_ATT);
|
---|
494 | String old_to = to;
|
---|
495 | to = GSPath.replaceFirstLink(to, real_service);
|
---|
496 | request.setAttribute(GSXML.TO_ATT, to);
|
---|
497 | }
|
---|
498 | // have to pass the request to the service
|
---|
499 | Element single_message = mess_doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
500 | single_message.appendChild(request);
|
---|
501 |
|
---|
502 | Node response_message = this.service_map.get(service).process(single_message);
|
---|
503 | if (response_message != null)
|
---|
504 | {
|
---|
505 | Element response = (Element) GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM);
|
---|
506 | String from = response.getAttribute(GSXML.FROM_ATT);
|
---|
507 | if (!real_service.equals(service))
|
---|
508 | {
|
---|
509 | // replace the real service name with the pseudo service name
|
---|
510 | from = GSPath.replaceFirstLink(from, service);
|
---|
511 | // also need to do it in the service itself
|
---|
512 | // shoudl this be done here??
|
---|
513 | Element service_elem = (Element) GSXML.getChildByTagName(response, GSXML.SERVICE_ELEM);
|
---|
514 | if (service_elem != null)
|
---|
515 | {
|
---|
516 | service_elem.setAttribute(GSXML.NAME_ATT, service);
|
---|
517 | }
|
---|
518 | }
|
---|
519 | from = GSPath.prependLink(from, this.cluster_name);
|
---|
520 | response.setAttribute(GSXML.FROM_ATT, from);
|
---|
521 | mainResult.appendChild(this.doc.importNode(response, true));
|
---|
522 | }
|
---|
523 |
|
---|
524 | } // else
|
---|
525 |
|
---|
526 | } // for each request
|
---|
527 | return mainResult;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * handles requests made to the ServiceCluster itself
|
---|
532 | *
|
---|
533 | * @param req
|
---|
534 | * - the request Element- <request>
|
---|
535 | * @return the result Element - should be <response>
|
---|
536 | */
|
---|
537 | protected Element processMessage(Element request)
|
---|
538 | {
|
---|
539 | Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
540 | response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
|
---|
541 | String type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
542 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
543 | response.setAttribute(GSXML.TYPE_ATT, type);
|
---|
544 |
|
---|
545 | if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE))
|
---|
546 | {
|
---|
547 | // create the collection element
|
---|
548 | Element description = (Element) this.description.cloneNode(false);
|
---|
549 | // set collection type : mg, mgpp, lucene or solr
|
---|
550 | description.setAttribute(GSXML.TYPE_ATT, col_type);
|
---|
551 | description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
|
---|
552 |
|
---|
553 | response.appendChild(description);
|
---|
554 | // check the param list
|
---|
555 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
556 | if (param_list == null)
|
---|
557 | {
|
---|
558 | addAllDisplayInfo(description, lang);
|
---|
559 | description.appendChild(this.service_list);
|
---|
560 | description.appendChild(this.metadata_list);
|
---|
561 | description.appendChild(this.plugin_item_list);
|
---|
562 | return response;
|
---|
563 | }
|
---|
564 |
|
---|
565 | // go through the param list and see what components are wanted
|
---|
566 | NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
567 | for (int i = 0; i < params.getLength(); i++)
|
---|
568 | {
|
---|
569 |
|
---|
570 | Element param = (Element) params.item(i);
|
---|
571 | // Identify the structure information desired
|
---|
572 | if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM))
|
---|
573 | {
|
---|
574 | String info = param.getAttribute(GSXML.VALUE_ATT);
|
---|
575 | if (info.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
|
---|
576 | {
|
---|
577 | description.appendChild(this.service_list);
|
---|
578 | }
|
---|
579 | else if (info.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER))
|
---|
580 | {
|
---|
581 | description.appendChild(metadata_list);
|
---|
582 | }
|
---|
583 | else if (info.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
|
---|
584 | {
|
---|
585 | addAllDisplayInfo(description, lang);
|
---|
586 | }
|
---|
587 | else if (info.equals(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER))
|
---|
588 | {
|
---|
589 | description.appendChild(plugin_item_list);
|
---|
590 | }
|
---|
591 | }
|
---|
592 | }
|
---|
593 | return response;
|
---|
594 | }
|
---|
595 | /*
|
---|
596 | * if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING)) {
|
---|
597 | * logger.error("Received format string request"); String service =
|
---|
598 | * request.getAttribute("service"); logger.error("Service is " +
|
---|
599 | * service); String classifier = null;
|
---|
600 | * if(service.equals("ClassifierBrowse")) { classifier =
|
---|
601 | * request.getAttribute("classifier"); logger.error("Classifier is " +
|
---|
602 | * classifier); } Element format_element = (Element)
|
---|
603 | * GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM); String
|
---|
604 | * format_string = GSXML.getNodeText(format_element);
|
---|
605 | * logger.error("Format string: " + format_string);
|
---|
606 | * logger.error("Config file location = " +
|
---|
607 | * GSFile.collectionConfigFile(this.site_home, this.cluster_name));
|
---|
608 | *
|
---|
609 | * // check for version file
|
---|
610 | *
|
---|
611 | * String directory = new
|
---|
612 | * File(GSFile.collectionConfigFile(this.site_home,
|
---|
613 | * this.cluster_name)).getParent() + File.pathSeparator;
|
---|
614 | * logger.error("Directory is " + directory);
|
---|
615 | *
|
---|
616 | * String version_filename = "";
|
---|
617 | *
|
---|
618 | * if(service.equals("ClassifierBrowse")) version_filename = directory +
|
---|
619 | * "browse_"+classifier+"_format_statement_version.txt"; else
|
---|
620 | * version_filename = directory + "query_format_statement_version.txt";
|
---|
621 | *
|
---|
622 | * File version_file = new File(version_filename);
|
---|
623 | * logger.error("Version filename is " + version_filename);
|
---|
624 | *
|
---|
625 | * String version_number = "1"; BufferedWriter writer; // = new
|
---|
626 | * BufferedWriter(new FileWriter(version_filename)); //RandomAccessFile
|
---|
627 | * version_file_random_access;
|
---|
628 | *
|
---|
629 | * try{
|
---|
630 | *
|
---|
631 | * if(version_file.exists()) { // Read version BufferedReader reader =
|
---|
632 | * new BufferedReader(new FileReader(version_filename));
|
---|
633 | * //version_file_random_access = new RandomAccessFile(version_file,
|
---|
634 | * "r"); //logger.error(" //version_number =
|
---|
635 | * version_file_random_access.readInt(); version_number =
|
---|
636 | * reader.readLine(); int aInt = Integer.parseInt(version_number) + 1;
|
---|
637 | * version_number = Integer.toString(aInt); reader.close();
|
---|
638 | * //version_file_random_access.close(); } else{ // Create
|
---|
639 | * version_file.createNewFile(); // write 1 to file writer = new
|
---|
640 | * BufferedWriter(new FileWriter(version_filename));
|
---|
641 | * //version_file_random_access = new RandomAccessFile(version_file,
|
---|
642 | * "w"); //version_file_random_access.writeInt(version_number);
|
---|
643 | * writer.write(version_number); writer.close();
|
---|
644 | * //version_file_random_access.close(); }
|
---|
645 | *
|
---|
646 | * // Write version file String format_statement_filename = "";
|
---|
647 | *
|
---|
648 | * if(service.equals("ClassifierBrowse")) format_statement_filename =
|
---|
649 | * directory + "browse_"+classifier+"_format_statement_v" +
|
---|
650 | * version_number + ".txt"; else format_statement_filename = directory +
|
---|
651 | * "query_format_statement_v" + version_number + ".txt";
|
---|
652 | *
|
---|
653 | * logger.error("Format statement filename is " +
|
---|
654 | * format_statement_filename);
|
---|
655 | *
|
---|
656 | * writer = new BufferedWriter(new
|
---|
657 | * FileWriter(format_statement_filename)); writer.write(format_string);
|
---|
658 | * writer.close();
|
---|
659 | *
|
---|
660 | * // Update version number //version_file_random_access = new
|
---|
661 | * RandomAccessFile(version_file, "w");
|
---|
662 | * //version_file_random_access.writeInt(version_number);
|
---|
663 | * //version_file_random_access.close();
|
---|
664 | *
|
---|
665 | * writer = new BufferedWriter(new FileWriter(version_filename));
|
---|
666 | * //version_file_random_access = new RandomAccessFile(version_file,
|
---|
667 | * "w"); //version_file_random_access.writeInt(version_number);
|
---|
668 | * writer.write(version_number); writer.close();
|
---|
669 | *
|
---|
670 | *
|
---|
671 | *
|
---|
672 | * } catch (IOException e) { logger.error("IO Exception "+e);
|
---|
673 | * //System.exit(1); }
|
---|
674 | *
|
---|
675 | *
|
---|
676 | * }
|
---|
677 | */
|
---|
678 | if (type.equals(GSXML.REQUEST_TYPE_SYSTEM))
|
---|
679 | {
|
---|
680 | response = processSystemRequest(request);
|
---|
681 | }
|
---|
682 | else
|
---|
683 | { // unknown type
|
---|
684 | logger.error("cant handle request of type " + type);
|
---|
685 |
|
---|
686 | }
|
---|
687 | return response;
|
---|
688 | }
|
---|
689 |
|
---|
690 | protected Element processSystemRequest(Element request)
|
---|
691 | {
|
---|
692 |
|
---|
693 | Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
694 | response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
|
---|
695 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SYSTEM);
|
---|
696 |
|
---|
697 | // a list of system requests - should put any error messages
|
---|
698 | // or success messages into response
|
---|
699 | NodeList commands = request.getElementsByTagName(GSXML.SYSTEM_ELEM);
|
---|
700 | String message = null;
|
---|
701 | for (int i = 0; i < commands.getLength(); i++)
|
---|
702 | {
|
---|
703 | // all the commands should be Elements
|
---|
704 | Element elem = (Element) commands.item(i);
|
---|
705 | String action = elem.getAttribute(GSXML.TYPE_ATT);
|
---|
706 | if (action.equals(GSXML.SYSTEM_TYPE_CONFIGURE))
|
---|
707 | {
|
---|
708 | String subset = elem.getAttribute(GSXML.SYSTEM_SUBSET_ATT);
|
---|
709 | if (subset.equals(""))
|
---|
710 | {
|
---|
711 | // need to reconfigure the service cluster
|
---|
712 |
|
---|
713 | if (this.configure())
|
---|
714 | {
|
---|
715 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " reconfigured");
|
---|
716 | response.appendChild(s);
|
---|
717 |
|
---|
718 | }
|
---|
719 | else
|
---|
720 | {
|
---|
721 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " could not be reconfigured");
|
---|
722 | response.appendChild(s);
|
---|
723 | }
|
---|
724 | }
|
---|
725 | else if (this.configureSubset(subset))
|
---|
726 | {
|
---|
727 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " " + subset + " reconfigured");
|
---|
728 | response.appendChild(s);
|
---|
729 | }
|
---|
730 | else
|
---|
731 | {
|
---|
732 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, this.cluster_name + " " + subset + " could not be reconfigured");
|
---|
733 | response.appendChild(s);
|
---|
734 | }
|
---|
735 | continue;
|
---|
736 | } // configure action
|
---|
737 |
|
---|
738 | String module_name = elem.getAttribute(GSXML.SYSTEM_MODULE_NAME_ATT);
|
---|
739 | String module_type = elem.getAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT);
|
---|
740 | if (action.equals(GSXML.SYSTEM_TYPE_ACTIVATE))
|
---|
741 | {
|
---|
742 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, "activate action not yet implemented - does it even make sense in this context??");
|
---|
743 | response.appendChild(s);
|
---|
744 | }
|
---|
745 | else if (action.equals(GSXML.SYSTEM_TYPE_DEACTIVATE))
|
---|
746 | {
|
---|
747 | if (module_type.equals(GSXML.SERVICE_ELEM))
|
---|
748 | {
|
---|
749 | // deactivate the service
|
---|
750 | // remove from service_map
|
---|
751 | this.service_map.remove(module_name);
|
---|
752 | Element service_elem = GSXML.getNamedElement(this.service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, module_name);
|
---|
753 | service_list.removeChild(service_elem);
|
---|
754 | message = module_type + ": " + module_name + " deactivated";
|
---|
755 | }
|
---|
756 | else
|
---|
757 | {
|
---|
758 | message = "can't deactivate " + module_type + " type modules!";
|
---|
759 | }
|
---|
760 | Element s = GSXML.createTextElement(this.doc, GSXML.STATUS_ELEM, message);
|
---|
761 | response.appendChild(s);
|
---|
762 | }
|
---|
763 | else
|
---|
764 | {
|
---|
765 | logger.error("cant process system request, action " + action);
|
---|
766 | continue;
|
---|
767 | }
|
---|
768 | } // for each command
|
---|
769 | return response;
|
---|
770 | }
|
---|
771 |
|
---|
772 | /**
|
---|
773 | * do a configure on only part of the collection
|
---|
774 | */
|
---|
775 | protected boolean configureSubset(String subset)
|
---|
776 | {
|
---|
777 |
|
---|
778 | File configFile = new File(GSFile.siteConfigFile(this.site_home));
|
---|
779 | if (!configFile.exists())
|
---|
780 | {
|
---|
781 | logger.error("site config file: " + configFile.getPath() + " not found!");
|
---|
782 | // wont be able to do any of the requests
|
---|
783 | return false;
|
---|
784 |
|
---|
785 | }
|
---|
786 |
|
---|
787 | Document site_config_doc = this.converter.getDOM(configFile);
|
---|
788 | if (site_config_doc == null)
|
---|
789 | {
|
---|
790 | logger.error("could not read in site config file: " + configFile.getPath());
|
---|
791 | return false;
|
---|
792 | }
|
---|
793 |
|
---|
794 | Element site_config_elem = site_config_doc.getDocumentElement();
|
---|
795 | Element cluster_config_elem = GSXML.getNamedElement((Element) GSXML.getChildByTagName(site_config_elem, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER), GSXML.CLUSTER_ELEM, GSXML.NAME_ATT, this.cluster_name);
|
---|
796 | if (cluster_config_elem == null)
|
---|
797 | {
|
---|
798 | logger.error("site config file: " + configFile.getPath() + " has no element for cluster " + this.cluster_name);
|
---|
799 | // wont be able to do any of teh requests
|
---|
800 | return false;
|
---|
801 |
|
---|
802 | }
|
---|
803 | if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
|
---|
804 | {
|
---|
805 | Element service_rack_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
|
---|
806 | clearServices();
|
---|
807 | return configureServiceRackList(service_rack_list, null);
|
---|
808 | }
|
---|
809 | else if (subset.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER))
|
---|
810 | {
|
---|
811 | this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
812 | Element metadata_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
813 | return addMetadata(metadata_list);
|
---|
814 | }
|
---|
815 | else if (subset.equals(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER))
|
---|
816 | {
|
---|
817 | this.plugin_item_list = this.doc.createElement(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
|
---|
818 | Element import_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.IMPORT_ELEM);
|
---|
819 | if (import_list != null)
|
---|
820 | {
|
---|
821 | Element plugin_item_list = (Element) GSXML.getChildByTagName(cluster_config_elem, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
|
---|
822 | return addPlugins(plugin_item_list);
|
---|
823 | }
|
---|
824 | else
|
---|
825 | return false;
|
---|
826 | }
|
---|
827 | else
|
---|
828 | {
|
---|
829 | logger.error("cannot process system request, configure " + subset);
|
---|
830 | return false;
|
---|
831 | }
|
---|
832 |
|
---|
833 | }
|
---|
834 |
|
---|
835 | protected boolean addAllDisplayInfo(Element description, String lang)
|
---|
836 | {
|
---|
837 |
|
---|
838 | NodeList items = this.display_item_list.getChildNodes();
|
---|
839 | for (int i = 0; i < items.getLength(); i++)
|
---|
840 | { // for each key
|
---|
841 | Element m = (Element) items.item(i);
|
---|
842 | // find the child with the correct language
|
---|
843 | Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
|
---|
844 | if (new_m == null && lang != DEFAULT_LANG)
|
---|
845 | {
|
---|
846 | // use the default lang
|
---|
847 | new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
|
---|
848 | }
|
---|
849 | if (new_m == null)
|
---|
850 | {
|
---|
851 | // just get the first one
|
---|
852 | new_m = (Element) GSXML.getChildByTagName(m, GSXML.DISPLAY_TEXT_ELEM);
|
---|
853 | }
|
---|
854 | description.appendChild(new_m.cloneNode(true));
|
---|
855 | }
|
---|
856 | return true;
|
---|
857 |
|
---|
858 | }
|
---|
859 |
|
---|
860 | protected Element getDisplayTextElement(String key, String lang)
|
---|
861 | {
|
---|
862 |
|
---|
863 | Element this_item = GSXML.getNamedElement(this.display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, key);
|
---|
864 | if (this_item == null)
|
---|
865 | {
|
---|
866 | return null;
|
---|
867 | }
|
---|
868 |
|
---|
869 | Element this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
|
---|
870 | if (this_lang == null && lang != DEFAULT_LANG)
|
---|
871 | {
|
---|
872 | // try the default
|
---|
873 | this_lang = GSXML.getNamedElement(this_item, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, DEFAULT_LANG);
|
---|
874 | }
|
---|
875 | if (this_lang == null)
|
---|
876 | {
|
---|
877 | // just return the first one
|
---|
878 | return GSXML.getFirstElementChild(this_item);//(Element)this_item.getFirstChild().cloneNode(true);
|
---|
879 | }
|
---|
880 | return (Element) this_lang.cloneNode(true);
|
---|
881 |
|
---|
882 | }
|
---|
883 |
|
---|
884 | public HashMap<String, ServiceRack> getServiceMap()
|
---|
885 | {
|
---|
886 | return service_map;
|
---|
887 | }
|
---|
888 | }
|
---|