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