source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/build/GS2PerlConstructor.java@ 24816

Last change on this file since 24816 was 24816, checked in by sjm84, 12 years ago

Reformatting this file ahead of some changes

  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1package org.greenstone.gsdl3.build;
2
3// greenstome classes
4import org.greenstone.gsdl3.util.*;
5
6// xml classes
7import org.w3c.dom.Element;
8import org.w3c.dom.NodeList;
9
10//general java classes
11import java.io.BufferedReader;
12import java.io.InputStreamReader;
13import java.io.File;
14import java.util.Vector;
15
16/**
17 * CollectionConstructor class for greenstone 2 compatible building it uses the
18 * perl scripts to do the building stuff
19 */
20public class GS2PerlConstructor extends CollectionConstructor
21{
22
23 public static final int NEW = 0;
24 public static final int IMPORT = 1;
25 public static final int BUILD = 2;
26 public static final int ACTIVATE = 3;
27
28 /**
29 * gsdlhome for greenstone 2 - we use the perl modules and building scripts
30 * from there
31 */
32 protected String gsdl2home = null;
33 /** gsdlhome for gsdl3 - shouldn't need this eventually ?? */
34 protected String gsdl3home = null;
35 /** gsdlos for greenstone 2 */
36 protected String gsdlos = null;
37 /** the path environment variable */
38 protected String path = null;
39
40 public GS2PerlConstructor(String name)
41 {
42 super(name);
43 }
44
45 /** retrieves the necessary environment variables */
46 public boolean configure()
47 {
48 // try to get the environment variables
49 this.gsdl2home = System.getProperty("GSDLHOME");
50 this.gsdl3home = System.getProperty("GSDL3HOME");
51 this.gsdlos = System.getProperty("GSDLOS");
52 this.path = System.getProperty("PATH");
53 if (this.gsdl2home == null)
54 {
55 System.err.println("You must have greenstone2 installed, and GSDLHOME set for GS2Perl building to work!!");
56 return false;
57 }
58 if (this.gsdl3home == null || this.gsdlos == null)
59 {
60 System.err.println("You must have GSDL3HOME and GSDLOS set for GS2Perl building to work!!");
61 return false;
62 }
63 if (this.path == null)
64 {
65 System.err.println("You must have the PATH set for GS2Perl building to work!!");
66 return false;
67 }
68 return true;
69 }
70
71 public void run()
72 {
73 String msg;
74 ConstructionEvent evt;
75 if (this.process_type == -1)
76 {
77 msg = "Error: you must set the action type";
78 evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
79 sendMessage(evt);
80 return;
81 }
82 if (this.site_home == null)
83 {
84 msg = "Error: you must set site_home";
85 evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
86 sendMessage(evt);
87 return;
88 }
89 if (this.process_type != NEW && this.collection_name == null)
90 {
91 msg = "Error: you must set collection_name";
92 evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
93 sendMessage(evt);
94 return;
95 }
96
97 switch (this.process_type)
98 {
99 case NEW:
100 newCollection();
101 break;
102 case IMPORT:
103 importCollection();
104 break;
105 case BUILD:
106 buildCollection();
107 break;
108 case ACTIVATE:
109 activateCollection();
110 break;
111 default:
112 msg = "wrong type of action specified!";
113 evt = new ConstructionEvent(this, GSStatus.ERROR, msg);
114 sendMessage(evt);
115 break;
116 }
117
118 }
119
120 protected void newCollection()
121 {
122 sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: new collection."));
123 Vector command = new Vector();
124 command.add("gs2_mkcol.pl");
125 command.add("-site");
126 command.add(this.site_home);
127 command.add("-collectdir");
128 command.add(GSFile.collectDir(this.site_home));
129 command.addAll(extractParameters(this.process_params));
130 command.add(this.collection_name);
131 String[] command_str = {};
132 command_str = (String[]) command.toArray(command_str);
133 if (runPerlCommand(command_str))
134 {
135 // success!! - need to send the final completed message
136 sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
137 } // else an error message has already been sent, do nothing
138
139 }
140
141 protected void importCollection()
142 {
143 sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: import collection."));
144 Vector command = new Vector();
145 command.add("import.pl");
146 command.add("-collectdir");
147 command.add(GSFile.collectDir(this.site_home));
148 command.addAll(extractParameters(this.process_params));
149 command.add(this.collection_name);
150 String[] command_str = {};
151 command_str = (String[]) command.toArray(command_str);
152
153 if (runPerlCommand(command_str))
154 {
155 // success!! - need to send the final completed message
156 sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
157 } // else an error message has already been sent, do nothing
158 }
159
160 protected void buildCollection()
161 {
162 sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: build collection."));
163 Vector command = new Vector();
164 command.add("buildcol.pl");
165 command.add("-collectdir");
166 command.add(GSFile.collectDir(this.site_home));
167 command.addAll(extractParameters(this.process_params));
168 command.add(this.collection_name);
169 String[] command_str = {};
170 command_str = (String[]) command.toArray(command_str);
171
172 if (runPerlCommand(command_str))
173 {
174 // success!! - need to send the final completed message
175 sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
176 }// else an error message has already been sent, do nothing
177 //
178
179 }
180
181 protected void activateCollection()
182 {
183 sendMessage(new ConstructionEvent(this, GSStatus.INFO, "Collection construction: activate collection."));
184
185 // first check that we have a building directory
186 File build_dir = new File(GSFile.collectionBuildDir(this.site_home, this.collection_name));
187 if (!build_dir.exists())
188 {
189 sendMessage(new ConstructionEvent(this, GSStatus.ERROR, "build dir doesn't exist!"));
190 return;
191 }
192
193 // move building to index
194 File index_dir = new File(GSFile.collectionIndexDir(this.site_home, this.collection_name));
195 if (index_dir.exists())
196 {
197 sendMessage(new ConstructionEvent(this, GSStatus.INFO, "deleting index directory"));
198 index_dir.delete();
199 if (index_dir.exists())
200 {
201 sendMessage(new ConstructionEvent(this, GSStatus.INFO, "index directory still exists!"));
202 }
203 }
204
205 GSFile.moveDirectory(build_dir, index_dir);
206 if (!index_dir.exists())
207 {
208 sendMessage(new ConstructionEvent(this, GSStatus.ERROR, "index dir wasn't created!"));
209 }
210
211 // run the convert coll script to convert the gs2 coll to gs3
212 Vector command = new Vector();
213 command.add("convert_coll_from_gs2.pl");
214 command.add("-collectdir");
215 command.add(GSFile.collectDir(this.site_home));
216 command.addAll(extractParameters(this.process_params));
217 command.add(this.collection_name);
218 String[] command_str = {};
219 command_str = (String[]) command.toArray(command_str);
220
221 if (!runPerlCommand(command_str))
222 {
223 // an error has occurred, dont carry on
224 return;
225 }
226
227 // success!! - need to send the final completed message
228 sendProcessComplete(new ConstructionEvent(this, GSStatus.COMPLETED, ""));
229 }
230
231 /** extracts all the args from the xml and returns them in a Vector */
232 protected Vector extractParameters(Element param_list)
233 {
234
235 Vector args = new Vector();
236 if (param_list == null)
237 {
238 return args; // return an empty vector
239 }
240 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
241
242 for (int i = 0; i < params.getLength(); i++)
243 {
244 Element p = (Element) params.item(i);
245 String name = p.getAttribute(GSXML.NAME_ATT);
246 String value = p.getAttribute(GSXML.VALUE_ATT);
247 if (!name.equals(""))
248 {
249 args.add("-" + name);
250 if (!value.equals(""))
251 {
252 args.add(value);
253 }
254 }
255 }
256
257 return args;
258 }
259
260 /** returns true if completed correctly, false otherwise */
261 protected boolean runPerlCommand(String[] command)
262 {
263 String msg;
264 ConstructionEvent evt;
265
266 String args[] = { "GSDLHOME=" + this.gsdl2home, "GSDL3HOME=" + this.gsdl3home, "GSDLOS=" + this.gsdlos, "PATH=" + this.path };
267 String command_str = "";
268 for (int i = 0; i < command.length; i++)
269 {
270 command_str = command_str + command[i] + " ";
271 }
272 sendMessage(new ConstructionEvent(this, GSStatus.INFO, "command = " + command_str));
273 try
274 {
275 Runtime rt = Runtime.getRuntime();
276 sendProcessBegun(new ConstructionEvent(this, GSStatus.ACCEPTED, "starting"));
277 Process prcs = rt.exec(command, args);
278
279 InputStreamReader eisr = new InputStreamReader(prcs.getErrorStream());
280 InputStreamReader stdinisr = new InputStreamReader(prcs.getInputStream());
281 BufferedReader ebr = new BufferedReader(eisr);
282 BufferedReader stdinbr = new BufferedReader(stdinisr);
283 // Captures the std err of a program and pipes it into
284 // std in of java
285 String eline = null;
286 String stdinline = null;
287 while (((eline = ebr.readLine()) != null || (stdinline = stdinbr.readLine()) != null) && !this.cancel)
288 {
289 if (eline != null)
290 {
291 sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, eline));
292 }
293 if (stdinline != null)
294 {
295 sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, stdinline));
296 }
297 }
298 if (!this.cancel)
299 {
300 // Now display final message based on exit value
301 prcs.waitFor();
302
303 if (prcs.exitValue() == 0)
304 {
305 //status = OK;
306 sendProcessStatus(new ConstructionEvent(this, GSStatus.CONTINUING, "Success"));
307
308 }
309 else
310 {
311 //status = ERROR;
312 sendProcessStatus(new ConstructionEvent(this, GSStatus.ERROR, "Failure"));
313
314 return false;
315
316 }
317 }
318 else
319 {
320 // I need to somehow kill the child process. Unfortunately Thread.stop() and Process.destroy() both fail to do this. But now, thankx to the magic of Michaels 'close the stream suggestion', it works fine.
321 sendProcessStatus(new ConstructionEvent(this, GSStatus.HALTED, "killing the process"));
322 prcs.getOutputStream().close();
323 prcs.destroy();
324 //status = ERROR;
325 return false;
326 }
327
328 }
329 catch (Exception e)
330 {
331 sendProcessStatus(new ConstructionEvent(this, GSStatus.ERROR, "Exception occurred " + e.toString()));
332 }
333
334 // we're done, but we dont send a process complete message here cos there ight be stuff to do after this has finished.
335 return true;
336
337 }
338
339}
Note: See TracBrowser for help on using the repository browser.