continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From c...@apache.org
Subject svn commit: r933451 [1/2] - in /continuum/trunk: continuum-api/src/main/java/org/apache/continuum/builder/distributed/manager/ continuum-buildagent/continuum-buildagent-api/src/main/java/org/apache/continuum/buildagent/ continuum-buildagent/continuum-b...
Date Tue, 13 Apr 2010 00:26:43 GMT
Author: ctan
Date: Tue Apr 13 00:26:42 2010
New Revision: 933451

URL: http://svn.apache.org/viewvc?rev=933451&view=rev
Log:
[CONTINUUM-2476] Projects are not built in the correct build agent when the project is already in the build queue and then triggered to be built again
- added unit tests
- fix concurrent modification exception
- added checks to see if project and build definition is already in queue or currently preparing build or building
- select the build agent based on the scm roots of the same project group


Added:
    continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/stubs/
    continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/stubs/DefaultDistributedBuildManagerStub.java
    continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/stubs/SlaveBuildAgentTransportClientStub.java
Modified:
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/builder/distributed/manager/DistributedBuildManager.java
    continuum/trunk/continuum-buildagent/continuum-buildagent-api/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentService.java
    continuum/trunk/continuum-buildagent/continuum-buildagent-core/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentServiceImpl.java
    continuum/trunk/continuum-core/src/main/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManager.java
    continuum/trunk/continuum-core/src/main/java/org/apache/continuum/release/distributed/manager/DefaultDistributedReleaseManager.java
    continuum/trunk/continuum-core/src/main/java/org/apache/maven/continuum/DefaultContinuum.java
    continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManagerTest.java
    continuum/trunk/continuum-distributed/continuum-distributed-slave/continuum-distributed-slave-api/src/main/java/org/apache/continuum/distributed/transport/slave/SlaveBuildAgentTransportService.java
    continuum/trunk/continuum-distributed/continuum-distributed-slave/continuum-distributed-slave-client/src/main/java/org/apache/continuum/distributed/transport/slave/SlaveBuildAgentTransportClient.java
    continuum/trunk/continuum-distributed/continuum-distributed-slave/continuum-distributed-slave-server/src/main/java/org/apache/continuum/distributed/transport/slave/SlaveBuildAgentTransportServer.java

Modified: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/builder/distributed/manager/DistributedBuildManager.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/builder/distributed/manager/DistributedBuildManager.java?rev=933451&r1=933450&r2=933451&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/builder/distributed/manager/DistributedBuildManager.java (original)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/builder/distributed/manager/DistributedBuildManager.java Tue Apr 13 00:26:42 2010
@@ -24,6 +24,7 @@ import java.util.Map;
 
 import org.apache.continuum.buildagent.NoBuildAgentException;
 import org.apache.continuum.buildagent.NoBuildAgentInGroupException;
+import org.apache.continuum.model.project.ProjectScmRoot;
 import org.apache.continuum.taskqueue.BuildProjectTask;
 import org.apache.continuum.taskqueue.PrepareBuildProjectsTask;
 import org.apache.continuum.utils.build.BuildTrigger;
@@ -64,7 +65,7 @@ public interface DistributedBuildManager
         throws ContinuumException;
 
     void prepareBuildProjects( Map<Integer, Integer> projectsBuildDefinitionsMap, BuildTrigger buildTrigger, int projectGroupId,  
-                               String projectGroupName, String scmRootAddress, int scmRootId )
+                               String projectGroupName, String scmRootAddress, int scmRootId, List<ProjectScmRoot> scmRoots )
         throws ContinuumException, NoBuildAgentException, NoBuildAgentInGroupException;
 
     Map<String, PrepareBuildProjectsTask> getProjectsCurrentlyPreparingBuild()
@@ -87,4 +88,16 @@ public interface DistributedBuildManager
 
     boolean isAgentAvailable( String buildAgentUrl )
         throws ContinuumException;
+
+    boolean isProjectInAnyPrepareBuildQueue( int projectId, int buildDefinitionId )
+        throws ContinuumException;
+
+    boolean isProjectInAnyBuildQueue( int projectId, int buildDefinitionId )
+        throws ContinuumException;
+
+    boolean isProjectCurrentlyPreparingBuild( int projectId, int buildDefinitionId )
+        throws ContinuumException;
+
+    boolean isProjectCurrentlyBuilding( int projectId, int buildDefinitionId )
+        throws ContinuumException;
 }
\ No newline at end of file

Modified: continuum/trunk/continuum-buildagent/continuum-buildagent-api/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentService.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-buildagent/continuum-buildagent-api/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentService.java?rev=933451&r1=933450&r2=933451&view=diff
==============================================================================
--- continuum/trunk/continuum-buildagent/continuum-buildagent-api/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentService.java (original)
+++ continuum/trunk/continuum-buildagent/continuum-buildagent-api/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentService.java Tue Apr 13 00:26:42 2010
@@ -84,6 +84,9 @@ public interface ContinuumBuildAgentServ
     List<Map<String, Object>> getProjectsInPrepareBuildQueue()
         throws ContinuumBuildAgentException;
 
+    List<Map<String, Object>> getProjectsAndBuildDefinitionsInPrepareBuildQueue()
+        throws ContinuumBuildAgentException;
+
     List<Map<String, Object>> getProjectsInBuildQueue()
         throws ContinuumBuildAgentException;
 
@@ -93,8 +96,13 @@ public interface ContinuumBuildAgentServ
     Map<String, Object> getProjectCurrentlyPreparingBuild()
         throws ContinuumBuildAgentException;
 
+    List<Map<String, Object>> getProjectsAndBuildDefinitionsCurrentlyPreparingBuild()
+        throws ContinuumBuildAgentException;
+
     boolean isProjectGroupInQueue( int projectGroupId );
 
+    boolean isProjectScmRootInQueue( int projectScmRootId, List<Integer> projectIds );
+
     boolean isProjectCurrentlyBuilding( int projectId );
 
     boolean isProjectInBuildQueue( int projectId );

Modified: continuum/trunk/continuum-buildagent/continuum-buildagent-core/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentServiceImpl.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-buildagent/continuum-buildagent-core/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentServiceImpl.java?rev=933451&r1=933450&r2=933451&view=diff
==============================================================================
--- continuum/trunk/continuum-buildagent/continuum-buildagent-core/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentServiceImpl.java (original)
+++ continuum/trunk/continuum-buildagent/continuum-buildagent-core/src/main/java/org/apache/continuum/buildagent/ContinuumBuildAgentServiceImpl.java Tue Apr 13 00:26:42 2010
@@ -628,6 +628,35 @@ public class ContinuumBuildAgentServiceI
         }
     }
 
+    public List<Map<String, Object>> getProjectsAndBuildDefinitionsInPrepareBuildQueue()
+        throws ContinuumBuildAgentException
+    {
+        try
+        {
+            List<Map<String, Object>> projects = new ArrayList<Map<String, Object>>();
+
+            for ( PrepareBuildProjectsTask task : buildAgentTaskQueueManager.getProjectsInPrepareBuildQueue() )
+            {
+                for ( BuildContext context : task.getBuildContexts() )
+                {
+                    Map<String, Object> map = new HashMap<String, Object>();
+
+                    map.put( ContinuumBuildAgentUtil.KEY_PROJECT_ID, context.getProjectId() );
+                    map.put( ContinuumBuildAgentUtil.KEY_BUILD_DEFINITION_ID, context.getBuildDefinitionId() );
+
+                    projects.add( map );
+                }
+            }
+
+            return projects;
+        }
+        catch( TaskQueueManagerException e )
+        {
+            log.error( "Error occurred while retrieving projects in prepare build queue", e );
+            throw new ContinuumBuildAgentException( "Error occurred while retrieving projects in prepare build queue", e );
+        }
+    }
+
     public List<Map<String, Object>> getProjectsInBuildQueue()
         throws ContinuumBuildAgentException
     {
@@ -684,6 +713,36 @@ public class ContinuumBuildAgentServiceI
         }
     }
 
+    public List<Map<String, Object>> getProjectsAndBuildDefinitionsCurrentlyPreparingBuild()
+        throws ContinuumBuildAgentException
+    {
+        try
+        {
+            List<Map<String, Object>> projects = new ArrayList<Map<String, Object>>();
+
+            PrepareBuildProjectsTask task = buildAgentTaskQueueManager.getCurrentProjectInPrepareBuild();
+
+            if ( task != null )
+            {
+                for ( BuildContext context : task.getBuildContexts() )
+                {
+                    Map<String, Object> map = new HashMap<String, Object>();
+
+                    map.put( ContinuumBuildAgentUtil.KEY_PROJECT_ID, context.getProjectId() );
+                    map.put( ContinuumBuildAgentUtil.KEY_BUILD_DEFINITION_ID, context.getBuildDefinitionId() );
+
+                    projects.add( map );
+                }
+            }
+
+            return projects;
+        }
+        catch ( TaskQueueManagerException e )
+        {
+            log.error( "Error occurred while retrieving current projects in prepare build", e );
+            throw new ContinuumBuildAgentException( "Error occurred while retrieving current projects in prepare build", e );
+        }
+    }
     public Map<String, Object> getProjectCurrentlyBuilding()
         throws ContinuumBuildAgentException
     {
@@ -754,6 +813,61 @@ public class ContinuumBuildAgentServiceI
         return false;
     }
 
