[3650] | 1 | /*
|
---|
| 2 | * ServiceRack.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.service;
|
---|
| 20 |
|
---|
| 21 | // greenstone classes
|
---|
| 22 | import org.greenstone.gsdl3.util.*;
|
---|
| 23 | import org.greenstone.gsdl3.core.*;
|
---|
| 24 |
|
---|
| 25 | // xml classes
|
---|
| 26 | import org.w3c.dom.Node;
|
---|
| 27 | import org.w3c.dom.NodeList;
|
---|
| 28 | import org.w3c.dom.Element;
|
---|
| 29 | import org.w3c.dom.Document;
|
---|
| 30 | import org.xml.sax.InputSource;
|
---|
| 31 | import javax.xml.parsers.*;
|
---|
| 32 | import org.apache.xpath.XPathAPI;
|
---|
| 33 |
|
---|
| 34 | // general java classes
|
---|
| 35 | import java.io.Reader;
|
---|
| 36 | import java.io.StringReader;
|
---|
| 37 | import java.io.File;
|
---|
| 38 | import java.util.HashMap;
|
---|
| 39 | import java.util.ResourceBundle;
|
---|
| 40 | import java.util.Locale;
|
---|
| 41 | import java.lang.reflect.Method;
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * ServiceRack - abstract base class for services
|
---|
| 45 | *
|
---|
| 46 | * A ServiceRack provides one or more Services.
|
---|
| 47 | * This base class implements the process method.
|
---|
| 48 | *Each service is invoked
|
---|
| 49 | * by a method called process<service name> which takes one parameter - the xml request Element, and returns an XML response Element.
|
---|
| 50 | * for example, the TextQuery service would be invoked by
|
---|
| 51 | * processTextQuery(Element request)
|
---|
| 52 | *
|
---|
| 53 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
| 54 | * @version $Revision: 9874 $
|
---|
| 55 | */
|
---|
| 56 | public abstract class ServiceRack
|
---|
| 57 | implements ModuleInterface
|
---|
| 58 | {
|
---|
| 59 |
|
---|
| 60 | /** the absolute address of the site home */
|
---|
[5098] | 61 | protected String site_home =null;
|
---|
[3994] | 62 | /** the http address of the site home */
|
---|
[5098] | 63 | protected String site_http_address =null;
|
---|
[3994] | 64 |
|
---|
[3650] | 65 | /** the name of the cluster (or collection) that this service
|
---|
| 66 | belongs to - if any */
|
---|
[5098] | 67 | protected String cluster_name = null;
|
---|
[3650] | 68 |
|
---|
| 69 | /** some services can talk back to the message router */
|
---|
[5098] | 70 | protected ModuleInterface router = null;
|
---|
[3650] | 71 |
|
---|
| 72 | /** a converter class to create Documents etc */
|
---|
[5098] | 73 | protected XMLConverter converter = null;
|
---|
[3650] | 74 |
|
---|
[3851] | 75 | /** the original config info - if need to store it */
|
---|
[5098] | 76 | protected Element config_info = null;
|
---|
[3851] | 77 |
|
---|
[3650] | 78 | /** XML element for describe requests - the container doc */
|
---|
[5098] | 79 | protected Document doc = null;
|
---|
[3650] | 80 |
|
---|
[4903] | 81 | /** XML element for describe requests - list of supported services
|
---|
| 82 | - this is static */
|
---|
[5098] | 83 | protected Element short_service_info = null;
|
---|
[3650] | 84 |
|
---|
[3909] | 85 | /** XML element for stylesheet requests - map of service name to format
|
---|
| 86 | elem */
|
---|
[5098] | 87 | protected HashMap format_info_map = null;
|
---|
[3650] | 88 |
|
---|
| 89 | /** sets the cluster name */
|
---|
| 90 | public void setClusterName(String cluster_name) {
|
---|
[5098] | 91 | this.cluster_name = cluster_name;
|
---|
[3650] | 92 | }
|
---|
| 93 | /** sets the collect name */
|
---|
| 94 | public void setCollectionName(String coll_name) {
|
---|
| 95 | setClusterName(coll_name);
|
---|
| 96 | }
|
---|
[9874] | 97 |
|
---|
| 98 | public void cleanUp() {}
|
---|
[3650] | 99 |
|
---|
| 100 | /** sets the site home */
|
---|
| 101 | public void setSiteHome(String site_home) {
|
---|
[5098] | 102 | this.site_home = site_home;
|
---|
[3650] | 103 | }
|
---|
[3994] | 104 | /** sets the site http address */
|
---|
| 105 | public void setSiteAddress(String site_address) {
|
---|
[5098] | 106 | this.site_http_address = site_address;
|
---|
[3994] | 107 | }
|
---|
[3650] | 108 |
|
---|
| 109 | /** sets the message router */
|
---|
| 110 | public void setMessageRouter(ModuleInterface m) {
|
---|
[5098] | 111 | this.router = m;
|
---|
[3650] | 112 | }
|
---|
| 113 |
|
---|
| 114 | /** the no-args constructor */
|
---|
| 115 | public ServiceRack() {
|
---|
[5098] | 116 | this.converter = new XMLConverter();
|
---|
| 117 | this.doc = this.converter.newDOM();
|
---|
| 118 | this.short_service_info = this.doc.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 119 | this.format_info_map = new HashMap();
|
---|
[3650] | 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | /** configure the service module
|
---|
| 124 | *
|
---|
| 125 | * @param info the XML node <serviceRack name="XXX"/> with name equal
|
---|
| 126 | * to the class name (of the subclass)
|
---|
| 127 | *
|
---|
| 128 | * must configure short_service_info_ and service_info_map_
|
---|
| 129 | * @return true if configured ok
|
---|
| 130 | * must be implemented in subclasses
|
---|
| 131 | */
|
---|
[3937] | 132 | public boolean configure(Element info) {
|
---|
| 133 | return configure(info, null);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | abstract public boolean configure(Element info, Element extra_info);
|
---|
[3650] | 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Process an XML document - convenience method that uses Strings rather than Elements. just calls process(Element).
|
---|
| 140 | *
|
---|
| 141 | * @param xml_in the Document to process - a string
|
---|
| 142 | * @return the resultant document as a string - contains any error messages
|
---|
| 143 | * @see String
|
---|
| 144 | */
|
---|
| 145 | public String process(String xml_in) {
|
---|
| 146 |
|
---|
[5098] | 147 | Document doc = this.converter.getDOM(xml_in);
|
---|
[9874] | 148 | if (doc == null) {
|
---|
| 149 | System.err.println("ServiceRack.process(String) Error: Couldn't parse request");
|
---|
| 150 | System.err.println(xml_in);
|
---|
| 151 | return null;
|
---|
| 152 | }
|
---|
[3650] | 153 | Element res = process(doc.getDocumentElement());
|
---|
[5098] | 154 | return this.converter.getString(res);
|
---|
[3650] | 155 |
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /** process an XML request in DOM form
|
---|
| 159 | *
|
---|
| 160 | * @param xml_in the Element node containing the request
|
---|
| 161 | * should be <message>
|
---|
| 162 | * @return an Element with the result XML
|
---|
| 163 | * @see Element
|
---|
| 164 | */
|
---|
| 165 | public Element process(Element message) {
|
---|
| 166 |
|
---|
| 167 | NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
|
---|
| 168 | Document mess_doc = message.getOwnerDocument();
|
---|
[5098] | 169 | Element mainResult = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[3650] | 170 | if (requests.getLength()==0) {
|
---|
| 171 | // no requests
|
---|
| 172 | return mainResult; // empty message for now
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | for (int i=0; i<requests.getLength(); i++) {
|
---|
| 176 | Element request = (Element)requests.item(i);
|
---|
| 177 |
|
---|
| 178 | String type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
| 179 | if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
|
---|
| 180 | Element response = processDescribe(request);
|
---|
| 181 | if (response !=null) {
|
---|
[5098] | 182 | mainResult.appendChild(this.doc.importNode(response, true));
|
---|
[3650] | 183 | }
|
---|
| 184 |
|
---|
[3909] | 185 | } else if (type.equals(GSXML.REQUEST_TYPE_FORMAT)) {
|
---|
| 186 | Element response = processFormat(request);
|
---|
[5098] | 187 | mainResult.appendChild(this.doc.importNode(response, true));
|
---|
[3909] | 188 |
|
---|
| 189 |
|
---|
[3650] | 190 | } else {
|
---|
| 191 | // other type of request, must be processed by the subclass -
|
---|
| 192 | // send to the service method
|
---|
[9265] | 193 | StringBuffer error_string = new StringBuffer();
|
---|
[3650] | 194 | String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
| 195 | Element response = null;
|
---|
[4903] | 196 | try {
|
---|
[3900] | 197 | Class c = this.getClass();
|
---|
| 198 | Class []params = {Class.forName("org.w3c.dom.Element")};
|
---|
[4903] | 199 |
|
---|
[3900] | 200 | String method_name = "process"+to;
|
---|
| 201 | Method m = null;
|
---|
| 202 | while (c != null) {
|
---|
| 203 |
|
---|
| 204 | try {
|
---|
| 205 | m = c.getDeclaredMethod(method_name, params);
|
---|
[9265] | 206 | // if this has worked, break
|
---|
[3900] | 207 | break;
|
---|
| 208 | } catch (NoSuchMethodException e) {
|
---|
| 209 | c = c.getSuperclass();
|
---|
| 210 | } catch (SecurityException e) {
|
---|
[9265] | 211 | System.err.println("ServiceRack.process: security exception for finding method "+method_name);
|
---|
| 212 | error_string.append("ServiceRack.process: security exception for finding method "+method_name);
|
---|
[3900] | 213 | }
|
---|
| 214 | } // while
|
---|
| 215 | if (m != null) {
|
---|
[3650] | 216 | Object []args = {request};
|
---|
[3900] | 217 | try {
|
---|
| 218 | response = (Element)m.invoke(this, args);
|
---|
| 219 |
|
---|
| 220 | } catch (Exception e) {
|
---|
[3946] | 221 | System.err.println("ServiceRack.process: Trying to call a processService type method (process"+to+") on a subclass("+this.getClass().getName()+"), but an exception happened:"+e.toString());
|
---|
[9265] | 222 | error_string.append("ServiceRack.process: Trying to call a processService type method (process"+to+") on a subclass("+this.getClass().getName()+"), but an exception happened:"+e.toString());
|
---|
[3900] | 223 | }
|
---|
| 224 | } else {
|
---|
[3946] | 225 | System.err.println("ServiceRack.process: method "+method_name+" not found for class "+this.getClass().getName());
|
---|
[9265] | 226 | error_string.append("ServiceRack.process: method "+method_name+" not found for class "+this.getClass().getName());
|
---|
[3650] | 227 | }
|
---|
[4903] | 228 |
|
---|
| 229 | } catch (ClassNotFoundException e) {
|
---|
| 230 | System.err.println("ServiceRack error: Element class not found");
|
---|
[9265] | 231 | error_string.append("ServiceRack error: Element class not found");
|
---|
[3650] | 232 | }
|
---|
[9265] | 233 | if (response !=null) {
|
---|
| 234 | mainResult.appendChild(this.doc.importNode(response, true));
|
---|
| 235 | } else {
|
---|
| 236 | // add in a dummy response
|
---|
| 237 | System.err.println("adding in an error element\n");
|
---|
| 238 | response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 239 | GSXML.addError(this.doc, response, error_string.toString());
|
---|
| 240 | mainResult.appendChild(response);
|
---|
| 241 |
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | } // else process request
|
---|
[3650] | 245 | } // for each request
|
---|
[3900] | 246 |
|
---|
[3650] | 247 | return mainResult;
|
---|
| 248 |
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[4903] | 251 |
|
---|
| 252 |
|
---|
[3650] | 253 | /** process method for describe requests
|
---|
| 254 | */
|
---|
| 255 | protected Element processDescribe(Element request) {
|
---|
| 256 |
|
---|
[5098] | 257 | Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[3650] | 258 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
| 259 |
|
---|
| 260 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 261 | String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
[4142] | 262 | if (to.equals("")) { // return the service list
|
---|
| 263 | response.appendChild(getServiceList(lang));
|
---|
| 264 | return response;
|
---|
| 265 | }
|
---|
[6364] | 266 | response.setAttribute(GSXML.FROM_ATT, to);
|
---|
[5126] | 267 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 268 | Element description = null;
|
---|
| 269 | if (param_list == null) {
|
---|
| 270 | description = getServiceDescription(to, lang, null);
|
---|
| 271 | } else {
|
---|
| 272 | NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
| 273 | for (int i=0; i<params.getLength(); i++) {
|
---|
| 274 |
|
---|
| 275 | Element param = (Element)params.item(i);
|
---|
| 276 | // Identify the structure information desired
|
---|
[6364] | 277 | if (param.getAttribute(GSXML.NAME_ATT).equals(GSXML.SUBSET_PARAM )) {
|
---|
[5126] | 278 | String info = param.getAttribute(GSXML.VALUE_ATT);
|
---|
| 279 | if (description == null) {
|
---|
| 280 | description = getServiceDescription(to, lang, info);
|
---|
| 281 | } else {
|
---|
| 282 | Element temp = getServiceDescription(to, lang, info);
|
---|
| 283 | GSXML.mergeElements(description, temp);
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
[4903] | 288 | if (description != null) { // may be null if non-existant service
|
---|
| 289 | response.appendChild(description);
|
---|
[3650] | 290 | }
|
---|
[4903] | 291 | return response;
|
---|
[3650] | 292 |
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[3909] | 295 | /** process method for stylesheet requests
|
---|
| 296 | */
|
---|
| 297 | protected Element processFormat(Element request) {
|
---|
[5098] | 298 | Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[3909] | 299 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT);
|
---|
| 300 |
|
---|
| 301 | String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
| 302 |
|
---|
| 303 | if (to.equals("")) { // serviceRack query - is this appropriate??
|
---|
| 304 | return response;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 |
|
---|
| 308 | // describe a particular service
|
---|
[5098] | 309 | if (this.format_info_map.containsKey(to)) {
|
---|
[3909] | 310 | response.appendChild(getServiceFormat(to));
|
---|
| 311 | response.setAttribute(GSXML.FROM_ATT, to);
|
---|
| 312 | return response;
|
---|
| 313 | }
|
---|
[4903] | 314 | // else no format info
|
---|
[3909] | 315 | System.err.println("ServiceRack describe request: no format info for "+to+".");
|
---|
| 316 | return response;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[3650] | 319 | /** returns the service list for the subclass */
|
---|
| 320 | protected Element getServiceList(String lang) {
|
---|
[4903] | 321 | // for now, it is static and has no language stuff
|
---|
[5098] | 322 | return (Element) this.short_service_info.cloneNode(true);
|
---|
[3650] | 323 | }
|
---|
[3909] | 324 |
|
---|
[3650] | 325 | /** returns a specific service description */
|
---|
[5126] | 326 | abstract protected Element getServiceDescription(String service, String lang, String subset);
|
---|
[3650] | 327 |
|
---|
[3909] | 328 | protected Element getServiceFormat(String service) {
|
---|
[5098] | 329 | Element format = (Element)((Element)this.format_info_map.get(service)).cloneNode(true);
|
---|
[3909] | 330 | return format;
|
---|
| 331 | }
|
---|
[3650] | 332 |
|
---|
| 333 | /** overloaded version for no args case */
|
---|
| 334 | protected String getTextString(String key, String lang) {
|
---|
[9279] | 335 | return getTextString(key, lang, null, null);
|
---|
[3650] | 336 | }
|
---|
[8952] | 337 |
|
---|
| 338 | protected String getTextString(String key, String lang, String dictionary) {
|
---|
| 339 | return getTextString(key, lang, dictionary, null);
|
---|
| 340 | }
|
---|
| 341 | protected String getTextString(String key, String lang, String [] args) {
|
---|
[9279] | 342 | return getTextString(key, lang, null, args);
|
---|
[8952] | 343 | }
|
---|
| 344 |
|
---|
[3650] | 345 | /** getTextString - retrieves a language specific text string for the given
|
---|
[9423] | 346 | key and locale, from the specified resource_bundle (dictionary)
|
---|
[3650] | 347 | */
|
---|
[8952] | 348 | protected String getTextString(String key, String lang, String dictionary, String[] args) {
|
---|
[3650] | 349 |
|
---|
[9426] | 350 | // we want to use the collection class loader in case there are coll specific files
|
---|
| 351 | CollectionClassLoader class_loader = new CollectionClassLoader(this.site_home, this.cluster_name);
|
---|
| 352 |
|
---|
[9279] | 353 | if (dictionary != null) {
|
---|
| 354 | // just try the one specified dictionary
|
---|
[9426] | 355 | Dictionary dict = new Dictionary(dictionary, lang, class_loader);
|
---|
[9279] | 356 | String result = dict.get(key, args);
|
---|
| 357 | if (result == null) { // not found
|
---|
| 358 | return "_"+key+"_";
|
---|
| 359 | }
|
---|
| 360 | return result;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[9426] | 363 | // now we try class names for dictionary names
|
---|
[9279] | 364 | String class_name = this.getClass().getName();
|
---|
| 365 | class_name = class_name.substring(class_name.lastIndexOf('.')+1);
|
---|
[9426] | 366 | Dictionary dict = new Dictionary(class_name, lang, class_loader);
|
---|
| 367 | String result = dict.get(key, args);
|
---|
[9279] | 368 | if (result != null) {
|
---|
| 369 | return result;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | // we have to try super classes
|
---|
| 373 | Class c = this.getClass().getSuperclass();
|
---|
| 374 | while (result == null && c != null) {
|
---|
| 375 | class_name = c.getName();
|
---|
| 376 | class_name = class_name.substring(class_name.lastIndexOf('.')+1);
|
---|
| 377 | if (class_name.equals("ServiceRack")) {
|
---|
| 378 | // this is as far as we go
|
---|
| 379 | break;
|
---|
| 380 | }
|
---|
[9426] | 381 | dict = new Dictionary(class_name, lang, class_loader);
|
---|
[9279] | 382 | result = dict.get(key, args);
|
---|
| 383 | c = c.getSuperclass();
|
---|
| 384 | }
|
---|
| 385 | if (result == null) {
|
---|
[3650] | 386 | return "_"+key+"_";
|
---|
| 387 | }
|
---|
| 388 | return result;
|
---|
[9279] | 389 |
|
---|
[3650] | 390 | }
|
---|
[4903] | 391 |
|
---|
[5190] | 392 | protected String getMetadataNameText(String key, String lang) {
|
---|
| 393 |
|
---|
| 394 | String properties_name = "metadata_names";
|
---|
| 395 | Dictionary dict = new Dictionary(properties_name, lang);
|
---|
| 396 |
|
---|
| 397 | String result = dict.get(key);
|
---|
| 398 | if (result == null) { // not found
|
---|
| 399 | return null;
|
---|
| 400 | }
|
---|
| 401 | return result;
|
---|
| 402 | }
|
---|
[3650] | 403 | }
|
---|
[4903] | 404 |
|
---|