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