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