+    public boolean isProjectScmRootInQueue( int projectScmRootId, List<Integer> projectIds )
+    {
+        try
+        {
+            PrepareBuildProjectsTask currentPrepareBuildTask = buildAgentTaskQueueManager.getCurrentProjectInPrepareBuild();
+
+            if ( currentPrepareBuildTask != null && currentPrepareBuildTask.getScmRootId() == projectScmRootId )
+            {
+                return true;
+            }
+
+            BuildProjectTask currentBuildTask = buildAgentTaskQueueManager.getCurrentProjectInBuilding();
+            
+            if ( currentBuildTask != null )
+            {
+                int projectId = currentBuildTask.getProjectId();
+
+                for ( Integer pid : projectIds )
+                {
+                    if ( pid == projectId )
+                    {
+                        return true;
+                    }
+                }
+            }
+
+            for ( PrepareBuildProjectsTask task : buildAgentTaskQueueManager.getProjectsInPrepareBuildQueue() )
+            {
+                if ( task.getScmRootId() == projectScmRootId )
+                {
+                    return true;
+                }
+            }
+
+            for ( BuildProjectTask task : buildAgentTaskQueueManager.getProjectsInBuildQueue() )
+            {
+                int projectId = task.getProjectId();
+
+                for ( Integer pid : projectIds )
+                {
+                    if ( pid == projectId )
+                    {
+                        return true;
+                    }
+                }
+            }
+        }
+        catch ( TaskQueueManagerException e )
+        {
+            log.error( "Error while checking if project scm root " + projectScmRootId + " is queued in agent", e);
+        }
+
+        return false;
+    }
+
     public boolean isProjectCurrentlyBuilding( int projectId )
     {
         try

Modified: continuum/trunk/continuum-core/src/main/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManager.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-core/src/main/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManager.java?rev=933451&r1=933450&r2=933451&view=diff
==============================================================================
--- continuum/trunk/continuum-core/src/main/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManager.java (original)
+++ continuum/trunk/continuum-core/src/main/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManager.java Tue Apr 13 00:26:42 2010
@@ -38,6 +38,8 @@ import org.apache.continuum.dao.BuildDef
 import org.apache.continuum.dao.BuildResultDao;
 import org.apache.continuum.dao.ProjectDao;
 import org.apache.continuum.distributed.transport.slave.SlaveBuildAgentTransportClient;
+import org.apache.continuum.distributed.transport.slave.SlaveBuildAgentTransportService;
+import org.apache.continuum.model.project.ProjectScmRoot;
 import org.apache.continuum.taskqueue.BuildProjectTask;
 import org.apache.continuum.taskqueue.OverallDistributedBuildQueue;
 import org.apache.continuum.taskqueue.PrepareBuildProjectsTask;
@@ -131,9 +133,8 @@ public class DefaultDistributedBuildMana
                     {
                         try
                         {
-                            SlaveBuildAgentTransportClient client =
-                                new SlaveBuildAgentTransportClient( new URL( agent.getUrl() ) );
-    
+                            SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( agent.getUrl() );
+
                             if ( client.ping() )
                             {
                                 log.info(
@@ -180,8 +181,7 @@ public class DefaultDistributedBuildMana
                 {
                     try
                     {
-                        SlaveBuildAgentTransportClient client =
-                            new SlaveBuildAgentTransportClient( new URL( agent.getUrl() ) );
+                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( agent.getUrl() );
     
                         if ( client.ping() )
                         {
@@ -215,34 +215,41 @@ public class DefaultDistributedBuildMana
     }
 
     public void prepareBuildProjects( Map<Integer, Integer>projectsBuildDefinitionsMap, BuildTrigger buildTrigger, int projectGroupId, 
-                                      String projectGroupName, String scmRootAddress, int scmRootId )
+                                      String projectGroupName, String scmRootAddress, int scmRootId, List<ProjectScmRoot> scmRoots )
         throws ContinuumException, NoBuildAgentException, NoBuildAgentInGroupException
     {
     	PrepareBuildProjectsTask task = new PrepareBuildProjectsTask( projectsBuildDefinitionsMap, buildTrigger,
                                                                       projectGroupId, projectGroupName, 
                                                                       scmRootAddress, scmRootId );
 
-        OverallDistributedBuildQueue overallDistributedBuildQueue = getOverallDistributedBuildQueueByGroup( projectGroupId );
-        
-        if ( hasBuildagentGroup( projectsBuildDefinitionsMap ) && overallDistributedBuildQueue == null )
+        OverallDistributedBuildQueue overallDistributedBuildQueue = getOverallDistributedBuildQueueByGroup( projectGroupId, scmRoots, scmRootId );
+
+        if ( overallDistributedBuildQueue == null )
         {
-            if ( !hasBuildagentInGroup( projectsBuildDefinitionsMap ) )
-            {
-                log.warn( "No build agent configured in build agent group. Not building projects." );
+            log.info( "no overall build queue by group" );
 
-                throw new NoBuildAgentInGroupException( "No build agent configured in build agent group" );
+            if ( hasBuildagentGroup( projectsBuildDefinitionsMap ) )
+            {
+                if ( !hasBuildagentInGroup( projectsBuildDefinitionsMap ) )
+                {
+                    log.warn( "No build agent configured in build agent group. Not building projects." );
+    
+                    throw new NoBuildAgentInGroupException( "No build agent configured in build agent group" );
+                }
+                else
+                {
+                    // get overall distributed build queue from build agent group
+                    overallDistributedBuildQueue = getOverallDistributedBuildQueueByAgentGroup( projectsBuildDefinitionsMap );
+                    log.info( "got overall build queue by agent group" );
+                }
             }
             else
             {
-                // get overall distributed build queue from build agent group
-                overallDistributedBuildQueue = getOverallDistributedBuildQueueByAgentGroup( projectsBuildDefinitionsMap );
+                // project does not have build agent group
+                overallDistributedBuildQueue = getOverallDistributedBuildQueue();
+                log.info( "get overall build queue in all agents" );
             }
         }
-        else if ( overallDistributedBuildQueue == null )
-        {
-            // project does not have build agent group
-            overallDistributedBuildQueue = getOverallDistributedBuildQueue();
-        }
 
         if ( overallDistributedBuildQueue != null )
         {
@@ -329,8 +336,7 @@ public class DefaultDistributedBuildMana
                 {
                     if ( isAgentAvailable( buildAgentUrl ) )
                     {
-                        SlaveBuildAgentTransportClient client =
-                            new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
 
                         List<Map<String, Object>> projects = client.getProjectsInPrepareBuildQueue();
     
@@ -353,6 +359,9 @@ public class DefaultDistributedBuildMana
             }
         }
 
+        // call reload in case we disable a build agent
+        reload();
+
         return map;
     }
 
@@ -369,8 +378,7 @@ public class DefaultDistributedBuildMana
                 {
                     if ( isAgentAvailable( buildAgentUrl ) )
                     {
-                        SlaveBuildAgentTransportClient client =
-                            new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                         Map<String, Object> project = client.getProjectCurrentlyPreparingBuild();
     
                         if ( !project.isEmpty() )
@@ -389,6 +397,10 @@ public class DefaultDistributedBuildMana
                 }
             }
         }
+
+        // call reload in case we disable a build agent
+        reload();
+
         return map;
     }
  
@@ -405,8 +417,7 @@ public class DefaultDistributedBuildMana
                 {
                     if ( isAgentAvailable( buildAgentUrl ) )
                     {
-                        SlaveBuildAgentTransportClient client =
-                            new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                         Map<String, Object> project = client.getProjectCurrentlyBuilding();
     
                         if ( !project.isEmpty() )
@@ -426,6 +437,9 @@ public class DefaultDistributedBuildMana
             }
         }
 
+        // call reload in case we disable a build agent
+        reload();
+
         return map;
     }
 
@@ -444,8 +458,7 @@ public class DefaultDistributedBuildMana
                 {
                     if ( isAgentAvailable( buildAgentUrl ) )
                     {
-                        SlaveBuildAgentTransportClient client =
-                            new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                         List<Map<String, Object>> projects = client.getProjectsInBuildQueue();
     
                         for ( Map<String, Object> context : projects )
@@ -466,6 +479,10 @@ public class DefaultDistributedBuildMana
                 }
             }
         }
+
+        // call reload in case we disable a build agent
+        reload();
+
         return map;
     }
 
@@ -494,10 +511,13 @@ public class DefaultDistributedBuildMana
         {
             if ( isAgentAvailable( buildAgentUrl ) )
             {
-                SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
     
                 client.cancelBuild();
             }
+
+            // call reload in case we disable the build agent
+            reload();
         }
         catch ( MalformedURLException e )
         {
@@ -515,40 +535,40 @@ public class DefaultDistributedBuildMana
         throws ContinuumException
     {
         Map<String, Object> map = new HashMap<String, Object>();
-    
+
         String buildAgentUrl = getBuildAgent( projectId );
-    
+
         if ( buildAgentUrl == null )
         {
             return null;
         }
-    
+
         try
         {
             if ( isAgentAvailable( buildAgentUrl ) )
             {
-                SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
-        
+                SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
+
                 Map<String, Object> result = client.getBuildResult( projectId );
-        
+
                 if ( result != null )
                 {
                     int buildDefinitionId = ContinuumBuildConstant.getBuildDefinitionId( result );
-        
+
                     Project project = projectDao.getProjectWithAllDetails( projectId );
                     BuildDefinition buildDefinition = buildDefinitionDao.getBuildDefinition( buildDefinitionId );
-        
+
                     BuildResult oldBuildResult =
                         buildResultDao.getLatestBuildResultForBuildDefinition( projectId, buildDefinitionId );
-        
+
                     BuildResult buildResult = distributedBuildUtil.convertMapToBuildResult( result );
                     buildResult.setBuildDefinition( buildDefinition );
                     buildResult.setBuildNumber( project.getBuildNumber() + 1 );
                     buildResult.setModifiedDependencies( distributedBuildUtil.getModifiedDependencies( oldBuildResult, result ) );
                     buildResult.setScmResult( distributedBuildUtil.getScmResult( result ) );
-        
+
                     String buildOutput = ContinuumBuildConstant.getBuildOutput( result );
-        
+
                     map.put( ContinuumBuildConstant.KEY_BUILD_RESULT, buildResult );
                     map.put( ContinuumBuildConstant.KEY_BUILD_OUTPUT, buildOutput );
                 }
@@ -562,7 +582,10 @@ public class DefaultDistributedBuildMana
         {
             throw new ContinuumException( "Error while retrieving build result for project" + projectId, e );
         }
-    
+
+        // call reload in case we disable the build agent
+        reload();
+
         return map;
     }
 
@@ -570,15 +593,15 @@ public class DefaultDistributedBuildMana
         throws ContinuumException
     {
         List<Installation> installations = new ArrayList<Installation>();
-    
+
         try
         {
             if ( isAgentAvailable( buildAgentUrl ) )
             {
-                SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
-        
+                SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
+
                 List<Map<String, String>> installationsList = client.getAvailableInstallations();
-        
+
                 for ( Map context : installationsList )
                 {
                     Installation installation = new Installation();
@@ -589,6 +612,10 @@ public class DefaultDistributedBuildMana
                     installations.add( installation );
                 }
             }
+
+            // call reload in case we disable the build agent
+            reload();
+
             return installations;
         }
         catch ( Exception e )
@@ -601,26 +628,26 @@ public class DefaultDistributedBuildMana
         throws ContinuumException
     {
         BuildResult buildResult = buildResultDao.getLatestBuildResultForProject( projectId );
-    
+
         if ( buildResult != null )
         {
             String buildAgentUrl = buildResult.getBuildUrl();
-    
+
             if ( buildAgentUrl == null )
             {
                 return "";
             }
-    
+
             try
             {
                 if ( directory == null )
                 {
                     directory = "";
                 }
-    
+
                 if ( isAgentAvailable( buildAgentUrl ) )
                 {
-                    SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                    SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                     return client.generateWorkingCopyContent( projectId, directory, baseUrl, imageBaseUrl );
                 }
             }
@@ -633,6 +660,10 @@ public class DefaultDistributedBuildMana
                 log.error( "Error while generating working copy content from build agent " + buildAgentUrl, e );
             }
         }
+
+        // call reload in case we disable the build agent
+        reload();
+
         return "";
     }
     
@@ -640,21 +671,21 @@ public class DefaultDistributedBuildMana
         throws ContinuumException
     {
         BuildResult buildResult = buildResultDao.getLatestBuildResultForProject( projectId );
-    
+
         if ( buildResult != null )
         {
             String buildAgentUrl = buildResult.getBuildUrl();
-    
+
             if ( buildAgentUrl == null )
             {
                 return "";
             }
-    
+
             try
             {
                 if ( isAgentAvailable( buildAgentUrl ) )
                 {
-                    SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                    SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                     return client.getProjectFileContent( projectId, directory, filename );
                 }
             }
@@ -667,6 +698,10 @@ public class DefaultDistributedBuildMana
                 log.error( "Error while retrieving content of " + filename, e );
             }
         }
+
+        // call reload in case we disable the build agent
+        reload();
+
         return "";
     }
 
@@ -677,7 +712,7 @@ public class DefaultDistributedBuildMana
         {
             if ( isAgentAvailable( buildAgentUrl ) )
             {
-                SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                 client.removeFromPrepareBuildQueue( projectGroupId, scmRootId );
             }
         }
@@ -695,6 +730,9 @@ public class DefaultDistributedBuildMana
             throw new ContinuumException( "Error occurred while removing projectGroupId=" + projectGroupId + " scmRootId=" +
                                           scmRootId + " from prepare build queue of agent " + buildAgentUrl, e );
         }
+
+        // call reload in case we disable the build agent
+        reload();
     }
 
     public void removeFromBuildQueue( String buildAgentUrl, int projectId, int buildDefinitionId )
@@ -704,7 +742,7 @@ public class DefaultDistributedBuildMana
         {
             if ( isAgentAvailable( buildAgentUrl ) )
             {
-                SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                 client.removeFromBuildQueue( projectId, buildDefinitionId );
             }
         }
@@ -722,6 +760,9 @@ public class DefaultDistributedBuildMana
             throw new ContinuumException( "Error occurred while removing project " + projectId + 
                                           " from build queue of agent " + buildAgentUrl, e );
         }
+
+        // call reload in case we disable the build agent
+        reload();
     }
 
     public void removeFromPrepareBuildQueue( List<String> hashCodes )
@@ -735,7 +776,7 @@ public class DefaultDistributedBuildMana
                 {
                     if ( isAgentAvailable( buildAgentUrl ) )
                     {
-                        SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                         client.removeFromPrepareBuildQueue( hashCodes );
                     }
                 }
@@ -749,6 +790,9 @@ public class DefaultDistributedBuildMana
                 }
             }
         }
