1 package org.apache.maven.continuum.web.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.ArrayUtils;
23 import org.apache.continuum.buildmanager.BuildManagerException;
24 import org.apache.continuum.buildmanager.BuildsManager;
25 import org.apache.continuum.model.project.ProjectScmRoot;
26 import org.apache.continuum.taskqueue.BuildProjectTask;
27 import org.apache.continuum.web.util.AuditLog;
28 import org.apache.continuum.web.util.AuditLogConstants;
29 import org.apache.maven.continuum.ContinuumException;
30 import org.apache.maven.continuum.model.project.Project;
31 import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
32 import org.codehaus.plexus.util.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41
42
43
44
45
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 if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
68 {
69 getContinuum().getDistributedBuildManager().cancelBuild( projectId );
70 }
71 else
72 {
73 BuildsManager buildsManager = getContinuum().getBuildsManager();
74
75 buildsManager.cancelBuild( projectId );
76 }
77
78 AuditLog event = new AuditLog( "Project id=" + projectId, AuditLogConstants.CANCEL_BUILD );
79 event.setCategory( AuditLogConstants.PROJECT );
80 event.setCurrentUser( getPrincipal() );
81 event.log();
82 }
83 catch ( AuthorizationRequiredException e )
84 {
85 return REQUIRES_AUTHORIZATION;
86 }
87 catch ( BuildManagerException e )
88 {
89 throw new ContinuumException( "Error while canceling build", e );
90 }
91
92 return SUCCESS;
93 }
94
95 public String cancelBuilds()
96 throws ContinuumException
97 {
98 if ( getSelectedProjects() == null || getSelectedProjects().isEmpty() )
99 {
100 return SUCCESS;
101 }
102 int[] projectsId = new int[getSelectedProjects().size()];
103 for ( String selectedProjectId : getSelectedProjects() )
104 {
105 int projectId = Integer.parseInt( selectedProjectId );
106 projectsId = ArrayUtils.add( projectsId, projectId );
107 }
108
109 if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
110 {
111 for ( int i = 0; i < projectsId.length; i++ )
112 {
113 getContinuum().getDistributedBuildManager().cancelBuild( projectsId[i] );
114
115 AuditLog event = new AuditLog( "Project id=" + projectsId[i], AuditLogConstants.CANCEL_BUILD );
116 event.setCategory( AuditLogConstants.PROJECT );
117 event.setCurrentUser( getPrincipal() );
118 event.log();
119 }
120 }
121 else
122 {
123 BuildsManager parallelBuildsManager = getContinuum().getBuildsManager();
124 parallelBuildsManager.removeProjectsFromBuildQueue( projectsId );
125
126 try
127 {
128
129 int index = ArrayUtils.indexOf( projectsId, getCurrentProjectIdBuilding() );
130 if ( index > 0 )
131 {
132 int projId = projectsId[index];
133 getContinuum().getBuildsManager().cancelBuild( projId );
134
135 AuditLog event = new AuditLog( "Project id=" + projId, AuditLogConstants.CANCEL_BUILD );
136 event.setCategory( AuditLogConstants.PROJECT );
137 event.setCurrentUser( getPrincipal() );
138 event.log();
139 }
140
141 }
142 catch ( BuildManagerException e )
143 {
144 logger.error( e.getMessage() );
145 throw new ContinuumException( e.getMessage(), e );
146 }
147 }
148
149 return SUCCESS;
150 }
151
152 public String cancelGroupBuild()
153 throws ContinuumException
154 {
155 try
156 {
157 checkBuildProjectInGroupAuthorization( getContinuum().getProjectGroup( projectGroupId ).getName() );
158 }
159 catch ( AuthorizationRequiredException e )
160 {
161 return REQUIRES_AUTHORIZATION;
162 }
163
164 if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
165 {
166 getContinuum().getDistributedBuildManager().cancelGroupBuild( projectGroupId );
167
168 AuditLog event = new AuditLog( "Project Group id=" + projectGroupId, AuditLogConstants.CANCEL_BUILD );
169 event.setCategory( AuditLogConstants.PROJECT_GROUP );
170 event.setCurrentUser( getPrincipal() );
171 event.log();
172
173 return SUCCESS;
174 }
175 else
176 {
177 BuildsManager buildsManager = getContinuum().getBuildsManager();
178
179 List<ProjectScmRoot> scmRoots = getContinuum().getProjectScmRootByProjectGroup( projectGroupId );
180
181 if ( scmRoots != null )
182 {
183 for ( ProjectScmRoot scmRoot : scmRoots )
184 {
185 try
186 {
187 buildsManager.removeProjectGroupFromPrepareBuildQueue( projectGroupId,
188 scmRoot.getScmRootAddress() );
189
190 }
191 catch ( BuildManagerException e )
192 {
193 throw new ContinuumException( "Unable to cancel group build", e );
194 }
195 }
196 }
197 Collection<Project> projects = getContinuum().getProjectsInGroup( projectGroupId );
198
199 List<String> projectIds = new ArrayList<String>();
200
201 for ( Project project : projects )
202 {
203 projectIds.add( Integer.toString( project.getId() ) );
204 }
205
206 setSelectedProjects( projectIds );
207
208 return cancelBuilds();
209 }
210 }
211
212 public void setProjectId( int projectId )
213 {
214 this.projectId = projectId;
215 }
216
217 public String getProjectGroupName()
218 throws ContinuumException
219 {
220 if ( StringUtils.isEmpty( projectGroupName ) )
221 {
222 projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName();
223 }
224
225 return projectGroupName;
226 }
227
228 public List<String> getSelectedProjects()
229 {
230 return selectedProjects;
231 }
232
233 public void setSelectedProjects( List<String> selectedProjects )
234 {
235 this.selectedProjects = selectedProjects;
236 }
237
238 public int getProjectGroupId()
239 {
240 return projectGroupId;
241 }
242
243 public void setProjectGroupId( int projectGroupId )
244 {
245 this.projectGroupId = projectGroupId;
246 }
247
248
249
250
251
252 protected int getCurrentProjectIdBuilding()
253 throws ContinuumException, BuildManagerException
254 {
255 Map<String, BuildProjectTask> currentBuilds = getContinuum().getBuildsManager().getCurrentBuilds();
256 Set<String> keySet = currentBuilds.keySet();
257
258 for ( String key : keySet )
259 {
260 BuildProjectTask task = currentBuilds.get( key );
261 if ( task != null )
262 {
263 return task.getProjectId();
264 }
265 }
266 return -1;
267 }
268 }