source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/ServiceRack.java@ 13270

Last change on this file since 13270 was 13270, checked in by shaoqun, 17 years ago

replace Category class which is deprecated with Logger class

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