+
+        // call reload in case we disable a build agent
+        reload();
     }
 
     public void removeFromBuildQueue( List<String> hashCodes )
@@ -762,7 +806,7 @@ public class DefaultDistributedBuildMana
                 {
                     if ( isAgentAvailable( buildAgentUrl ) )
                     {
-                        SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                         client.removeFromBuildQueue( hashCodes );
                     }
                 }
@@ -776,11 +820,171 @@ public class DefaultDistributedBuildMana
                 }
             }
         }
+
+        // call reload in case we disable a build agent
+        reload();
+    }
+
+    public boolean isProjectInAnyPrepareBuildQueue( int projectId, int buildDefinitionId )
+        throws ContinuumException
+    {
+        boolean found = false;
+
+        synchronized( overallDistributedBuildQueues )
+        {
+            for ( String buildAgentUrl : overallDistributedBuildQueues.keySet() )
+            {
+                try
+                {
+                    if ( isAgentAvailable( buildAgentUrl ) )
+                    {
+                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
+
+                        List<Map<String, Object>> projects = client.getProjectsAndBuildDefinitionsInPrepareBuildQueue();
+    
+                        for ( Map<String, Object> context : projects )
+                        {
+                            int pid = ContinuumBuildConstant.getProjectId( context );
+                            int buildId = ContinuumBuildConstant.getBuildDefinitionId( context );
+
+                            if ( pid == projectId && buildId == buildDefinitionId )
+                            {
+                                found = true;
+                                break;
+                            }
+
+                        }
+                    }
+
+                    if ( found )
+                    {
+                        break;
+                    }
+                }
+                catch ( MalformedURLException e )
+                {
+                    throw new ContinuumException( "Invalid build agent url: " + buildAgentUrl ); 
+                }
+                catch ( Exception e )
+                {
+                    throw new ContinuumException( "Error while retrieving projects in prepare build queue", e );
+                }
+            }
+        }
+
+        // call reload in case we disable a build agent
+        reload();
+
+        if ( found )
+        {
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public boolean isProjectInAnyBuildQueue( int projectId, int buildDefinitionId )
+        throws ContinuumException
+    {
+        Map<String, List<BuildProjectTask>> map = getProjectsInBuildQueue();
+
+        for ( String url : map.keySet() )
+        {
+            for ( BuildProjectTask task : map.get( url ) )
+            {
+                if ( task.getProjectId() == projectId && task.getBuildDefinitionId() == buildDefinitionId )
+                {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+    public boolean isProjectCurrentlyPreparingBuild( int projectId, int buildDefinitionId )
+        throws ContinuumException
+    {
+        boolean found = false;
+
+        synchronized( overallDistributedBuildQueues )
+        {
+            for ( String buildAgentUrl : overallDistributedBuildQueues.keySet() )
+            {
+                try
+                {
+                    if ( isAgentAvailable( buildAgentUrl ) )
+                    {
+                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
+                        List<Map<String, Object>> projects = client.getProjectsAndBuildDefinitionsCurrentlyPreparingBuild();
+    
+                        for ( Map<String, Object> context : projects )
+                        {
+                            int pid = ContinuumBuildConstant.getProjectId( context );
+                            int buildId = ContinuumBuildConstant.getBuildDefinitionId( context );
+    
+                            if ( pid == projectId && buildId == buildDefinitionId )
+                            {
+                                found = true;
+                                break;
+                            }
+                        }
+                    }
+
+                    if ( found )
+                    {
+                        break;
+                    }
+                }
+                catch ( MalformedURLException e )
+                {
+                    throw new ContinuumException( "Invalid build agent url: " + buildAgentUrl );
+                }
+                catch ( Exception e )
+                {
+                    throw new ContinuumException( "Error retrieving projects currently preparing build in " + buildAgentUrl, e );
+                }
+            }
+        }
+
+        // call reload in case we disable a build agent
+        reload();
+
+        if ( found )
+        {
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public boolean isProjectCurrentlyBuilding( int projectId, int buildDefinitionId )
+        throws ContinuumException
+    {
+        Map<String, BuildProjectTask> map = getProjectsCurrentlyBuilding();
+
+        for ( String url : map.keySet() )
+        {
+            BuildProjectTask task = map.get( url );
+
+            if ( task.getProjectId() == projectId && task.getBuildDefinitionId() == buildDefinitionId )
+            {
+                return true;
+            }
+        }
+
+        return false;
     }
 
     private String getBuildAgent( int projectId )
         throws ContinuumException
     {
+        String agentUrl = null;
+
         synchronized( overallDistributedBuildQueues )
         {
             for ( String buildAgentUrl : overallDistributedBuildQueues.keySet() )
@@ -794,12 +998,12 @@ public class DefaultDistributedBuildMana
                     {
                         if ( isAgentAvailable( buildAgentUrl ) )
                         {
-                            SlaveBuildAgentTransportClient client = 
-                                new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                            SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                             
                             if ( client.isProjectCurrentlyBuilding( projectId ) )
                             {
-                                return buildAgentUrl;
+                                agentUrl = buildAgentUrl;
+                                break;
                             }
                         }
                     }
@@ -814,8 +1018,11 @@ public class DefaultDistributedBuildMana
                 }
             }
         }
-    
-        return null;
+
+        // call reload in case we disable a build agent
+        reload();
+
+        return agentUrl;
     }
 
     private void createDistributedBuildQueueForAgent( String buildAgentUrl )
@@ -862,9 +1069,12 @@ public class DefaultDistributedBuildMana
         return null;
     }
 
-    private OverallDistributedBuildQueue getOverallDistributedBuildQueueByGroup( int projectGroupId )
+    private OverallDistributedBuildQueue getOverallDistributedBuildQueueByScmRoot( ProjectScmRoot scmRoot, int projectGroupId )
         throws ContinuumException
     {
+        OverallDistributedBuildQueue overallDistributedBuildQueue = null;
+        int scmRootId = scmRoot.getId();
+
         synchronized( overallDistributedBuildQueues )
         {
             for ( String buildAgentUrl : overallDistributedBuildQueues.keySet() )
@@ -873,51 +1083,96 @@ public class DefaultDistributedBuildMana
 
                 try
                 {
-                    
                     for ( PrepareBuildProjectsTask task : distributedBuildQueue.getProjectsInQueue() )
                     {
-                        if ( task.getProjectGroupId() == projectGroupId )
+                        if ( task.getProjectScmRootId() == scmRootId )
                         {
-                            return distributedBuildQueue;
+                            overallDistributedBuildQueue = distributedBuildQueue;
+                            break;
                         }
                     }
 
-                    Task task = distributedBuildQueue.getDistributedBuildTaskQueueExecutor().getCurrentTask();
-                    if ( task != null && ( (PrepareBuildProjectsTask) task ).getProjectGroupId() == projectGroupId )
+                    if ( overallDistributedBuildQueue == null )
                     {
-                        return distributedBuildQueue;
+                        Task task = distributedBuildQueue.getDistributedBuildTaskQueueExecutor().getCurrentTask();
+                        if ( task != null && ( (PrepareBuildProjectsTask) task ).getProjectScmRootId() == scmRootId )
+                        {
+                            overallDistributedBuildQueue = distributedBuildQueue;
+                        }
                     }
 
-                    if ( isAgentAvailable( buildAgentUrl ) )
+                    if ( overallDistributedBuildQueue == null )
                     {
-                        SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
-    
-                        if ( client.isProjectGroupInQueue( projectGroupId ) )
+                        if ( isAgentAvailable( buildAgentUrl ) )
                         {
-                            return distributedBuildQueue;
+                            List<Project> projects = projectDao.getProjectsInGroup( projectGroupId );
+                            List<Integer> pIds = new ArrayList<Integer>();
+                            
+                            for ( Project project : projects )
+                            {
+                                if ( project.getScmUrl().startsWith( scmRoot.getScmRootAddress() ) )
+                                {
+                                    pIds.add( project.getId() );
+                                }
+                            }
+
+                            SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
+
+                            if ( client.isProjectScmRootInQueue( scmRootId, pIds ) )
+                            {
+                                overallDistributedBuildQueue = distributedBuildQueue;
+                            }
                         }
                     }
+
+                    if ( overallDistributedBuildQueue != null )
+                    {
+                        break;
+                    }
                 }
                 catch ( TaskQueueException e )
                 {
-                    log.error( "Error occurred while retrieving distributed build queue of projectGroupId=" + projectGroupId, e );
-                    throw new ContinuumException( "Error occurred while retrieving distributed build queue of group", e );
+                    log.error( "Error occurred while retrieving distributed build queue of scmRootId=" + scmRootId, e );
+                    throw new ContinuumException( "Error occurred while retrieving distributed build queue of scmRoot", e );
                 }
                 catch ( MalformedURLException e )
                 {
-                    log.error( "Error occurred while retrieving distributed build queue of projectGroupId=" + projectGroupId + 
+                    log.error( "Error occurred while retrieving distributed build queue of scmRootId=" + scmRootId + 
                                ": Invalid build agent url " + buildAgentUrl );
-                    throw new ContinuumException( "Error occurred while retrieving distributed build queue of projectGroupId=" + projectGroupId + 
+                    throw new ContinuumException( "Error occurred while retrieving distributed build queue of scmRootId=" + scmRootId + 
                                ": Invalid build agent url " + buildAgentUrl );
                 }
                 catch ( Exception e )
                 {
-                    log.error( "Error occurred while retrieving distributed build queue of projectGroupId=" + projectGroupId, e );
-                    throw new ContinuumException( "Error occurred while retrieving distributed build queue of group", e );
+                    log.error( "Error occurred while retrieving distributed build queue of scmRootId=" + scmRootId, e );
+                    throw new ContinuumException( "Error occurred while retrieving distributed build queue of scmRoot", e );
                 }
             }
         }
 
+        // call reload in case we disable a build agent
+        reload();
+
+        return overallDistributedBuildQueue;
+    }
+
+    private OverallDistributedBuildQueue getOverallDistributedBuildQueueByGroup( int projectGroupId, List<ProjectScmRoot> scmRoots, int scmRootId )
+        throws ContinuumException
+    {
+        if ( scmRoots != null )
+        {
+            for ( ProjectScmRoot scmRoot : scmRoots )
+            {
+                if ( scmRoot.getId() == scmRootId )
+                {
+                    break;
+                }
+                else if ( scmRoot.getProjectGroup().getId() == projectGroupId )
+                {
+                    return getOverallDistributedBuildQueueByScmRoot( scmRoot, projectGroupId );
+                }
+            }
+        }
         return null;
     }
 
@@ -989,7 +1244,7 @@ public class DefaultDistributedBuildMana
                                 {
                                     if ( isAgentAvailable( buildAgentUrl ) )
                                     {
-                                        SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                                        SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                                         int agentBuildSize = client.getBuildSizeOfAgent();
     
                                         if ( idx == 0 )
@@ -1018,6 +1273,9 @@ public class DefaultDistributedBuildMana
                         }
                     }
                 }
+
+                // call reload in case we disable a build agent
+                reload();
             }
         }
         return whereToBeQueued;
@@ -1049,7 +1307,7 @@ public class DefaultDistributedBuildMana
                     {
                         if ( isAgentAvailable( buildAgentUrl ) )
                         {
-                            SlaveBuildAgentTransportClient client = new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+                            SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
                             int agentBuildSize = client.getBuildSizeOfAgent();
     
                             if ( idx == 0 )
@@ -1079,6 +1337,9 @@ public class DefaultDistributedBuildMana
             }
         }
 
+        // call reload in case we disable a build agent
+        reload();
+
         return whereToBeQueued;
     }
 
@@ -1099,7 +1360,7 @@ public class DefaultDistributedBuildMana
                 projects.add( projectDao.getProjectWithDependencies( projectId ) );
             }
 
-            projects = ProjectSorter.getSortedProjects( projects, null );
+            projects = ProjectSorter.getSortedProjects( projects, log );
 
             int buildDefinitionId = projectsAndBuildDefinitions.get( projects.get( 0 ).getId() );
             BuildDefinition buildDefinition = buildDefinitionDao.getBuildDefinition( buildDefinitionId );
@@ -1152,8 +1413,7 @@ public class DefaultDistributedBuildMana
     {
         try
         {
-            SlaveBuildAgentTransportClient client =
-                new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+            SlaveBuildAgentTransportService client = createSlaveBuildAgentTransportClientConnection( buildAgentUrl );
 
             return client.ping();
         }
@@ -1192,8 +1452,6 @@ public class DefaultDistributedBuildMana
                 {
                     throw new ContinuumException( "Unable to disable build agent: " + buildAgentUrl, e );
                 }
-
-                removeDistributedBuildQueueOfAgent( buildAgentUrl );
             }
         }
     }
