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.stats.RepositoryStatisticsManager;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.codehaus.plexus.redback.role.RoleManagerException;
30 import org.codehaus.plexus.scheduler.CronExpressionValidator;
33 import java.io.IOException;
36 * AddManagedRepositoryAction
39 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="editManagedRepositoryAction" instantiation-strategy="per-lookup"
41 public class EditManagedRepositoryAction
42 extends AbstractManagedRepositoriesAction
43 implements Preparable, Validateable
46 * The model for this action.
48 private ManagedRepositoryConfiguration repository;
50 private String repoid;
52 private final String action = "editRepository";
54 private boolean stageNeeded;
59 private RepositoryStatisticsManager repositoryStatisticsManager;
63 if ( StringUtils.isNotBlank( repoid ) )
65 repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid );
67 else if ( repository != null )
69 repository.setReleases( false );
70 repository.setScanned( false );
76 if ( repository == null )
78 addActionError( "Edit failure, unable to edit a repository with a blank repository id." );
85 public String confirmUpdate()
87 // location was changed
91 public String commit()
93 ManagedRepositoryConfiguration existingConfig =
94 archivaConfiguration.getConfiguration().findManagedRepositoryById( repository.getId() );
96 boolean resetStats = false;
98 // check if the location was changed
99 if ( !StringUtils.equalsIgnoreCase( existingConfig.getLocation().trim(), repository.getLocation().trim() ) )
103 File dir = new File( repository.getLocation() );
110 return save( resetStats );
113 private String save( boolean resetStats )
115 // Ensure that the fields are valid.
116 Configuration configuration = archivaConfiguration.getConfiguration();
118 // We are in edit mode, remove the old repository configuration.
119 removeRepository( repository.getId(), configuration );
121 // Save the repository configuration.
125 addRepository( repository, configuration );
126 triggerAuditEvent( repository.getId(), null, AuditEvent.MODIFY_MANAGED_REPO );
127 addRepositoryRoles( repository );
131 ManagedRepositoryConfiguration stagingRepository = getStageRepoConfig();
132 // if(new File( stagingRepository.getLocation()).exists())
133 addRepository( stagingRepository, configuration );
134 triggerAuditEvent( stagingRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO );
135 addRepositoryRoles( stagingRepository );
139 result = saveConfiguration( configuration );
145 catch ( IOException e )
147 addActionError( "I/O Exception: " + e.getMessage() );
150 catch ( RoleManagerException e )
152 addActionError( "Role Manager Exception: " + e.getMessage() );
159 private ManagedRepositoryConfiguration getStageRepoConfig()
161 ManagedRepositoryConfiguration stagingRepository = new ManagedRepositoryConfiguration();
162 stagingRepository.setId( repository.getId() + "-stage" );
163 stagingRepository.setLayout( repository.getLayout() );
164 stagingRepository.setName( repository.getName() + "-stage" );
165 stagingRepository.setBlockRedeployments( repository.isBlockRedeployments() );
166 stagingRepository.setDaysOlder( repository.getDaysOlder() );
167 stagingRepository.setDeleteReleasedSnapshots( repository.isDeleteReleasedSnapshots() );
168 stagingRepository.setIndexDir( repository.getIndexDir() );
169 String path = repository.getLocation();
170 int lastIndex = path.lastIndexOf( '/' );
171 stagingRepository.setLocation( path.substring( 0, lastIndex ) + "/" + stagingRepository.getId() );
172 stagingRepository.setRefreshCronExpression( repository.getRefreshCronExpression() );
173 stagingRepository.setReleases( repository.isReleases() );
174 stagingRepository.setRetentionCount( repository.getRetentionCount() );
175 stagingRepository.setScanned( repository.isScanned() );
176 stagingRepository.setSnapshots( repository.isSnapshots() );
177 return stagingRepository;
181 public void validate()
183 CronExpressionValidator validator = new CronExpressionValidator();
185 if ( !validator.validate( repository.getRefreshCronExpression() ) )
187 addFieldError( "repository.refreshCronExpression", "Invalid cron expression." );
191 private void resetStatistics()
193 repositoryStatisticsManager.deleteStatistics( repository.getId() );
196 public String getRepoid()
201 public void setRepoid( String repoid )
203 this.repoid = repoid;
206 public ManagedRepositoryConfiguration getRepository()
211 public void setRepository( ManagedRepositoryConfiguration repository )
213 this.repository = repository;
216 public boolean isStageNeeded()
221 public void setStageNeeded( boolean stageNeeded )
223 this.stageNeeded = stageNeeded;
226 public String getAction()
231 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
233 this.repositoryStatisticsManager = repositoryStatisticsManager;