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.release.distributed.manager.DistributedReleaseManager;
24  import org.apache.continuum.web.util.AuditLog;
25  import org.apache.continuum.web.util.AuditLogConstants;
26  import org.apache.maven.continuum.ContinuumException;
27  import org.apache.maven.continuum.model.project.Project;
28  import org.apache.maven.continuum.release.ContinuumReleaseException;
29  import org.apache.maven.continuum.release.ContinuumReleaseManager;
30  import org.apache.maven.continuum.release.ContinuumReleaseManagerListener;
31  import org.apache.maven.continuum.release.DefaultReleaseManagerListener;
32  import org.apache.maven.continuum.utils.WorkingDirectoryService;
33  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
34  import org.codehaus.plexus.util.StringUtils;
35  
36  /**
37   * @author Edwin Punzalan
38   * @version $Id: ReleaseRollbackAction.java 1395451 2012-10-08 05:06:18Z brett $
39   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="releaseRollback"
40   */
41  public class ReleaseRollbackAction
42      extends ContinuumActionSupport
43  {
44      /**
45       * @plexus.requirement
46       */
47      private WorkingDirectoryService workingDirectoryService;
48  
49      private int projectId;
50  
51      private String releaseId;
52  
53      private String projectGroupName = "";
54  
55      private String releaseGoal;
56  
57      public String execute()
58          throws Exception
59      {
60          try
61          {
62              checkBuildProjectInGroupAuthorization( getProjectGroupName() );
63          }
64          catch ( AuthorizationRequiredException e )
65          {
66              return REQUIRES_AUTHORIZATION;
67          }
68  
69          if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
70          {
71              DistributedReleaseManager releaseManager = getContinuum().getDistributedReleaseManager();
72  
73              try
74              {
75                  releaseManager.releaseRollback( releaseId, projectId );
76              }
77              catch ( ContinuumReleaseException e )
78              {
79                  if ( e.getMessage() != null )
80                  {
81                      addActionError( e.getMessage() );
82                      return RELEASE_ERROR;
83                  }
84                  else
85                  {
86                      throw e;
87                  }
88              }
89              catch ( BuildAgentConfigurationException e )
90              {
91                  addActionError( "Error with configuration of build agent: " + e.getMessage() );
92                  return RELEASE_ERROR;
93              }
94          }
95          else
96          {
97              ContinuumReleaseManager releaseManager = getContinuum().getReleaseManager();
98  
99              ContinuumReleaseManagerListener listener = new DefaultReleaseManagerListener();
100 
101             listener.setUsername( getPrincipal() );
102 
103             Project project = getContinuum().getProject( projectId );
104 
105             releaseManager.rollback( releaseId, workingDirectoryService.getWorkingDirectory( project ).getPath(),
106                                      listener );
107 
108             //recurse until rollback is finished
109             while ( listener.getState() != ContinuumReleaseManagerListener.FINISHED )
110             {
111                 try
112                 {
113                     Thread.sleep( 1000 );
114                 }
115                 catch ( InterruptedException e )
116                 {
117                     //do nothing
118                 }
119             }
120 
121             AuditLog event = new AuditLog( "Release id=" + releaseId, AuditLogConstants.ROLLBACK_RELEASE );
122             event.setCategory( AuditLogConstants.PROJECT );
123             event.setCurrentUser( getPrincipal() );
124             event.log();
125 
126             releaseManager.getPreparedReleases().remove( releaseId );
127         }
128 
129         return SUCCESS;
130     }
131 
132     public String warn()
133         throws Exception
134     {
135         try
136         {
137             checkBuildProjectInGroupAuthorization( getProjectGroupName() );
138         }
139         catch ( AuthorizationRequiredException e )
140         {
141             return REQUIRES_AUTHORIZATION;
142         }
143 
144         return SUCCESS;
145     }
146 
147     public int getProjectId()
148     {
149         return projectId;
150     }
151 
152     public void setProjectId( int projectId )
153     {
154         this.projectId = projectId;
155     }
156 
157     public String getReleaseId()
158     {
159         return releaseId;
160     }
161 
162     public void setReleaseId( String releaseId )
163     {
164         this.releaseId = releaseId;
165     }
166 
167     public String getProjectGroupName()
168         throws ContinuumException
169     {
170         if ( StringUtils.isEmpty( projectGroupName ) )
171         {
172             projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName();
173         }
174 
175         return projectGroupName;
176     }
177 
178     public String getReleaseGoal()
179     {
180         return releaseGoal;
181     }
182 
183     public void setReleaseGoal( String releaseGoal )
184     {
185         this.releaseGoal = releaseGoal;
186     }
187 }