@@ -1216,6 +1474,12 @@ public class DefaultDistributedBuildMana
                buildAgentGroup.getBuildAgents().size() > 0 ? true : false;
     }
 
+    public SlaveBuildAgentTransportService createSlaveBuildAgentTransportClientConnection( String buildAgentUrl ) 
+        throws MalformedURLException, Exception
+    {
+        return new SlaveBuildAgentTransportClient( new URL( buildAgentUrl ) );
+    }
+
     // for unit testing
 
     public void setOverallDistributedBuildQueues( Map<String, OverallDistributedBuildQueue> overallDistributedBuildQueues )
@@ -1243,4 +1507,9 @@ public class DefaultDistributedBuildMana
         this.buildResultDao = buildResultDao;
     }
 
+    public void setContainer( PlexusContainer container )
+    {
+        this.container = container;
+    }
+
 }
\ No newline at end of file

Modified: continuum/trunk/continuum-core/src/main/java/org/apache/continuum/release/distributed/manager/DefaultDistributedReleaseManager.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-core/src/main/java/org/apache/continuum/release/distributed/manager/DefaultDistributedReleaseManager.java?rev=933451&r1=933450&r2=933451&view=diff
==============================================================================
--- continuum/trunk/continuum-core/src/main/java/org/apache/continuum/release/distributed/manager/DefaultDistributedReleaseManager.java (original)
+++ continuum/trunk/continuum-core/src/main/java/org/apache/continuum/release/distributed/manager/DefaultDistributedReleaseManager.java Tue Apr 13 00:26:42 2010
@@ -105,6 +105,9 @@ public class DefaultDistributedReleaseMa
                 return client.getReleasePluginParameters( projectId, pomFilename );
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to retrieve release plugin parameters because build agent " + buildAgentUrl + " is not available" );
         }
         catch ( MalformedURLException e )
