KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > Rpm


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional;
19
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Vector JavaDoc;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.Task;
31 import org.apache.tools.ant.util.FileUtils;
32 import org.apache.tools.ant.taskdefs.Execute;
33 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
34 import org.apache.tools.ant.taskdefs.LogOutputStream;
35 import org.apache.tools.ant.taskdefs.LogStreamHandler;
36 import org.apache.tools.ant.taskdefs.PumpStreamHandler;
37 import org.apache.tools.ant.taskdefs.condition.Os;
38 import org.apache.tools.ant.types.Commandline;
39 import org.apache.tools.ant.types.Path;
40
41 /**
42  * Invokes the rpm tool to build a Linux installation file.
43  *
44  */

45 public class Rpm extends Task {
46
47     private static final String JavaDoc PATH1 = "PATH=";
48     private static final String JavaDoc PATH2 = "Path=";
49     private static final String JavaDoc PATH3 = "path=";
50     private static final int PATH_LEN = PATH1.length();
51
52     /**
53      * the spec file
54      */

55     private String JavaDoc specFile;
56
57     /**
58      * the rpm top dir
59      */

60     private File JavaDoc topDir;
61
62     /**
63      * the rpm command to use
64      */

65     private String JavaDoc command = "-bb";
66
67     /**
68      * The executable to use for building the packages.
69      * @since Ant 1.6
70      */

71     private String JavaDoc rpmBuildCommand = null;
72
73     /**
74      * clean BUILD directory
75      */

76     private boolean cleanBuildDir = false;
77
78     /**
79      * remove spec file
80      */

81     private boolean removeSpec = false;
82
83     /**
84      * remove sources
85      */

86     private boolean removeSource = false;
87
88     /**
89      * the file to direct standard output from the command
90      */

91     private File JavaDoc output;
92
93     /**
94      * the file to direct standard error from the command
95      */

96     private File JavaDoc error;
97
98     /**
99      * Halt on error return value from rpm build.
100      */

101     private boolean failOnError = false;
102
103     /**
104      * Don't show output of RPM build command on console. This does not affect
105      * the printing of output and error messages to files.
106      */

107     private boolean quiet = false;
108
109     /**
110      * Execute the task
111      *
112      * @throws BuildException is there is a problem in the task execution.
113      */

114     public void execute() throws BuildException {
115
116         Commandline toExecute = new Commandline();
117
118         toExecute.setExecutable(rpmBuildCommand == null
119                                 ? guessRpmBuildCommand()
120                                 : rpmBuildCommand);
121         if (topDir != null) {
122             toExecute.createArgument().setValue("--define");
123             toExecute.createArgument().setValue("_topdir" + topDir);
124         }
125
126         toExecute.createArgument().setLine(command);
127
128         if (cleanBuildDir) {
129             toExecute.createArgument().setValue("--clean");
130         }
131         if (removeSpec) {
132             toExecute.createArgument().setValue("--rmspec");
133         }
134         if (removeSource) {
135             toExecute.createArgument().setValue("--rmsource");
136         }
137
138         toExecute.createArgument().setValue("SPECS/" + specFile);
139
140         ExecuteStreamHandler streamhandler = null;
141         OutputStream JavaDoc outputstream = null;
142         OutputStream JavaDoc errorstream = null;
143         if (error == null && output == null) {
144             if (!quiet) {
145                 streamhandler = new LogStreamHandler(this, Project.MSG_INFO,
146                                                      Project.MSG_WARN);
147             } else {
148                 streamhandler = new LogStreamHandler(this, Project.MSG_DEBUG,
149                                                      Project.MSG_DEBUG);
150             }
151         } else {
152             if (output != null) {
153                 try {
154                     BufferedOutputStream JavaDoc bos
155                         = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(output));
156                     outputstream = new PrintStream JavaDoc(bos);
157                 } catch (IOException JavaDoc e) {
158                     throw new BuildException(e, getLocation());
159                 }
160             } else if (!quiet) {
161                 outputstream = new LogOutputStream(this, Project.MSG_INFO);
162             } else {
163                 outputstream = new LogOutputStream(this, Project.MSG_DEBUG);
164             }
165             if (error != null) {
166                 try {
167                     BufferedOutputStream JavaDoc bos
168                         = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(error));
169                     errorstream = new PrintStream JavaDoc(bos);
170                 } catch (IOException JavaDoc e) {
171                     throw new BuildException(e, getLocation());
172                 }
173             } else if (!quiet) {
174                 errorstream = new LogOutputStream(this, Project.MSG_WARN);
175             } else {
176                 errorstream = new LogOutputStream(this, Project.MSG_DEBUG);
177             }
178             streamhandler = new PumpStreamHandler(outputstream, errorstream);
179         }
180
181         Execute exe = getExecute(toExecute, streamhandler);
182         try {
183             log("Building the RPM based on the " + specFile + " file");
184             int returncode = exe.execute();
185             if (Execute.isFailure(returncode)) {
186                 String JavaDoc msg = "'" + toExecute.getExecutable()
187                     + "' failed with exit code " + returncode;
188                 if (failOnError) {
189                     throw new BuildException(msg);
190                 } else {
191                     log(msg, Project.MSG_ERR);
192                 }
193             }
194         } catch (IOException JavaDoc e) {
195             throw new BuildException(e, getLocation());
196         } finally {
197             FileUtils.close(outputstream);
198             FileUtils.close(errorstream);
199         }
200     }
201
202     /**
203      * The directory which will have the expected
204      * subdirectories, SPECS, SOURCES, BUILD, SRPMS ; optional.
205      * If this isn't specified,
206      * the <tt>baseDir</tt> value is used
207      *
208      * @param td the directory containing the normal RPM directories.
209      */

