View Javadoc

1   package org.apache.maven.continuum.web.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.Collection;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.commons.lang.ArrayUtils;
29  import org.apache.continuum.buildmanager.BuildManagerException;
30  import org.apache.continuum.buildmanager.BuildsManager;
31  import org.apache.continuum.model.project.ProjectScmRoot;
32  import org.apache.continuum.taskqueue.BuildProjectTask;
33  import org.apache.continuum.web.util.AuditLog;
34  import org.apache.continuum.web.util.AuditLogConstants;
35  import org.apache.maven.continuum.ContinuumException;
36  import org.apache.maven.continuum.model.project.Project;
37  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
38  import org.codehaus.plexus.util.StringUtils;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
44   * @version $Id: CancelBuildAction.java 781924 2009-06-05 06:42:54Z ctan $
45   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="cancelBuild"
46   */
47  public class CancelBuildAction
48      extends ContinuumActionSupport
49  {
50      private static final Logger logger = LoggerFactory.getLogger( CancelBuildAction.class );
51  
52      private int projectId;
53  
54      private int projectGroupId;
55  
56      private List<String> selectedProjects;
57  
58      private String projectGroupName = "";
59  
60      public String execute()
61          throws ContinuumException
62      {
63          try
64          {
65              checkBuildProjectInGroupAuthorization( getProjectGroupName() );
66  
67              BuildsManager buildsManager = getContinuum().getBuildsManager();
68  
69              buildsManager.cancelBuild( projectId );
70              
71              AuditLog event = new AuditLog( "Project id=" + projectId, AuditLogConstants.CANCEL_BUILD );
72              event.setCategory( AuditLogConstants.PROJECT );
73              event.setCurrentUser( getPrincipal() );
74              event.log();
75          }
76          catch ( AuthorizationRequiredException e )
77          {
78              return REQUIRES_AUTHORIZATION;
79          }
80          catch ( BuildManagerException e )
81          {
82              throw new ContinuumException( "Error while canceling build", e );
83          }
84  
85          return SUCCESS;
86      }
87  
88      public String cancelBuilds()
89          throws ContinuumException
90      {
91          if ( getSelectedProjects() == null || getSelectedProjects().isEmpty() )
92          {
93              return SUCCESS;
94          }
95          int[] projectsId = new int[getSelectedProjects().size()];
96          for ( String selectedProjectId : getSelectedProjects() )
97          {
98              int projectId = Integer.parseInt( selectedProjectId );
99              projectsId = ArrayUtils.add( projectsId, projectId );
100         }
101 
102         BuildsManager parallelBuildsManager = getContinuum().getBuildsManager();
103         parallelBuildsManager.removeProjectsFromBuildQueue( projectsId );
104 
105         try
106         {
107             // now we must check if the current build is one of this
108             int index = ArrayUtils.indexOf( projectsId, getCurrentProjectIdBuilding() );
109             if ( index > 0 )
110             {
111                 int projId = projectsId[index];
112                 
113                 getContinuum().getBuildsManager().cancelBuild( projId );
114 	            
115   	        AuditLog event = new AuditLog( "Project id=" + projId, AuditLogConstants.CANCEL_BUILD );
116                 event.setCategory( AuditLogConstants.PROJECT );
117                 event.setCurrentUser( getPrincipal() );
118                 event.log();
119             }
120 
121         }
122         catch ( BuildManagerException e )
123         {
124             logger.error( e.getMessage() );
125             throw new ContinuumException( e.getMessage(), e );
126         }
127 
128         return SUCCESS;
129     }
130 
131     public String cancelGroupBuild()
132         throws ContinuumException
133     {
134         try
135         {
136             checkBuildProjectInGroupAuthorization( getContinuum().getProjectGroup( projectGroupId ).getName() );
137         }
138         catch ( AuthorizationRequiredException e )
139         {
140             return REQUIRES_AUTHORIZATION;
141         }
142 
143         BuildsManager buildsManager = getContinuum().getBuildsManager();
144 
145         List<ProjectScmRoot> scmRoots = getContinuum().getProjectScmRootByProjectGroup( projectGroupId );
146 
147         if ( scmRoots != null )
148         {
149             for ( ProjectScmRoot scmRoot : scmRoots )
150             {
151                 try
152                 {
153                     buildsManager.removeProjectGroupFromPrepareBuildQueue( projectGroupId,
154                                                                            scmRoot.getScmRootAddress() );
155                     //taskQueueManager.removeFromPrepareBuildQueue( projectGroupId, scmRoot.getScmRootAddress() );
156                 }
157                 catch ( BuildManagerException e )
158                 {
159                     throw new ContinuumException( "Unable to cancel group build", e );
160                 }
161             }
162         }
163         Collection<Project> projects = getContinuum().getProjectsInGroup( projectGroupId );
164 
165         List<String> projectIds = new ArrayList<String>();
166 
167         for ( Project project : projects )
168         {
169             projectIds.add( Integer.toString( project.getId() ) );
170         }
171 
172         setSelectedProjects( projectIds );
173 
174         return cancelBuilds();
175     }
176 
177     public void setProjectId( int projectId )
178     {
179         this.projectId = projectId;
180     }
181 
182     public String getProjectGroupName()
183         throws ContinuumException
184     {
185         if ( StringUtils.isEmpty( projectGroupName ) )
186         {
187             projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName();
188         }
189 
190         return projectGroupName;
191     }
192 
193     public List<String> getSelectedProjects()
194     {
195         return selectedProjects;
196     }
197 
198     public void setSelectedProjects( List<String> selectedProjects )
199     {
200         this.selectedProjects = selectedProjects;
201     }
202 
203     public int getProjectGroupId()
204     {
205         return projectGroupId;
206     }
207 
208     public void setProjectGroupId( int projectGroupId )
209     {
210         this.projectGroupId = projectGroupId;
211     }
212 
213     /**
214      * @return -1 if not project currently building
215      * @throws ContinuumException
216      */
217     protected int getCurrentProjectIdBuilding()
218         throws ContinuumException, BuildManagerException
219     {
220         Map<String, BuildProjectTask> currentBuilds = getContinuum().getBuildsManager().getCurrentBuilds();
221         Set<String> keySet = currentBuilds.keySet();
222 
223         for ( String key : keySet )
224         {
225             BuildProjectTask task = currentBuilds.get( key );
226             if ( task != null )
227             {
228                 return task.getProjectId();
229             }
230         }
231         return -1;
232     }
233 }