@@ -139,6 +142,9 @@ public class DefaultDistributedReleaseMa
                 return client.processProject( projectId, pomFilename, autoVersionSubmodules );
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to process project for releasing because build agent " + buildAgentUrl + " is unavailable" );
         }
         catch ( MalformedURLException e )
@@ -181,6 +187,9 @@ public class DefaultDistributedReleaseMa
                 return releaseId;
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to prepare release project because the build agent " + buildAgentUrl + " is not available" );
         }
         catch ( MalformedURLException e )
@@ -221,6 +230,9 @@ public class DefaultDistributedReleaseMa
                 return releaseResult;
             }
 
+            // call reload in case we disable a build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to get release result of " + releaseId + 
                                                  " because the build agent " + buildAgentUrl + " is not available" );
         }
@@ -254,6 +266,9 @@ public class DefaultDistributedReleaseMa
                 return client.getListener( releaseId );
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to get listener for " + releaseId + 
                                                  " because the build agent " + buildAgentUrl + " is not available" );
         }
@@ -287,6 +302,9 @@ public class DefaultDistributedReleaseMa
                 client.removeListener( releaseId );
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to remove listener of " + releaseId + 
                                                  " because the build agent " + buildAgentUrl + " is not available" );
         }
@@ -321,6 +339,9 @@ public class DefaultDistributedReleaseMa
                 return client.getPreparedReleaseName( releaseId );
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to get prepared release name of " + releaseId + 
                                                  " because the build agent " + buildAgentUrl + " is not available" );
         }
@@ -387,6 +408,9 @@ public class DefaultDistributedReleaseMa
                 addReleaseInProgress( releaseId, "perform", projectId, username );
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to perform release of " + releaseId + 
                                                  " because the build agent " + buildAgentUrl + " is not available" );
         }
@@ -447,6 +471,9 @@ public class DefaultDistributedReleaseMa
                 return releaseId;
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to perform release because the build agent " + buildAgentUrl + " is not available" );
         }
         catch ( MalformedURLException e )
@@ -479,6 +506,9 @@ public class DefaultDistributedReleaseMa
                 client.releaseRollback( releaseId, projectId );
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Unable to rollback release " + releaseId + 
                                                  " because the build agent " + buildAgentUrl + " is not available" );
         }
@@ -517,6 +547,9 @@ public class DefaultDistributedReleaseMa
                 return result;
             }
 
+            // call reload in case we disable the build agent
+            distributedBuildManager.reload();
+
             throw new ContinuumReleaseException( "Failed to cleanup release of " + releaseId + 
                                                  " because the build agent " + buildAgentUrl + " is not available" );
         }
@@ -587,6 +620,16 @@ public class DefaultDistributedReleaseMa
             releasesInProgress = releasesMap;
         }
 
+        try
+        {
+            // call reload in case we disable a build agent
+            distributedBuildManager.reload();
+        }
+        catch ( Exception e )
+        {
+            throw new ContinuumReleaseException( e.getMessage(), e );
+        }
+
         return releases;
     }
     

Modified: continuum/trunk/continuum-core/src/main/java/org/apache/maven/continuum/DefaultContinuum.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-core/src/main/java/org/apache/maven/continuum/DefaultContinuum.java?rev=933451&r1=933450&r2=933451&view=diff
==============================================================================
--- continuum/trunk/continuum-core/src/main/java/org/apache/maven/continuum/DefaultContinuum.java (original)
+++ continuum/trunk/continuum-core/src/main/java/org/apache/maven/continuum/DefaultContinuum.java Tue Apr 13 00:26:42 2010
@@ -945,37 +945,26 @@ public class DefaultContinuum
             {
                 for ( Integer buildDefId : buildDefIds )
                 {
-                    try
+                    if ( buildDefId != null && isProjectOkToBuild( project.getId(), buildDefId ) )
                     {
-                        if ( buildDefId != null &&
-                            !parallelBuildsManager.isInAnyBuildQueue( project.getId(), buildDefId ) &&
-                            !parallelBuildsManager.isInAnyCheckoutQueue( project.getId() ) &&
-                            !parallelBuildsManager.isInPrepareBuildQueue( project.getId() ) &&
-                            !parallelBuildsManager.isProjectCurrentlyPreparingBuild( project.getId() ) )
-                        {
-                            ProjectScmRoot scmRoot = getProjectScmRootByProject( project.getId() );
+                        ProjectScmRoot scmRoot = getProjectScmRootByProject( project.getId() );
 
-                            Map<Integer, Integer> projectsAndBuildDefinitionsMap = map.get( scmRoot );
+                        Map<Integer, Integer> projectsAndBuildDefinitionsMap = map.get( scmRoot );
 
-                            if ( projectsAndBuildDefinitionsMap == null )
-                            {
-                                projectsAndBuildDefinitionsMap = new HashMap<Integer, Integer>();
-                            }
+                        if ( projectsAndBuildDefinitionsMap == null )
+                        {
+                            projectsAndBuildDefinitionsMap = new HashMap<Integer, Integer>();
+                        }
 
-                            projectsAndBuildDefinitionsMap.put( project.getId(), buildDefId );
+                        projectsAndBuildDefinitionsMap.put( project.getId(), buildDefId );
 
-                            map.put( scmRoot, projectsAndBuildDefinitionsMap );
+                        map.put( scmRoot, projectsAndBuildDefinitionsMap );
 
-                            if ( !sortedScmRoot.contains( scmRoot ) )
-                            {
-                                sortedScmRoot.add( scmRoot );
-                            }
+                        if ( !sortedScmRoot.contains( scmRoot ) )
+                        {
+                            sortedScmRoot.add( scmRoot );
                         }
                     }
-                    catch ( BuildManagerException e )
-                    {
-                        throw new ContinuumException( e.getMessage(), e );
-                    }
                 }
             }
         }
@@ -987,7 +976,7 @@ public class DefaultContinuum
             try
             {
                 prepareBuildProjects( map.get( scmRoot ), buildTrigger, scmRoot.getScmRootAddress(),
-                                      scmRoot.getProjectGroup().getId(), scmRoot.getId() );
+                                      scmRoot.getProjectGroup().getId(), scmRoot.getId(), sortedScmRoot );
             }
             catch ( NoBuildAgentException e )
             {
@@ -1031,26 +1020,20 @@ public class DefaultContinuum
             throw new ContinuumException( "Project (id=" + projectId + " doens't have a default build definition." );
         }
 
-        try
-        {
-            if ( parallelBuildsManager.isInAnyBuildQueue( projectId, buildDef.getId() ) ||
-                parallelBuildsManager.isInAnyCheckoutQueue( projectId ) ||
-                parallelBuildsManager.isInPrepareBuildQueue( projectId ) )
-            {
-                return;
-            }
-        }
-        catch ( BuildManagerException e )
+        if ( !isProjectOkToBuild( projectId, buildDef.getId() ) )
         {
-            throw new ContinuumException( e.getMessage(), e );
+            return;
         }
 
         Map<Integer, Integer> projectsBuildDefinitionsMap = new HashMap<Integer, Integer>();
         projectsBuildDefinitionsMap.put( projectId, buildDef.getId() );
 
         ProjectScmRoot scmRoot = getProjectScmRootByProject( projectId );
+        List<ProjectScmRoot> sortedScmRoot = new ArrayList<ProjectScmRoot>();
+        sortedScmRoot.add(scmRoot);
+
         prepareBuildProjects( projectsBuildDefinitionsMap, buildTrigger, scmRoot.getScmRootAddress(),
-                              scmRoot.getProjectGroup().getId(), scmRoot.getId() );
+                              scmRoot.getProjectGroup().getId(), scmRoot.getId(), sortedScmRoot );
     }
 
     public void buildProject( int projectId, int buildDefinitionId, BuildTrigger buildTrigger )
@@ -1062,26 +1045,20 @@ public class DefaultContinuum
             throw new ContinuumException( "Project (id=" + projectId + ") is currently in release stage." );
         }
 
