View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: DefaultAntExecutor.java 3838 2011-10-08 20:15:41Z schulte2005 $
29   *
30   */
31  package org.jomc.ant.test.support;
32  
33  import java.io.PrintStream;
34  import java.io.StringWriter;
35  import org.apache.commons.io.output.WriterOutputStream;
36  import org.apache.tools.ant.BuildEvent;
37  import org.apache.tools.ant.BuildListener;
38  
39  /**
40   * Default {@code AntExecutor} implementation.
41   *
42   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
43   * @version $JOMC: DefaultAntExecutor.java 3838 2011-10-08 20:15:41Z schulte2005 $
44   */
45  public class DefaultAntExecutor implements AntExecutor
46  {
47  
48      /** Creates a new {@code DefaultAntExecutor}. */
49      public DefaultAntExecutor()
50      {
51          super();
52      }
53  
54      public AntExecutionResult executeAnt( final AntExecutionRequest request )
55      {
56          if ( request == null )
57          {
58              throw new NullPointerException( "request" );
59          }
60          if ( request.getProject() == null )
61          {
62              throw new NullPointerException( "project" );
63          }
64          if ( request.getTarget() == null )
65          {
66              throw new NullPointerException( "target" );
67          }
68  
69          final StringWriter out = new StringWriter();
70          final StringWriter err = new StringWriter();
71          final PrintStream writerOut = new PrintStream( new WriterOutputStream( out ) );
72          final PrintStream writerErr = new PrintStream( new WriterOutputStream( err ) );
73          final PrintStream systemOut = System.out;
74          final PrintStream systemErr = System.err;
75          final AntExecutionResult result = new AntExecutionResult();
76          final BuildListener buildListener = new BuildListener()
77          {
78  
79              public void buildStarted( final BuildEvent event )
80              {
81                  result.getBuildStartedEvents().add( event );
82              }
83  
84              public void buildFinished( final BuildEvent event )
85              {
86                  result.getBuildFinishedEvents().add( event );
87              }
88  
89              public void targetStarted( final BuildEvent event )
90              {
91                  result.getTargetStartedEvents().add( event );
92              }
93  
94              public void targetFinished( final BuildEvent event )
95              {
96                  result.getTaskFinishedEvents().add( event );
97              }
98  
99              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 }