|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
-
-
- package org.apache.tools.ant;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.PrintStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Vector;
- import java.util.Properties;
- import java.util.Enumeration;
-
-
- public class AntBean extends Task {
-
-
- public final static String DEFAULT_BUILD_FILENAME = "build.xml";
-
-
- private int msgOutputLevel = Project.MSG_INFO;
-
-
- private File buildFile;
- private String searchForThis=null;
-
-
- private PrintStream out = System.out;
-
-
- private PrintStream err = System.err;
-
-
- private Vector targets = new Vector(5);
-
-
- private Properties definedProps = new Properties();
-
-
- private Vector listeners = new Vector(5);
-
-
- private Vector propertyFiles = new Vector(5);
-
-
-
- private String loggerClassname = null;
-
-
-
- private boolean emacsMode = false;
-
- private ClassLoader coreLoader;
-
- private ProjectHelper helper=null;
-
- private Project newProject=null;
-
- private boolean redirectOutput=true;
-
-
- public AntBean() {
- }
-
-
-
-
-
-
- public void setOutputLevel( int level ) {
- msgOutputLevel=level;
- }
-
- public void setBuildfile( String name ) {
- buildFile = new File(name);
- }
-
-
-
- public void addListener( String s ) {
- listeners.addElement(s);
- }
-
-
-
- public void setLogger( String s ) {
- if (loggerClassname != null) {
- System.out.println("Only one logger class may be specified.");
- return;
- }
- loggerClassname = s;
- }
-
-
-
- public void setEmacs( boolean b ) {
- emacsMode = b;
- }
-
-
-
- public void setFind( String s ) {
- if (s==null) {
- searchForThis = s;
- } else {
- searchForThis = DEFAULT_BUILD_FILENAME;
- }
- }
-
-
-
- public void addPropertyfile( String s ) {
- propertyFiles.addElement(s);
- }
-
-
-
- public void setCoreLoader( ClassLoader coreLoader ) {
- coreLoader=coreLoader;
- }
-
-
-
- public void setUserProperty( String name, String value ) {
- definedProps.put( name, value );
- }
-
-
-
-
- public void addTarget(String arg ) {
- targets.addElement(arg);
- }
-
-
-
- public void setLogfile( String name ) {
- try {
- File logFile = new File(name);
- out = new PrintStream(new FileOutputStream(logFile));
- err = out;
- System.setOut(out);
- System.setErr(out);
- } catch (IOException ioe) {
- String msg = "Cannot write on the specified log file. " +
- "Make sure the path exists and you have write permissions.";
- System.out.println(msg);
- return;
- }
- }
-
-
-
- public void setRedirectOutput( boolean b ) {
- redirectOutput=b;
- }
-
-
-
-
-
-
- public File getBuildFile()
- throws BuildException
- {
-
- if (buildFile == null) {
-
- if (searchForThis != null) {
- buildFile = findBuildFile(System.getProperty("user.dir"),
- searchForThis);
- } else {
- buildFile = new File(DEFAULT_BUILD_FILENAME);
- }
- }
- getProject().setUserProperty("ant.file" , buildFile.getAbsolutePath() );
- return buildFile;
- }
-
-
-
- public Project getProject() {
- if( newProject!=null )
- return newProject;
- loadProperties();
-
- helper=ProjectHelper.getProjectHelper();
- newProject = helper.createProject(coreLoader);
- newProject.setCoreLoader(coreLoader);
-
- addBuildListeners(newProject);
-
- newProject.fireBuildStarted();
-
- newProject.init();
- newProject.setUserProperty("ant.version", getAntVersion());
-
-
- Enumeration e = definedProps.keys();
- while (e.hasMoreElements()) {
- String arg = (String)e.nextElement();
- String value = (String)definedProps.get(arg);
- newProject.setUserProperty(arg, value);
- }
-
- return newProject;
- }
-
- private static String antVersion = null;
-
-
-
- public static synchronized String getAntVersion() throws BuildException {
- if (antVersion == null) {
- try {
- Properties props = new Properties();
- InputStream in =
- Main.class.getResourceAsStream("/org/apache/tools/ant/version.txt");
- props.load(in);
- in.close();
-
- String lSep = System.getProperty("line.separator");
- StringBuffer msg = new StringBuffer();
- msg.append("Apache Ant version ");
- msg.append(props.getProperty("VERSION"));
- msg.append(" compiled on ");
- msg.append(props.getProperty("DATE"));
- antVersion = msg.toString();
- } catch (IOException ioe) {
- throw new BuildException("Could not load the version information:"
- + ioe.getMessage());
- } catch (NullPointerException npe) {
- throw new BuildException("Could not load the version information.");
- }
- }
- return antVersion;
- }
-
-
-
-
- Throwable error = null;
-
-
-
-
- public void done() {
- newProject.fireBuildFinished(error);
- }
-
-
-
-
- public void processBuildXml() throws BuildException {
- checkBuildFile();
- File buildFile=getBuildFile();
- Project newProject=getProject();
-
-
-
- String noParserMessage =
- "No JAXP compliant XML parser found. Please visit http://xml.apache.org for a suitable parser";
- try {
- Class.forName("javax.xml.parsers.SAXParserFactory");
- helper.parse(newProject, buildFile);
- } catch (NoClassDefFoundError ncdfe) {
- throw new BuildException(noParserMessage, ncdfe);
- } catch (ClassNotFoundException cnfe) {
- throw new BuildException(noParserMessage, cnfe);
- } catch (NullPointerException npe) {
- throw new BuildException(noParserMessage, npe);
- }
-
-
- if (targets.size() == 0) {
- targets.addElement(newProject.getDefaultTarget());
- }
-
- newProject.executeTargets(targets);
- }
-
- public void execute() throws BuildException {
-
- try {
- if( redirectOutput ) {
- pushSystemOut();
- }
-
- processBuildXml();
- } catch(RuntimeException exc) {
- error = exc;
- throw exc;
- } catch(Error err) {
- error = err;
- throw err;
- } finally {
- done();
- if( redirectOutput )
- popSystemOut();
- }
- }
-
-
-
- private void checkBuildFile() throws BuildException {
- File buildFile=getBuildFile();
-
-
- if (!buildFile.exists()) {
- System.out.println("Buildfile: " + buildFile + " does not exist!");
- throw new BuildException("Build failed");
- }
-
-
-
- if (buildFile.isDirectory()) {
- System.out.println("What? Buildfile: " + buildFile + " is a dir!");
- throw new BuildException("Build failed");
- }
-
-
- if (msgOutputLevel >= Project.MSG_INFO) {
- System.out.println("Buildfile: " + buildFile);
- }
- }
-
- private PrintStream oldErr=null;
- private PrintStream oldOut=null;
- private SecurityManager oldsm = null;
-
- private void pushSystemOut() {
- oldErr = System.err;
- oldOut = System.out;
-
-
-
- if ( !Project.JAVA_1_0.equals(Project.getJavaVersion()) &&
- !Project.JAVA_1_1.equals(Project.getJavaVersion()) ){
- oldsm = System.getSecurityManager();
-
-
-
-
-
- }
- System.setOut(new PrintStream(new DemuxOutputStream(getProject(), false)));
- System.setErr(new PrintStream(new DemuxOutputStream(getProject(), true)));
- }
-
- private void popSystemOut() {
-
-
- if (oldsm != null){
- System.setSecurityManager(oldsm);
- }
-
- if( oldOut!=null && oldErr!=null ) {
- System.setOut(oldOut);
- System.setErr(oldErr);
- }
- }
-
- protected void addBuildListeners(Project newProject) {
-
-
- newProject.addBuildListener(createLogger());
-
- for (int i = 0; i < listeners.size(); i++) {
- String className = (String) listeners.elementAt(i);
- try {
- BuildListener listener =
- (BuildListener) Class.forName(className).newInstance();
- newProject.addBuildListener(listener);
- }
- catch(Throwable exc) {
- throw new BuildException("Unable to instantiate listener " + className, exc);
- }
- }
- }
-
-
-
- protected BuildLogger createLogger() {
- BuildLogger logger = null;
- if (loggerClassname != null) {
- try {
- logger = (BuildLogger)(Class.forName(loggerClassname).newInstance());
- } catch (ClassCastException e) {
- System.err.println("The specified logger class " + loggerClassname +
- " does not implement the BuildLogger interface");
- throw new RuntimeException();
- } catch (Exception e) {
- System.err.println("Unable to instantiate specified logger class " +
- loggerClassname + " : " + e.getClass().getName());
- throw new RuntimeException();
- }
- }
- else {
- logger = new DefaultLogger();
- }
-
- logger.setMessageOutputLevel(msgOutputLevel);
- logger.setOutputPrintStream(out);
- logger.setErrorPrintStream(err);
- logger.setEmacsMode(emacsMode);
-
- return logger;
- }
-
-
-
- private void loadProperties()
- {
-
- for (int propertyFileIndex=0;
- propertyFileIndex < propertyFiles.size();
- propertyFileIndex++) {
- String filename = (String) propertyFiles.elementAt(propertyFileIndex);
- Properties props = new Properties();
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(filename);
- props.load(fis);
- }
- catch (IOException e) {
- System.out.println("Could not load property file "
- + filename + ": " + e.getMessage());
- } finally {
- if (fis != null){
- try {
- fis.close();
- } catch (IOException e){
- }
- }
- }
-
-
- Enumeration propertyNames = props.propertyNames();
- while (propertyNames.hasMoreElements()) {
- String name = (String) propertyNames.nextElement();
- if (definedProps.getProperty(name) == null) {
- definedProps.put(name, props.getProperty(name));
- }
- }
- }
- }
-
-
-
-
-
- private File getParentFile(File file) {
- String filename = file.getAbsolutePath();
- file = new File(filename);
- filename = file.getParent();
-
- if (filename != null && msgOutputLevel >= Project.MSG_VERBOSE) {
- System.out.println("Searching in "+filename);
- }
-
- return (filename == null) ? null : new File(filename);
- }
-
-
-
- private File findBuildFile(String start, String suffix) throws BuildException {
- if (msgOutputLevel >= Project.MSG_INFO) {
- System.out.println("Searching for " + suffix + " ...");
- }
-
- File parent = new File(new File(start).getAbsolutePath());
- File file = new File(parent, suffix);
-
-
- while (!file.exists()) {
-
- parent = getParentFile(parent);
-
-
-
- if (parent == null) {
- throw new BuildException("Could not locate a build file!");
- }
-
-
- file = new File(parent, suffix);
- }
-
- return file;
- }
-
- }
|