View Javadoc

1   package org.apache.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.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.continuum.model.repository.LocalRepository;
28  import org.apache.continuum.model.repository.RepositoryPurgeConfiguration;
29  import org.apache.continuum.purge.ContinuumPurgeManager;
30  import org.apache.continuum.purge.PurgeConfigurationService;
31  import org.apache.continuum.repository.RepositoryService;
32  import org.apache.continuum.taskqueue.manager.TaskQueueManager;
33  import org.apache.continuum.web.util.AuditLog;
34  import org.apache.continuum.web.util.AuditLogConstants;
35  import org.apache.maven.continuum.model.project.ProjectGroup;
36  import org.apache.maven.continuum.security.ContinuumRoleConstants;
37  import org.apache.maven.continuum.web.action.ContinuumConfirmAction;
38  import org.codehaus.plexus.redback.rbac.Resource;
39  import org.codehaus.redback.integration.interceptor.SecureAction;
40  import org.codehaus.redback.integration.interceptor.SecureActionBundle;
41  import org.codehaus.redback.integration.interceptor.SecureActionException;
42  
43  import com.opensymphony.xwork2.Preparable;
44  
45  /**
46   * @author Maria Catherine Tan
47   * @version $Id: LocalRepositoryAction.java 1101669 2011-05-10 22:46:21Z ctan $
48   * @since 25 jul 07
49   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="localRepository"
50   */
51  public class LocalRepositoryAction
52      extends ContinuumConfirmAction
53      implements Preparable, SecureAction
54  {
55      private static final String LAYOUT_DEFAULT = "default";
56      
57      private static final String LAYOUT_LEGACY = "legacy";
58      
59      private boolean confirmed;
60  
61      private boolean defaultRepo;
62      
63      private LocalRepository repository;
64      
65      private List<LocalRepository> repositories;
66      
67      private List<ProjectGroup> groups;
68      
69      private List<String> layouts;
70      
71      private Map<String, Boolean> defaultPurgeMap;
72  
73      /**
74       * @plexus.requirement
75       */
76      private RepositoryService repositoryService;
77      
78      /**
79       * @plexus.requirement
80       */
81      private PurgeConfigurationService purgeConfigService;
82      
83      public void prepare()
84          throws Exception
85      {
86          super.prepare();
87          
88          layouts = new ArrayList<String>();
89          layouts.add( LAYOUT_DEFAULT );
90          layouts.add( LAYOUT_LEGACY );
91      }
92      
93      public String input()
94          throws Exception
95      {
96          defaultRepo = false;
97          
98          if ( repository != null && repository.getId() > 0 )
99          {
100             repository = repositoryService.getLocalRepository( repository.getId() );
101             
102             if ( repository.getName().equals( "DEFAULT" ) )
103             {
104                 defaultRepo = true;
105             }
106         }
107         
108         return INPUT;
109     }
110     
111     public String list()
112         throws Exception
113     {
114         repositories = repositoryService.getAllLocalRepositories();
115         
116         defaultPurgeMap = new HashMap<String, Boolean>();
117         
118         for ( LocalRepository repo : repositories )
119         {
120             // get default purge config of repository
121             RepositoryPurgeConfiguration purgeConfig = purgeConfigService.getDefaultPurgeConfigurationForRepository( repo.getId() );
122             
123             if ( purgeConfig == null )
124             {
125                 defaultPurgeMap.put(  repo.getName(), Boolean.FALSE );
126             }
127             else
128             {
129                 defaultPurgeMap.put(  repo.getName(), Boolean.TRUE );
130             }
131         }
132         
133         return SUCCESS;
134     }
135     
136     public String save()
137         throws Exception
138     {
139         List<LocalRepository> allRepositories = repositoryService.getAllLocalRepositories();
140         
141         for( LocalRepository repo : allRepositories )
142         {
143             if ( repository.getId() != repo.getId() )
144             {
145                 if ( repository.getName().equals( repo.getName() ) )
146                 {
147                     addActionError( getText( "repository.error.name.unique" ) );
148                 }
149                 
150                 if ( repository.getLocation().equals( repo.getLocation() ) )
151                 {
152                     addActionError( getText( "repository.error.location.unique" ) );
153                 }
154             }
155         }
156         
157         if ( repository.getName().trim().equals( "" ) )
158         {
159             addActionError( getText( "repository.error.name.cannot.be.spaces" ) );
160         }
161         
162         if ( repository.getLocation().trim().equals( "" ) )
163         {
164             addActionError( getText( "repository.error.location.cannot.be.spaces" ) );
165         }
166         
167         if ( hasActionErrors() )
168         {
169             return INPUT;
170         }
171         
172         if ( repository.getId() == 0 )
173         {
174             repository = repositoryService.addLocalRepository( repository );
175             
176             createDefaultPurgeConfiguration();
177         }
178         else
179         {
180             // check if repository is in use
181             TaskQueueManager taskQueueManager = getContinuum().getTaskQueueManager();
182             if ( taskQueueManager.isRepositoryInUse( repository.getId() ) )
183             {
184                 addActionError( getText( "repository.error.save.in.use" ) );
185                 return ERROR;
186             }
187             
188             LocalRepository retrievedRepo = repositoryService.getLocalRepository( repository.getId() );
189             
190             retrievedRepo.setName( repository.getName() );
191             retrievedRepo.setLocation( repository.getLocation() );
192             retrievedRepo.setLayout( repository.getLayout() );
193             
194             repositoryService.updateLocalRepository( retrievedRepo );
195         }
196         
197         return SUCCESS;
198     }
199     
200     public String remove()
201         throws Exception
202     {
203         TaskQueueManager taskQueueManager = getContinuum().getTaskQueueManager();
204 
205         repository = repositoryService.getLocalRepository( repository.getId() );
206 
207         if ( taskQueueManager.isRepositoryInUse( repository.getId() ) )
208         {
209             addActionError( getText( "repository.error.remove.in.use", "Unable to remove local repository because it is in use" ) );
210         }
211 
212         if ( repository.getName().equals( "DEFAULT" ) )
213         {
214             addActionError( getText( "repository.error.remove.default", "Unable to remove default local repository" ) );
215         }
216 
217         if ( !hasActionErrors() )
218         {
219             if ( confirmed )
220             {
221                 repositoryService.removeLocalRepository( repository.getId() );
222             }
223             else
224             {
225                 return CONFIRM;
226             }
227         }
228 
229         return SUCCESS;
230     }
231     
232     public String doPurge()
233         throws Exception
234     {
235         ContinuumPurgeManager purgeManager = getContinuum().getPurgeManager();
236         TaskQueueManager taskQueueManager = getContinuum().getTaskQueueManager();
237 
238         // check if repository is in use
239         if ( taskQueueManager.isRepositoryInUse( repository.getId() ) )
240         {
241             addActionError( getText( "repository.error.purge.in.use", "Unable to purge repository because it is in use" ) );
242         }
243      
244         if ( !hasActionErrors() )
245         {
246             // get default purge configuration for repository
247             RepositoryPurgeConfiguration purgeConfig = purgeConfigService.getDefaultPurgeConfigurationForRepository( repository.getId() );
248         
249             if ( purgeConfig != null )
250             {
251                 purgeManager.purgeRepository( purgeConfig );
252 
253                 AuditLog event = new AuditLog( "Repository id=" + repository.getId(), AuditLogConstants.PURGE_LOCAL_REPOSITORY );
254                 event.setCategory( AuditLogConstants.LOCAL_REPOSITORY );
255                 event.setCurrentUser( getPrincipal() );
256                 event.log();
257             }
258         }
259         return SUCCESS;
260     }
261     
262     public LocalRepository getRepository()
263     {
264         return this.repository;
265     }
266     
267     public void setRepository( LocalRepository repository )
268     {
269         this.repository = repository;
270     }
271     
272     public List<LocalRepository> getRepositories()
273     {
274         return this.repositories;
275     }
276     
277     public void setRepositories( List<LocalRepository> repositories )
278     {
279         this.repositories = repositories;
280     }
281     
282     public List<ProjectGroup> getGroups()
283     {
284         return this.groups;
285     }
286     
287     public void setGroups( List<ProjectGroup> groups )
288     {
289         this.groups = groups;
290     }
291     
292     public boolean isConfirmed()
293     {
294         return this.confirmed;
295     }
296     
297     public void setConfirmed( boolean confirmed )
298     {
299         this.confirmed = confirmed;
300     }
301     
302     public boolean isDefaultRepo()
303     {
304         return this.defaultRepo;
305     }
306     
307     public void setDefaultRepo( boolean defaultRepo )
308     {
309         this.defaultRepo = defaultRepo;
310     }
311     
312     public List<String> getLayouts()
313     {
314         return this.layouts;
315     }
316     
317     public Map<String, Boolean> getDefaultPurgeMap()
318     {
319         return this.defaultPurgeMap;
320     }
321     
322     public void setDefaultPurgeMap( Map<String, Boolean> defaultPurgeMap )
323     {
324         this.defaultPurgeMap = defaultPurgeMap;
325     }
326 
327     private void createDefaultPurgeConfiguration()
328         throws Exception
329     {
330         RepositoryPurgeConfiguration repoPurge = new RepositoryPurgeConfiguration();
331         
332         repoPurge.setRepository( repository );
333         repoPurge.setDefaultPurge( true );
334         
335         purgeConfigService.addRepositoryPurgeConfiguration( repoPurge );
336     }
337 
338     public SecureActionBundle getSecureActionBundle()
339         throws SecureActionException
340     {
341         SecureActionBundle bundle = new SecureActionBundle();
342         bundle.setRequiresAuthentication( true );
343         bundle.addRequiredAuthorization( ContinuumRoleConstants.CONTINUUM_MANAGE_REPOSITORIES, Resource.GLOBAL );
344 
345         return bundle;
346     }
347 }