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

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

tidied up a lot of stuff, particularly the display text stuff, including how its formatted, and some of the service rack methods

  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 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: 4903 $
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 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 site_home_ = site_home;
101 }
102 /** sets the site http address */
103 public void setSiteAddress(String site_address) {
104 site_http_address_ = site_address;
105 }
106
107 /** sets the message router */
108 public void setMessageRouter(ModuleInterface m) {
109 router_ = m;
110 }
111
112 /** the no-args constructor */
113 public ServiceRack() {
114 converter_ = new XMLConverter();
115 doc_ = converter_.newDOM();
116 short_service_info_ = doc_.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
117 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 = converter_.getDOM(xml_in);
146
147 Element res = process(doc.getDocumentElement());
148 return converter_.getString(res);
149
150 }
151
152 /** process an XML request in DOM form
153 *
154 * @param xml_in the Element node containing the request
155 * should be <message>
156 * @return an Element with the result XML
157 * @see Element
158 */
159 public Element process(Element message) {
160
161 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
162 Document mess_doc = message.getOwnerDocument();
163 Element mainResult = doc_.createElement(GSXML.MESSAGE_ELEM);
164 if (requests.getLength()==0) {
165 // no requests
166 return mainResult; // empty message for now
167 }
168
169 for (int i=0; i<requests.getLength(); i++) {
170 Element request = (Element)requests.item(i);
171
172 String type = request.getAttribute(GSXML.TYPE_ATT);
173 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
174 Element response = processDescribe(request);
175 if (response !=null) {
176 mainResult.appendChild(doc_.importNode(response, true));
177 }
178
179 } else if (type.equals(GSXML.REQUEST_TYPE_FORMAT)) {
180 Element response = processFormat(request);
181 mainResult.appendChild(doc_.importNode(response, true));
182
183
184 } else {
185 // other type of request, must be processed by the subclass -
186 // send to the service method
187
188 String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
189 Element response = null;
190 try {
191 Class c = this.getClass();
192 Class []params = {Class.forName("org.w3c.dom.Element")};
193
194 String method_name = "process"+to;
195 Method m = null;
196 while (c != null) {
197
198 try {
199 m = c.getDeclaredMethod(method_name, params);
200 // if this has worked, break
201 break;
202 } catch (NoSuchMethodException e) {
203 c = c.getSuperclass();
204 } catch (SecurityException e) {
205 System.err.println("ServiceRack.process: security exception for finding method "+method_name+", returning null");
206 return null;
207 }
208 } // while
209 if (m != null) {
210 Object []args = {request};
211 try {
212 response = (Element)m.invoke(this, args);
213
214 } catch (Exception e) {
215 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());
216 return null;
217 }
218 } else {
219 System.err.println("ServiceRack.process: method "+method_name+" not found for class "+this.getClass().getName());
220 }
221 if (response !=null) {
222 mainResult.appendChild(doc_.importNode(response, true));
223 }
224
225 } catch (ClassNotFoundException e) {
226 System.err.println("ServiceRack error: Element class not found");
227 return null;
228 }
229 }
230 } // for each request
231
232 return mainResult;
233
234 }
235
236
237
238 /** process method for describe requests
239 */
240 protected Element processDescribe(Element request) {
241
242 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
243 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
244
245 String lang = request.getAttribute(GSXML.LANG_ATT);
246 String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
247 if (to.equals("")) { // return the service list
248 response.appendChild(getServiceList(lang));
249 return response;
250 }
251
252 // describe a particular service
253 //if (service_info_map_.containsKey(to)) {
254 Element description = getServiceDescription(to, lang);
255 if (description != null) { // may be null if non-existant service
256 response.appendChild(description);
257 response.setAttribute(GSXML.FROM_ATT, to);
258 }
259 return response;
260
261 }
262
263 /** process method for stylesheet requests
264 */
265 protected Element processFormat(Element request) {
266 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
267 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT);
268
269 String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
270
271 if (to.equals("")) { // serviceRack query - is this appropriate??
272 return response;
273 }
274
275
276 // describe a particular service
277 if (format_info_map_.containsKey(to)) {
278 response.appendChild(getServiceFormat(to));
279 response.setAttribute(GSXML.FROM_ATT, to);
280 return response;
281 }
282 // else no format info
283 System.err.println("ServiceRack describe request: no format info for "+to+".");
284 return response;
285 }
286
287 /** returns the service list for the subclass */
288 protected Element getServiceList(String lang) {
289 // for now, it is static and has no language stuff
290 return (Element) short_service_info_.cloneNode(true);
291 }
292
293 /** returns a specific service description */
294 abstract protected Element getServiceDescription(String service, String lang);
295
296 protected Element getServiceFormat(String service) {
297 Element format = (Element)((Element)format_info_map_.get(service)).cloneNode(true);
298 return format;
299 }
300
301 /** overloaded version for no args case */
302 protected String getTextString(String key, String lang) {
303 return getTextString(key, null, lang);
304 }
305 /** getTextString - retrieves a language specific text string for the given
306key and locale
307 */
308 protected String getTextString(String key, String[] args, String lang) {
309
310 String class_name = this.getClass().getName();
311 class_name = class_name.substring(class_name.lastIndexOf('.')+1);
312
313 Dictionary dict = new Dictionary(class_name, lang);
314 String result = dict.get(key, args);
315 if (result == null) { // not found
316 return "_"+key+"_";
317 }
318 return result;
319 }
320
321
322}
323
Note: See TracBrowser for help on using the repository browser.