-        try
-        {
-            if ( parallelBuildsManager.isInAnyBuildQueue( projectId, buildDefinitionId ) ||
-                parallelBuildsManager.isInAnyCheckoutQueue( projectId ) ||
-                parallelBuildsManager.isInPrepareBuildQueue( projectId ) )
-            {
-                return;
-            }
-        }
-        catch ( BuildManagerException e )
+        if ( !isProjectOkToBuild( projectId, buildDefinitionId ) )
         {
-            throw new ContinuumException( e.getMessage(), e );
+            return;
         }
 
         Map<Integer, Integer> projectsBuildDefinitionsMap = new HashMap<Integer, Integer>();
         projectsBuildDefinitionsMap.put( projectId, buildDefinitionId );
 
         ProjectScmRoot scmRoot = getProjectScmRootByProject( projectId );
+        List<ProjectScmRoot> sortedScmRoot = new ArrayList<ProjectScmRoot>();
+        sortedScmRoot.add(scmRoot);
+
         prepareBuildProjects( projectsBuildDefinitionsMap, buildTrigger, scmRoot.getScmRootAddress(),
-                              scmRoot.getProjectGroup().getId(), scmRoot.getId() );
+                              scmRoot.getProjectGroup().getId(), scmRoot.getId(), sortedScmRoot );
     }
 
     public BuildResult getBuildResult( int buildId )
@@ -3434,27 +3411,6 @@ public class DefaultContinuum
         {
             int projectId = project.getId();
 
-            try
-            {
-                // check if project already in queue
-                if ( parallelBuildsManager.isInAnyBuildQueue( projectId ) ||
-                    parallelBuildsManager.isProjectInAnyCurrentBuild( projectId ) ||
-                    parallelBuildsManager.isInPrepareBuildQueue( projectId ) ||
-                    parallelBuildsManager.isProjectCurrentlyPreparingBuild( projectId ) )
-                {
-                    continue;
-                }
-
-                if ( parallelBuildsManager.isInAnyCheckoutQueue( projectId ) )
-                {
-                    parallelBuildsManager.removeProjectFromCheckoutQueue( projectId );
-                }
-            }
-            catch ( BuildManagerException e )
-            {
-                throw new ContinuumException( e.getMessage(), e );
-            }
-
             int buildDefId = -1;
 
             if ( bds != null )
@@ -3497,7 +3453,13 @@ public class DefaultContinuum
             if ( buildDefId == -1 )
             {
                 log.info( "Project " + projectId +
-                    " don't have a default build definition defined in the project or project group, will not be included in group prepare." );
+                    " don't have a default build definition defined in the project or project group, will not be included in group build." );
+                continue;
+            }
+
+            // check if project already in queue
+            if ( !isProjectOkToBuild( projectId, buildDefId ) )
+            {
                 continue;
             }
 
@@ -3533,44 +3495,29 @@ public class DefaultContinuum
         {
             int projectId = project.getId();
 
-            try
+            // check if project already in queue
+            if ( !isProjectOkToBuild( projectId, buildDefinitionId ) )
             {
-                // check if project already in queue
-                if ( parallelBuildsManager.isInAnyBuildQueue( projectId ) ||
-                    parallelBuildsManager.isProjectInAnyCurrentBuild( projectId ) ||
-                    parallelBuildsManager.isInPrepareBuildQueue( projectId ) ||
-                    parallelBuildsManager.isProjectCurrentlyPreparingBuild( projectId ) )
-                {
-                    log.info( "not building" );
-                    continue;
-                }
-
-                if ( parallelBuildsManager.isInAnyCheckoutQueue( projectId ) )
-                {
-                    parallelBuildsManager.removeProjectFromCheckoutQueue( projectId );
-                }
+                log.info( "not building" );
+                continue;
+            }
 
-                ProjectScmRoot scmRoot = getProjectScmRootByProject( projectId );
+            ProjectScmRoot scmRoot = getProjectScmRootByProject( projectId );
 
-                Map<Integer, Integer> projectsAndBuildDefinitionsMap = map.get( scmRoot );
+            Map<Integer, Integer> projectsAndBuildDefinitionsMap = map.get( scmRoot );
 
-                if ( projectsAndBuildDefinitionsMap == null )
-                {
-                    projectsAndBuildDefinitionsMap = new HashMap<Integer, Integer>();
-                }
+            if ( projectsAndBuildDefinitionsMap == null )
+            {
+                projectsAndBuildDefinitionsMap = new HashMap<Integer, Integer>();
+            }
 
-                projectsAndBuildDefinitionsMap.put( projectId, buildDefinitionId );
+            projectsAndBuildDefinitionsMap.put( projectId, buildDefinitionId );
 
-                map.put( scmRoot, projectsAndBuildDefinitionsMap );
+            map.put( scmRoot, projectsAndBuildDefinitionsMap );
 
-                if ( !sortedScmRoot.contains( scmRoot ) )
-                {
-                    sortedScmRoot.add( scmRoot );
-                }
-            }
-            catch ( BuildManagerException e )
+            if ( !sortedScmRoot.contains( scmRoot ) )
             {
-                throw new ContinuumException( e.getMessage(), e );
+                    sortedScmRoot.add( scmRoot );
             }
         }
 
@@ -3584,12 +3531,12 @@ public class DefaultContinuum
         for ( ProjectScmRoot scmRoot : scmRoots )
         {
         	prepareBuildProjects( map.get( scmRoot ), buildTrigger, scmRoot.getScmRootAddress(),
-                                  scmRoot.getProjectGroup().getId(), scmRoot.getId() );
+                                  scmRoot.getProjectGroup().getId(), scmRoot.getId(), scmRoots );
         }
     }
 
     private void prepareBuildProjects( Map<Integer, Integer> projectsBuildDefinitionsMap, BuildTrigger buildTrigger,
-                                       String scmRootAddress, int projectGroupId, int scmRootId )
+                                       String scmRootAddress, int projectGroupId, int scmRootId, List<ProjectScmRoot> scmRoots )
         throws ContinuumException, NoBuildAgentException, NoBuildAgentInGroupException
     {
         ProjectGroup group = getProjectGroup( projectGroupId );
@@ -3599,7 +3546,7 @@ public class DefaultContinuum
             if ( configurationService.isDistributedBuildEnabled() )
             {
             	distributedBuildManager.prepareBuildProjects( projectsBuildDefinitionsMap, buildTrigger, projectGroupId, 
-                                                              group.getName(), scmRootAddress, scmRootId );
+                                                              group.getName(), scmRootAddress, scmRootId, scmRoots );
             }
             else
             {
@@ -3778,6 +3725,44 @@ public class DefaultContinuum
         }
     }
 
