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

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

changed the way the process methods are called - now if one is not found for the actuall class, the superclasses are checked for the method

  • 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: 3900 $
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 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
85 /** sets the cluster name */
86 public void setClusterName(String cluster_name) {
87 cluster_name_ = cluster_name;
88 }
89 /** sets the collect name */
90 public void setCollectionName(String coll_name) {
91 setClusterName(coll_name);
92 }
93
94 /** sets the site home */
95 public void setSiteHome(String site_home) {
96 site_home_ = site_home;
97 }
98
99 /** sets the message router */
100 public void setMessageRouter(ModuleInterface m) {
101 router_ = m;
102 }
103
104 /** the no-args constructor */
105 public ServiceRack() {
106 converter_ = new XMLConverter();
107 doc_ = converter_.newDOM();
108 short_service_info_ = doc_.createElement(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
109 service_info_map_ = new HashMap();
110 }
111
112
113 /** configure the service module
114 *
115 * @param info the XML node <serviceRack name="XXX"/> with name equal
116 * to the class name (of the subclass)
117 *
118 * must configure short_service_info_ and service_info_map_
119 * @return true if configured ok
120 * must be implemented in subclasses
121 */
122 abstract public boolean configure(Element info);
123
124 /**
125 * Process an XML document - convenience method that uses Strings rather than Elements. just calls process(Element).
126 *
127 * @param xml_in the Document to process - a string
128 * @return the resultant document as a string - contains any error messages
129 * @see String
130 */
131 public String process(String xml_in) {
132
133 Document doc = converter_.getDOM(xml_in);
134
135 Element res = process(doc.getDocumentElement());
136 return converter_.getString(res);
137
138 }
139
140 /** process an XML request in DOM form
141 *
142 * @param xml_in the Element node containing the request
143 * should be <message>
144 * @return an Element with the result XML
145 * @see Element
146 */
147 public Element process(Element message) {
148
149 NodeList requests = message.getElementsByTagName(GSXML.REQUEST_ELEM);
150 Document mess_doc = message.getOwnerDocument();
151 Element mainResult = doc_.createElement(GSXML.MESSAGE_ELEM);
152 if (requests.getLength()==0) {
153 // no requests
154 return mainResult; // empty message for now
155 }
156
157 for (int i=0; i<requests.getLength(); i++) {
158 Element request = (Element)requests.item(i);
159
160 String type = request.getAttribute(GSXML.TYPE_ATT);
161 if (type.equals(GSXML.REQUEST_TYPE_DESCRIBE)) {
162 Element response = processDescribe(request);
163 if (response !=null) {
164 mainResult.appendChild(doc_.importNode(response, true));
165 }
166
167 } else {
168 // other type of request, must be processed by the subclass -
169 // send to the service method
170
171 String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
172 Element response = null;
173 if (service_info_map_.containsKey(to)) {
174 try {
175 Class c = this.getClass();
176 Class []params = {Class.forName("org.w3c.dom.Element")};
177
178 String method_name = "process"+to;
179 Method m = null;
180 while (c != null) {
181
182 try {
183 m = c.getDeclaredMethod(method_name, params);
184 // if this has worked, break
185 break;
186 } catch (NoSuchMethodException e) {
187 System.out.println("method not found for this class, trying super class");
188 c = c.getSuperclass();
189 } catch (SecurityException e) {
190 System.out.println("security exception for finding a method, returning null");
191 return null;
192 }
193 } // while
194 if (m != null) {
195 Object []args = {request};
196 try {
197 response = (Element)m.invoke(this, args);
198
199 } catch (Exception e) {
200 System.out.println("ServiceRack: Trying to call a processService type method (process"+to+") on a subclass("+this.getClass().getName()+"), but an exception happened:"+e.toString());
201 return null;
202 }
203 } else {
204 System.out.println("no method found");
205 }
206 if (response !=null) {
207 mainResult.appendChild(doc_.importNode(response, true));
208 }
209
210 } catch (ClassNotFoundException e) {
211 System.out.println("ServiceRack error:element class not found");
212 return null;
213 }
214 } else {
215 // else error in to field
216 System.err.println("ServiceRack describe request: error in 'to' field, non-existant service "+to+" specified!");
217 return null;
218 }
219 }
220 } // for each request
221
222 return mainResult;
223
224 }
225
226 /** process method for describe requests
227 */
228 protected Element processDescribe(Element request) {
229
230 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
231 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
232
233 String lang = request.getAttribute(GSXML.LANG_ATT);
234 String info = request.getAttribute(GSXML.INFO_ATT);
235 String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
236
237 if (to.equals("")) { // to="", look at info
238 if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
239 response.appendChild(getServiceList(lang));
240 return response;
241 }
242 // else error in info field
243 System.err.println("ServiceRack describe request: error in 'info' field, info='"+info+"'.");
244 return null;
245 }
246
247 // describe a particular service
248 if (service_info_map_.containsKey(to)) {
249 response.appendChild(getServiceDescription(to, lang));
250 response.setAttribute(GSXML.FROM_ATT, to);
251 return response;
252 }
253 // else error in to field
254 System.err.println("ServiceRack describe request: error in 'to' field, to='"+to+"'.");
255 return null;
256
257 }
258
259 // the following two should be overwritten for info with any language stuff in it
260 /** returns the service list for the subclass */
261 protected Element getServiceList(String lang) {
262 // for now, it is static and there is no lang stuff
263 return short_service_info_;
264 }
265
266 /** returns a specific service description */
267 protected Element getServiceDescription(String service, String lang) {
268 Element descript = (Element)((Element)service_info_map_.get(service)).cloneNode(true);
269 // for now, create the display element on the fly - look at caching it later
270 Element display = createServiceDisplay(service, lang);
271 addServiceDisplay(descript, display);
272 return descript;
273 }
274
275 /** adds the display element into the description - appends to the root for now, but may do something fancy later */
276 protected boolean addServiceDisplay(Element descript, Element display) {
277 if (descript.getOwnerDocument() != display.getOwnerDocument()) {
278 display = (Element)descript.getOwnerDocument().importNode(display, true);
279 }
280 descript.appendChild(display);
281 return true;
282 }
283
284 /** creates a display element containing all the text strings needed to display the service page, in the language specified */
285 abstract protected Element createServiceDisplay(String service, String lang);
286
287 /** overloaded version for no args case */
288 protected String getTextString(String key, String lang) {
289 return getTextString(key, null, lang);
290 }
291 /** getTextString - retrieves a language specific text string for the given
292key and locale
293 */
294 protected String getTextString(String key, String[] args, String lang) {
295
296 String class_name = this.getClass().getName();
297 class_name = class_name.substring(class_name.lastIndexOf('.')+1);
298
299 Dictionary dict = new Dictionary(class_name, lang);
300 String result = dict.get(key, args);
301 if (result == null) { // not found
302 return "_"+key+"_";
303 }
304 return result;
305 }
306}
Note: See TracBrowser for help on using the repository browser.