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

Last change on this file since 4142 was 4142, checked in by kjdon, 21 years ago

service describe requests no longer use info att

  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 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: 4142 $
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 protected Element short_service_info_ = null;
83
84 /** XML element for describe requests - map of service name to full
85 description */
86 protected HashMap service_info_map_ = null;
87 /** XML element for stylesheet requests - map of service name to format
88 elem */
89 protected HashMap format_info_map_ = null;
90
91 /** sets the cluster name */
92 public void setClusterName(String cluster_name) {
93 cluster_name_ = cluster_name;
94 }
95 /** sets the collect name */
96 public void setCollectionName(String coll_name) {
97 setClusterName(coll_name);
98 }
99
100 /** sets the site home */
101 public void setSiteHome(String site_home) {
102 site_home_ = site_home;
103 }
104 /** sets the site http address */
105 public void setSiteAddress(String site_address) {
106 site_http_address_ = site_address;
107 }
108
109 /** sets the message router */
110 public void setMessageRouter(ModuleInterface m) {
111 router_ = m;
112 }
113
114 /** the no-args constructor */
115 public ServiceRack() {
116 converter_ = new XMLConverter();
117 doc_ = converter_.newDOM();
118 short_service_info_ = doc_.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
119 service_info_map_ = new HashMap();
120 format_info_map_ = new HashMap();
121 }
122
123
124 /** configure the service module
125 *
126 * @param info the XML node <serviceRack name="XXX"/> with name equal
127 * to the class name (of the subclass)
128 *
129 * must configure short_service_info_ and service_info_map_
130 * @return true if configured ok
131 * must be implemented in subclasses
132 */
133 public boolean configure(Element info) {
134 return configure(info, null);
135 }
136
137 abstract public boolean configure(Element info, Element extra_info);
138
139 /**
140 * Process an XML document - convenience method that uses Strings rather than Elements. just calls process(Element).
141 *
142 * @param xml_in the Document to process - a string
143 * @return the resultant document as a string - contains any error messages
144 * @see String
145 */
146 public String process(String xml_in) {
147
148 Document doc = converter_.getDOM(xml_in);
149
150 Element res = process(doc.getDocumentElement());
151 return converter_.getString(res);
152
153 }
154
155 /** process an XML request in DOM form
156 *
157 * @param xml_in the Element node containing the request
158 * should be <message>
159 * @return an Element with the result XML
160 * @see Element
161 */
162 public Element process(Element message) {
163
164 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
165 Document mess_doc = message.getOwnerDocument();
166 Element mainResult = doc_.createElement(GSXML.MESSAGE_ELEM);
167 if (requests.getLength()==0) {
168 // no requests
169 return mainResult; // empty message for now
170 }
171
172 for (int i=0; i<requests.getLength(); i++) {
173 Element request = (Element)requests.item(i);
174
175 String type = request.getAttribute(GSXML.TYPE_ATT);
176 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
177 Element response = processDescribe(request);
178 if (response !=null) {
179 mainResult.appendChild(doc_.importNode(response, true));
180 }
181
182 } else if (type.equals(GSXML.REQUEST_TYPE_FORMAT)) {
183 Element response = processFormat(request);
184 mainResult.appendChild(doc_.importNode(response, true));
185
186
187 } else {
188 // other type of request, must be processed by the subclass -
189 // send to the service method
190
191 String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
192 Element response = null;
193 if (service_info_map_.containsKey(to)) {
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+", returning null");
210 return null;
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 return null;
221 }
222 } else {
223 System.err.println("ServiceRack.process: method "+method_name+" not found for class "+this.getClass().getName());
224 }
225 if (response !=null) {
226 mainResult.appendChild(doc_.importNode(response, true));
227 }
228
229 } catch (ClassNotFoundException e) {
230 System.err.println("ServiceRack error: Element class not found");
231 return null;
232 }
233 } else {
234 // else error in to field
235 System.err.println("ServiceRack describe request: error in 'to' field, non-existant service "+to+" specified!");
236 return null;
237 }
238 }
239 } // for each request
240
241 return mainResult;
242
243 }
244
245 /** process method for describe requests
246 */
247 protected Element processDescribe(Element request) {
248
249 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
250 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
251
252 String lang = request.getAttribute(GSXML.LANG_ATT);
253 String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
254 if (to.equals("")) { // return the service list
255 response.appendChild(getServiceList(lang));
256 return response;
257 }
258
259 // describe a particular service
260 if (service_info_map_.containsKey(to)) {
261 response.appendChild(getServiceDescription(to, lang));
262 response.setAttribute(GSXML.FROM_ATT, to);
263 return response;
264 }
265 // else error in to field
266 System.err.println("ServiceRack describe request: error in 'to' field, to='"+to+"'.");
267 return null;
268
269 }
270
271 /** process method for stylesheet requests
272 */
273 protected Element processFormat(Element request) {
274 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
275 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT);
276
277 String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
278
279 if (to.equals("")) { // serviceRack query - is this appropriate??
280 return response;
281 }
282
283
284 // describe a particular service
285 if (format_info_map_.containsKey(to)) {
286 response.appendChild(getServiceFormat(to));
287 response.setAttribute(GSXML.FROM_ATT, to);
288 return response;
289 }
290 // else error in to field
291 System.err.println("ServiceRack describe request: no format info for "+to+".");
292 return response;
293 }
294
295 // the following two should be overwritten for info with any language stuff in it
296 /** returns the service list for the subclass */
297 protected Element getServiceList(String lang) {
298 // for now, it is static and there is no lang stuff
299 return short_service_info_;
300 }
301
302 /** returns a specific service description */
303 protected Element getServiceDescription(String service, String lang) {
304 Element descript = (Element)((Element)service_info_map_.get(service)).cloneNode(true);
305 // for now, create the display element on the fly - look at caching it later
306 Element display = createServiceDisplay(service, lang);
307 addServiceDisplay(descript, display);
308 return descript;
309 }
310
311
312 /** adds the display element into the description - appends to the root for now, but may do something fancy later */
313 protected boolean addServiceDisplay(Element descript, Element display) {
314 if (descript.getOwnerDocument() != display.getOwnerDocument()) {
315 display = (Element)descript.getOwnerDocument().importNode(display, true);
316 }
317 descript.appendChild(display);
318 return true;
319 }
320
321 protected Element getServiceFormat(String service) {
322 Element format = (Element)((Element)format_info_map_.get(service)).cloneNode(true);
323 return format;
324 }
325 /** creates a display element containing all the text strings needed to display the service page, in the language specified */
326 abstract protected Element createServiceDisplay(String service, String lang);
327
328 /** overloaded version for no args case */
329 protected String getTextString(String key, String lang) {
330 return getTextString(key, null, lang);
331 }
332 /** getTextString - retrieves a language specific text string for the given
333key and locale
334 */
335 protected String getTextString(String key, String[] args, String lang) {
336
337 String class_name = this.getClass().getName();
338 class_name = class_name.substring(class_name.lastIndexOf('.')+1);
339
340 Dictionary dict = new Dictionary(class_name, lang);
341 String result = dict.get(key, args);
342 if (result == null) { // not found
343 return "_"+key+"_";
344 }
345 return result;
346 }
347}
Note: See TracBrowser for help on using the repository browser.