package org.apache.maven.execution;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectSorter;
import org.codehaus.plexus.util.dag.CycleDetectedException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ReactorManager
{
    public static final String FAIL_FAST = "fail-fast";

    public static final String FAIL_AT_END = "fail-at-end";

    public static final String FAIL_NEVER = "fail-never";

    private List blackList = new ArrayList();

    private Map buildFailuresByProject = new HashMap();

    private Map pluginContextsByProjectAndPluginKey = new HashMap();

    private String failureBehavior = FAIL_FAST;

    private final ProjectSorter sorter;

    private Map buildSuccessesByProject = new HashMap();

    public ReactorManager( List projects )
        throws CycleDetectedException
    {
        this.sorter = new ProjectSorter( projects );
    }

    public Map getPluginContext( PluginDescriptor plugin, MavenProject project )
    {
        Map pluginContextsByKey = (Map) pluginContextsByProjectAndPluginKey.get( project.getId() );

        if ( pluginContextsByKey == null )
        {
            pluginContextsByKey = new HashMap();
            pluginContextsByProjectAndPluginKey.put( project.getId(), pluginContextsByKey );
        }

        Map pluginContext = (Map) pluginContextsByKey.get( plugin.getPluginLookupKey() );

        if ( pluginContext == null )
        {
            pluginContext = new HashMap();
            pluginContextsByKey.put( plugin.getPluginLookupKey(), pluginContext );
        }

        return pluginContext;
    }

    public void setFailureBehavior( String failureBehavior )
    {
        if ( FAIL_FAST.equals( failureBehavior ) || FAIL_AT_END.equals( failureBehavior ) ||
            FAIL_NEVER.equals( failureBehavior ) )
        {
            this.failureBehavior = failureBehavior;
        }
        else
        {
            throw new IllegalArgumentException( "Invalid failure behavior (must be one of: \'" + FAIL_FAST + "\', \'" +
                FAIL_AT_END + "\', \'" + FAIL_NEVER + "\')." );
        }
    }

    public String getFailureBehavior()
    {
        return failureBehavior;
    }

    public void blackList( MavenProject project )
    {
        blackList( ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() ) );
    }

    private void blackList( String id )
    {
        if ( !blackList.contains( id ) )
        {
            blackList.add( id );

            List dependents = sorter.getDependents( id );

            if ( dependents != null && !dependents.isEmpty() )
            {
                for ( Iterator it = dependents.iterator(); it.hasNext(); )
                {
                    String dependentId = (String) it.next();

                    blackList( dependentId );
                }
            }
        }
    }

    public boolean isBlackListed( MavenProject project )
    {
        return blackList.contains( ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() ) );
    }

    public void registerBuildFailure( MavenProject project, Exception error, String task, long time )
    {
        buildFailuresByProject.put( project.getId(), new BuildFailure( error, task, time ) );
    }

    public boolean hasBuildFailures()
    {
        return !buildFailuresByProject.isEmpty();
    }

    public boolean hasBuildFailure( MavenProject project )
    {
        return buildFailuresByProject.containsKey( project.getId() );
    }

    public boolean hasMultipleProjects()
    {
        return sorter.hasMultipleProjects();
    }

    public List getSortedProjects()
    {
        return sorter.getSortedProjects();
    }

    public MavenProject getTopLevelProject()
    {
        return sorter.getTopLevelProject();
    }

    public boolean hasBuildSuccess( MavenProject project )
    {
        return buildSuccessesByProject.containsKey( project.getId() );
    }

    public void registerBuildSuccess( MavenProject project, long time )
    {
        buildSuccessesByProject.put( project.getId(), new BuildSuccess( project, time ) );
    }

    public BuildFailure getBuildFailure( MavenProject project )
    {
        return (BuildFailure) buildFailuresByProject.get( project.getId() );
    }

    public BuildSuccess getBuildSuccess( MavenProject project )
    {
        return (BuildSuccess) buildSuccessesByProject.get( project.getId() );
    }

}

@@ -3093,83 +3093,30 @@
 st( 
-ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId()
+getProjectKey( project
  ) )
@@ -3542,24 +3542,207 @@
 it.next();%0A%0A
+                    if ( !buildSuccessesByProject.containsKey( dependentId ) &&%0A                        !buildFailuresByProject.containsKey( dependentId ) )%0A                    %7B%0A    
             
@@ -3771,24 +3771,46 @@
 endentId );%0A
+                    %7D%0A
             
@@ -3943,16 +3943,136 @@
 ontains(
+ getProjectKey( project ) );%0A    %7D%0A%0A    private static String getProjectKey( MavenProject project )%0A    %7B%0A        return
  Artifac
@@ -4139,18 +4139,16 @@
 factId()
- )
  );%0A    
@@ -4290,38 +4290,47 @@
 roject.put( 
-p
+getP
 roject
-.getId(
+Key( project 
 ), new Build
@@ -4581,38 +4581,47 @@
 ontainsKey( 
-p
+getP
 roject
-.getId(
+Key( project 
 ) );%0A    %7D%0A%0A
@@ -5030,30 +5030,39 @@
 insKey( 
-p
+getP
 roject
-.getId(
+Key( project 
 ) );%0A   
@@ -5180,30 +5180,39 @@
 ct.put( 
-p
+getP
 roject
-.getId(
+Key( project 
 ), new B
@@ -5374,38 +5374,47 @@
 roject.get( 
-p
+getP
 roject
-.getId(
+Key( project 
 ) );%0A    %7D%0A%0A
@@ -5538,38 +5538,47 @@
 roject.get( 
-p
+getP
 roject
-.getId(
+Key( project 
 ) );%0A    %7D%0A%0A

correct blacklisting logic