1 package org.tigris.scarab.actions.setup; 2 3 4 49 50 51 52 import java.io.File ; 53 import java.io.PrintStream ; 54 import java.util.Hashtable ; 55 import java.util.Iterator ; 56 import java.util.Map ; 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 83 public class AntRunner 84 { 85 86 89 private static String antVersion = null; 90 91 96 private String loggerClassname = null; 97 98 99 private int msgOutputLevel = Project.MSG_INFO; 100 101 102 103 private static PrintStream out = System.out; 104 105 106 private static PrintStream err = System.err; 107 108 109 119 public void execute(File theBuildFile, String theTarget, Map properties) 120 { 121 122 final Project project = new Project(); 123 project.setCoreLoader(null); 124 125 Throwable error = null; 126 127 try 128 { 129 addBuildListener(project); 130 addInputHandler(project); 131 132 PrintStream err = System.err; 133 PrintStream out = System.out; 134 135 SecurityManager oldsm = System.getSecurityManager(); 137 138 try 139 { 140 System.setOut(new PrintStream (new DemuxOutputStream(project, false))); 141 System.setErr(new PrintStream (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 iter = properties.keySet().iterator(); 151 while(iter.hasNext()) 152 { 153 String key = (String ) iter.next(); 154 String val = (String ) 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 if (oldsm != null) 176 { 177 System.setSecurityManager(oldsm); 178 } 179 180 System.setOut(out); 181 System.setErr(err); 182 } 183 } 184 catch (RuntimeException exc) 185 { 186 error = exc; 187 throw exc; 188 } 189 catch (Error 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 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 e) 228 { 229 System.err 230 .println("The specified logger class " + loggerClassname 231 + " does not implement the BuildLogger interface"); 232 throw new RuntimeException (); 233 } 234 catch (Exception e) 235 { 236 System.err 237 .println("Unable to instantiate specified logger " + "class " 238 + loggerClassname 239 + " : " 240 + e.getClass().getName()); 241 throw new RuntimeException (); 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 [] args) 258 { 259 String theBuildFileName = args[0]; 260 File theBuildFile = new File (theBuildFileName); 261 String theTarget = args[1]; 262 Map properties = null; 263 for(int index=2; index< args.length; index++) 264 { 265 if(properties == null) 266 { 267 properties = new Hashtable (); 268 } 269 String pdef = args[index]; 270 int i = pdef.indexOf('='); 271 String key; 272 String 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 |