210     public void setTopDir(File JavaDoc td) {
211         this.topDir = td;
212     }
213
214     /**
215      * What command to issue to the rpm build tool; optional.
216      * The default is "-bb"
217      * @param c the command to use.
218      */

219     public void setCommand(String JavaDoc c) {
220         this.command = c;
221     }
222
223     /**
224      * The name of the spec File to use; required.
225      * @param sf the spec file name to use.
226      */

227     public void setSpecFile(String JavaDoc sf) {
228         if ((sf == null) || (sf.trim().equals(""))) {
229             throw new BuildException("You must specify a spec file", getLocation());
230         }
231         this.specFile = sf;
232     }
233
234     /**
235      * Flag (optional, default=false) to remove
236      * the generated files in the BUILD directory
237      * @param cbd a <code>boolean</code> value.
238      */

239     public void setCleanBuildDir(boolean cbd) {
240         cleanBuildDir = cbd;
241     }
242
243     /**
244      * Flag (optional, default=false) to remove the spec file from SPECS
245      * @param rs a <code>boolean</code> value.
246      */

247     public void setRemoveSpec(boolean rs) {
248         removeSpec = rs;
249     }
250
251     /**
252      * Flag (optional, default=false)
253      * to remove the sources after the build.
254      * See the <tt>--rmsource</tt> option of rpmbuild.
255      * @param rs a <code>boolean</code> value.
256      */

257     public void setRemoveSource(boolean rs) {
258         removeSource = rs;
259     }
260
261     /**
262      * Optional file to save stdout to.
263      * @param output the file to save stdout to.
264      */

265     public void setOutput(File JavaDoc output) {
266         this.output = output;
267     }
268
269     /**
270      * Optional file to save stderr to
271      * @param error the file to save error output to.
272      */

273     public void setError(File JavaDoc error) {
274         this.error = error;
275     }
276
277     /**
278      * The executable to run when building; optional.
279      * The default is <code>rpmbuild</code>.
280      *
281      * @since Ant 1.6
282      * @param c the rpm build executable
283      */

284     public void setRpmBuildCommand(String JavaDoc c) {
285         this.rpmBuildCommand = c;
286     }
287
288     /**
289      * If <code>true</code>, stop the build process when the rpmbuild command
290      * exits with an error status.
291      * @param value <code>true</code> if it should halt, otherwise
292      * <code>false</code>. The default is <code>false</code>.
293      *
294      * @since Ant 1.6.3
295      */

296     public void setFailOnError(boolean value) {
297         failOnError = value;
298     }
299
300     /**
301      * If true, output from the RPM build command will only be logged to DEBUG.
302      * @param value <code>false</code> if output should be logged, otherwise
303      * <code>true</code>. The default is <code>false</code>.
304      *
305      * @since Ant 1.6.3
306      */

307     public void setQuiet(boolean value) {
308         quiet = value;
309     }
310
311     /**
312      * Checks whether <code>rpmbuild</code> is on the PATH and returns
313      * the absolute path to it - falls back to <code>rpm</code>
314      * otherwise.
315      *
316      * @return the command used to build RPM's
317      *
318      * @since 1.6
319      */

320     protected String JavaDoc guessRpmBuildCommand() {
321         Vector JavaDoc env = Execute.getProcEnvironment();
322         String JavaDoc path = null;
323         for (Enumeration JavaDoc e = env.elements(); e.hasMoreElements();) {
324             String JavaDoc var = (String JavaDoc) e.nextElement();
325             if (var.startsWith(PATH1) || var.startsWith(PATH2) || var.startsWith(PATH3)) {
326                 path = var.substring(PATH_LEN);
327                 break;
328             }
329         }
330
331         if (path != null) {
332             Path p = new Path(getProject(), path);
333             String JavaDoc[] pElements = p.list();
334             for (int i = 0; i < pElements.length; i++) {
335                 File JavaDoc f = new File JavaDoc(pElements[i],
336                                   "rpmbuild"
337                                   + (Os.isFamily("dos") ? ".exe" : ""));
338                 if (f.canRead()) {
339                     return f.getAbsolutePath();
340                 }
341             }
342         }
343
344         return "rpm";
345     }
346
347     /**
348      * Get the execute object.
349      * @param toExecute the command line to use.
350      * @param streamhandler the stream handler to use.
351      * @return the execute object.
352      * @since Ant 1.6.3
353      */

354     protected Execute getExecute(Commandline toExecute,
355                                  ExecuteStreamHandler streamhandler) {
356         Execute exe = new Execute(streamhandler, null);
357
358         exe.setAntRun(getProject());
359         if (topDir == null) {
360             topDir = getProject().getBaseDir();
361         }
362         exe.setWorkingDirectory(topDir);
363
364         exe.setCommandline(toExecute.getCommandline());
365         return exe;
366     }
367 }
368
Popular Tags