1 | /*
|
---|
2 | * GS2Construct.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 | import org.greenstone.gsdl3.util.*;
|
---|
22 | import org.greenstone.gsdl3.build.*;
|
---|
23 |
|
---|
24 | import org.w3c.dom.Document;
|
---|
25 | import org.w3c.dom.Node;
|
---|
26 | import org.w3c.dom.Text;
|
---|
27 | import org.w3c.dom.Element;
|
---|
28 | import org.w3c.dom.NodeList;
|
---|
29 |
|
---|
30 | import java.util.Collections;
|
---|
31 | import java.util.HashMap;
|
---|
32 | import java.util.Map;
|
---|
33 | import java.util.List;
|
---|
34 | import java.util.ArrayList;
|
---|
35 | import java.io.File;
|
---|
36 | import java.util.Locale;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * A Services class for building collections
|
---|
40 | * provides a wrapper around the old perl scripts
|
---|
41 | *
|
---|
42 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
43 | * @version $Revision: 3681 $
|
---|
44 | */
|
---|
45 | public class GS2Construct
|
---|
46 | extends ServiceRack {
|
---|
47 |
|
---|
48 | // services offered
|
---|
49 | private static final String NEW_SERVICE = "NewCollection";
|
---|
50 | private static final String ADD_DOC_SERVICE = "AddDocument";
|
---|
51 | private static final String IMPORT_SERVICE = "ImportCollection";
|
---|
52 | private static final String BUILD_SERVICE = "BuildCollection";
|
---|
53 | private static final String ACTIVATE_SERVICE = "ActivateCollection";
|
---|
54 | private static final String DELETE_SERVICE = "DeleteCollection";
|
---|
55 | private static final String RELOAD_SERVICE = "ReloadCollection";
|
---|
56 |
|
---|
57 | // params used
|
---|
58 | private static final String COL_PARAM = "collection";
|
---|
59 | private static final String NEW_COL_TITLE_PARAM = "collTitle";
|
---|
60 | private static final String NEW_COL_ABOUT_PARAM = "collAbout";
|
---|
61 | private static final String CREATOR_PARAM = "creator";
|
---|
62 | private static final String NEW_FILE_PARAM = "newfile";
|
---|
63 | private static final String HANDLE_PARAM = "id";
|
---|
64 |
|
---|
65 | // the list of the collections - store between some method calls
|
---|
66 | private String[] collection_list_ = null;
|
---|
67 |
|
---|
68 | // set of listeners for any construction commands
|
---|
69 | protected Map listeners_ = null;
|
---|
70 |
|
---|
71 | public GS2Construct() {
|
---|
72 | listeners_ = Collections.synchronizedMap(new HashMap());
|
---|
73 |
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** returns the service list for the subclass */
|
---|
77 | protected Element getServiceList(String lang) {
|
---|
78 | // for now, it is static and there is no lang stuff
|
---|
79 | return short_service_info_;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** returns a specific service description */
|
---|
83 | protected Element getServiceDescription(String service, String lang) {
|
---|
84 | Element descript = (Element)((Element)service_info_map_.get(service)).cloneNode(true);
|
---|
85 | // need to add in collection param list
|
---|
86 | if (!service.equals(NEW_SERVICE)) {//new doesn't need a collection list
|
---|
87 | Element param_list = (Element) GSXML.getChildByTagName(descript, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
88 | Element old_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, COL_PARAM);
|
---|
89 | collection_list_ = getCollectionList();
|
---|
90 | Element param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, null, collection_list_);
|
---|
91 | // put at front of the list
|
---|
92 | param_list.replaceChild(param, old_param);
|
---|
93 | }
|
---|
94 | // for now, create it on the fly - look at caching it later
|
---|
95 | Element display = createServiceDisplay(service, lang);
|
---|
96 | addServiceDisplay(descript, display);
|
---|
97 | return descript;
|
---|
98 |
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** creates a display element containing all the text strings needed to display the service page, in the language specified */
|
---|
102 | protected Element createServiceDisplay(String service, String lang) {
|
---|
103 | Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
|
---|
104 | display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM, getTextString(service+".name", lang)));
|
---|
105 | display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_SUBMIT_ELEM, getTextString(service+".submit", lang)));
|
---|
106 |
|
---|
107 | Element param;
|
---|
108 | if (service.equals(NEW_SERVICE)) {
|
---|
109 | // have the coll title, about and creator params
|
---|
110 | param = GSXML.createParameterDisplay(doc_, NEW_COL_TITLE_PARAM, getTextString("param."+NEW_COL_TITLE_PARAM, lang), null, null);
|
---|
111 | display.appendChild(param);
|
---|
112 | param = GSXML.createParameterDisplay(doc_, CREATOR_PARAM, getTextString("param."+CREATOR_PARAM, lang), null, null);
|
---|
113 | display.appendChild(param);
|
---|
114 | param = GSXML.createParameterDisplay(doc_, NEW_COL_ABOUT_PARAM, getTextString("param."+NEW_COL_ABOUT_PARAM, lang), null, null);
|
---|
115 | display.appendChild(param);
|
---|
116 | } else {
|
---|
117 | // have just the coll param
|
---|
118 | //use the collection list already created - this method is only called directly after getServiceDescription()
|
---|
119 | param = GSXML.createParameterDisplay(doc_, COL_PARAM, getTextString("param."+COL_PARAM, lang), collection_list_, collection_list_);
|
---|
120 | display.appendChild(param);
|
---|
121 | if (service.equals(ADD_DOC_SERVICE)) {
|
---|
122 | // also have the file param
|
---|
123 | param = GSXML.createParameterDisplay(doc_, NEW_FILE_PARAM, getTextString("param."+NEW_FILE_PARAM, lang), null, null);
|
---|
124 | display.appendChild(param);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | return display;
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | // each service must have a method "process<New service name>"
|
---|
133 |
|
---|
134 | protected Element processNewCollection(Element request) {
|
---|
135 | return runCommand(request, CollectionConstructor.NEW);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** TODO:implement this */
|
---|
139 | protected Element processAddDocument(Element request) {
|
---|
140 | // decode the file name, add it to the import directory
|
---|
141 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
142 | Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
143 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
144 | Element status = doc_.createElement(GSXML.STATUS_ELEM);
|
---|
145 | response.appendChild(status);
|
---|
146 | //String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
147 | //String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
148 | Text t = doc_.createTextNode("AddDocument: not implemented yet");
|
---|
149 | status.appendChild(t);
|
---|
150 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
151 | return response;
|
---|
152 | }
|
---|
153 | protected Element processImportCollection(Element request) {
|
---|
154 | return runCommand(request, CollectionConstructor.IMPORT);
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected Element processBuildCollection(Element request) {
|
---|
158 | return runCommand(request, CollectionConstructor.BUILD);
|
---|
159 | }
|
---|
160 | protected Element processActivateCollection(Element request) {
|
---|
161 | Element response = runCommand(request, CollectionConstructor.ACTIVATE);
|
---|
162 | // this activates the collection on disk. but now we need to tell
|
---|
163 | // the MR about it. but we have to wait until the process is finished.
|
---|
164 | Element status = (Element)GSXML.getChildByTagName(response, GSXML.STATUS_ELEM);
|
---|
165 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
166 | HashMap params = GSXML.extractParams(param_list);
|
---|
167 | String coll_name = (String) params.get(COL_PARAM);
|
---|
168 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
169 | // check for finished
|
---|
170 | int status_code = Integer.parseInt(status.getAttribute(GSXML.STATUS_ERROR_CODE_ATT));
|
---|
171 | if (GSStatus.isCompleted(status_code) &&
|
---|
172 | GSStatus.isError(status_code)) {
|
---|
173 | // we shouldn't carry out the next bit, just return the response
|
---|
174 | return response;
|
---|
175 | }
|
---|
176 | String id = status.getAttribute(GSXML.STATUS_PROCESS_ID_ATT);
|
---|
177 | GS2PerlListener listener = (GS2PerlListener)listeners_.get(id);
|
---|
178 | if (listener==null) {
|
---|
179 | System.err.println("somethings gone wrong, couldn't find the listener");
|
---|
180 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
181 | return response;
|
---|
182 | }
|
---|
183 | while (!GSStatus.isCompleted(status_code)) {
|
---|
184 | // wait for the process, and keep checking the status code
|
---|
185 | // there is probably a better way to do this.
|
---|
186 | try {
|
---|
187 | Thread.currentThread().sleep(100);
|
---|
188 | } catch (Exception e) { // ignore
|
---|
189 | }
|
---|
190 | status_code = listener.getStatus();
|
---|
191 | }
|
---|
192 |
|
---|
193 | // add the rest of the messages to the status node
|
---|
194 | Text t = doc_.createTextNode("\n"+listener.getUpdate());
|
---|
195 | status.appendChild(t);
|
---|
196 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(listener.getStatus()));
|
---|
197 | if (GSStatus.isError(status_code)) {
|
---|
198 | return response; // without doing the next bit
|
---|
199 | }
|
---|
200 |
|
---|
201 | t = doc_.createTextNode("\n");
|
---|
202 | status.appendChild(t);
|
---|
203 | // once have got here, we assume
|
---|
204 | // the first bit proceeded successfully, now reload the collection
|
---|
205 | configureRequest("reload", coll_name, status, lang); // this will append more messages to the status, and overwrite the error code att
|
---|
206 | return response;
|
---|
207 |
|
---|
208 | }
|
---|
209 | protected Element processDeleteCollection(Element request) {
|
---|
210 |
|
---|
211 | // the response to send back
|
---|
212 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
213 | Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
214 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
215 | Element status = doc_.createElement(GSXML.STATUS_ELEM);
|
---|
216 | response.appendChild(status);
|
---|
217 | Text t = null; // the text node for the error/success message
|
---|
218 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
219 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
220 |
|
---|
221 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
222 | HashMap params = GSXML.extractParams(param_list);
|
---|
223 |
|
---|
224 | boolean get_status_only = false;
|
---|
225 | if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
|
---|
226 | get_status_only = true;
|
---|
227 | }
|
---|
228 | System.out.println("get status only = "+get_status_only);
|
---|
229 | if (get_status_only) {
|
---|
230 | // at the moment, delete is synchronous. but it may take ages so should do the command in another thread maybe? in which case we will want to ask for status
|
---|
231 | System.err.println("had a status request for delete - this shouldn't happen!!");
|
---|
232 | //t = doc_.createTextNode("");
|
---|
233 | //status.appendChild(t);
|
---|
234 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
235 | return response;
|
---|
236 | }
|
---|
237 | String coll_name = (String)params.get(COL_PARAM);
|
---|
238 | String[] args={coll_name};
|
---|
239 | File coll_dir = new File(GSFile.collectionBaseDir(site_home_, coll_name));
|
---|
240 | // check that the coll is there in the first place
|
---|
241 | if (!coll_dir.exists()) {
|
---|
242 | t = doc_.createTextNode(getTextString("delete.exists_error", args, lang));
|
---|
243 | status.appendChild(t);
|
---|
244 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
245 | return response;
|
---|
246 | }
|
---|
247 |
|
---|
248 | // try to delete the directory
|
---|
249 | if (!GSFile.deleteFile(coll_dir)) {
|
---|
250 | t = doc_.createTextNode(getTextString("delete.delete_error", args, lang));
|
---|
251 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
252 | status.appendChild(t);
|
---|
253 | return response;
|
---|
254 | }
|
---|
255 | configureRequest("delete", coll_name, status, lang);
|
---|
256 | return response;
|
---|
257 | }
|
---|
258 |
|
---|
259 | protected Element processReloadCollection(Element request) {
|
---|
260 |
|
---|
261 | // the response to send back
|
---|
262 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
263 | Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
264 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
265 | Element status = doc_.createElement(GSXML.STATUS_ELEM);
|
---|
266 | response.appendChild(status);
|
---|
267 | Text t = null; // the text node for the error/success message
|
---|
268 |
|
---|
269 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
270 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
271 |
|
---|
272 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
273 | HashMap params = GSXML.extractParams(param_list);
|
---|
274 |
|
---|
275 | boolean get_status_only = false;
|
---|
276 | if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
|
---|
277 | get_status_only = true;
|
---|
278 | }
|
---|
279 | if (get_status_only) {
|
---|
280 | // reload is synchronous - this makes no sense
|
---|
281 | System.err.println("had a status request for reload - this shouldn't happen!!");
|
---|
282 | //t = doc_.createTextNode("");
|
---|
283 | //status.appendChild(t);
|
---|
284 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
285 | return response;
|
---|
286 | }
|
---|
287 |
|
---|
288 | String coll_name = (String)params.get(COL_PARAM);
|
---|
289 | configureRequest("reload", coll_name, status, lang);
|
---|
290 | return response;
|
---|
291 |
|
---|
292 | }
|
---|
293 |
|
---|
294 | /** send a configure request to the message router
|
---|
295 | * action name should be "delete" or "reload"
|
---|
296 | * response will be put into the status element
|
---|
297 | */
|
---|
298 | protected void configureRequest(String action_name, String coll_name, Element status, String lang) {
|
---|
299 |
|
---|
300 | // send the request to the MR
|
---|
301 | Element message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
302 | Element request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
303 | request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_CONFIGURE);
|
---|
304 | request.setAttribute(GSXML.TO_ATT, "");
|
---|
305 | message.appendChild(request);
|
---|
306 | Element command = doc_.createElement(GSXML.CONFIGURE_ELEM);
|
---|
307 | command.setAttribute(GSXML.TYPE_ATT, GSXML.COLLECTION_ELEM);
|
---|
308 | command.setAttribute(GSXML.NAME_ATT, coll_name);
|
---|
309 | if (action_name.equals("delete")) {
|
---|
310 | command.setAttribute(GSXML.ACTION_ATT, GSXML.CONFIG_ACTION_DEACTIVATE);
|
---|
311 | } else if (action_name.equals("reload")) {
|
---|
312 | command.setAttribute(GSXML.ACTION_ATT, GSXML.CONFIG_ACTION_ACTIVATE);
|
---|
313 | } else {
|
---|
314 | System.err.println("invalid action name passed to configureRequest:"+action_name);
|
---|
315 | return;
|
---|
316 | }
|
---|
317 | request.appendChild(command);
|
---|
318 | Element response = router_.process(message); // at the moment, get no info in response so ignore it
|
---|
319 | Text t;
|
---|
320 | String[] args={coll_name};
|
---|
321 |
|
---|
322 | if (response == null) {
|
---|
323 | t = doc_.createTextNode(getTextString(action_name+".configure_error", args , lang));
|
---|
324 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
325 | status.appendChild(t);
|
---|
326 | return;
|
---|
327 | }
|
---|
328 |
|
---|
329 | // if we got here, we have succeeded!
|
---|
330 | t = doc_.createTextNode(getTextString(action_name+".success", args, lang));
|
---|
331 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.SUCCESS));
|
---|
332 | status.appendChild(t);
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | /** configure the service module
|
---|
337 | * for now, all services have type=build - need to think about this
|
---|
338 | */
|
---|
339 | public boolean configure(Element info) {
|
---|
340 | System.out.println("configuring GS2Construct");
|
---|
341 |
|
---|
342 | Element e = null;
|
---|
343 | // hard code in the services for now
|
---|
344 |
|
---|
345 | // set up short_service_info_ - for now just has name and type
|
---|
346 |
|
---|
347 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
348 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
349 | e.setAttribute(GSXML.NAME_ATT, NEW_SERVICE);
|
---|
350 | short_service_info_.appendChild(e);
|
---|
351 |
|
---|
352 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
353 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
354 | e.setAttribute(GSXML.NAME_ATT, IMPORT_SERVICE);
|
---|
355 | short_service_info_.appendChild(e);
|
---|
356 |
|
---|
357 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
358 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
359 | e.setAttribute(GSXML.NAME_ATT, BUILD_SERVICE);
|
---|
360 | short_service_info_.appendChild(e);
|
---|
361 |
|
---|
362 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
363 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
364 | e.setAttribute(GSXML.NAME_ATT, ACTIVATE_SERVICE);
|
---|
365 | short_service_info_.appendChild(e);
|
---|
366 |
|
---|
367 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
368 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
369 | e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE);
|
---|
370 | short_service_info_.appendChild(e);
|
---|
371 |
|
---|
372 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
373 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
374 | e.setAttribute(GSXML.NAME_ATT, RELOAD_SERVICE);
|
---|
375 | short_service_info_.appendChild(e);
|
---|
376 |
|
---|
377 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
378 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
379 | e.setAttribute(GSXML.NAME_ATT, ADD_DOC_SERVICE);
|
---|
380 | short_service_info_.appendChild(e);
|
---|
381 |
|
---|
382 | // set up service_info_map_
|
---|
383 | // should have full details about each service incl params lists etc.
|
---|
384 |
|
---|
385 | Element param_list;
|
---|
386 | Element param;
|
---|
387 |
|
---|
388 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
389 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
390 | e.setAttribute(GSXML.NAME_ATT, NEW_SERVICE);
|
---|
391 | param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
392 | param = GSXML.createParameter(doc_, NEW_COL_TITLE_PARAM, GSXML.PARAM_TYPE_STRING, "", null);
|
---|
393 | param_list.appendChild(param);
|
---|
394 | param = GSXML.createParameter(doc_, CREATOR_PARAM, GSXML.PARAM_TYPE_STRING, "", null);
|
---|
395 | param_list.appendChild(param);
|
---|
396 | param = GSXML.createParameter(doc_, NEW_COL_ABOUT_PARAM, GSXML.PARAM_TYPE_TEXT, "", null);
|
---|
397 | param_list.appendChild(param);
|
---|
398 | e.appendChild(param_list);
|
---|
399 | service_info_map_.put(NEW_SERVICE, e);
|
---|
400 |
|
---|
401 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
402 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
403 | e.setAttribute(GSXML.NAME_ATT, IMPORT_SERVICE);
|
---|
404 | param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
405 | param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
|
---|
406 | param_list.appendChild(param);
|
---|
407 | e.appendChild(param_list);
|
---|
408 | service_info_map_.put(IMPORT_SERVICE, e);
|
---|
409 |
|
---|
410 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
411 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
412 | e.setAttribute(GSXML.NAME_ATT, BUILD_SERVICE);
|
---|
413 | param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
414 | param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
|
---|
415 | param_list.appendChild(param);
|
---|
416 | e.appendChild(param_list);
|
---|
417 | service_info_map_.put(BUILD_SERVICE, e);
|
---|
418 |
|
---|
419 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
420 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
421 | e.setAttribute(GSXML.NAME_ATT, ACTIVATE_SERVICE);
|
---|
422 | param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
423 | param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
|
---|
424 | param_list.appendChild(param);
|
---|
425 | e.appendChild(param_list);
|
---|
426 | service_info_map_.put(ACTIVATE_SERVICE, e);
|
---|
427 |
|
---|
428 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
429 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
430 | e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE);
|
---|
431 | param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
432 | param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
|
---|
433 | param_list.appendChild(param);
|
---|
434 | e.appendChild(param_list);
|
---|
435 | service_info_map_.put(DELETE_SERVICE, e);
|
---|
436 |
|
---|
437 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
438 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
439 | e.setAttribute(GSXML.NAME_ATT, RELOAD_SERVICE);
|
---|
440 | param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
441 | param = GSXML.createParameter(doc_, COL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", null);
|
---|
442 | param_list.appendChild(param);
|
---|
443 | e.appendChild(param_list);
|
---|
444 | service_info_map_.put(RELOAD_SERVICE, e);
|
---|
445 |
|
---|
446 |
|
---|
447 | return true;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /** returns a response element */
|
---|
451 | protected Element runCommand(Element request, int type ) {
|
---|
452 |
|
---|
453 | // the response to send back
|
---|
454 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
455 | Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
456 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
457 | Element status = doc_.createElement(GSXML.STATUS_ELEM);
|
---|
458 | response.appendChild(status);
|
---|
459 |
|
---|
460 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
461 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
462 |
|
---|
463 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
464 | HashMap params = GSXML.extractParams(param_list);
|
---|
465 |
|
---|
466 | boolean get_status_only = false;
|
---|
467 | if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
|
---|
468 | get_status_only = true;
|
---|
469 | }
|
---|
470 | System.out.println("get status only = "+get_status_only);
|
---|
471 |
|
---|
472 | // just check for status messages if that's all that's required
|
---|
473 | if (get_status_only) {
|
---|
474 | System.out.println("in get status only bit");
|
---|
475 | String id = (String)params.get(HANDLE_PARAM);
|
---|
476 | GS2PerlListener listener = (GS2PerlListener)listeners_.get(id);
|
---|
477 | if (listener==null) {
|
---|
478 | Text t = doc_.createTextNode(getTextString("general.process_id_error", lang));
|
---|
479 | status.appendChild(t);
|
---|
480 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
481 | } else {
|
---|
482 | Text t = doc_.createTextNode(listener.getUpdate());
|
---|
483 | status.appendChild(t);
|
---|
484 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(listener.getStatus()));
|
---|
485 | // check that we actually should be removing the listener here
|
---|
486 | if (listener.isFinished()) { // remove this listener - its job is done
|
---|
487 | listeners_.remove(id); // not working
|
---|
488 | }
|
---|
489 | }
|
---|
490 | return response;
|
---|
491 |
|
---|
492 | }
|
---|
493 | System.out.println("not in get status only bit");
|
---|
494 |
|
---|
495 | // do teh actual command
|
---|
496 | String coll_name=null;
|
---|
497 | if (type==CollectionConstructor.NEW) {
|
---|
498 | String coll_title = (String)params.get(NEW_COL_TITLE_PARAM);
|
---|
499 | coll_name = createNewCollName(coll_title);
|
---|
500 | } else {
|
---|
501 | coll_name = (String)params.get(COL_PARAM);
|
---|
502 | }
|
---|
503 | System.err.println("Coll name = "+coll_name);
|
---|
504 | // makes a paramList of the relevant params
|
---|
505 | Element other_params = extractOtherParams(params, type);
|
---|
506 |
|
---|
507 | //create the constructor to do the work
|
---|
508 |
|
---|
509 | CollectionConstructor constructor = new GS2PerlConstructor("perl_build");
|
---|
510 | if (!constructor.configure()) {
|
---|
511 | Text t = doc_.createTextNode(getTextString("general.configure_constructor_error", lang));
|
---|
512 | status.appendChild(t);
|
---|
513 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
514 | return response;
|
---|
515 | }
|
---|
516 |
|
---|
517 | constructor.setSiteHome(site_home_);
|
---|
518 | constructor.setCollectionName(coll_name);
|
---|
519 | constructor.setActionType(type);
|
---|
520 | constructor.setProcessParams(other_params);
|
---|
521 | // the listener
|
---|
522 | GS2PerlListener listener = new GS2PerlListener();
|
---|
523 | constructor.addListener(listener);
|
---|
524 | constructor.start();
|
---|
525 |
|
---|
526 | String id = newID();
|
---|
527 | listeners_.put(id, listener);
|
---|
528 |
|
---|
529 | status.setAttribute(GSXML.STATUS_PROCESS_ID_ATT, id);
|
---|
530 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ACCEPTED));
|
---|
531 | Text t = doc_.createTextNode(getTextString("general.process_start", lang));
|
---|
532 | status.appendChild(t);
|
---|
533 | return response;
|
---|
534 |
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | //************************
|
---|
539 | // some helper functions
|
---|
540 | //************************
|
---|
541 |
|
---|
542 | /** parse the collect directory and return a list of collection names */
|
---|
543 | protected String[] getCollectionList() {
|
---|
544 |
|
---|
545 | File collectDir = new File(GSFile.collectDir(site_home_));
|
---|
546 | if (!collectDir.exists()) {
|
---|
547 | System.err.println("couldn't find collect dir: "+collectDir.toString());
|
---|
548 | return null;
|
---|
549 | }
|
---|
550 | System.out.println("GS2Construct: reading thru directory "+collectDir.getPath()+" to find collections.");
|
---|
551 | File[] contents = collectDir.listFiles();
|
---|
552 | int num_colls=0;
|
---|
553 | for (int i=0; i<contents.length;i++) {
|
---|
554 | if(contents[i].isDirectory() && !contents[i].getName().startsWith("CVS")) {
|
---|
555 | num_colls ++;
|
---|
556 | }
|
---|
557 | }
|
---|
558 |
|
---|
559 | String[] names= new String[num_colls];
|
---|
560 |
|
---|
561 | for (int i=0, j=0; i<contents.length;i++) {
|
---|
562 | if(contents[i].isDirectory()) {
|
---|
563 | String colName = contents[i].getName();
|
---|
564 | if (!colName.startsWith("CVS")) {
|
---|
565 | names[j] = colName;
|
---|
566 | j++;
|
---|
567 | }
|
---|
568 |
|
---|
569 | }
|
---|
570 | }
|
---|
571 |
|
---|
572 | return names;
|
---|
573 |
|
---|
574 | }
|
---|
575 |
|
---|
576 | /** ids used for process id */
|
---|
577 | private int current_id = 0;
|
---|
578 | private String newID() {
|
---|
579 | current_id++;
|
---|
580 | return Integer.toString(current_id);
|
---|
581 | }
|
---|
582 |
|
---|
583 | /** creates a new short name from the collection title */
|
---|
584 | protected String createNewCollName(String coll_title) {
|
---|
585 |
|
---|
586 | String base_name = null;
|
---|
587 | // take the first 6 letters
|
---|
588 | if (coll_title.length()<6) {
|
---|
589 | base_name = coll_title;
|
---|
590 | } else {
|
---|
591 | base_name = coll_title.substring(0,6);
|
---|
592 | }
|
---|
593 | File coll_dir = new File(GSFile.collectionBaseDir(site_home_, base_name));
|
---|
594 | if (!coll_dir.exists()) { // this name is ok - not used yet
|
---|
595 | return base_name;
|
---|
596 | }
|
---|
597 |
|
---|
598 | // now we have to make a new name until we get a good one
|
---|
599 | // try name1, name2 name3 etc
|
---|
600 | int i=0;
|
---|
601 | while(coll_dir.exists()) {
|
---|
602 | i++;
|
---|
603 | coll_dir = new File(GSFile.collectionBaseDir(site_home_, base_name+Integer.toString(i)));
|
---|
604 | }
|
---|
605 | return base_name+Integer.toString(i);
|
---|
606 |
|
---|
607 | }
|
---|
608 |
|
---|
609 | /** takes the params from the request (in the HashMap) and extracts any
|
---|
610 | * that need to be passed to the constructor and puts them into a
|
---|
611 | * paramList element */
|
---|
612 | protected Element extractOtherParams(HashMap params, int type) {
|
---|
613 |
|
---|
614 | Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
615 | if (type == CollectionConstructor.NEW) {
|
---|
616 | Element param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
617 | param.setAttribute(GSXML.NAME_ATT, "creator");
|
---|
618 | param.setAttribute(GSXML.VALUE_ATT, (String)params.get(CREATOR_PARAM));
|
---|
619 |
|
---|
620 | param_list.appendChild(param);
|
---|
621 | param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
622 | param.setAttribute(GSXML.NAME_ATT, "about");
|
---|
623 | param.setAttribute(GSXML.VALUE_ATT, (String)params.get(NEW_COL_ABOUT_PARAM));
|
---|
624 | param_list.appendChild(param);
|
---|
625 | param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
626 | param.setAttribute(GSXML.NAME_ATT, "title");
|
---|
627 | param.setAttribute(GSXML.VALUE_ATT, (String)params.get(NEW_COL_TITLE_PARAM));
|
---|
628 | param_list.appendChild(param);
|
---|
629 | return param_list;
|
---|
630 | }
|
---|
631 |
|
---|
632 | // other ones dont have params yet
|
---|
633 | return null;
|
---|
634 | }
|
---|
635 |
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|