+    private boolean isProjectOkToBuild( int projectId, int buildDefinitionId )
+        throws ContinuumException
+    {
+        if ( configurationService.isDistributedBuildEnabled() )
+        {
+            if ( !distributedBuildManager.isProjectInAnyPrepareBuildQueue( projectId, buildDefinitionId ) && 
+                 !distributedBuildManager.isProjectInAnyBuildQueue( projectId, buildDefinitionId ) &&
+                 !distributedBuildManager.isProjectCurrentlyPreparingBuild( projectId, buildDefinitionId ) &&
+                 !distributedBuildManager.isProjectCurrentlyBuilding( projectId, buildDefinitionId ) )
+            {
+                return true;
+            }
+        }
+        else
+        {
+            try
+            {
+                if ( !parallelBuildsManager.isInAnyBuildQueue( projectId, buildDefinitionId ) &&
+                     !parallelBuildsManager.isInAnyCheckoutQueue( projectId ) &&
+                     !parallelBuildsManager.isInPrepareBuildQueue( projectId ) &&
+                     !parallelBuildsManager.isProjectCurrentlyPreparingBuild( projectId ) )
+                {
+                    if ( parallelBuildsManager.isInAnyCheckoutQueue( projectId ) )
+                    {
+                        parallelBuildsManager.removeProjectFromCheckoutQueue( projectId );
+                    }
+
+                    return true;
+                }
+            }
+            catch ( BuildManagerException e )
+            {
+                throw new ContinuumException( e.getMessage(), e );
+            }
+        }
+
+        return false;
+    }
     void setTaskQueueManager( TaskQueueManager taskQueueManager )
     {
         this.taskQueueManager = taskQueueManager;

Modified: continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManagerTest.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManagerTest.java?rev=933451&r1=933450&r2=933451&view=diff
==============================================================================
--- continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManagerTest.java (original)
+++ continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/manager/DefaultDistributedBuildManagerTest.java Tue Apr 13 00:26:42 2010
@@ -26,15 +26,25 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.continuum.builder.distributed.executor.ThreadedDistributedBuildTaskQueueExecutor;
+import org.apache.continuum.builder.distributed.stubs.SlaveBuildAgentTransportClientStub;
 import org.apache.continuum.configuration.BuildAgentConfiguration;
+import org.apache.continuum.configuration.BuildAgentGroupConfiguration;
 import org.apache.continuum.dao.BuildDefinitionDao;
 import org.apache.continuum.dao.BuildResultDao;
 import org.apache.continuum.dao.ProjectDao;
+import org.apache.continuum.distributed.transport.slave.SlaveBuildAgentTransportService;
+import org.apache.continuum.model.project.ProjectScmRoot;
 import org.apache.continuum.taskqueue.BuildProjectTask;
 import org.apache.continuum.taskqueue.OverallDistributedBuildQueue;
 import org.apache.continuum.taskqueue.PrepareBuildProjectsTask;
+import org.apache.continuum.utils.build.BuildTrigger;
 import org.apache.maven.continuum.configuration.ConfigurationService;
+import org.apache.maven.continuum.model.project.BuildDefinition;
+import org.apache.maven.continuum.model.project.Project;
+import org.apache.maven.continuum.model.project.ProjectGroup;
+import org.apache.maven.continuum.model.system.Profile;
 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
+import org.codehaus.plexus.taskqueue.Task;
 import org.codehaus.plexus.taskqueue.TaskQueue;
 import org.jmock.Expectations;
 import org.jmock.Mockery;
@@ -44,10 +54,16 @@ import org.jmock.lib.legacy.ClassImposte
 public class DefaultDistributedBuildManagerTest
     extends PlexusInSpringTestCase
 {
-    private final String TEST_BUILD_AGENT = "http://sampleagent";
+    private final String TEST_BUILD_AGENT1 = "http://sampleagent";
+
+    private final String TEST_BUILD_AGENT2 = "http://testagent";
+    
+    private final String TEST_BUILD_AGENT_GROUP1 = "buildAgentGroup1";
 
     private DefaultDistributedBuildManager distributedBuildManager;
 
+    private DefaultDistributedBuildManager distributedBuildManagerStub = new DefaultDistributedBuildManagerStub();
+
     private Mockery context;
 
     private OverallDistributedBuildQueue overallDistributedBuildQueue;
@@ -62,12 +78,24 @@ public class DefaultDistributedBuildMana
 
     private List<BuildAgentConfiguration> buildAgents;
 
-    private BuildAgentConfiguration buildAgent;
+    private BuildAgentConfiguration buildAgent1;
+
+    private BuildAgentConfiguration buildAgent2;
 
     private ThreadedDistributedBuildTaskQueueExecutor distributedBuildTaskQueueExecutor;
 
     private TaskQueue distributedBuildQueue;
 
+    private Project project;
+
+    private Project project2;
+
+    private ProjectGroup projectGroup;
+
+    private BuildDefinition buildDefinition1;
+
+    private BuildAgentGroupConfiguration buildAgentGroup1;
+
     @Override
     public void setUp()
         throws Exception
@@ -81,27 +109,71 @@ public class DefaultDistributedBuildMana
 
         buildDefinitionDao = context.mock( BuildDefinitionDao.class );
         distributedBuildManager.setBuildDefinitionDao( buildDefinitionDao );
+        distributedBuildManagerStub.setBuildDefinitionDao( buildDefinitionDao );
 
         buildResultDao = context.mock( BuildResultDao.class );
         distributedBuildManager.setBuildResultDao( buildResultDao );
+        distributedBuildManagerStub.setBuildResultDao( buildResultDao );
 
         projectDao = context.mock( ProjectDao.class );
         distributedBuildManager.setProjectDao( projectDao );
+        distributedBuildManagerStub.setProjectDao( projectDao );
 
-        configurationService = context.mock( ConfigurationService.class );
+        distributedBuildManagerStub.setContainer( getContainer() );
 
-        buildAgent = new BuildAgentConfiguration();
-        buildAgent.setEnabled( true );
-        buildAgent.setUrl( TEST_BUILD_AGENT );
+        configurationService = context.mock( ConfigurationService.class );
 
         distributedBuildManager.setConfigurationService( configurationService );
-
-        buildAgents = new ArrayList<BuildAgentConfiguration>();
-        buildAgents.add( buildAgent );
+        distributedBuildManagerStub.setConfigurationService( configurationService );
 
         distributedBuildTaskQueueExecutor = (ThreadedDistributedBuildTaskQueueExecutor) context.mock( ThreadedDistributedBuildTaskQueueExecutor.class, "distributed-build-project" );
 
         distributedBuildQueue = context.mock( TaskQueue.class, "distributed-build-queue" );
+
+        overallDistributedBuildQueue = context.mock( OverallDistributedBuildQueue.class );
+
+        init();
+    }
+
+    private void init()
+    {
+        buildAgent1 = new BuildAgentConfiguration();
+        buildAgent1.setEnabled( true );
+        buildAgent1.setUrl( TEST_BUILD_AGENT1 );
+
+        buildAgent2 = new BuildAgentConfiguration();
+        buildAgent2.setEnabled( true );
+        buildAgent2.setUrl( TEST_BUILD_AGENT2 );
+        
+        buildAgentGroup1 = new BuildAgentGroupConfiguration();
+        buildAgentGroup1.setName( TEST_BUILD_AGENT_GROUP1 );
+        buildAgentGroup1.addBuildAgent( buildAgent1 );
+        buildAgentGroup1.addBuildAgent( buildAgent2 );
+
+        Profile buildEnv1 = new Profile();
+        buildEnv1.setBuildAgentGroup( TEST_BUILD_AGENT_GROUP1 );
+
+        buildDefinition1 = new BuildDefinition();
+        buildDefinition1.setId( 1 );
+        buildDefinition1.setProfile( buildEnv1 );
+
+        projectGroup = new ProjectGroup();
+        projectGroup.setId( 1 );
+        projectGroup.setGroupId( "group" );
+
+        project = new Project();
+        project.setId( 1 );
+        project.setGroupId( "groupId" );
+        project.setArtifactId( "artifactId" );
+        project.setVersion( "1.0" );
+        project.setProjectGroup( projectGroup );
+
+        project2 = new Project();
+        project2.setId( 2 );
+        project2.setGroupId( "groupId" );
+        project2.setArtifactId( "artifactId" );
+        project2.setVersion( "1.0" );
+        project2.setProjectGroup( projectGroup );
     }
 
     public void testViewQueuesAfterBuildAgentIsLost()
@@ -109,7 +181,7 @@ public class DefaultDistributedBuildMana
     {
         setUpMockOverallDistributedBuildQueues();
 
-        recordDisableOfBuildAgent();
+        recordViewQueuesAfterBuildAgentIsLost();
 
         Map<String, List<PrepareBuildProjectsTask>> prepareBuildQueues = distributedBuildManager.getProjectsInPrepareBuildQueue();
         Map<String, List<BuildProjectTask>> buildQueues = distributedBuildManager.getProjectsInBuildQueue();
@@ -128,34 +200,135 @@ public class DefaultDistributedBuildMana
         throws Exception
     {
         setUpMockOverallDistributedBuildQueues();
-
+        
         recordDisableOfBuildAgent();
 
-        distributedBuildManager.isAgentAvailable( TEST_BUILD_AGENT );
-
+        distributedBuildManager.isAgentAvailable( TEST_BUILD_AGENT1 );
+        
         context.assertIsSatisfied();
     }
 
-    public void testBuildProjectWithoutBuildAgent()
+    public void testViewQueuesWhen2BuildAgentsAreLost()
+        throws Exception
     {
-        
+        setUpMockOverallDistributedBuildQueues2();
+
+        recordViewQueuesAfter2BuildAgentsAreLost();
+
+        Map<String, List<PrepareBuildProjectsTask>> prepareBuildQueues = distributedBuildManager.getProjectsInPrepareBuildQueue();
+        Map<String, List<BuildProjectTask>> buildQueues = distributedBuildManager.getProjectsInBuildQueue();
+        Map<String, PrepareBuildProjectsTask> currentPrepareBuild = distributedBuildManager.getProjectsCurrentlyPreparingBuild();
+        Map<String, BuildProjectTask> currentBuild = distributedBuildManager.getProjectsCurrentlyBuilding();
+
+        assertEquals( prepareBuildQueues.size(), 0 );
+        assertEquals( buildQueues.size(), 0 );
+        assertEquals( currentPrepareBuild.size(), 0 );
+        assertEquals( currentBuild.size(), 0 );
+
+        context.assertIsSatisfied();        
     }
 
-    public void testBuildProjectWithBuildAgentGroupButWithoutBuildAgent()
+    public void testBuildProjectWithBuildAgentGroupWithNoCurrentBuilds()
+        throws Exception
     {
-        
+        setUpMockOverallDistributedBuildQueues3();
+
+        Map<Integer, Integer> projectsBuildDefinitionsMap = new HashMap<Integer, Integer>();
+        projectsBuildDefinitionsMap.put( 1, 1 );
+        projectsBuildDefinitionsMap.put( 2, 1 );
+
+        BuildTrigger buildTrigger = new BuildTrigger( 1 );
+
+        recordBuildOfProjectWithBuildAgentGroupWithNoCurrentBuilds();
+
+        List<ProjectScmRoot> scmRoots = new ArrayList<ProjectScmRoot>();
+        ProjectScmRoot scmRoot = new ProjectScmRoot();
+        scmRoot.setId( 1 );
+        scmRoot.setProjectGroup( projectGroup );
+        scmRoot.setScmRootAddress( "scmRootAddress1" );
+        scmRoots.add( scmRoot );
+
+        scmRoot = new ProjectScmRoot();
+        scmRoot.setId( 2 );
+        scmRoot.setProjectGroup( projectGroup );
+        scmRoot.setScmRootAddress( "scmRootAddress2" );
+        scmRoots.add( scmRoot );
+
+        distributedBuildManagerStub.prepareBuildProjects( projectsBuildDefinitionsMap, buildTrigger, 1, "sample", "scmRootAddress1", 1, scmRoots );
+
+        context.assertIsSatisfied();
     }
 
+    public void testBuildProjectWithBuildAgentGroupWithCurrentBuild()
+        throws Exception
+    {
+        setUpMockOverallDistributedBuildQueues3();
+
+        Map<Integer, Integer> projectsBuildDefinitionsMap = new HashMap<Integer, Integer>();
+        projectsBuildDefinitionsMap.put( 1, 1 );
+
+        BuildTrigger buildTrigger = new BuildTrigger( 1 );
+
+        recordBuildOfProjectWithBuildAgentGroupWithCurrentBuild();
+
+        List<ProjectScmRoot> scmRoots = new ArrayList<ProjectScmRoot>();
+        ProjectScmRoot scmRoot = new ProjectScmRoot();
+        scmRoot.setId( 2 );
+        scmRoot.setProjectGroup( projectGroup );
+        scmRoot.setScmRootAddress( "scmRootAddress2" );
+        scmRoots.add( scmRoot );
+
+        scmRoot = new ProjectScmRoot();
+        scmRoot.setId( 1 );
+        scmRoot.setProjectGroup( projectGroup );
+        scmRoot.setScmRootAddress( "scmRootAddress1" );
+        scmRoots.add( scmRoot );
+
+        distributedBuildManagerStub.prepareBuildProjects( projectsBuildDefinitionsMap, buildTrigger, 1, "sample", "scmRootAddress1", 1, scmRoots );
+
+        context.assertIsSatisfied();
+    }
     private void setUpMockOverallDistributedBuildQueues()
     {
         Map<String, OverallDistributedBuildQueue> overallDistributedBuildQueues =
             Collections.synchronizedMap( new HashMap<String, OverallDistributedBuildQueue>() );
-        overallDistributedBuildQueue = context.mock( OverallDistributedBuildQueue.class );
 
-        overallDistributedBuildQueues.put( TEST_BUILD_AGENT, overallDistributedBuildQueue );
+        buildAgents = new ArrayList<BuildAgentConfiguration>();
+        buildAgents.add( buildAgent1 );
+
+        overallDistributedBuildQueues.put( TEST_BUILD_AGENT1, overallDistributedBuildQueue );
+        distributedBuildManager.setOverallDistributedBuildQueues( overallDistributedBuildQueues );
+    }
+
+    private void setUpMockOverallDistributedBuildQueues2()
+    {
+        Map<String, OverallDistributedBuildQueue> overallDistributedBuildQueues =
+            Collections.synchronizedMap( new HashMap<String, OverallDistributedBuildQueue>() );
+
+        buildAgents = new ArrayList<BuildAgentConfiguration>();
+        buildAgents.add( buildAgent1 );
+        buildAgents.add( buildAgent2 );
+
+        overallDistributedBuildQueues.put( TEST_BUILD_AGENT1, overallDistributedBuildQueue );
+        overallDistributedBuildQueues.put( TEST_BUILD_AGENT2, overallDistributedBuildQueue );
         distributedBuildManager.setOverallDistributedBuildQueues( overallDistributedBuildQueues );
     }
 
+    private void setUpMockOverallDistributedBuildQueues3()
+    {
+
+        Map<String, OverallDistributedBuildQueue> overallDistributedBuildQueues =
+            Collections.synchronizedMap( new HashMap<String, OverallDistributedBuildQueue>() );
+
+        buildAgents = new ArrayList<BuildAgentConfiguration>();
+        buildAgents.add( buildAgent1 );
+        buildAgents.add( buildAgent2 );
+
+        overallDistributedBuildQueues.put( TEST_BUILD_AGENT1, overallDistributedBuildQueue );
+        overallDistributedBuildQueues.put( TEST_BUILD_AGENT2, overallDistributedBuildQueue );
+        distributedBuildManagerStub.setOverallDistributedBuildQueues( overallDistributedBuildQueues );
+    }
+
     private void recordDisableOfBuildAgent()
         throws Exception
     {
@@ -163,9 +336,24 @@ public class DefaultDistributedBuildMana
         {
             {
                 one( configurationService ).getBuildAgents();
-                will( returnValue( buildAgents )  );
+                will( returnValue( buildAgents ) );
 
-                one( configurationService ).updateBuildAgent( buildAgent );
+                one( configurationService ).updateBuildAgent( buildAgent1 );
+                one( configurationService ).store();
+            }
+        } );
+    }
+
+    private void recordViewQueuesAfterBuildAgentIsLost()
+        throws Exception
+    {
+        context.checking( new Expectations()
+        {
+            {
+                exactly( 5 ).of( configurationService ).getBuildAgents();
+                will( returnValue( buildAgents ) );
+
+                one( configurationService ).updateBuildAgent( buildAgent1 );
                 one( configurationService ).store();
 
                 exactly( 2 ).of( overallDistributedBuildQueue ).getDistributedBuildTaskQueueExecutor();
@@ -186,4 +374,97 @@ public class DefaultDistributedBuildMana
             }
         } );
     }
+
+    private void recordViewQueuesAfter2BuildAgentsAreLost()
+        throws Exception
+    {
+        context.checking( new Expectations()
+        {
+            {
+                exactly( 6 ).of( configurationService ).getBuildAgents();
+                will( returnValue( buildAgents ) );
+
+                one( configurationService ).updateBuildAgent( buildAgent1 );
+                one( configurationService ).updateBuildAgent( buildAgent2 );
+                exactly( 2 ).of( configurationService ).store();
+
+                exactly( 4 ).of( overallDistributedBuildQueue ).getDistributedBuildTaskQueueExecutor();
+                will( returnValue( distributedBuildTaskQueueExecutor ) );
+
+                exactly( 2 ).of( distributedBuildTaskQueueExecutor ).getCurrentTask();
+                will( returnValue( null ) );
+
+                exactly( 2 ).of( overallDistributedBuildQueue ).getProjectsInQueue();
+                will( returnValue( new ArrayList<PrepareBuildProjectsTask>() ) );
+
+                exactly( 2 ).of( overallDistributedBuildQueue ).getDistributedBuildQueue();
+                will( returnValue( distributedBuildQueue ) );
+
+                exactly( 2 ).of( distributedBuildQueue ).removeAll( new ArrayList<PrepareBuildProjectsTask>() );
+
+                exactly( 2 ).of( distributedBuildTaskQueueExecutor ).stop();
+            }
+        } );
+    }
+
+    private void recordBuildOfProjectWithBuildAgentGroupWithNoCurrentBuilds()
+        throws Exception
+    {
+        context.checking( new Expectations()
+        {
+            {
+                exactly( 3 ).of( projectDao ).getProjectWithDependencies( 1 );
+                will( returnValue( project ) );
+
+                exactly( 3 ).of( projectDao ).getProjectWithDependencies( 2 );
+                will( returnValue( project2) );
+
+                exactly( 3 ).of( buildDefinitionDao ).getBuildDefinition( 1 );
+                will( returnValue( buildDefinition1 ) );
+
+                exactly( 3 ).of( configurationService ).getBuildAgentGroup( TEST_BUILD_AGENT_GROUP1 );
+                will( returnValue( buildAgentGroup1 ) );
+
+                one( configurationService ).getBuildAgents();
+                will( returnValue( buildAgents ) );
+
+                one( overallDistributedBuildQueue ).addToDistributedBuildQueue( with( any( Task.class ) ) );
+            }
+        } );
+    }
+
+    private void recordBuildOfProjectWithBuildAgentGroupWithCurrentBuild()
+        throws Exception
+    {
+        context.checking( new Expectations()
+        {
+            {
+                one( overallDistributedBuildQueue ).getProjectsInQueue();
+
+                one( overallDistributedBuildQueue ).getDistributedBuildTaskQueueExecutor();
+                will( returnValue( distributedBuildTaskQueueExecutor ) );
+
+                one( distributedBuildTaskQueueExecutor ).getCurrentTask();
+                will( returnValue( null ) );
+
+                one( projectDao ).getProjectsInGroup( 1 );
+                will( returnValue( new ArrayList<Project>() ) );
+
+                one( configurationService ).getBuildAgents();
+                will( returnValue( buildAgents ) );
+
+                one( overallDistributedBuildQueue ).addToDistributedBuildQueue( with( any( Task.class ) ) );
+            }
+        } );
+    }
 }
