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