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.RepositorySession;
27 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
32 import org.codehaus.plexus.redback.role.RoleManagerException;
33 import org.springframework.context.annotation.Scope;
34 import org.springframework.stereotype.Controller;
36 import javax.inject.Inject;
37 import java.io.IOException;
38 import java.util.List;
42 * DeleteManagedRepositoryAction
45 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="deleteManagedRepositoryAction" instantiation-strategy="per-lookup"
47 @Controller( "deleteManagedRepositoryAction" )
49 public class DeleteManagedRepositoryAction
50 extends AbstractManagedRepositoriesAction
53 private ManagedRepositoryConfiguration repository;
55 private ManagedRepositoryConfiguration stagingRepository;
57 private String repoid;
63 private RepositoryStatisticsManager repositoryStatisticsManager;
67 if ( StringUtils.isNotBlank( repoid ) )
69 this.repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid );
70 this.stagingRepository =
71 archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid + "-stage" );
75 public String confirmDelete()
77 if ( StringUtils.isBlank( repoid ) )
79 addActionError( "Unable to delete managed repository: repository id was blank." );
86 public String deleteEntry()
88 return deleteRepository( false );
91 public String deleteContents()
93 return deleteRepository( true );
96 private String deleteRepository( boolean deleteContents )
98 ManagedRepositoryConfiguration existingRepository = repository;
99 ManagedRepositoryConfiguration attachedStagingRepo = stagingRepository;
100 if ( existingRepository == null )
102 addActionError( "A repository with that id does not exist" );
108 RepositorySession repositorySession = repositorySessionFactory.createSession();
111 Configuration configuration = archivaConfiguration.getConfiguration();
112 if ( attachedStagingRepo != null )
114 cleanupRepositoryData( attachedStagingRepo, repositorySession );
115 removeRepository( repoid + "-stage", configuration );
116 triggerAuditEvent( repoid + "-stage", null, AuditEvent.DELETE_MANAGED_REPO );
119 cleanupRepositoryData( existingRepository, repositorySession );
120 removeRepository( repoid, configuration );
121 triggerAuditEvent( repoid, null, AuditEvent.DELETE_MANAGED_REPO );
122 result = saveConfiguration( configuration );
124 if ( result.equals( SUCCESS ) )
126 if ( deleteContents )
128 if ( attachedStagingRepo != null )
130 removeContents( attachedStagingRepo );
132 removeContents( existingRepository );
136 catch ( IOException e )
139 "Unable to delete repository, content may already be partially removed: " + e.getMessage() );
142 catch ( RoleManagerException e )
145 "Unable to delete repository, content may already be partially removed: " + e.getMessage() );
148 catch ( MetadataRepositoryException e )
151 "Unable to delete repository, content may already be partially removed: " + e.getMessage() );
156 repositorySession.close();
162 private void cleanupRepositoryData( ManagedRepositoryConfiguration cleanupRepository,
163 RepositorySession repositorySession )
164 throws RoleManagerException, MetadataRepositoryException
166 removeRepositoryRoles( cleanupRepository );
167 MetadataRepository metadataRepository = repositorySession.getRepository();
168 cleanupDatabase( metadataRepository, cleanupRepository.getId() );
169 repositoryStatisticsManager.deleteStatistics( metadataRepository, cleanupRepository.getId() );
170 // TODO: delete all content for a repository from the content API?
171 repositorySession.save();
173 List<ProxyConnectorConfiguration> proxyConnectors = getProxyConnectors();
174 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
176 if ( StringUtils.equals( proxyConnector.getSourceRepoId(), cleanupRepository.getId() ) )
178 archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector );
182 Map<String, List<String>> repoToGroupMap = archivaConfiguration.getConfiguration().getRepositoryToGroupMap();
183 if ( repoToGroupMap != null )
185 if ( repoToGroupMap.containsKey( cleanupRepository.getId() ) )
187 List<String> repoGroups = repoToGroupMap.get( cleanupRepository.getId() );
188 for ( String repoGroup : repoGroups )
190 archivaConfiguration.getConfiguration().findRepositoryGroupById( repoGroup ).removeRepository(
191 cleanupRepository.getId() );
197 private void cleanupDatabase( MetadataRepository metadataRepository, String repoId )
198 throws MetadataRepositoryException
200 metadataRepository.removeRepository( repoId );
203 public ManagedRepositoryConfiguration getRepository()
208 public void setRepository( ManagedRepositoryConfiguration repository )
210 this.repository = repository;
213 public String getRepoid()
218 public void setRepoid( String repoid )
220 this.repoid = repoid;
223 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
225 this.repositoryStatisticsManager = repositoryStatisticsManager;