001    /*
002     *   Copyright (C) Christian Schulte, 2005-206
003     *   All rights reserved.
004     *
005     *   Redistribution and use in source and binary forms, with or without
006     *   modification, are permitted provided that the following conditions
007     *   are met:
008     *
009     *     o Redistributions of source code must retain the above copyright
010     *       notice, this list of conditions and the following disclaimer.
011     *
012     *     o Redistributions in binary form must reproduce the above copyright
013     *       notice, this list of conditions and the following disclaimer in
014     *       the documentation and/or other materials provided with the
015     *       distribution.
016     *
017     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     *
028     *   $JOMC: DefaultAntExecutor.java 3838 2011-10-08 20:15:41Z schulte2005 $
029     *
030     */
031    package org.jomc.ant.test.support;
032    
033    import java.io.PrintStream;
034    import java.io.StringWriter;
035    import org.apache.commons.io.output.WriterOutputStream;
036    import org.apache.tools.ant.BuildEvent;
037    import org.apache.tools.ant.BuildListener;
038    
039    /**
040     * Default {@code AntExecutor} implementation.
041     *
042     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
043     * @version $JOMC: DefaultAntExecutor.java 3838 2011-10-08 20:15:41Z schulte2005 $
044     */
045    public class DefaultAntExecutor implements AntExecutor
046    {
047    
048        /** Creates a new {@code DefaultAntExecutor}. */
049        public DefaultAntExecutor()
050        {
051            super();
052        }
053    
054        public AntExecutionResult executeAnt( final AntExecutionRequest request )
055        {
056            if ( request == null )
057            {
058                throw new NullPointerException( "request" );
059            }
060            if ( request.getProject() == null )
061            {
062                throw new NullPointerException( "project" );
063            }
064            if ( request.getTarget() == null )
065            {
066                throw new NullPointerException( "target" );
067            }
068    
069            final StringWriter out = new StringWriter();
070            final StringWriter err = new StringWriter();
071            final PrintStream writerOut = new PrintStream( new WriterOutputStream( out ) );
072            final PrintStream writerErr = new PrintStream( new WriterOutputStream( err ) );
073            final PrintStream systemOut = System.out;
074            final PrintStream systemErr = System.err;
075            final AntExecutionResult result = new AntExecutionResult();
076            final BuildListener buildListener = new BuildListener()
077            {
078    
079                public void buildStarted( final BuildEvent event )
080                {
081                    result.getBuildStartedEvents().add( event );
082                }
083    
084                public void buildFinished( final BuildEvent event )
085                {
086                    result.getBuildFinishedEvents().add( event );
087                }
088    
089                public void targetStarted( final BuildEvent event )
090                {
091                    result.getTargetStartedEvents().add( event );
092                }
093    
094                public void targetFinished( final BuildEvent event )
095                {
096                    result.getTaskFinishedEvents().add( event );
097                }
098    
099                public void taskStarted( final BuildEvent event )
100                {
101                    result.getTaskStartedEvents().add( event );
102                }
103    
104                public void taskFinished( final BuildEvent event )
105                {
106                    result.getTaskFinishedEvents().add( event );
107                }
108    
109                public void messageLogged( final BuildEvent event )
110                {
111                    result.getMessageLoggedEvents().add( event );
112                }
113    
114            };
115    
116            try
117            {
118                systemOut.flush();
119                systemErr.flush();
120                System.setOut( writerOut );
121                System.setErr( writerErr );
122    
123                request.getProject().addBuildListener( buildListener );
124                request.getProject().executeTarget( request.getTarget() );
125            }
126            catch ( final Throwable t )
127            {
128                result.setThrowable( t );
129            }
130            finally
131            {
132                request.getProject().removeBuildListener( buildListener );
133                System.setOut( systemOut );
134                System.setErr( systemErr );
135                writerOut.close();
136                writerErr.close();
137                result.setSystemError( err.toString() );
138                result.setSystemOutput( out.toString() );
139            }
140    
141            return result;
142        }
143    
144    }