source: trunk/gsdl3/src/java/org/greenstone/gsdl3/build/GS2PerlConstructor.java@ 9886

Last change on this file since 9886 was 6270, checked in by kjdon, 20 years ago

added in GSDL3HOME to the perl args

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