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.io.File; |
23 | import java.io.FileWriter; |
24 | import java.io.IOException; |
25 | import java.io.PrintWriter; |
26 | import java.util.ArrayList; |
27 | import java.util.List; |
28 | import java.util.Map; |
29 | |
30 | import org.apache.continuum.configuration.BuildAgentConfigurationException; |
31 | import org.apache.continuum.model.release.ContinuumReleaseResult; |
32 | import org.apache.continuum.release.distributed.DistributedReleaseUtil; |
33 | import org.apache.continuum.release.distributed.manager.DistributedReleaseManager; |
34 | import org.apache.maven.continuum.ContinuumException; |
35 | import org.apache.maven.continuum.configuration.ConfigurationException; |
36 | import org.apache.maven.continuum.model.project.Project; |
37 | import org.apache.maven.continuum.model.project.ProjectGroup; |
38 | import org.apache.maven.continuum.release.ContinuumReleaseManager; |
39 | import org.apache.maven.continuum.release.ContinuumReleaseManagerListener; |
40 | import org.apache.maven.continuum.web.exception.AuthorizationRequiredException; |
41 | import org.apache.maven.continuum.web.model.ReleaseListenerSummary; |
42 | import org.apache.maven.shared.release.ReleaseResult; |
43 | |
44 | /** |
45 | * @author Edwin Punzalan |
46 | * @version $Id: ReleaseInProgressAction.java 904302 2010-01-28 23:33:45Z ctan $ |
47 | * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="releaseInProgress" |
48 | */ |
49 | public class ReleaseInProgressAction |
50 | extends ContinuumActionSupport |
51 | { |
52 | private int projectId; |
53 | |
54 | private String releaseId; |
55 | |
56 | private String releaseGoal; |
57 | |
58 | private ContinuumReleaseManagerListener listener; |
59 | |
60 | private ReleaseResult result; |
61 | |
62 | private String projectGroupName = ""; |
63 | |
64 | private ReleaseListenerSummary listenerSummary; |
65 | |
66 | public String execute() |
67 | throws Exception |
68 | { |
69 | try |
70 | { |
71 | checkBuildProjectInGroupAuthorization( getProjectGroupName() ); |
72 | } |
73 | catch ( AuthorizationRequiredException e ) |
74 | { |
75 | return REQUIRES_AUTHORIZATION; |
76 | } |
77 | |
78 | String status = ""; |
79 | |
80 | listenerSummary = new ReleaseListenerSummary(); |
81 | |
82 | if ( getContinuum().getConfiguration().isDistributedBuildEnabled() ) |
83 | { |
84 | DistributedReleaseManager releaseManager = getContinuum().getDistributedReleaseManager(); |
85 | |
86 | Map map; |
87 | |
88 | try |
89 | { |
90 | map = releaseManager.getListener( releaseId ); |
91 | } |
92 | catch ( BuildAgentConfigurationException e ) |
93 | { |
94 | List<String> args = new ArrayList<String>(); |
95 | args.add( e.getMessage() ); |
96 | |
97 | addActionError( getText( "releaseInProgress.error", args ) ); |
98 | return ERROR; |
99 | } |
100 | |
101 | if ( map != null && !map.isEmpty() ) |
102 | { |
103 | int state = DistributedReleaseUtil.getReleaseState( map ); |
104 | |
105 | if ( state == ContinuumReleaseManagerListener.LISTENING ) |
106 | { |
107 | status = "inProgress"; |
108 | } |
109 | else if ( state == ContinuumReleaseManagerListener.FINISHED ) |
110 | { |
111 | status = SUCCESS; |
112 | } |
113 | else |
114 | { |
115 | status = "initialized"; |
116 | } |
117 | |
118 | if ( status.equals( SUCCESS ) ) |
119 | { |
120 | ReleaseResult result = releaseManager.getReleaseResult( releaseId ); |
121 | |
122 | if ( result != null && getContinuum().getContinuumReleaseResult( projectId, releaseGoal, result.getStartTime(), result.getEndTime() ) == null ) |
123 | { |
124 | ContinuumReleaseResult releaseResult = createContinuumReleaseResult( result ); |
125 | getContinuum().addContinuumReleaseResult( releaseResult ); |
126 | } |
127 | } |
128 | |
129 | listenerSummary.setPhases( DistributedReleaseUtil.getReleasePhases( map ) ); |
130 | listenerSummary.setCompletedPhases( DistributedReleaseUtil.getCompletedReleasePhases( map ) ); |
131 | listenerSummary.setInProgress( DistributedReleaseUtil.getReleaseInProgress( map ) ); |
132 | listenerSummary.setError( DistributedReleaseUtil.getReleaseError( map ) ); |
133 | } |
134 | else |
135 | { |
136 | throw new Exception( "There is no on-going or finished release operation with id " + releaseId ); |
137 | } |
138 | } |
139 | else |
140 | { |
141 | ContinuumReleaseManager releaseManager = getContinuum().getReleaseManager(); |
142 | |
143 | listener = (ContinuumReleaseManagerListener) releaseManager.getListeners().get( releaseId ); |
144 | |
145 | if ( listener != null ) |
146 | { |
147 | if ( listener.getState() == ContinuumReleaseManagerListener.LISTENING ) |
148 | { |
149 | status = "inProgress"; |
150 | } |
151 | else if ( listener.getState() == ContinuumReleaseManagerListener.FINISHED ) |
152 | { |
153 | status = SUCCESS; |
154 | } |
155 | else |
156 | { |
157 | status = "initialized"; |
158 | } |
159 | |
160 | listenerSummary.setPhases( listener.getPhases() ); |
161 | listenerSummary.setCompletedPhases( listener.getCompletedPhases() ); |
162 | listenerSummary.setInProgress( listener.getInProgress() ); |
163 | listenerSummary.setError( listener.getError() ); |
164 | } |
165 | else |
166 | { |
167 | throw new Exception( "There is no on-going or finished release operation with id " + releaseId ); |
168 | } |
169 | |
170 | if ( status.equals( SUCCESS ) ) |
171 | { |
172 | ReleaseResult result = (ReleaseResult) releaseManager.getReleaseResults().get( releaseId ); |
173 | |
174 | if ( result != null && getContinuum().getContinuumReleaseResult( projectId, releaseGoal, result.getStartTime(), result.getEndTime() ) == null ) |
175 | { |
176 | ContinuumReleaseResult releaseResult = createContinuumReleaseResult( result ); |
177 | getContinuum().addContinuumReleaseResult( releaseResult ); |
178 | } |
179 | } |
180 | } |
181 | |
182 | return status; |
183 | } |
184 | |
185 | public String viewResult() |
186 | throws Exception |
187 | { |
188 | try |
189 | { |
190 | checkBuildProjectInGroupAuthorization( getProjectGroupName() ); |
191 | } |
192 | catch ( AuthorizationRequiredException e ) |
193 | { |
194 | return REQUIRES_AUTHORIZATION; |
195 | } |
196 | |
197 | listenerSummary = new ReleaseListenerSummary(); |
198 | |
199 | if ( getContinuum().getConfiguration().isDistributedBuildEnabled() ) |
200 | { |
201 | DistributedReleaseManager releaseManager = getContinuum().getDistributedReleaseManager(); |
202 | |
203 | try |
204 | { |
205 | Map map = releaseManager.getListener( releaseId ); |
206 | |
207 | if ( map != null && !map.isEmpty() ) |
208 | { |
209 | int state = DistributedReleaseUtil.getReleaseState( map ); |
210 | |
211 | listenerSummary.setPhases( DistributedReleaseUtil.getReleasePhases( map ) ); |
212 | listenerSummary.setCompletedPhases( DistributedReleaseUtil.getCompletedReleasePhases( map ) ); |
213 | listenerSummary.setInProgress( DistributedReleaseUtil.getReleaseInProgress( map ) ); |
214 | listenerSummary.setError( DistributedReleaseUtil.getReleaseError( map ) ); |
215 | |
216 | if ( state == ContinuumReleaseManagerListener.FINISHED ) |
217 | { |
218 | result = releaseManager.getReleaseResult( releaseId ); |
219 | |
220 | return SUCCESS; |
221 | } |
222 | else |
223 | { |
224 | throw new Exception( "The release operation with id " + releaseId + "has not finished yet." ); |
225 | } |
226 | } |
227 | else |
228 | { |
229 | throw new Exception( "There is no finished release operation with id " + releaseId ); |
230 | } |
231 | } |
232 | catch ( BuildAgentConfigurationException e ) |
233 | { |
234 | List<String> args = new ArrayList<String>(); |
235 | args.add( e.getMessage() ); |
236 | |
237 | addActionError( getText( "releaseViewResult.error", args ) ); |
238 | return ERROR; |
239 | } |
240 | } |
241 | else |
242 | { |
243 | ContinuumReleaseManager releaseManager = getContinuum().getReleaseManager(); |
244 | |
245 | listener = (ContinuumReleaseManagerListener) releaseManager.getListeners().get( releaseId ); |
246 | |
247 | if ( listener != null ) |
248 | { |
249 | listenerSummary.setPhases( listener.getPhases() ); |
250 | listenerSummary.setCompletedPhases( listener.getCompletedPhases() ); |
251 | listenerSummary.setInProgress( listener.getInProgress() ); |
252 | listenerSummary.setError( listener.getError() ); |
253 | |
254 | if ( listener.getState() == ContinuumReleaseManagerListener.FINISHED ) |
255 | { |
256 | result = (ReleaseResult) releaseManager.getReleaseResults().get( releaseId ); |
257 | |
258 | return SUCCESS; |
259 | } |
260 | else |
261 | { |
262 | throw new Exception( "The release operation with id " + releaseId + "has not finished yet." ); |
263 | } |
264 | } |
265 | else |
266 | { |
267 | throw new Exception( "There is no finished release operation with id " + releaseId ); |
268 | } |
269 | } |
270 | } |
271 | |
272 | public String getReleaseId() |
273 | { |
274 | return releaseId; |
275 | } |
276 | |
277 | public void setReleaseId( String releaseId ) |
278 | { |
279 | this.releaseId = releaseId; |
280 | } |
281 | |
282 | public ContinuumReleaseManagerListener getListener() |
283 | { |
284 | return listener; |
285 | } |
286 | |
287 | public void setListener( ContinuumReleaseManagerListener listener ) |
288 | { |
289 | this.listener = listener; |
290 | } |
291 | |
292 | public ReleaseResult getResult() |
293 | { |
294 | return result; |
295 | } |
296 | |
297 | public void setResult( ReleaseResult result ) |
298 | { |
299 | this.result = result; |
300 | } |
301 | |
302 | public int getProjectId() |
303 | { |
304 | return projectId; |
305 | } |
306 | |
307 | public void setProjectId( int projectId ) |
308 | { |
309 | this.projectId = projectId; |
310 | } |
311 | |
312 | public String getReleaseGoal() |
313 | { |
314 | return releaseGoal; |
315 | } |
316 | |
317 | public void setReleaseGoal( String releaseGoal ) |
318 | { |
319 | this.releaseGoal = releaseGoal; |
320 | } |
321 | |
322 | public String getProjectGroupName() |
323 | throws ContinuumException |
324 | { |
325 | if ( projectGroupName == null || "".equals( projectGroupName ) ) |
326 | { |
327 | projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName(); |
328 | } |
329 | |
330 | return projectGroupName; |
331 | } |
332 | |
333 | public ReleaseListenerSummary getListenerSummary() |
334 | { |
335 | return listenerSummary; |
336 | } |
337 | |
338 | public void setListenerSummary( ReleaseListenerSummary listenerSummary ) |
339 | { |
340 | this.listenerSummary = listenerSummary; |
341 | } |
342 | |
343 | private ContinuumReleaseResult createContinuumReleaseResult( ReleaseResult result ) |
344 | throws ContinuumException |
345 | { |
346 | ContinuumReleaseResult releaseResult = new ContinuumReleaseResult(); |
347 | releaseResult.setStartTime( result.getStartTime() ); |
348 | releaseResult.setEndTime( result.getEndTime() ); |
349 | releaseResult.setResultCode( result.getResultCode() ); |
350 | |
351 | Project project = getContinuum().getProject( projectId ); |
352 | ProjectGroup projectGroup = project.getProjectGroup(); |
353 | releaseResult.setProjectGroup( projectGroup ); |
354 | releaseResult.setProject( project ); |
355 | releaseResult.setReleaseGoal( releaseGoal ); |
356 | |
357 | String releaseName = "releases-" + result.getStartTime(); |
358 | |
359 | try |
360 | { |
361 | File logFile = getContinuum().getConfiguration().getReleaseOutputFile( projectGroup.getId(), releaseName ); |
362 | |
363 | PrintWriter writer = new PrintWriter( new FileWriter( logFile ) ); |
364 | writer.write( result.getOutput() ); |
365 | writer.close(); |
366 | } |
367 | catch ( ConfigurationException e ) |
368 | { |
369 | throw new ContinuumException( e.getMessage(), e ); |
370 | } |
371 | catch ( IOException e ) |
372 | { |
373 | throw new ContinuumException( "Unable to write output to file", e ); |
374 | } |
375 | |
376 | return releaseResult; |
377 | } |
378 | |
379 | public String getProjectName() |
380 | throws ContinuumException |
381 | { |
382 | return getProjectGroupName(); |
383 | } |
384 | |
385 | } |