source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/service/ServiceRack.java@ 9798

Last change on this file since 9798 was 9798, checked in by kjdon, 19 years ago

for all the converter.getDOM calls, now check for null document before using it - hopefully avoid lots of the exceptions that get printed to the screen if something goes wrong

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