View Javadoc

1   package org.apache.maven.continuum.core.action;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.continuum.buildmanager.BuildsManager;
23  import org.apache.continuum.dao.ProjectDao;
24  import org.apache.continuum.utils.build.BuildTrigger;
25  import org.apache.maven.continuum.execution.ContinuumBuildExecutor;
26  import org.apache.maven.continuum.execution.manager.BuildExecutorManager;
27  import org.apache.maven.continuum.model.project.BuildDefinition;
28  import org.apache.maven.continuum.model.project.Project;
29  import org.apache.maven.continuum.model.scm.ScmResult;
30  import org.apache.maven.continuum.project.ContinuumProjectState;
31  import org.apache.maven.continuum.store.ContinuumStoreException;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * @author <a href="mailto:ctan@apache.org">Maria Catherine Tan</a>
39   * @version $Id: CreateBuildProjectTaskAction.java 1372260 2012-08-13 04:29:09Z brett $
40   * @plexus.component role="org.codehaus.plexus.action.Action" role-hint="create-build-project-task"
41   */
42  public class CreateBuildProjectTaskAction
43      extends AbstractContinuumAction
44  {
45      /**
46       * @plexus.requirement
47       */
48      private BuildExecutorManager executorManager;
49  
50      /**
51       * @plexus.requirement
52       */
53      private ProjectDao projectDao;
54  
55      /**
56       * @plexus.requirement role-hint="parallel"
57       */
58      private BuildsManager parallelBuildsManager;
59  
60      public synchronized void execute( Map context )
61          throws Exception
62      {
63          List<Project> projects = AbstractContinuumAction.getListOfProjects( context );
64          Map<Integer, BuildDefinition> projectsBuildDefinitionsMap =
65              AbstractContinuumAction.getProjectsBuildDefinitionsMap( context );
66          Map<Integer, ScmResult> scmResultMap = AbstractContinuumAction.getScmResultMap( context );
67          List<Project> projectsToBeBuilt = new ArrayList<Project>();
68          BuildTrigger buildTrigger = AbstractContinuumAction.getBuildTrigger( context );
69          int projectGroupId = AbstractContinuumAction.getProjectGroupId( context );
70  
71          // update state of each project first
72          for ( Project project : projects )
73          {
74              BuildDefinition buildDefinition = projectsBuildDefinitionsMap.get( project.getId() );
75  
76              if ( parallelBuildsManager.isInAnyBuildQueue( project.getId(), buildDefinition.getId() ) )
77              {
78                  getLogger().info( "Project '" + project.getName() + "' is already in build queue." );
79                  continue;
80              }
81  
82              if ( parallelBuildsManager.isInAnyCheckoutQueue( project.getId() ) )
83              {
84                  parallelBuildsManager.removeProjectFromCheckoutQueue( project.getId() );
85              }
86  
87              try
88              {
89                  if ( project.getState() != ContinuumProjectState.NEW &&
90                      project.getState() != ContinuumProjectState.CHECKEDOUT &&
91                      project.getState() != ContinuumProjectState.OK &&
92                      project.getState() != ContinuumProjectState.FAILED &&
93                      project.getState() != ContinuumProjectState.ERROR )
94                  {
95                      ContinuumBuildExecutor executor = executorManager.getBuildExecutor( project.getExecutorId() );
96  
97                      if ( executor.isBuilding( project ) || project.getState() == ContinuumProjectState.UPDATING )
98                      {
99                          // project is building
100                         getLogger().info( "Project '" + project.getName() + "' already being built." );
101 
102                         continue;
103                     }
104                     else
105                     {
106                         project.setState( ContinuumProjectState.ERROR );
107                     }
108                 }
109                 project.setOldState( project.getState() );
110 
111                 projectDao.updateProject( project );
112 
113                 project = projectDao.getProject( project.getId() );
114 
115                 projectsToBeBuilt.add( project );
116             }
117             catch ( ContinuumStoreException e )
118             {
119                 getLogger().error( "Error while creating build object", e );
120             }
121         }
122 
123         parallelBuildsManager.buildProjects( projectsToBeBuilt, projectsBuildDefinitionsMap, buildTrigger, scmResultMap,
124                                              projectGroupId );
125     }
126 }