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