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 org.apache.archiva.audit.AuditEvent;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
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.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
31 import org.codehaus.plexus.redback.role.RoleManagerException;
33 import java.io.IOException;
34 import java.util.List;
38 * DeleteManagedRepositoryAction
41 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="deleteManagedRepositoryAction" instantiation-strategy="per-lookup"
43 public class DeleteManagedRepositoryAction
44 extends AbstractManagedRepositoriesAction
47 private ManagedRepositoryConfiguration repository;
49 private ManagedRepositoryConfiguration stagingRepository;
51 private String repoid;
56 private RepositoryStatisticsManager repositoryStatisticsManager;
61 private MetadataRepository metadataRepository;
65 if ( StringUtils.isNotBlank( repoid ) )
67 this.repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid );
68 this.stagingRepository = archivaConfiguration.getConfiguration().findManagedRepositoryById(
73 public String confirmDelete()
75 if ( StringUtils.isBlank( repoid ) )
77 addActionError( "Unable to delete managed repository: repository id was blank." );
84 public String deleteEntry()
86 return deleteRepository( false );
89 public String deleteContents()
91 return deleteRepository( true );
94 private String deleteRepository( boolean deleteContents )
96 ManagedRepositoryConfiguration existingRepository = repository;
97 ManagedRepositoryConfiguration attachedStagingRepo = stagingRepository;
98 if ( existingRepository == null )
100 addActionError( "A repository with that id does not exist" );
108 Configuration configuration = archivaConfiguration.getConfiguration();
109 if ( attachedStagingRepo != null )
111 cleanupRepositoryData( attachedStagingRepo );
112 removeRepository( repoid + "-stage", configuration );
113 triggerAuditEvent( repoid + "-stage", null, AuditEvent.DELETE_MANAGED_REPO );
116 cleanupRepositoryData( existingRepository );
117 removeRepository( repoid, configuration );
118 triggerAuditEvent( repoid, null, AuditEvent.DELETE_MANAGED_REPO );
119 result = saveConfiguration( configuration );
121 if ( result.equals( SUCCESS ) )
123 if ( deleteContents )
125 if ( attachedStagingRepo != null )
127 removeContents( attachedStagingRepo );
129 removeContents( existingRepository );
133 catch ( IOException e )
136 "Unable to delete repository, content may already be partially removed: " + e.getMessage() );
139 catch ( RoleManagerException e )
142 "Unable to delete repository, content may already be partially removed: " + e.getMessage() );
145 catch ( MetadataRepositoryException e )
148 "Unable to delete repository, content may already be partially removed: " + e.getMessage() );
155 private void cleanupRepositoryData( ManagedRepositoryConfiguration cleanupRepository )
156 throws RoleManagerException, MetadataRepositoryException
158 removeRepositoryRoles( cleanupRepository );
159 cleanupDatabase( cleanupRepository.getId() );
160 repositoryStatisticsManager.deleteStatistics( cleanupRepository.getId() );
161 // TODO: delete all content for a repository from the content API?
163 List<ProxyConnectorConfiguration> proxyConnectors = getProxyConnectors();
164 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
166 if ( StringUtils.equals( proxyConnector.getSourceRepoId(), cleanupRepository.getId() ) )
168 archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector );
172 Map<String, List<String>> repoToGroupMap = archivaConfiguration.getConfiguration().getRepositoryToGroupMap();
173 if ( repoToGroupMap != null )
175 if ( repoToGroupMap.containsKey( cleanupRepository.getId() ) )
177 List<String> repoGroups = repoToGroupMap.get( cleanupRepository.getId() );
178 for ( String repoGroup : repoGroups )
180 archivaConfiguration.getConfiguration().findRepositoryGroupById( repoGroup ).removeRepository(
181 cleanupRepository.getId() );
187 private void cleanupDatabase( String repoId )
188 throws MetadataRepositoryException
190 metadataRepository.removeRepository( repoId );
193 public ManagedRepositoryConfiguration getRepository()
198 public void setRepository( ManagedRepositoryConfiguration repository )
200 this.repository = repository;
203 public String getRepoid()
208 public void setRepoid( String repoid )
210 this.repoid = repoid;
213 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
215 this.repositoryStatisticsManager = repositoryStatisticsManager;
218 public void setMetadataRepository( MetadataRepository metadataRepository )
220 this.metadataRepository = metadataRepository;