KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > actions > setup > AntRunner


1 package org.tigris.scarab.actions.setup;
2
3
4 /* ================================================================
5  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement: "This product includes
20  * software developed by Collab.Net <http://www.Collab.Net/>."
21  * Alternately, this acknowlegement may appear in the software itself, if
22  * and wherever such third-party acknowlegements normally appear.
23  *
24  * 4. The hosted project names must not be used to endorse or promote
25  * products derived from this software without prior written
26  * permission. For written permission, please contact info@collab.net.
27  *
28  * 5. Products derived from this software may not use the "Tigris" or
29  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
30  * prior written permission of Collab.Net.
31  *
32  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
36  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
40  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  * ====================================================================
45  *
46  * This software consists of voluntary contributions made by many
47  * individuals on behalf of Collab.Net.
48  */

49
50
51
52 import java.io.File JavaDoc;
53 import java.io.PrintStream JavaDoc;
54 import java.util.Hashtable JavaDoc;
55 import java.util.Iterator JavaDoc;
56 import java.util.Map JavaDoc;
57
58 import org.apache.tools.ant.BuildLogger;
59 import org.apache.tools.ant.DefaultLogger;
60 import org.apache.tools.ant.DemuxOutputStream;
61 import org.apache.tools.ant.Main;
62 import org.apache.tools.ant.Project;
63 import org.apache.tools.ant.ProjectHelper;
64 import org.apache.tools.ant.input.DefaultInputHandler;
65 import org.apache.tools.ant.input.InputHandler;
66
67 /**
68  * This class is the bridge to ant.
69  * Most of the code is derived form the
70  * org.apache.ant.Main class, but simplyfied for
71  * our purposes. i.e. you can only run one single
72  * ant task per call. Also most of the configuration
73  * properties available in the original class have
74  * been dropped to keep the bridge small.
75  * We could have used the ant Main class directly,
76  * but i think, having control over what happens
77  * here is better (i.e. where does out and err go,
78  * how do we log, etc...)
79  *
80  * @author <a HREF="mailto:dabbous@saxess.com">Hussayn Dabbous</a>
81  * @version $Id: AntRunner.java 9325 2004-12-22 22:48:10Z dabbous $
82  */

