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: 5098 $
|
---|
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 PROCESS_ID_PARAM = GSCGI.PROCESS_ID_ARG;
|
---|
64 | private static final String BUILDTYPE_PARAM = "buildType";
|
---|
65 | private static final String BUILDTYPE_MG = "mg";
|
---|
66 | private static final String BUILDTYPE_MGPP = "mgpp";
|
---|
67 |
|
---|
68 | // the list of the collections - store between some method calls
|
---|
69 | private String[] collection_list = null;
|
---|
70 |
|
---|
71 | // set of listeners for any construction commands
|
---|
72 | protected Map listeners = null;
|
---|
73 |
|
---|
74 | public GS2Construct() {
|
---|
75 | this.listeners = Collections.synchronizedMap(new HashMap());
|
---|
76 |
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** returns a specific service description */
|
---|
80 | protected Element getServiceDescription(String service, String lang) {
|
---|
81 |
|
---|
82 | Element description = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
83 | description.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
84 | description.setAttribute(GSXML.NAME_ATT, service);
|
---|
85 | description.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(service+".name", lang)));
|
---|
86 | description.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(service+".submit", lang)));
|
---|
87 |
|
---|
88 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
89 | description.appendChild(param_list);
|
---|
90 |
|
---|
91 | if (service.equals(NEW_SERVICE)) {
|
---|
92 |
|
---|
93 | Element param = GSXML.createParameterDescription(this.doc, NEW_COL_TITLE_PARAM, getTextString("param."+NEW_COL_TITLE_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
|
---|
94 | param_list.appendChild(param);
|
---|
95 | param = GSXML.createParameterDescription(this.doc, CREATOR_PARAM, getTextString("param."+CREATOR_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
|
---|
96 | param_list.appendChild(param);
|
---|
97 | param = GSXML.createParameterDescription(this.doc, NEW_COL_ABOUT_PARAM, getTextString("param."+NEW_COL_ABOUT_PARAM, lang), GSXML.PARAM_TYPE_TEXT, null, null, null);
|
---|
98 | param_list.appendChild(param);
|
---|
99 | String[] types = {BUILDTYPE_MGPP, BUILDTYPE_MG};
|
---|
100 | String[] type_texts = {getTextString("param."+BUILDTYPE_PARAM+"."+BUILDTYPE_MGPP, lang), getTextString("param."+BUILDTYPE_PARAM+"."+BUILDTYPE_MG, lang)};
|
---|
101 |
|
---|
102 | param = GSXML.createParameterDescription(this.doc, BUILDTYPE_PARAM, getTextString("param."+BUILDTYPE_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, BUILDTYPE_MGPP, types, type_texts);
|
---|
103 | param_list.appendChild(param);
|
---|
104 | return description;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | if (service.equals(ACTIVATE_SERVICE) || service.equals(IMPORT_SERVICE) || service.equals(BUILD_SERVICE) || service.equals(RELOAD_SERVICE) || service.equals(DELETE_SERVICE)) {
|
---|
109 |
|
---|
110 | this.collection_list = getCollectionList();
|
---|
111 | Element param = GSXML.createParameterDescription(this.doc, COL_PARAM, getTextString("param."+COL_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, null, this.collection_list, this.collection_list);
|
---|
112 | param_list.appendChild(param);
|
---|
113 |
|
---|
114 | return description;
|
---|
115 | }
|
---|
116 | // invalid service name
|
---|
117 | return null;
|
---|
118 |
|
---|
119 | }
|
---|
120 |
|
---|
121 | // each service must have a method "process<New service name>"
|
---|
122 |
|
---|
123 | protected Element processNewCollection(Element request) {
|
---|
124 | return runCommand(request, GS2PerlConstructor.NEW);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** TODO:implement this */
|
---|
128 | protected Element processAddDocument(Element request) {
|
---|
129 | // decode the file name, add it to the import directory
|
---|
130 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
131 | Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
132 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
133 | Element status = this.doc.createElement(GSXML.STATUS_ELEM);
|
---|
134 | response.appendChild(status);
|
---|
135 | //String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
136 | //String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
137 | Text t = this.doc.createTextNode("AddDocument: not implemented yet");
|
---|
138 | status.appendChild(t);
|
---|
139 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
140 | return response;
|
---|
141 | }
|
---|
142 | protected Element processImportCollection(Element request) {
|
---|
143 | return runCommand(request, GS2PerlConstructor.IMPORT);
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected Element processBuildCollection(Element request) {
|
---|
147 | return runCommand(request, GS2PerlConstructor.BUILD);
|
---|
148 | }
|
---|
149 | protected Element processActivateCollection(Element request) {
|
---|
150 | Element response = runCommand(request, GS2PerlConstructor.ACTIVATE);
|
---|
151 | // this activates the collection on disk. but now we need to tell
|
---|
152 | // the MR about it. but we have to wait until the process is finished.
|
---|
153 | Element status = (Element)GSXML.getChildByTagName(response, GSXML.STATUS_ELEM);
|
---|
154 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
155 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
156 | String coll_name = (String) params.get(COL_PARAM);
|
---|
157 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
158 | // check for finished
|
---|
159 | int status_code = Integer.parseInt(status.getAttribute(GSXML.STATUS_ERROR_CODE_ATT));
|
---|
160 | if (GSStatus.isCompleted(status_code) &&
|
---|
161 | GSStatus.isError(status_code)) {
|
---|
162 | // we shouldn't carry out the next bit, just return the response
|
---|
163 | return response;
|
---|
164 | }
|
---|
165 | String id = status.getAttribute(GSXML.STATUS_PROCESS_ID_ATT);
|
---|
166 | GS2PerlListener listener = (GS2PerlListener)this.listeners.get(id);
|
---|
167 | if (listener==null) {
|
---|
168 | System.err.println("somethings gone wrong, couldn't find the listener");
|
---|
169 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
170 | return response;
|
---|
171 | }
|
---|
172 | while (!GSStatus.isCompleted(status_code)) {
|
---|
173 | // wait for the process, and keep checking the status code
|
---|
174 | // there is probably a better way to do this.
|
---|
175 | try {
|
---|
176 | Thread.currentThread().sleep(100);
|
---|
177 | } catch (Exception e) { // ignore
|
---|
178 | }
|
---|
179 | status_code = listener.getStatus();
|
---|
180 | }
|
---|
181 |
|
---|
182 | // add the rest of the messages to the status node
|
---|
183 | Text t = this.doc.createTextNode("\n"+listener.getUpdate());
|
---|
184 | status.appendChild(t);
|
---|
185 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(listener.getStatus()));
|
---|
186 | if (GSStatus.isError(status_code)) {
|
---|
187 | return response; // without doing the next bit
|
---|
188 | }
|
---|
189 |
|
---|
190 | t = this.doc.createTextNode("\n");
|
---|
191 | status.appendChild(t);
|
---|
192 | // once have got here, we assume
|
---|
193 | // the first bit proceeded successfully, now reload the collection
|
---|
194 | configureRequest("reload", coll_name, status, lang); // this will append more messages to the status, and overwrite the error code att
|
---|
195 | return response;
|
---|
196 |
|
---|
197 | }
|
---|
198 | protected Element processDeleteCollection(Element request) {
|
---|
199 |
|
---|
200 | // the response to send back
|
---|
201 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
202 | Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
203 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
204 | Element status = this.doc.createElement(GSXML.STATUS_ELEM);
|
---|
205 | response.appendChild(status);
|
---|
206 | Text t = null; // the text node for the error/success message
|
---|
207 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
208 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
209 |
|
---|
210 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
211 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
212 |
|
---|
213 | boolean get_status_only = false;
|
---|
214 | if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
|
---|
215 | get_status_only = true;
|
---|
216 | }
|
---|
217 | if (get_status_only) {
|
---|
218 | // 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
|
---|
219 | System.err.println("had a status request for delete - this shouldn't happen!!");
|
---|
220 | //t = doc_.createTextNode("");
|
---|
221 | //status.appendChild(t);
|
---|
222 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
223 | return response;
|
---|
224 | }
|
---|
225 | String coll_name = (String)params.get(COL_PARAM);
|
---|
226 | String[] args={coll_name};
|
---|
227 | File coll_dir = new File(GSFile.collectionBaseDir(this.site_home, coll_name));
|
---|
228 | // check that the coll is there in the first place
|
---|
229 | if (!coll_dir.exists()) {
|
---|
230 | t = this.doc.createTextNode(getTextString("delete.exists_error", args, lang));
|
---|
231 | status.appendChild(t);
|
---|
232 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
233 | return response;
|
---|
234 | }
|
---|
235 |
|
---|
236 | // try to delete the directory
|
---|
237 | if (!GSFile.deleteFile(coll_dir)) {
|
---|
238 | t = this.doc.createTextNode(getTextString("delete.delete_error", args, lang));
|
---|
239 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
240 | status.appendChild(t);
|
---|
241 | return response;
|
---|
242 | }
|
---|
243 | configureRequest("delete", coll_name, status, lang);
|
---|
244 | return response;
|
---|
245 | }
|
---|
246 |
|
---|
247 | protected Element processReloadCollection(Element request) {
|
---|
248 |
|
---|
249 | // the response to send back
|
---|
250 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
251 | Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
252 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
253 | Element status = this.doc.createElement(GSXML.STATUS_ELEM);
|
---|
254 | response.appendChild(status);
|
---|
255 | Text t = null; // the text node for the error/success message
|
---|
256 |
|
---|
257 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
258 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
259 |
|
---|
260 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
261 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
262 |
|
---|
263 | boolean get_status_only = false;
|
---|
264 | if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
|
---|
265 | get_status_only = true;
|
---|
266 | }
|
---|
267 | if (get_status_only) {
|
---|
268 | // reload is synchronous - this makes no sense
|
---|
269 | System.err.println("had a status request for reload - this shouldn't happen!!");
|
---|
270 | //t = doc_.createTextNode("");
|
---|
271 | //status.appendChild(t);
|
---|
272 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
273 | return response;
|
---|
274 | }
|
---|
275 |
|
---|
276 | String coll_name = (String)params.get(COL_PARAM);
|
---|
277 | configureRequest("reload", coll_name, status, lang);
|
---|
278 | return response;
|
---|
279 |
|
---|
280 | }
|
---|
281 |
|
---|
282 | /** send a configure request to the message router
|
---|
283 | * action name should be "delete" or "reload"
|
---|
284 | * response will be put into the status element
|
---|
285 | */
|
---|
286 | protected void configureRequest(String action_name, String coll_name, Element status, String lang) {
|
---|
287 |
|
---|
288 | // send the request to the MR
|
---|
289 | Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
290 | Element request = this.doc.createElement(GSXML.REQUEST_ELEM);
|
---|
291 | request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_CONFIGURE);
|
---|
292 | request.setAttribute(GSXML.TO_ATT, "");
|
---|
293 | message.appendChild(request);
|
---|
294 | Element command = this.doc.createElement(GSXML.CONFIGURE_ELEM);
|
---|
295 | command.setAttribute(GSXML.TYPE_ATT, GSXML.COLLECTION_ELEM);
|
---|
296 | command.setAttribute(GSXML.NAME_ATT, coll_name);
|
---|
297 | if (action_name.equals("delete")) {
|
---|
298 | command.setAttribute(GSXML.ACTION_ATT, GSXML.CONFIG_ACTION_DEACTIVATE);
|
---|
299 | } else if (action_name.equals("reload")) {
|
---|
300 | command.setAttribute(GSXML.ACTION_ATT, GSXML.CONFIG_ACTION_ACTIVATE);
|
---|
301 | } else {
|
---|
302 | System.err.println("invalid action name passed to configureRequest:"+action_name);
|
---|
303 | return;
|
---|
304 | }
|
---|
305 | request.appendChild(command);
|
---|
306 | Element response = this.router.process(message); // at the moment, get no info in response so ignore it
|
---|
307 | Text t;
|
---|
308 | String[] args={coll_name};
|
---|
309 |
|
---|
310 | if (response == null) {
|
---|
311 | t = this.doc.createTextNode(getTextString(action_name+".configure_error", args , lang));
|
---|
312 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
313 | status.appendChild(t);
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | // if we got here, we have succeeded!
|
---|
318 | t = this.doc.createTextNode(getTextString(action_name+".success", args, lang));
|
---|
319 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.SUCCESS));
|
---|
320 | status.appendChild(t);
|
---|
321 | }
|
---|
322 |
|
---|
323 |
|
---|
324 | /** configure the service module
|
---|
325 | * for now, all services have type=build - need to think about this
|
---|
326 | */
|
---|
327 | public boolean configure(Element info, Element extra_info) {
|
---|
328 | System.out.println("configuring GS2Construct");
|
---|
329 |
|
---|
330 | Element e = null;
|
---|
331 | // hard code in the services for now
|
---|
332 |
|
---|
333 | // set up short_service_info_ - for now just has name and type
|
---|
334 |
|
---|
335 | e = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
336 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
337 | e.setAttribute(GSXML.NAME_ATT, NEW_SERVICE);
|
---|
338 | this.short_service_info.appendChild(e);
|
---|
339 |
|
---|
340 | e = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
341 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
342 | e.setAttribute(GSXML.NAME_ATT, IMPORT_SERVICE);
|
---|
343 | this.short_service_info.appendChild(e);
|
---|
344 |
|
---|
345 | e = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
346 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
347 | e.setAttribute(GSXML.NAME_ATT, BUILD_SERVICE);
|
---|
348 | this.short_service_info.appendChild(e);
|
---|
349 |
|
---|
350 | e = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
351 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
352 | e.setAttribute(GSXML.NAME_ATT, ACTIVATE_SERVICE);
|
---|
353 | this.short_service_info.appendChild(e);
|
---|
354 |
|
---|
355 | e = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
356 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
357 | e.setAttribute(GSXML.NAME_ATT, DELETE_SERVICE);
|
---|
358 | this.short_service_info.appendChild(e);
|
---|
359 |
|
---|
360 | e = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
361 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
362 | e.setAttribute(GSXML.NAME_ATT, RELOAD_SERVICE);
|
---|
363 | this.short_service_info.appendChild(e);
|
---|
364 |
|
---|
365 | //e = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
366 | //e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
|
---|
367 | //e.setAttribute(GSXML.NAME_ATT, ADD_DOC_SERVICE);
|
---|
368 | //this.short_service_info.appendChild(e);
|
---|
369 |
|
---|
370 |
|
---|
371 | return true;
|
---|
372 | }
|
---|
373 |
|
---|
374 | /** returns a response element */
|
---|
375 | protected Element runCommand(Element request, int type ) {
|
---|
376 |
|
---|
377 | // the response to send back
|
---|
378 | String name = GSPath.getFirstLink(request.getAttribute(GSXML.TO_ATT));
|
---|
379 | Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
380 | response.setAttribute(GSXML.FROM_ATT, name);
|
---|
381 | Element status = this.doc.createElement(GSXML.STATUS_ELEM);
|
---|
382 | response.appendChild(status);
|
---|
383 |
|
---|
384 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
385 | String request_type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
386 |
|
---|
387 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
388 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
389 |
|
---|
390 | boolean get_status_only = false;
|
---|
391 | if (request_type.equals(GSXML.REQUEST_TYPE_STATUS)) {
|
---|
392 | get_status_only = true;
|
---|
393 | }
|
---|
394 |
|
---|
395 | // just check for status messages if that's all that's required
|
---|
396 | if (get_status_only) {
|
---|
397 | String id = (String)params.get(PROCESS_ID_PARAM);
|
---|
398 | status.setAttribute(GSXML.STATUS_PROCESS_ID_ATT, id);
|
---|
399 | GS2PerlListener listener = (GS2PerlListener)this.listeners.get(id);
|
---|
400 | if (listener==null) {
|
---|
401 | Text t = this.doc.createTextNode(getTextString("general.process_id_error", lang));
|
---|
402 | status.appendChild(t);
|
---|
403 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
404 | } else {
|
---|
405 | Text t = this.doc.createTextNode(listener.getUpdate());
|
---|
406 | status.appendChild(t);
|
---|
407 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(listener.getStatus()));
|
---|
408 | // check that we actually should be removing the listener here
|
---|
409 | if (listener.isFinished()) { // remove this listener - its job is done
|
---|
410 | this.listeners.remove(id); // not working
|
---|
411 | }
|
---|
412 | }
|
---|
413 | return response;
|
---|
414 |
|
---|
415 | }
|
---|
416 |
|
---|
417 | // do teh actual command
|
---|
418 | String coll_name=null;
|
---|
419 | if (type==GS2PerlConstructor.NEW) {
|
---|
420 | String coll_title = (String)params.get(NEW_COL_TITLE_PARAM);
|
---|
421 | coll_name = createNewCollName(coll_title);
|
---|
422 | } else {
|
---|
423 | coll_name = (String)params.get(COL_PARAM);
|
---|
424 | }
|
---|
425 | System.err.println("Coll name = "+coll_name);
|
---|
426 | // makes a paramList of the relevant params
|
---|
427 | Element other_params = extractOtherParams(params, type);
|
---|
428 |
|
---|
429 | //create the constructor to do the work
|
---|
430 |
|
---|
431 | GS2PerlConstructor constructor = new GS2PerlConstructor("perl_build");
|
---|
432 | if (!constructor.configure()) {
|
---|
433 | Text t = this.doc.createTextNode(getTextString("general.configure_constructor_error", lang));
|
---|
434 | status.appendChild(t);
|
---|
435 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ERROR));
|
---|
436 | return response;
|
---|
437 | }
|
---|
438 |
|
---|
439 | constructor.setSiteHome(this.site_home);
|
---|
440 | constructor.setCollectionName(coll_name);
|
---|
441 | constructor.setActionType(type);
|
---|
442 | constructor.setProcessParams(other_params);
|
---|
443 | // the listener
|
---|
444 | GS2PerlListener listener = new GS2PerlListener();
|
---|
445 | constructor.addListener(listener);
|
---|
446 | constructor.start();
|
---|
447 |
|
---|
448 | String id = newID();
|
---|
449 | this.listeners.put(id, listener);
|
---|
450 |
|
---|
451 | status.setAttribute(GSXML.STATUS_PROCESS_ID_ATT, id);
|
---|
452 | status.setAttribute(GSXML.STATUS_ERROR_CODE_ATT, Integer.toString(GSStatus.ACCEPTED));
|
---|
453 | Text t = this.doc.createTextNode(getTextString("general.process_start", lang));
|
---|
454 | status.appendChild(t);
|
---|
455 | return response;
|
---|
456 |
|
---|
457 | }
|
---|
458 |
|
---|
459 |
|
---|
460 | //************************
|
---|
461 | // some helper functions
|
---|
462 | //************************
|
---|
463 |
|
---|
464 | /** parse the collect directory and return a list of collection names */
|
---|
465 | protected String[] getCollectionList() {
|
---|
466 |
|
---|
467 | File collectDir = new File(GSFile.collectDir(this.site_home));
|
---|
468 | if (!collectDir.exists()) {
|
---|
469 | System.err.println("couldn't find collect dir: "+collectDir.toString());
|
---|
470 | return null;
|
---|
471 | }
|
---|
472 | System.out.println("GS2Construct: reading thru directory "+collectDir.getPath()+" to find collections.");
|
---|
473 | File[] contents = collectDir.listFiles();
|
---|
474 | int num_colls=0;
|
---|
475 | for (int i=0; i<contents.length;i++) {
|
---|
476 | if(contents[i].isDirectory() && !contents[i].getName().startsWith("CVS")) {
|
---|
477 | num_colls ++;
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | String[] names= new String[num_colls];
|
---|
482 |
|
---|
483 | for (int i=0, j=0; i<contents.length;i++) {
|
---|
484 | if(contents[i].isDirectory()) {
|
---|
485 | String colName = contents[i].getName();
|
---|
486 | if (!colName.startsWith("CVS")) {
|
---|
487 | names[j] = colName;
|
---|
488 | j++;
|
---|
489 | }
|
---|
490 |
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | return names;
|
---|
495 |
|
---|
496 | }
|
---|
497 |
|
---|
498 | /** ids used for process id */
|
---|
499 | private int current_id = 0;
|
---|
500 | private String newID() {
|
---|
501 | current_id++;
|
---|
502 | return Integer.toString(current_id);
|
---|
503 | }
|
---|
504 |
|
---|
505 | /** creates a new short name from the collection title */
|
---|
506 | protected String createNewCollName(String coll_title) {
|
---|
507 |
|
---|
508 | String base_name = null;
|
---|
509 | // take the first 6 letters
|
---|
510 | if (coll_title.length()<6) {
|
---|
511 | base_name = coll_title;
|
---|
512 | } else {
|
---|
513 | base_name = coll_title.substring(0,6);
|
---|
514 | }
|
---|
515 | File coll_dir = new File(GSFile.collectionBaseDir(this.site_home, base_name));
|
---|
516 | if (!coll_dir.exists()) { // this name is ok - not used yet
|
---|
517 | return base_name;
|
---|
518 | }
|
---|
519 |
|
---|
520 | // now we have to make a new name until we get a good one
|
---|
521 | // try name1, name2 name3 etc
|
---|
522 | int i=0;
|
---|
523 | while(coll_dir.exists()) {
|
---|
524 | i++;
|
---|
525 | coll_dir = new File(GSFile.collectionBaseDir(this.site_home, base_name+Integer.toString(i)));
|
---|
526 | }
|
---|
527 | return base_name+Integer.toString(i);
|
---|
528 |
|
---|
529 | }
|
---|
530 |
|
---|
531 | /** takes the params from the request (in the HashMap) and extracts any
|
---|
532 | * that need to be passed to the constructor and puts them into a
|
---|
533 | * paramList element */
|
---|
534 | protected Element extractOtherParams(HashMap params, int type) {
|
---|
535 |
|
---|
536 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
537 | if (type == GS2PerlConstructor.NEW) {
|
---|
538 | Element param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
539 | param.setAttribute(GSXML.NAME_ATT, "creator");
|
---|
540 | param.setAttribute(GSXML.VALUE_ATT, (String)params.get(CREATOR_PARAM));
|
---|
541 |
|
---|
542 | param_list.appendChild(param);
|
---|
543 | param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
544 | param.setAttribute(GSXML.NAME_ATT, "about");
|
---|
545 | param.setAttribute(GSXML.VALUE_ATT, (String)params.get(NEW_COL_ABOUT_PARAM));
|
---|
546 | param_list.appendChild(param);
|
---|
547 | param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
548 | param.setAttribute(GSXML.NAME_ATT, "title");
|
---|
549 | param.setAttribute(GSXML.VALUE_ATT, (String)params.get(NEW_COL_TITLE_PARAM));
|
---|
550 | param_list.appendChild(param);
|
---|
551 | param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
552 | param.setAttribute(GSXML.NAME_ATT, "buildtype");
|
---|
553 | param.setAttribute(GSXML.VALUE_ATT, (String)params.get(BUILDTYPE_PARAM));
|
---|
554 | param_list.appendChild(param);
|
---|
555 | return param_list;
|
---|
556 | }
|
---|
557 |
|
---|
558 | // other ones dont have params yet
|
---|
559 | return null;
|
---|
560 | }
|
---|
561 |
|
---|
562 | }
|
---|
563 |
|
---|
564 |
|
---|