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: 3946 $
|
---|
55 | */
|
---|
56 | public abstract class ServiceRack
|
---|
57 | implements ModuleInterface
|
---|
58 | {
|
---|
59 |
|
---|
60 | /** the absolute address of the site home */
|
---|
61 | protected String site_home_ =null;
|
---|
62 | /** the name of the cluster (or collection) that this service
|
---|
63 | belongs to - if any */
|
---|
64 | protected String cluster_name_ = null;
|
---|
65 |
|
---|
66 | /** some services can talk back to the message router */
|
---|
67 | protected ModuleInterface router_ = null;
|
---|
68 |
|
---|
69 | /** a converter class to create Documents etc */
|
---|
70 | protected XMLConverter converter_ = null;
|
---|
71 |
|
---|
72 | /** the original config info - if need to store it */
|
---|
73 | protected Element config_info_ = null;
|
---|
74 |
|
---|
75 | /** XML element for describe requests - the container doc */
|
---|
76 | protected Document doc_ = null;
|
---|
77 |
|
---|
78 | /** XML element for describe requests - list of supported services */
|
---|
79 | protected Element short_service_info_ = null;
|
---|
80 |
|
---|
81 | /** XML element for describe requests - map of service name to full
|
---|
82 | description */
|
---|
83 | protected HashMap service_info_map_ = null;
|
---|
84 | /** XML element for stylesheet requests - map of service name to format
|
---|
85 | elem */
|
---|
86 | protected HashMap format_info_map_ = null;
|
---|
87 |
|
---|
88 | /** sets the cluster name */
|
---|
89 | public void setClusterName(String cluster_name) {
|
---|
90 | cluster_name_ = cluster_name;
|
---|
91 | }
|
---|
92 | /** sets the collect name */
|
---|
93 | public void setCollectionName(String coll_name) {
|
---|
94 | setClusterName(coll_name);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** sets the site home */
|
---|
98 | public void setSiteHome(String site_home) {
|
---|
99 | site_home_ = site_home;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** sets the message router */
|
---|
103 | public void setMessageRouter(ModuleInterface m) {
|
---|
104 | router_ = m;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** the no-args constructor */
|
---|
108 | public ServiceRack() {
|
---|
109 | converter_ = new XMLConverter();
|
---|
110 | doc_ = converter_.newDOM();
|
---|
111 | short_service_info_ = doc_.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
|
---|
112 | service_info_map_ = new HashMap();
|
---|
113 | format_info_map_ = new HashMap();
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /** configure the service module
|
---|
118 | *
|
---|
119 | * @param info the XML node <serviceRack name="XXX"/> with name equal
|
---|
120 | * to the class name (of the subclass)
|
---|
121 | *
|
---|
122 | * must configure short_service_info_ and service_info_map_
|
---|
123 | * @return true if configured ok
|
---|
124 | * must be implemented in subclasses
|
---|
125 | */
|
---|
126 | public boolean configure(Element info) {
|
---|
127 | return configure(info, null);
|
---|
128 | }
|
---|
129 |
|
---|
130 | abstract public boolean configure(Element info, Element extra_info);
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Process an XML document - convenience method that uses Strings rather than Elements. just calls process(Element).
|
---|
134 | *
|
---|
135 | * @param xml_in the Document to process - a string
|
---|
136 | * @return the resultant document as a string - contains any error messages
|
---|
137 | * @see String
|
---|
138 | */
|
---|
139 | public String process(String xml_in) {
|
---|
140 |
|
---|
141 | Document doc = converter_.getDOM(xml_in);
|
---|
142 |
|
---|
143 | Element res = process(doc.getDocumentElement());
|
---|
144 | return converter_.getString(res);
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** process an XML request in DOM form
|
---|
149 | *
|
---|
150 | * @param xml_in the Element node containing the request
|
---|
151 | * should be <message>
|
---|
152 | * @return an Element with the result XML
|
---|
153 | * @see Element
|
---|
154 | */
|
---|
155 | public Element process(Element message) {
|
---|
156 |
|
---|
157 | NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
|
---|
158 | Document mess_doc = message.getOwnerDocument();
|
---|
159 | Element mainResult = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
160 | if (requests.getLength()==0) {
|
---|
161 | // no requests
|
---|
162 | return mainResult; // empty message for now
|
---|
163 | }
|
---|
164 |
|
---|
165 | for (int i=0; i<requests.getLength(); i++) {
|
---|
166 | Element request = (Element)requests.item(i);
|
---|
167 |
|
---|
168 | String type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
169 | if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
|
---|
170 | Element response = processDescribe(request);
|
---|
171 | if (response !=null) {
|
---|
172 | mainResult.appendChild(doc_.importNode(response, true));
|
---|
173 | }
|
---|
174 |
|
---|
175 | } else if (type.equals(GSXML.REQUEST_TYPE_FORMAT)) {
|
---|
176 | Element response = processFormat(request);
|
---|
177 | mainResult.appendChild(doc_.importNode(response, true));
|
---|
178 |
|
---|
179 |
|
---|
180 | } else {
|
---|
181 | // other type of request, must be processed by the subclass -
|
---|
182 | // send to the service method
|
---|
183 |
|
---|
184 | String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
185 | Element response = null;
|
---|
186 | if (service_info_map_.containsKey(to)) {
|
---|
187 | try {
|
---|
188 | Class c = this.getClass();
|
---|
189 | Class []params = {Class.forName("org.w3c.dom.Element")};
|
---|
190 |
|
---|
191 | String method_name = "process"+to;
|
---|
192 | Method m = null;
|
---|
193 | while (c != null) {
|
---|
194 |
|
---|
195 | try {
|
---|
196 | m = c.getDeclaredMethod(method_name, params);
|
---|
197 | // if this has worked, break
|
---|
198 | break;
|
---|
199 | } catch (NoSuchMethodException e) {
|
---|
200 | c = c.getSuperclass();
|
---|
201 | } catch (SecurityException e) {
|
---|
202 | System.err.println("ServiceRack.process: security exception for finding method "+method_name+", returning null");
|
---|
203 | return null;
|
---|
204 | }
|
---|
205 | } // while
|
---|
206 | if (m != null) {
|
---|
207 | Object []args = {request};
|
---|
208 | try {
|
---|
209 | response = (Element)m.invoke(this, args);
|
---|
210 |
|
---|
211 | } catch (Exception e) {
|
---|
212 | 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());
|
---|
213 | return null;
|
---|
214 | }
|
---|
215 | } else {
|
---|
216 | System.err.println("ServiceRack.process: method "+method_name+" not found for class "+this.getClass().getName());
|
---|
217 | }
|
---|
218 | if (response !=null) {
|
---|
219 | mainResult.appendChild(doc_.importNode(response, true));
|
---|
220 | }
|
---|
221 |
|
---|
222 | } catch (ClassNotFoundException e) {
|
---|
223 | System.err.println("ServiceRack error: Element class not found");
|
---|
224 | return null;
|
---|
225 | }
|
---|
226 | } else {
|
---|
227 | // else error in to field
|
---|
228 | System.err.println("ServiceRack describe request: error in 'to' field, non-existant service "+to+" specified!");
|
---|
229 | return null;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | } // for each request
|
---|
233 |
|
---|
234 | return mainResult;
|
---|
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 info = request.getAttribute(GSXML.INFO_ATT);
|
---|
247 | String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
248 |
|
---|
249 | if (to.equals("")) { // to="", look at info
|
---|
250 | if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
251 | response.appendChild(getServiceList(lang));
|
---|
252 | return response;
|
---|
253 | }
|
---|
254 | // else error in info field
|
---|
255 | System.err.println("ServiceRack describe request: error in 'info' field, info='"+info+"'.");
|
---|
256 | return null;
|
---|
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
|
---|
333 | key 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 | }
|
---|