View Javadoc

1   package org.apache.maven.continuum.web.action.admin;
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.List;
23  
24  import org.apache.continuum.buildmanager.BuildManagerException;
25  import org.apache.continuum.web.util.AuditLog;
26  import org.apache.continuum.web.util.AuditLogConstants;
27  import org.apache.maven.continuum.ContinuumException;
28  import org.apache.maven.continuum.model.project.BuildQueue;
29  import org.apache.maven.continuum.security.ContinuumRoleConstants;
30  import org.apache.maven.continuum.web.action.ContinuumConfirmAction;
31  import org.codehaus.plexus.redback.rbac.Resource;
32  import org.codehaus.redback.integration.interceptor.SecureAction;
33  import org.codehaus.redback.integration.interceptor.SecureActionBundle;
34  import org.codehaus.redback.integration.interceptor.SecureActionException;
35  
36  import com.opensymphony.xwork2.Preparable;
37  
38  /**
39   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="buildQueueAction"
40   */
41  public class BuildQueueAction
42      extends ContinuumConfirmAction
43      implements Preparable, SecureAction
44  {
45      private String name;
46  
47      private int size;
48  
49      private List<BuildQueue> buildQueueList;
50  
51      private BuildQueue buildQueue;
52      
53      private String message;
54      
55      private boolean confirmed;
56  
57      public void prepare()
58          throws ContinuumException
59      {
60          this.buildQueueList = getContinuum().getAllBuildQueues();
61      }
62  
63      public String input()
64      {
65          return INPUT;
66      }
67  
68      public String list()
69          throws Exception
70      {
71          try
72          {
73              this.buildQueueList = getContinuum().getAllBuildQueues();
74          }
75          catch ( ContinuumException e )
76          {
77              addActionError( "Cannot get build queues from the database : " + e.getMessage() );
78              return ERROR;
79          }
80          return SUCCESS;
81      }
82  
83      public String save()
84          throws Exception
85      {
86          int allowedBuilds = getContinuum().getConfiguration().getNumberOfBuildsInParallel();
87          if ( allowedBuilds < ( this.buildQueueList.size() + 1 ) )
88          {
89              addActionError( "You are only allowed " + allowedBuilds + " number of builds in parallel." );
90              return ERROR;
91          }
92          else
93          {
94              try
95              {
96                  if ( !isDuplicate( name ) )
97                  {
98                      BuildQueue buildQueue = new BuildQueue();
99                      buildQueue.setName( name );
100                     BuildQueue addedBuildQueue = getContinuum().addBuildQueue( buildQueue );
101     
102                     getContinuum().getBuildsManager().addOverallBuildQueue( addedBuildQueue );
103 
104                     AuditLog event = new AuditLog( "Build Queue id=" + addedBuildQueue.getId(), AuditLogConstants.ADD_BUILD_QUEUE );
105                     event.setCategory( AuditLogConstants.BUILD_QUEUE );
106                     event.setCurrentUser( getPrincipal() );
107                     event.log();
108                 }
109                 else
110                 {
111                     addActionError( "Build queue name already exists." );
112                     return ERROR;
113                 }
114             }
115             catch ( ContinuumException e )
116             {
117                 addActionError( "Error adding build queue to database: " + e.getMessage() );
118                 return ERROR;
119             }
120             catch ( BuildManagerException e )
121             {
122                 addActionError( "Error creating overall build queue: " + e.getMessage() );
123                 return ERROR;
124             }
125 
126             return SUCCESS;
127         }
128     }
129 
130     public String edit()
131         throws Exception
132     {
133         try
134         {
135             BuildQueue buildQueueToBeEdited = getContinuum().getBuildQueue( this.buildQueue.getId() );
136         }
137         catch ( ContinuumException e )
138         {
139             addActionError( "Error retrieving build queue from the database : " + e.getMessage() );
140             return ERROR;
141         }
142         return SUCCESS;
143     }
144 
145     public String delete()
146         throws Exception
147     {        
148         if ( confirmed )
149         {
150             BuildQueue buildQueueToBeDeleted = getContinuum().getBuildQueue( this.buildQueue.getId() );
151             getContinuum().getBuildsManager().removeOverallBuildQueue( buildQueueToBeDeleted.getId() );
152             getContinuum().removeBuildQueue( buildQueueToBeDeleted );
153 
154             this.buildQueueList = getContinuum().getAllBuildQueues();
155 
156             AuditLog event = new AuditLog( "Build Queue id=" + buildQueue.getId(), AuditLogConstants.REMOVE_BUILD_QUEUE );
157             event.setCategory( AuditLogConstants.BUILD_QUEUE );
158             event.setCurrentUser( getPrincipal() );
159             event.log();
160         }
161         else
162         {
163             return CONFIRM;
164         }
165         
166         return SUCCESS;
167     }
168 
169     public SecureActionBundle getSecureActionBundle()
170         throws SecureActionException
171     {
172         SecureActionBundle bundle = new SecureActionBundle();
173         bundle.setRequiresAuthentication( true );
174         bundle.addRequiredAuthorization( ContinuumRoleConstants.CONTINUUM_MANAGE_PARALLEL_BUILDS, Resource.GLOBAL );
175     
176         return bundle;
177     }
178 
179     public String getName()
180     {
181         return name;
182     }
183 
184     public void setName( String name )
185     {
186         this.name = name;
187     }
188 
189     public List<BuildQueue> getBuildQueueList()
190     {
191         return buildQueueList;
192     }
193 
194     public void setBuildQueueList( List<BuildQueue> buildQueueList )
195     {
196         this.buildQueueList = buildQueueList;
197     }
198 
199     public int getSize()
200     {
201         return size;
202     }
203 
204     public void setSize( int size )
205     {
206         this.size = size;
207     }
208 
209     public BuildQueue getBuildQueue()
210     {
211         return buildQueue;
212     }
213 
214     public void setBuildQueue( BuildQueue buildQueue )
215     {
216         this.buildQueue = buildQueue;
217     }
218 
219     public String getMessage()
220     {
221         return message;
222     }
223 
224     public void setMessage( String message )
225     {
226         this.message = message;
227     }
228     
229     private boolean isDuplicate( String queueName )
230         throws ContinuumException
231     {
232         boolean isExisting = false;
233         
234         List<BuildQueue> buildQueues = getContinuum().getAllBuildQueues();
235         
236         for ( BuildQueue bq : buildQueues )
237         {
238             if ( queueName.equals( bq.getName() ) )
239             {
240                 isExisting = true;
241                 break;
242             }
243         }
244         
245         return isExisting;
246     }
247 
248     public boolean isConfirmed()
249     {
250         return confirmed;
251     }
252 
253     public void setConfirmed( boolean confirmed )
254     {
255         this.confirmed = confirmed;
256     }
257 }