1 | /*
|
---|
2 | * Collection.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.collection;
|
---|
20 |
|
---|
21 | import org.greenstone.gsdl3.util.*;
|
---|
22 | import org.greenstone.gsdl3.core.*;
|
---|
23 | import org.greenstone.gsdl3.service.*;
|
---|
24 |
|
---|
25 |
|
---|
26 | // java XML classes we're using
|
---|
27 | import org.w3c.dom.Document;
|
---|
28 | import org.w3c.dom.Node;
|
---|
29 | import org.w3c.dom.Element;
|
---|
30 | import org.w3c.dom.NodeList;
|
---|
31 |
|
---|
32 | import java.io.File;
|
---|
33 | import java.util.HashMap;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Represents a collection in Greenstone.
|
---|
37 | *
|
---|
38 | * Implements ModuleInterface. Contains a list of services supported by the collection, as well as collection metadata
|
---|
39 | *
|
---|
40 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
41 | * @version $Revision: 3441 $
|
---|
42 | * @see ModuleInterface
|
---|
43 | */
|
---|
44 | public class Collection
|
---|
45 | implements ModuleInterface {
|
---|
46 |
|
---|
47 | /** base directory for the site */
|
---|
48 | protected String site_home_ = null;
|
---|
49 |
|
---|
50 | /** The name of the collection - this must be the name of the collection
|
---|
51 | * directory inside collect */
|
---|
52 | protected String collection_name_ = null;
|
---|
53 |
|
---|
54 | /** The map of services.
|
---|
55 | *
|
---|
56 | * Maps Services to ServiceModule objects
|
---|
57 | * @see ServiceModule
|
---|
58 | *
|
---|
59 | */
|
---|
60 | protected HashMap service_map_=null;
|
---|
61 |
|
---|
62 | /** XML converter for String to DOM and vice versa */
|
---|
63 | protected XMLConverter converter_=null;
|
---|
64 |
|
---|
65 | /** container doc for description elements */
|
---|
66 | protected Document doc_ = null;
|
---|
67 | /** list of services */
|
---|
68 | protected Element service_info_ = null;
|
---|
69 | /** list of Metadata */
|
---|
70 | protected Element meta_info_ = null;
|
---|
71 | /** full description */
|
---|
72 | protected Element description_ = null;
|
---|
73 |
|
---|
74 | public void setSiteHome(String home) {
|
---|
75 | site_home_ = home;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void setCollectionName(String name) {
|
---|
79 | collection_name_ = name;
|
---|
80 | description_.setAttribute("name", name);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public Collection() {
|
---|
84 | service_map_ = new HashMap();
|
---|
85 | converter_ = new XMLConverter();
|
---|
86 | doc_ = converter_.newDOM();
|
---|
87 | description_ = doc_.createElement("collection");
|
---|
88 | service_info_ = doc_.createElement("serviceList");
|
---|
89 | meta_info_ = doc_.createElement("metadataList");
|
---|
90 |
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Configures the collection.
|
---|
96 | *
|
---|
97 | * gsdlHome and collectionName must be set before configure is called.
|
---|
98 | *
|
---|
99 | * the file buildcfg.xml is located in gsdlHome/collect/collectionName
|
---|
100 | * collection metadata is obtained, and services loaded.
|
---|
101 | *
|
---|
102 | * @return true/false on success/fail
|
---|
103 | */
|
---|
104 | public boolean configure() {
|
---|
105 |
|
---|
106 | if (site_home_ == null || collection_name_== null) {
|
---|
107 | System.err.println("site_home_ and collection_name_ must be set before configure called!");
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 | // read the collection build configuration file
|
---|
111 | File config_file_ = new File(GSFile.collectionBuildConfigFile(site_home_, collection_name_));
|
---|
112 |
|
---|
113 | if (!config_file_.exists()) {
|
---|
114 | System.err.println(config_file_+" does not exist");
|
---|
115 | System.err.println("couldn't configure collection: "+collection_name_);
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 |
|
---|
119 | try {
|
---|
120 |
|
---|
121 | Document doc = converter_.getDOM(config_file_);
|
---|
122 |
|
---|
123 | // create all the services
|
---|
124 | NodeList nodes = doc.getElementsByTagName("serviceModule");
|
---|
125 |
|
---|
126 | // the xml request to send to each serviceModule to query what
|
---|
127 | // services it provides
|
---|
128 | Element request = doc_.createElement("request");
|
---|
129 | request.setAttribute("type", "describe");
|
---|
130 | request.setAttribute("info", "serviceList");
|
---|
131 |
|
---|
132 | if (nodes.getLength()==0) {
|
---|
133 | System.err.println("Collection configuration error: collection "+collection_name_+" has no services!");
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 |
|
---|
137 | for(int i=0; i<nodes.getLength(); i++) {
|
---|
138 | Element n = (Element)nodes.item(i);
|
---|
139 | String servicetype = n.getAttribute("name");
|
---|
140 |
|
---|
141 | ServiceModule s = (ServiceModule)Class.forName("org.greenstone.gsdl3.service."+servicetype).newInstance();
|
---|
142 | s.setSiteHome(site_home_);
|
---|
143 | s.setCollectionName(collection_name_);
|
---|
144 | // pass the xml node to the service for configuration
|
---|
145 | s.configure(n);
|
---|
146 |
|
---|
147 | // find out the supported service types for this service module
|
---|
148 | Node types = s.process(request);
|
---|
149 | NodeList typenodes = ((Element)types).getElementsByTagName("service");
|
---|
150 |
|
---|
151 | for (int j=0; j<typenodes.getLength();j++) {
|
---|
152 | String service = ((Element) typenodes.item(j)).getAttribute("name");
|
---|
153 | service_map_.put(service, s);
|
---|
154 |
|
---|
155 | // also add info to the ServiceInfo XML element
|
---|
156 | service_info_.appendChild(doc_.importNode(typenodes.item(j), true));
|
---|
157 | }
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | // get the metadata - for now just add it to the list
|
---|
162 | NodeList metanodes = doc.getElementsByTagName("metadata");
|
---|
163 |
|
---|
164 | if (metanodes.getLength()>0) {
|
---|
165 | for(int k=0; k<metanodes.getLength(); k++) {
|
---|
166 | meta_info_.appendChild(doc_.importNode(metanodes.item(k),true));
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | } catch (Exception e) {
|
---|
171 | System.out.println("Collection configure exception: "+e.getMessage() + e.getClass() );
|
---|
172 | e.printStackTrace();
|
---|
173 | return false;
|
---|
174 | }
|
---|
175 |
|
---|
176 | // add elements to description
|
---|
177 | description_.appendChild(service_info_);
|
---|
178 | description_.appendChild(meta_info_);
|
---|
179 | return true;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Process an XML document - uses Strings
|
---|
185 | * just calls process(Node).
|
---|
186 | *
|
---|
187 | * @param in the Document to process - a string
|
---|
188 | * @return the resultant document as a string - contains any error messages
|
---|
189 | * @see String
|
---|
190 | */
|
---|
191 | public String process(String in) {
|
---|
192 |
|
---|
193 | Document doc = converter_.getDOM(in);
|
---|
194 | Element e = doc.getDocumentElement();
|
---|
195 |
|
---|
196 | Node res = process(e);
|
---|
197 | return converter_.getString(res);
|
---|
198 |
|
---|
199 | }
|
---|
200 |
|
---|
201 | /** process XML as Node
|
---|
202 | *
|
---|
203 | */
|
---|
204 | public Node process(Node xml_in) {
|
---|
205 |
|
---|
206 | // xml_in should always be an Element - should check this
|
---|
207 | Element xml = (Element)xml_in;
|
---|
208 | String to = xml.getAttribute("to");
|
---|
209 |
|
---|
210 | // the collection name should be first, check, then remove
|
---|
211 | String col = GSPath.getFirstLink(to);
|
---|
212 | if (col.equals(collection_name_)){
|
---|
213 | to = GSPath.removeFirstLink(to);
|
---|
214 | xml.setAttribute("to", to);
|
---|
215 | } else {
|
---|
216 | System.out.println("Collection error: collection name wrong! was "+col+" should have been "+collection_name_);
|
---|
217 | return null;
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (to.equals("")) { // this command is for me
|
---|
221 | String type = xml.getAttribute("type");
|
---|
222 | if (type.equals("describe")) {
|
---|
223 | // create a response with the description inside it
|
---|
224 | Element response = doc_.createElement("response");
|
---|
225 | response.setAttribute("from", collection_name_);
|
---|
226 | response.setAttribute("type", "describe");
|
---|
227 | response.appendChild(description_);
|
---|
228 | return response;
|
---|
229 | } else {
|
---|
230 | System.out.println("Collection: error, cant handle request:");
|
---|
231 | System.out.println(converter_.getString(xml));
|
---|
232 | return null;
|
---|
233 | }
|
---|
234 |
|
---|
235 | } else { // the request is for one of my services
|
---|
236 | String service = GSPath.getFirstLink(to);
|
---|
237 |
|
---|
238 | if (service_map_.containsKey(service)) {
|
---|
239 | return ((ServiceModule)service_map_.get(service)).process(xml);
|
---|
240 | } else {
|
---|
241 | System.err.println("Collection "+collection_name_+": non-existant service in:\n"+converter_.getString(xml));
|
---|
242 | return null;
|
---|
243 | }
|
---|
244 |
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | }
|
---|