1 package org.apache.maven.archiva.web.action.admin.repositories;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import com.opensymphony.xwork2.Preparable;
23 import com.opensymphony.xwork2.Validateable;
24 import org.apache.archiva.audit.AuditEvent;
25 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
26 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.codehaus.plexus.redback.role.RoleManagerException;
31 import org.codehaus.plexus.scheduler.CronExpressionValidator;
34 import java.io.IOException;
37 * AddManagedRepositoryAction
40 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="editManagedRepositoryAction" instantiation-strategy="per-lookup"
42 public class EditManagedRepositoryAction
43 extends AbstractManagedRepositoriesAction
44 implements Preparable, Validateable
47 * The model for this action.
49 private ManagedRepositoryConfiguration repository;
51 private ManagedRepositoryConfiguration stagingRepository;
53 private String repoid;
55 private final String action = "editRepository";
57 private boolean stageNeeded;
62 private RepositoryStatisticsManager repositoryStatisticsManager;
66 if ( StringUtils.isNotBlank( repoid ) )
68 repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid );
69 stagingRepository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid + "-stage" );
71 else if ( repository != null )
73 repository.setReleases( false );
74 repository.setScanned( false );
80 if ( repository == null )
82 addActionError( "Edit failure, unable to edit a repository with a blank repository id." );
89 public String confirmUpdate()
91 // location was changed
95 public String commit()
97 ManagedRepositoryConfiguration existingConfig =
98 archivaConfiguration.getConfiguration().findManagedRepositoryById( repository.getId() );
99 boolean resetStats = false;
101 // check if the location was changed
102 if ( !StringUtils.equalsIgnoreCase( existingConfig.getLocation().trim(), repository.getLocation().trim() ) )
106 File dir = new File( repository.getLocation() );
113 return save( resetStats );
116 private String save( boolean resetStats )
118 // Ensure that the fields are valid.
119 Configuration configuration = archivaConfiguration.getConfiguration();
121 // We are in edit mode, remove the old repository configuration.
122 removeRepository( repository.getId(), configuration );
123 if ( stagingRepository != null )
125 removeRepository( stagingRepository.getId(), configuration );
128 // Save the repository configuration.
132 addRepository( repository, configuration );
133 triggerAuditEvent( repository.getId(), null, AuditEvent.MODIFY_MANAGED_REPO );
134 addRepositoryRoles( repository );
136 //update changes of the staging repo
140 stagingRepository = getStageRepoConfig( configuration );
141 addRepository( stagingRepository, configuration );
142 addRepositoryRoles( stagingRepository );
145 //delete staging repo when we dont need it
148 stagingRepository = getStageRepoConfig( configuration );
149 removeRepository( stagingRepository.getId(), configuration );
150 removeContents( stagingRepository );
151 removeRepositoryRoles( stagingRepository );
154 result = saveConfiguration( configuration );
160 catch ( IOException e )
162 addActionError( "I/O Exception: " + e.getMessage() );
165 catch ( RoleManagerException e )
167 addActionError( "Role Manager Exception: " + e.getMessage() );
170 catch ( MetadataRepositoryException e )
172 addActionError( "Metadata Exception: " + e.getMessage() );
179 private ManagedRepositoryConfiguration getStageRepoConfig( Configuration configuration )
181 for ( ManagedRepositoryConfiguration repoConf : configuration.getManagedRepositories() )
183 if ( repoConf.getId().equals( repository.getId() + "-stage" ) )
185 stagingRepository = repoConf;
186 removeRepository( repoConf.getId(), configuration );
187 updateStagingRepository( stagingRepository );
188 return stagingRepository;
192 stagingRepository = new ManagedRepositoryConfiguration();
193 updateStagingRepository( stagingRepository );
195 return stagingRepository;
198 private void updateStagingRepository( ManagedRepositoryConfiguration stagingRepository )
200 stagingRepository.setId( repository.getId() + "-stage" );
201 stagingRepository.setLayout( repository.getLayout() );
202 stagingRepository.setName( repository.getName() + "-stage" );
203 stagingRepository.setBlockRedeployments( repository.isBlockRedeployments() );
204 stagingRepository.setDaysOlder( repository.getDaysOlder() );
205 stagingRepository.setDeleteReleasedSnapshots( repository.isDeleteReleasedSnapshots() );
206 stagingRepository.setIndexDir( repository.getIndexDir() );
207 String path = repository.getLocation();
208 int lastIndex = path.lastIndexOf( '/' );
209 stagingRepository.setLocation( path.substring( 0, lastIndex ) + "/" + stagingRepository.getId() );
210 stagingRepository.setRefreshCronExpression( repository.getRefreshCronExpression() );
211 stagingRepository.setReleases( repository.isReleases() );
212 stagingRepository.setRetentionCount( repository.getRetentionCount() );
213 stagingRepository.setScanned( repository.isScanned() );
214 stagingRepository.setSnapshots( repository.isSnapshots() );
218 public void validate()
220 CronExpressionValidator validator = new CronExpressionValidator();
222 if ( !validator.validate( repository.getRefreshCronExpression() ) )
224 addFieldError( "repository.refreshCronExpression", "Invalid cron expression." );
228 private void resetStatistics()
229 throws MetadataRepositoryException
231 repositoryStatisticsManager.deleteStatistics( repository.getId() );
234 public String getRepoid()
239 public void setRepoid( String repoid )
241 this.repoid = repoid;
244 public ManagedRepositoryConfiguration getRepository()
249 public void setRepository( ManagedRepositoryConfiguration repository )
251 this.repository = repository;
254 public boolean isStageNeeded()
259 public void setStageNeeded( boolean stageNeeded )
261 this.stageNeeded = stageNeeded;
264 public String getAction()
269 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
271 this.repositoryStatisticsManager = repositoryStatisticsManager;
274 public ManagedRepositoryConfiguration getStagingRepository()
276 return stagingRepository;
279 public void setStagingRepository( ManagedRepositoryConfiguration stagingRepository )
281 this.stagingRepository = stagingRepository;