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

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

a wide variety of changes, next I will go through and make sure the code is tidy, nad tidy up teh xml a bit, but I wanted to check in all this first.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.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: 3851 $
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
175 try {
176 Class c = this.getClass();
177 Class []params = {Class.forName("org.w3c.dom.Element")};
178 Method m = c.getDeclaredMethod("process"+to, params);
179 Object []args = {request};
180 response = (Element)m.invoke(this, args);
181
182 } catch (Exception e) {
183 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());
184 return null;
185 }
186 if (response !=null) {
187 mainResult.appendChild(doc_.importNode(response, true));
188 }
189 } else {
190 // else error in to field
191 System.err.println("ServiceRack describe request: error in 'to' field, non-existant service "+to+" specified!");
192 return null;
193 }
194 }
195 } // for each request
196
197
198 return mainResult;
199
200 }
201
202 /** process method for describe requests
203 */
204 protected Element processDescribe(Element request) {
205
206 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
207 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
208
209 String lang = request.getAttribute(GSXML.LANG_ATT);
210 String info = request.getAttribute(GSXML.INFO_ATT);
211 String to = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
212
213 if (to.equals("")) { // to="", look at info
214 if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
215 response.appendChild(getServiceList(lang));
216 return response;
217 }
218 // else error in info field
219 System.err.println("ServiceRack describe request: error in 'info' field, info='"+info+"'.");
220 return null;
221 }
222
223 // describe a particular service
224 if (service_info_map_.containsKey(to)) {
225 response.appendChild(getServiceDescription(to, lang));
226 response.setAttribute(GSXML.FROM_ATT, to);
227 return response;
228 }
229 // else error in to field
230 System.err.println("ServiceRack describe request: error in 'to' field, to='"+to+"'.");
231 return null;
232
233 }
234
235 // the following two should be overwritten for info with any language stuff in it
236 /** returns the service list for the subclass */
237 protected Element getServiceList(String lang) {
238 // for now, it is static and there is no lang stuff
239 return short_service_info_;
240 }
241
242 /** returns a specific service description */
243 protected Element getServiceDescription(String service, String lang) {
244 Element descript = (Element)((Element)service_info_map_.get(service)).cloneNode(true);
245 // for now, create the display element on the fly - look at caching it later
246 Element display = createServiceDisplay(service, lang);
247 addServiceDisplay(descript, display);
248 return descript;
249 }
250
251 /** adds the display element into the description - appends to the root for now, but may do something fancy later */
252 protected boolean addServiceDisplay(Element descript, Element display) {
253 if (descript.getOwnerDocument() != display.getOwnerDocument()) {
254 display = (Element)descript.getOwnerDocument().importNode(display, true);
255 }
256 descript.appendChild(display);
257 return true;
258 }
259
260 /** creates a display element containing all the text strings needed to display the service page, in the language specified */
261 abstract protected Element createServiceDisplay(String service, String lang);
262
263 /** overloaded version for no args case */
264 protected String getTextString(String key, String lang) {
265 return getTextString(key, null, lang);
266 }
267 /** getTextString - retrieves a language specific text string for the given
268key and locale
269 */
270 protected String getTextString(String key, String[] args, String lang) {
271
272 String class_name = this.getClass().getName();
273 class_name = class_name.substring(class_name.lastIndexOf('.')+1);
274
275 Dictionary dict = new Dictionary(class_name, lang);
276 String result = dict.get(key, args);
277 if (result == null) { // not found
278 return "_"+key+"_";
279 }
280 return result;
281 }
282}
Note: See TracBrowser for help on using the repository browser.