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 java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.continuum.buildmanager.BuildsManager;
27  import org.apache.continuum.dao.ProjectDao;
28  import org.apache.maven.continuum.execution.ContinuumBuildExecutor;
29  import org.apache.maven.continuum.execution.manager.BuildExecutorManager;
30  import org.apache.maven.continuum.model.project.BuildDefinition;
31  import org.apache.maven.continuum.model.project.Project;
32  import org.apache.maven.continuum.model.scm.ScmResult;
33  import org.apache.maven.continuum.project.ContinuumProjectState;
34  import org.apache.maven.continuum.store.ContinuumStoreException;
35  
36  /**
37   * @author <a href="mailto:ctan@apache.org">Maria Catherine Tan</a>
38   * @version $Id: CreateBuildProjectTaskAction.java 788260 2009-06-25 05:02:07Z evenisse $
39   * @plexus.component role="org.codehaus.plexus.action.Action" role-hint="create-build-project-task"
40   */
41  public class CreateBuildProjectTaskAction
42      extends AbstractContinuumAction
43  {
44      /**
45       * @plexus.requirement
46       */
47      private BuildExecutorManager executorManager;
48  
49      /**
50       * @plexus.requirement
51       */
52      private ProjectDao projectDao;
53  
54      /**
55       * @plexus.requirement role-hint="parallel"
56       */
57      private BuildsManager parallelBuildsManager;
58  
59      public synchronized void execute( Map context )
60          throws Exception
61      {
62          List<Project> projects = AbstractContinuumAction.getListOfProjects( context );
63          Map<Integer, BuildDefinition> projectsBuildDefinitionsMap =
64              AbstractContinuumAction.getProjectsBuildDefinitionsMap( context );
65          Map<Integer, ScmResult> scmResultMap = AbstractContinuumAction.getScmResultMap( context );
66          List<Project> projectsToBeBuilt = new ArrayList<Project>();
67          int trigger = AbstractContinuumAction.getTrigger( context );
68          int projectGroupId = AbstractContinuumAction.getProjectGroupId( context );
69  
70          // update state of each project first
71          for ( Project project : projects )
72          {
73              BuildDefinition buildDefinition = projectsBuildDefinitionsMap.get( project.getId() );
74  
75              if ( parallelBuildsManager.isInAnyBuildQueue( project.getId(), buildDefinition.getId() ) )
76              {
77                  return;
78              }
79  
80              if ( parallelBuildsManager.isInAnyCheckoutQueue( project.getId() ) )
81              {
82                  parallelBuildsManager.removeProjectFromCheckoutQueue( project.getId() );
83              }
84  
85              try
86              {
87                  if ( project.getState() != ContinuumProjectState.NEW &&
88                      project.getState() != ContinuumProjectState.CHECKEDOUT &&
89                      project.getState() != ContinuumProjectState.OK &&
90                      project.getState() != ContinuumProjectState.FAILED &&
91                      project.getState() != ContinuumProjectState.ERROR )
92                  {
93                      ContinuumBuildExecutor executor = executorManager.getBuildExecutor( project.getExecutorId() );
94  
95                      if ( executor.isBuilding( project ) || project.getState() == ContinuumProjectState.UPDATING )
96                      {
97                          // project is building
98                          getLogger().info( "Project '" + project.getName() + "' already being built." );
99  
100                         continue;
101                     }
102                     else
103                     {
104                         project.setState( ContinuumProjectState.ERROR );
105                     }
106                 }
107                 project.setOldState( project.getState() );
108 
109                 projectDao.updateProject( project );
110 
111                 project = projectDao.getProject( project.getId() );
112 
113                 projectsToBeBuilt.add( project );
114             }
115             catch ( ContinuumStoreException e )
116             {
117                 getLogger().error( "Error while creating build object", e );
118                 //throw new ContinuumException( "Error while creating build object.", e );
119             }
120         }
121 
122         parallelBuildsManager.buildProjects( projectsToBeBuilt, projectsBuildDefinitionsMap, trigger, scmResultMap,
123                                              projectGroupId );
124     }
125 }