83 public class AntRunner
84 {
85
86     /**
87      * Cache of the Ant version information when it has been loaded.
88      */

89     private static String JavaDoc antVersion = null;
90
91     /**
92      * The Ant logger class. There may be only one logger. It will have
93      * the right to use the 'out' PrintStream. The class must implements the
94      * BuildLogger interface.
95      */

96     private String JavaDoc loggerClassname = null;
97
98     /** Our current message output status. Follows Project.MSG_XXX. */
99     private int msgOutputLevel = Project.MSG_INFO;
100
101
102     /** Stream to use for logging. */
103     private static PrintStream JavaDoc out = System.out;
104
105     /** Stream that we are using for logging error messages. */
106     private static PrintStream JavaDoc err = System.err;
107
108
109     /**
110      * Execute the given target in the given buildFile.
111      * If target is null, the default target is executred.
112      * theBuildfile must NOT be null. and readable in the
113      * current sessoin context.
114      * Currently no exception handling. Every Exception is
115      * rethrown.
116      * @param theBuildFile
117      * @param theTarget
118      */

119     public void execute(File JavaDoc theBuildFile, String JavaDoc theTarget, Map JavaDoc properties)
120     {
121
122         final Project project = new Project();
123         project.setCoreLoader(null);
124
125         Throwable JavaDoc error = null;
126
127         try
128         {
129             addBuildListener(project);
130             addInputHandler(project);
131
132             PrintStream JavaDoc err = System.err;
133             PrintStream JavaDoc out = System.out;
134
135             // use a system manager that prevents from System.exit()
136
SecurityManager JavaDoc oldsm = System.getSecurityManager();
137
138             try
139             {
140                 System.setOut(new PrintStream JavaDoc(new DemuxOutputStream(project, false)));
141                 System.setErr(new PrintStream JavaDoc(new DemuxOutputStream(project, true)));
142                 project.fireBuildStarted();
143
144                 project.init();
145                 project.setUserProperty("ant.version", Main.getAntVersion());
146                 project.setUserProperty("ant.file", theBuildFile.getAbsolutePath());
147                 
148                 if(properties != null)
149                 {
150                     Iterator JavaDoc iter = properties.keySet().iterator();
151                     while(iter.hasNext())
152                     {
153                      String JavaDoc key = (String JavaDoc) iter.next();
154                      String JavaDoc val = (String JavaDoc) properties.get(key);
155                      project.setUserProperty(key,val);
156                     }
157                 }
158                 
159                 ProjectHelper.configureProject(project, theBuildFile);
160
161                 if (theTarget == null)
162                 {
163                     theTarget = project.getDefaultTarget();
164                 }
165
166                 project.executeTarget(theTarget);
167
168             }
169             finally
170             {
171                 // put back the original security manager
172
// actually, did it ever change ???
173
// Need to look into the ant sources to answer this
174
// question [HD]
175
if (oldsm != null)
176                 {
177                     System.setSecurityManager(oldsm);
178                 }
179
180                 System.setOut(out);
181                 System.setErr(err);
182             }
183         }
184         catch (RuntimeException JavaDoc exc)
185         {
186             error = exc;
187             throw exc;
188         }
189         catch (Error JavaDoc err)
190         {
191             error = err;
192             throw err;
193         }
194         finally
195         {
196             project.fireBuildFinished(error);
197         }
198
199     }
200
201
202     protected void addBuildListener(Project project)
203     {
204         // Add the default listener
205
BuildLogger logger = createLogger();
206         project.addBuildListener(logger);
207     }
208
209
210     private void addInputHandler(Project project)
211     {
212         InputHandler handler = new DefaultInputHandler();
213         project.setInputHandler(handler);
214     }
215
216
217     private BuildLogger createLogger()
218     {
219         BuildLogger logger = null;
220         if (loggerClassname != null)
221         {
222             try
223             {
224                 logger = (BuildLogger) (Class.forName(loggerClassname)
225                         .newInstance());
226             }
227             catch (ClassCastException JavaDoc e)
228             {
229                 System.err
230                         .println("The specified logger class " + loggerClassname
231                                 + " does not implement the BuildLogger interface");
232                 throw new RuntimeException JavaDoc();
233             }
234             catch (Exception JavaDoc e)
235             {
236                 System.err
237                         .println("Unable to instantiate specified logger " + "class "
238                                 + loggerClassname
239                                 + " : "
240                                 + e.getClass().getName());
241                 throw new RuntimeException JavaDoc();
242             }
243         }
244         else
245         {
246             logger = new DefaultLogger();
247         }
248
249         logger.setMessageOutputLevel(msgOutputLevel);
250         logger.setOutputPrintStream(out);
251         logger.setErrorPrintStream(err);
252         logger.setEmacsMode(false);
253
254         return logger;
255     }
256     
257     static public void main(String JavaDoc[] args)
258     {
259         String JavaDoc theBuildFileName = args[0];
260         File JavaDoc theBuildFile = new File JavaDoc(theBuildFileName);
261         String JavaDoc theTarget = args[1];
262         Map JavaDoc properties = null;
263         for(int index=2; index< args.length; index++)
264         {
265             if(properties == null)
266             {
267                 properties = new Hashtable JavaDoc();
268             }
269             String JavaDoc pdef = args[index];
270             int i = pdef.indexOf('=');
271             String JavaDoc key;
272             String JavaDoc val;
273             if(i > 0)
274             {
275                 key = pdef.substring(0,i);
276                 val = pdef.substring(i+1);
277             }
278             else
279             {
280                 key = pdef;
281                 val = "";
282             }
283             properties.put(key,val);
284         }
285         
286         AntRunner antRunner= new AntRunner();
287         antRunner.execute(theBuildFile, theTarget, properties);
288              
289     }
290
291 }
292
Popular Tags