001package org.apache.maven.execution;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.artifact.ArtifactUtils;
023import org.apache.maven.plugin.descriptor.PluginDescriptor;
024import org.apache.maven.project.DuplicateProjectException;
025import org.apache.maven.project.MavenProject;
026import org.apache.maven.project.ProjectSorter;
027import org.codehaus.plexus.util.dag.CycleDetectedException;
028
029import java.util.ArrayList;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033
034@Deprecated
035public class ReactorManager
036{
037    public static final String FAIL_FAST = "fail-fast";
038
039    public static final String FAIL_AT_END = "fail-at-end";
040
041    public static final String FAIL_NEVER = "fail-never";
042
043    public static final String MAKE_MODE = "make";
044
045    public static final String MAKE_DEPENDENTS_MODE = "make-dependents";
046
047    // make projects that depend on me, and projects that I depend on
048    public static final String MAKE_BOTH_MODE = "make-both";
049
050    private List<String> blackList = new ArrayList<String>();
051
052    private Map<String, BuildFailure> buildFailuresByProject = new HashMap<String, BuildFailure>();
053
054    private Map pluginContextsByProjectAndPluginKey = new HashMap();
055
056    private String failureBehavior = FAIL_FAST;
057
058    private final ProjectSorter sorter;
059
060    private Map<String, BuildSuccess> buildSuccessesByProject = new HashMap<String, BuildSuccess>();
061
062    public ReactorManager( List<MavenProject> projects )
063        throws CycleDetectedException, DuplicateProjectException
064    {
065        this.sorter = new ProjectSorter( projects );
066    }
067
068    public Map getPluginContext( PluginDescriptor plugin, MavenProject project )
069    {
070        Map pluginContextsByKey = (Map) pluginContextsByProjectAndPluginKey.get( project.getId() );
071
072        if ( pluginContextsByKey == null )
073        {
074            pluginContextsByKey = new HashMap();
075            pluginContextsByProjectAndPluginKey.put( project.getId(), pluginContextsByKey );
076        }
077
078        Map pluginContext = (Map) pluginContextsByKey.get( plugin.getPluginLookupKey() );
079
080        if ( pluginContext == null )
081        {
082            pluginContext = new HashMap();
083            pluginContextsByKey.put( plugin.getPluginLookupKey(), pluginContext );
084        }
085
086        return pluginContext;
087    }
088
089    public void setFailureBehavior( String failureBehavior )
090    {
091        if ( failureBehavior == null )
092        {
093            this.failureBehavior = FAIL_FAST; // default
094            return;
095        }
096        if ( FAIL_FAST.equals( failureBehavior ) || FAIL_AT_END.equals( failureBehavior )
097            || FAIL_NEVER.equals( failureBehavior ) )
098        {
099            this.failureBehavior = failureBehavior;
100        }
101        else
102        {
103            throw new IllegalArgumentException( "Invalid failure behavior (must be one of: \'" + FAIL_FAST + "\', \'"
104                + FAIL_AT_END + "\', \'" + FAIL_NEVER + "\')." );
105        }
106    }
107
108    public String getFailureBehavior()
109    {
110        return failureBehavior;
111    }
112
113    public void blackList( MavenProject project )
114    {
115        blackList( getProjectKey( project ) );
116    }
117
118    private void blackList( String id )
119    {
120        if ( !blackList.contains( id ) )
121        {
122            blackList.add( id );
123
124            List<String> dependents = sorter.getDependents( id );
125
126            if ( dependents != null && !dependents.isEmpty() )
127            {
128                for ( String dependentId : dependents )
129                {
130                    if ( !buildSuccessesByProject.containsKey( dependentId )
131                        && !buildFailuresByProject.containsKey( dependentId ) )
132                    {
133                        blackList( dependentId );
134                    }
135                }
136            }
137        }
138    }
139
140    public boolean isBlackListed( MavenProject project )
141    {
142        return blackList.contains( getProjectKey( project ) );
143    }
144
145    private static String getProjectKey( MavenProject project )
146    {
147        return ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
148    }
149
150    public void registerBuildFailure( MavenProject project, Exception error, String task, long time )
151    {
152        buildFailuresByProject.put( getProjectKey( project ), new BuildFailure( project, time, error ) );
153    }
154
155    public boolean hasBuildFailures()
156    {
157        return !buildFailuresByProject.isEmpty();
158    }
159
160    public boolean hasBuildFailure( MavenProject project )
161    {
162        return buildFailuresByProject.containsKey( getProjectKey( project ) );
163    }
164
165    public boolean hasMultipleProjects()
166    {
167        return sorter.hasMultipleProjects();
168    }
169
170    public List<MavenProject> getSortedProjects()
171    {
172        return sorter.getSortedProjects();
173    }
174
175    public boolean hasBuildSuccess( MavenProject project )
176    {
177        return buildSuccessesByProject.containsKey( getProjectKey( project ) );
178    }
179
180    public void registerBuildSuccess( MavenProject project, long time )
181    {
182        buildSuccessesByProject.put( getProjectKey( project ), new BuildSuccess( project, time ) );
183    }
184
185    public BuildFailure getBuildFailure( MavenProject project )
186    {
187        return (BuildFailure) buildFailuresByProject.get( getProjectKey( project ) );
188    }
189
190    public BuildSuccess getBuildSuccess( MavenProject project )
191    {
192        return (BuildSuccess) buildSuccessesByProject.get( getProjectKey( project ) );
193    }
194
195    public boolean executedMultipleProjects()
196    {
197        return buildFailuresByProject.size() + buildSuccessesByProject.size() > 1;
198    }
199}