+
+class DefaultDistributedBuildManagerStub
+    extends DefaultDistributedBuildManager
+{
+    @Override
+    public SlaveBuildAgentTransportService createSlaveBuildAgentTransportClientConnection( String buildAgentUrl )
+    {
+        return new SlaveBuildAgentTransportClientStub();
+    }
+}
\ No newline at end of file

Added: continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/stubs/DefaultDistributedBuildManagerStub.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/stubs/DefaultDistributedBuildManagerStub.java?rev=933451&view=auto
==============================================================================
--- continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/stubs/DefaultDistributedBuildManagerStub.java (added)
+++ continuum/trunk/continuum-core/src/test/java/org/apache/continuum/builder/distributed/stubs/DefaultDistributedBuildManagerStub.java Tue Apr 13 00:26:42 2010
@@ -0,0 +1,14 @@
+package org.apache.continuum.builder.distributed.stubs;
+
+import org.apache.continuum.builder.distributed.manager.DefaultDistributedBuildManager;
+import org.apache.continuum.distributed.transport.slave.SlaveBuildAgentTransportService;
+
+public class DefaultDistributedBuildManagerStub
+    extends DefaultDistributedBuildManager
+{
+    @Override
+    public SlaveBuildAgentTransportService createSlaveBuildAgentTransportClientConnection( String buildAgentUrl )
+    {
+        return new SlaveBuildAgentTransportClientStub();
+    }
+}



Mime
View raw message