1 | /*
|
---|
2 | * ServicesImpl.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.Element;
|
---|
28 | import org.w3c.dom.Document;
|
---|
29 | import org.xml.sax.InputSource;
|
---|
30 | import javax.xml.parsers.*;
|
---|
31 | import org.apache.xpath.XPathAPI;
|
---|
32 |
|
---|
33 | // general java classes
|
---|
34 | import java.io.Reader;
|
---|
35 | import java.io.StringReader;
|
---|
36 | import java.io.File;
|
---|
37 | import java.util.HashMap;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * ServicesImpl - abstract base class
|
---|
41 | *
|
---|
42 | * A ServicesImpl implements some Service modules -
|
---|
43 | * It may provide more than one Service. for eg
|
---|
44 | * MGGDBMServicesImpl may support "DocRetrieve", "TextQuery", "MetadataRetrieve" services
|
---|
45 | *
|
---|
46 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
47 | * @version $Revision: 3492 $
|
---|
48 | */
|
---|
49 | public abstract class ServicesImpl
|
---|
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 ServicesImpl() {
|
---|
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 <servicesImpl 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("ServicesImpl: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("ServicesImpl: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 | if (type.equals("describe")) {
|
---|
162 | return processDescribe(request);
|
---|
163 | }
|
---|
164 |
|
---|
165 | String to = GSPath.getFirstLink(request.getAttribute("to"));
|
---|
166 |
|
---|
167 | // other type of request, must be processed by the subclass -
|
---|
168 | // send to the service method
|
---|
169 | if (service_info_map_.containsKey(to)) {
|
---|
170 | return processService(to, request);
|
---|
171 | }
|
---|
172 | else {
|
---|
173 | // else error in to field
|
---|
174 | System.err.println("ServicesImpl describe request: error in 'to' field, to='"+to+"'.");
|
---|
175 | return null;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** process method for describe requests - is implemented here for services
|
---|
182 | * whose descriptions are static - those services whose params may change dynamically should overwrite this method
|
---|
183 | */
|
---|
184 | protected Element processDescribe(Element request) {
|
---|
185 |
|
---|
186 | Element response = doc_.createElement("response");
|
---|
187 | response.setAttribute("type", "describe");
|
---|
188 |
|
---|
189 | String info = request.getAttribute("info");
|
---|
190 | String to = GSPath.getFirstLink(request.getAttribute("to"));
|
---|
191 |
|
---|
192 | if (to.equals("")) { // to="", look at info
|
---|
193 | if (info.equals("serviceList")) {
|
---|
194 | response.appendChild(short_service_info_);
|
---|
195 | return response;
|
---|
196 | }
|
---|
197 | // else error in info field
|
---|
198 | System.err.println("ServicesImpl describe request: error in 'info' field, info='"+info+"'.");
|
---|
199 | return null;
|
---|
200 | }
|
---|
201 |
|
---|
202 | // describe a particular service
|
---|
203 |
|
---|
204 | if (service_info_map_.containsKey(to)) {
|
---|
205 | response.appendChild((Element)service_info_map_.get(to));
|
---|
206 | return response;
|
---|
207 | }
|
---|
208 | // else error in to field
|
---|
209 | System.err.println("ServicesImpl describe request: error in 'to' field, to='"+to+"'.");
|
---|
210 | return null;
|
---|
211 |
|
---|
212 | }
|
---|
213 | /** process method for specific services - must be implemented by all
|
---|
214 | * subclasses
|
---|
215 | * should implement all services supported by the servicesImpl except
|
---|
216 | * for describe, which is implemented by this base class
|
---|
217 | *
|
---|
218 | */
|
---|
219 | abstract protected Element processService(String service, Element request);
|
---|
220 |
|
---|
221 |
|
---|
222 | }
|
---|