001    /*
002     * Copyright 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.tools.tasks;
020    
021    import java.io.File;
022    
023    import net.dpml.library.Resource;
024    import net.dpml.library.impl.DefaultLibrary;
025    
026    import net.dpml.tools.impl.StandardBuilder;
027    
028    import net.dpml.transit.monitor.LoggingAdapter;
029    
030    import net.dpml.util.Logger;
031    
032    import org.apache.tools.ant.BuildException;
033    import org.apache.tools.ant.Task;
034    import org.apache.tools.ant.taskdefs.Ant;
035    
036    /**
037     * Resolves a sorted sequence of projects with a basedir equal to or 
038     * derived from the current ${basedir}.
039     *
040     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
041     * @version 1.2.0
042     */
043    public class ReactorTask extends Task
044    {
045        private String m_target;
046        
047       /**
048        * Set the target task name.
049        * @param target the target task
050        */
051        public void setTarget( String target )
052        {
053            m_target = target;
054        }
055        
056        private String[] getTargets()
057        {
058            if( null != m_target )
059            {
060                return new String[]{m_target};
061            }
062            else
063            {
064                return new String[0];
065            }
066        }
067        
068       /**
069        * Execute the task.
070        */
071        public void execute()
072        {
073            try
074            {
075                Logger logger = new LoggingAdapter();
076                DefaultLibrary library = new DefaultLibrary( logger );
077                StandardBuilder builder = new StandardBuilder( logger, library, false );
078                File basedir = getProject().getBaseDir().getCanonicalFile();
079                Resource[] resources = library.select( basedir, false );
080                log( "Reactive selection: " + resources.length );
081                for( int i=0; i<resources.length; i++ )
082                {
083                    Resource resource = resources[i];
084                    if( !resource.getBaseDir().equals( basedir ) )
085                    {
086                        executeTarget( builder, resource );
087                    }
088                }
089            }
090            catch( BuildException e )
091            {
092                throw e;
093            }
094            catch( Exception e )
095            {
096                final String error = 
097                  "Unexpected error while resolving reactive project list.";
098                throw new BuildException( error, e );
099            }
100        }
101        
102        private void executeTarget( StandardBuilder builder, final Resource resource ) throws BuildException
103        {
104            log( "Executing reactive build: " + resource );
105            final Ant ant = (Ant) getProject().createTask( "ant" );
106            ant.setDir( resource.getBaseDir() );
107            ant.setInheritRefs( false );
108            ant.setInheritAll( false );
109    
110            File template = builder.getTemplateFile( resource );
111            ant.setAntfile( template.toString() );
112            if( null != m_target )
113            {
114                if( !"default".equals( m_target ) )
115                {
116                    log( "building " + resource + " with target: " + m_target );
117                    ant.setTarget( m_target );
118                }
119                else
120                {
121                    log( "building " + resource + " with default target" );
122                }
123            }
124            else
125            {
126                log( "building " + resource + " with default target" );
127            }
128            ant.init();
129            ant.execute();
130        }
131    }