View Javadoc

1   package org.apache.continuum.buildagent.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.buildagent.buildcontext.BuildContext;
23  import org.apache.continuum.buildagent.taskqueue.manager.BuildAgentTaskQueueManager;
24  import org.apache.continuum.buildagent.utils.ContinuumBuildAgentUtil;
25  import org.apache.continuum.taskqueue.BuildProjectTask;
26  import org.apache.continuum.taskqueue.manager.TaskQueueManagerException;
27  import org.apache.continuum.utils.build.BuildTrigger;
28  import org.apache.maven.continuum.ContinuumException;
29  import org.codehaus.plexus.action.AbstractAction;
30  import org.codehaus.plexus.taskqueue.TaskQueueException;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * @plexus.component role="org.codehaus.plexus.action.Action" role-hint="create-agent-build-project-task"
39   */
40  public class CreateBuildProjectTaskAction
41      extends AbstractAction
42  {
43      private static final Logger log = LoggerFactory.getLogger( CreateBuildProjectTaskAction.class );
44  
45      /**
46       * @plexus.requirement
47       */
48      private BuildAgentTaskQueueManager buildAgentTaskQueueManager;
49  
50      public void execute( Map context )
51          throws Exception
52      {
53          List<BuildContext> buildContexts = ContinuumBuildAgentUtil.getBuildContexts( context );
54  
55          for ( BuildContext buildContext : buildContexts )
56          {
57              BuildTrigger buildTrigger = new BuildTrigger( buildContext.getTrigger(), buildContext.getUsername() );
58  
59              BuildProjectTask buildProjectTask = new BuildProjectTask( buildContext.getProjectId(),
60                                                                        buildContext.getBuildDefinitionId(), buildTrigger,
61                                                                        buildContext.getProjectName(),
62                                                                        buildContext.getBuildDefinitionLabel(),
63                                                                        buildContext.getScmResult(),
64                                                                        buildContext.getProjectGroupId() );
65              buildProjectTask.setMaxExecutionTime( buildContext.getMaxExecutionTime() * 1000 );
66  
67              try
68              {
69                  if ( !buildAgentTaskQueueManager.isProjectInBuildQueue( buildProjectTask.getProjectId() ) )
70                  {
71                      log.info( "Adding project {} to build queue", buildProjectTask.getProjectId() );
72                      buildAgentTaskQueueManager.getBuildQueue().put( buildProjectTask );
73                  }
74              }
75              catch ( TaskQueueException e )
76              {
77                  log.error( "Error while enqueing build task for project " + buildContext.getProjectId(), e );
78                  throw new ContinuumException(
79                      "Error while enqueuing build task for project " + buildContext.getProjectId(), e );
80              }
81              catch ( TaskQueueManagerException e )
82              {
83                  log.error( "Error while checking if project " + buildContext.getProjectId() + " is in build queue", e );
84                  throw new ContinuumException(
85                      "Error while checking if project " + buildContext.getProjectId() + " is in build queue", e );
86              }
87          }
88      }
89  
90  }