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