source: trunk/gsdl3/src/java/org/greenstone/gsdl3/collection/ServiceCluster.java@ 3490

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

ServiceModule now called ServicesImpl

  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1/*
2 * ServiceCluster.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// leave the package name as is for now - should be changed to something better
20// cluster? groups?
21package org.greenstone.gsdl3.collection;
22
23
24import org.greenstone.gsdl3.util.*;
25import org.greenstone.gsdl3.core.*;
26import org.greenstone.gsdl3.service.*;
27
28// java XML classes we're using
29import org.w3c.dom.Document;
30import org.w3c.dom.Node;
31import org.w3c.dom.Element;
32import org.w3c.dom.NodeList;
33
34import java.io.File;
35import java.util.HashMap;
36
37/* ServiceCluster - a groups of services that are related in some way
38 * Implements ModuleInterface. Contains a list of services provided by the cluster, along with metadata about the cluster itself.
39 * a collection is a special type of cluster
40 * @author <a href="mailto:[email protected]">Katherine Don</a>
41 * @version $Revision: 3490 $
42 * @see ModuleInterface
43 */
44public class ServiceCluster
45 implements ModuleInterface {
46
47
48 /** base directory for the site that this cluster belongs to*/
49 protected String site_home_ = null;
50
51 /** The name of the cluster - for a collection, this is the collection name*/
52 protected String cluster_name_ = null;
53
54 /** a reference to the message router */
55 protected MessageRouter router_ = null;
56 /** The map of services.
57 *
58 * Maps Services to ServicesImpl objects
59 * @see ServicesImpl
60 *
61 */
62 protected HashMap service_map_=null;
63
64 /** XML converter for String to DOM and vice versa */
65 protected XMLConverter converter_=null;
66
67 /** container doc for description elements */
68 protected Document doc_ = null;
69 /** list of services */
70 protected Element service_info_ = null;
71 /** list of Metadata */
72 protected Element meta_info_ = null;
73 /** full description */
74 protected Element description_ = null;
75
76 public void setSiteHome(String home) {
77 site_home_ = home;
78 }
79
80 public void setClusterName(String name) {
81 cluster_name_ = name;
82 description_.setAttribute("name", name);
83 }
84
85 public void setMessageRouter(MessageRouter m) {
86 router_ = m;
87 }
88
89 public ServiceCluster() {
90 service_map_ = new HashMap();
91 converter_ = new XMLConverter();
92 doc_ = converter_.newDOM();
93 description_ = doc_.createElement("serviceCluster");
94 service_info_ = doc_.createElement("serviceList");
95 meta_info_ = doc_.createElement("metadataList");
96
97 }
98
99 /**
100 * Configures the cluster.
101 *
102 * gsdlHome and clusterName must be set before configure is called.
103 *
104 * reads the site configuration file, and configures itself
105 * this calls configure(Element) with the XML element node from the config
106 * file.
107 * configure(Element) should be used if the config file has already been
108 * parsed. This method will work with any subclass.
109 *
110 * @return true if configure successful, false otherwise.
111 */
112 public boolean configure() {
113
114 if (site_home_ == null || cluster_name_== null) {
115 System.err.println("site_home_ and cluster_name_ must be set before configure called!");
116 return false;
117 }
118 System.out.println("configuring service cluster");
119 // read the site configuration file
120 File config_file_ = new File(GSFile.siteConfigFile(site_home_));
121
122 if (!config_file_.exists()) {
123 System.err.println(config_file_+" does not exist");
124 System.err.println("couldn't configure cluster: "+cluster_name_);
125 return false;
126 }
127
128 Document doc = converter_.getDOM(config_file_);
129
130 // get the appropriate service cluster element
131 Element cluster_list = (Element)GSXML.getChildByTagName(doc.getDocumentElement(), "ServiceClusterList");
132 Element sc = GSXML.getNamedElement(cluster_list, "ServiceCluster",
133 "name", cluster_name_);
134
135 return this.configure(sc);
136 }
137
138 public boolean configure(Element cluster_info) {
139
140 // create all the services
141 NodeList nodes = cluster_info.getElementsByTagName("servicesImpl");
142
143 // the xml request to send to each servicesImpl to query what
144 // services it provides
145 Element request = doc_.createElement("request");
146 request.setAttribute("type", "describe");
147 request.setAttribute("info", "serviceList");
148
149 if (nodes.getLength()==0) {
150 System.err.println("Cluster configuration error: cluster "+cluster_name_+" has no service modules!");
151 return false;
152 }
153
154 for(int i=0; i<nodes.getLength(); i++) {
155 Element n = (Element)nodes.item(i);
156 String servicetype = n.getAttribute("name");
157
158 try {
159 ServicesImpl s = (ServicesImpl)Class.forName("org.greenstone.gsdl3.service."+servicetype).newInstance();
160
161 s.setSiteHome(site_home_);
162 s.setClusterName(cluster_name_);
163 s.setMessageRouter(router_);
164 // pass the xml node to the service for configuration
165 s.configure(n);
166
167 // find out the supported service types for this service module
168 Node types = s.process(request);
169 NodeList typenodes = ((Element)types).getElementsByTagName("service");
170
171 for (int j=0; j<typenodes.getLength();j++) {
172 String service = ((Element) typenodes.item(j)).getAttribute("name");
173 service_map_.put(service, s);
174
175 // also add info to the ServiceInfo XML element
176 service_info_.appendChild(doc_.importNode(typenodes.item(j), true));
177 }
178 } catch (Exception e) {
179 System.out.println("Cluster configure exception: couldn't create service module:org.greenstone.gsdl3.service."+servicetype+"\n"+e.getMessage() + e.getClass() );
180 e.printStackTrace();
181 //return false;
182 }
183
184 }
185
186 // get the metadata - for now just add it to the list
187 NodeList metanodes = cluster_info.getElementsByTagName("metadata");
188
189 if (metanodes.getLength()>0) {
190 for(int k=0; k<metanodes.getLength(); k++) {
191 meta_info_.appendChild(doc_.importNode(metanodes.item(k),true));
192 }
193 }
194
195 // add elements to description
196 description_.appendChild(service_info_);
197 description_.appendChild(meta_info_);
198 return true;
199
200
201 }
202
203 /**
204 * Process an XML document - uses Strings
205 * just calls process(Node).
206 *
207 * @param in the Document to process - a string
208 * @return the resultant document as a string - contains any error messages
209 * @see String
210 */
211 public String process(String in) {
212
213 Document doc = converter_.getDOM(in);
214 Element e = doc.getDocumentElement();
215
216 Node res = process(e);
217 return converter_.getString(res);
218
219 }
220
221 /** process XML as Node
222 *
223 */
224 public Node process(Node xml_in) {
225
226 // xml_in should always be an Element - should check this
227 Element xml = (Element)xml_in;
228 String to = xml.getAttribute("to");
229
230 // the cluster name should be first, check, then remove
231 String clustername = GSPath.getFirstLink(to);
232 if (clustername.equals(cluster_name_)){
233 to = GSPath.removeFirstLink(to);
234 xml.setAttribute("to", to);
235 } else {
236 System.out.println("ServiceCluster error: cluster name wrong! was "+clustername+" should have been "+cluster_name_);
237 return null;
238 }
239
240 if (to.equals("")) { // this command is for me
241 String type = xml.getAttribute("type");
242 if (type.equals("describe")) {
243 // create a response with the description inside it
244 Element response = doc_.createElement("response");
245 response.setAttribute("from", cluster_name_);
246 response.setAttribute("type", "describe");
247 response.appendChild(description_);
248 return response;
249 } else {
250 System.out.println("ServiceCluster: error, cant handle request:");
251 System.out.println(converter_.getString(xml));
252 return null;
253 }
254
255 } else { // the request is for one of my services
256 String service = GSPath.getFirstLink(to);
257
258 if (service_map_.containsKey(service)) {
259 return ((ModuleInterface)service_map_.get(service)).process(xml);
260 } else {
261 System.err.println("ServiceCluster: non-existant service, "+service+", specified, in:\n"+converter_.getString(xml));
262 return null;
263 }
264
265 }
266 }
267
268}
269
270
271
272
273
274
Note: See TracBrowser for help on using the repository browser.