source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/ServiceModule.java@ 3471

Last change on this file since 3471 was 3471, checked in by kjdon, 22 years ago

service modules now belong to a serviceCluster or colleciton - collection_name has been changed to the more general cluster_name. service module cant configure itself from a file - we no longer know where the appropriate file is. so must be configured by passing the xml node to the configure method

  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/*
2 * ServiceModule.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.Element;
28import org.w3c.dom.Document;
29import org.xml.sax.InputSource;
30import javax.xml.parsers.*;
31import org.apache.xpath.XPathAPI;
32
33// general java classes
34import java.io.Reader;
35import java.io.StringReader;
36import java.io.File;
37import java.util.HashMap;
38
39/**
40 * ServiceModule - abstract base class
41 *
42 * A ServiceModule provides Services to a collection/cluster/site.
43 * It may provide more than one Service. for eg
44 * MGGDBMServiceModule may support "DocRetrieve", "TextQuery", "MetadataRetrieve" services
45 *
46 * @author <a href="mailto:[email protected]">Katherine Don</a>
47 * @version $Revision: 3471 $
48 */
49public abstract class ServiceModule
50 implements ModuleInterface
51{
52
53
54 /** the absolute address of the site home */
55 protected String site_home_ =null;
56 /** the name of the cluster that this service belongs to -
57 if any */
58 protected String cluster_name_ = null;
59
60 /** some services can talk back to the message router */
61 protected ModuleInterface router_ = null;
62
63 /** a converter class to create Documents etc */
64 protected XMLConverter converter_ = null;
65
66 /** XML element for describe requests - the container doc */
67 protected Document doc_ = null;
68
69 /** XML element for describe requests - list of supported services */
70 protected Element short_service_info_ = null;
71
72 /** XML element for describe requests - map of service name to full
73 description */
74 protected HashMap service_info_map_ = null;
75
76 /** sets the cluster name */
77 public void setClusterName(String cluster_name) {
78 cluster_name_ = cluster_name;
79 }
80 /** sets the collect name */
81 public void setCollectionName(String coll_name) {
82 setClusterName(coll_name);
83 }
84
85 /** sets the site home */
86 public void setSiteHome(String site_home) {
87 site_home_ = site_home;
88 }
89
90 /** sets the message router */
91 public void setMessageRouter(ModuleInterface m) {
92 router_ = m;
93 }
94
95 /** the no-args constructor */
96 public ServiceModule() {
97 converter_ = new XMLConverter();
98 doc_ = converter_.newDOM();
99 short_service_info_ = doc_.createElement("serviceList");
100 service_info_map_ = new HashMap();
101 }
102
103
104 /** configure the service module
105 *
106 * @param info the XML node <serviceModule name="XXX"/> with name equal
107 * to the class name (of the subclass)
108 *
109 * must initialise short_service_info_ and service_info_map_
110 * @return true if configured ok
111 * must be implemented in subclasses
112 */
113 abstract public boolean configure(Element info);
114
115 /**
116 * Process an XML document - convenience method that uses Strings rather than Nodes. just calls process(Node).
117 *
118 * @param xml_in the Document to process - a string
119 * @return the resultant document as a string - contains any error messages
120 * @see String
121 */
122 public String process(String xml_in) {
123
124 Document doc = converter_.getDOM(xml_in);
125
126 Node res = process(doc);
127 return converter_.getString(res);
128
129 }
130
131 /** process an XML request in DOM form
132 *
133 * @param xml_in the Element node containing the request
134 * should be <request> or maybe <multipleRequest>?? if more than one
135 * request coming at once
136 * @return an Element with the result XML
137 * @see Element
138 */
139 public Node process(Node xml_in) {
140
141 // check if request is Element or Document - we want to work
142 // with an Element
143 Element request=null;
144
145 short node_type = xml_in.getNodeType();
146 if (node_type == Node.DOCUMENT_NODE) {
147 request = ((Document)xml_in).getDocumentElement();
148 } else if (node_type == Node.ELEMENT_NODE) {
149 request = (Element)xml_in;
150 } else {
151 System.err.println("ServiceModule:process error: input should be an Element or Document!");
152 }
153
154 String req = request.getNodeName();
155 if (!req.equals("request")) {
156 System.err.println("ServiceModule:process - should have been passed a request element, instead was given a "+req+" element!");
157 return null;
158 }
159
160 String type = request.getAttribute("type");
161 String to = GSPath.getFirstLink(request.getAttribute("to"));
162 String info = request.getAttribute("info");
163 if (type.equals("describe")) { // process here, not by subclass
164 Element response = doc_.createElement("response");
165 response.setAttribute("type", "describe");
166 if (to.equals("")) { // to="", look at info
167 if (info.equals("serviceList")) {
168 response.appendChild(short_service_info_);
169 return response;
170 }
171 // else error in info field
172 System.err.println("ServiceModule describe request: error in 'info' field, info='"+info+"'.");
173 return null;
174 } else { // describe a particular service
175 if (service_info_map_.containsKey(to)) {
176 response.appendChild((Element)service_info_map_.get(to));
177 return response;
178 }
179 // else error in to field
180 System.err.println("ServiceModule describe request: error in 'to' field, to='"+to+"'.");
181 return null;
182 }
183 } else { // other type of request, must be processed by the subclass -
184 // send to the service method
185 if (service_info_map_.containsKey(to)) {
186 return processService(to, request);
187 }
188 else {
189 // else error in to field
190 System.err.println("ServiceModule describe request: error in 'to' field, to='"+to+"'.");
191 return null;
192 }
193 }
194
195 }
196
197 /** process method for specific services - must be implemented by all
198 * subclasses
199 * should implement all services supported by the serviceModule except
200 * for describe, which is implemented by this base class
201 *
202 */
203 abstract protected Element processService(String service, Element request);
204
205
206}
Note: See TracBrowser for help on using the repository browser.