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

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

Updated the import command to include the ability to add a manifest file

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