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.xwork.Preparable;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.database.ArchivaDAO;
29 import org.apache.maven.archiva.database.ArchivaDatabaseException;
30 import org.apache.maven.archiva.database.Constraint;
31 import org.apache.maven.archiva.database.ObjectNotFoundException;
32 import org.apache.maven.archiva.database.constraints.ArtifactsByRepositoryConstraint;
33 import org.apache.maven.archiva.database.constraints.RepositoryContentStatisticsByRepositoryConstraint;
34 import org.apache.maven.archiva.model.ArchivaArtifact;
35 import org.apache.maven.archiva.model.ArchivaProjectModel;
36 import org.apache.maven.archiva.model.RepositoryContentStatistics;
38 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
40 import org.codehaus.plexus.redback.role.RoleManagerException;
42 import java.io.IOException;
43 import java.util.List;
46 * DeleteManagedRepositoryAction
48 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
50 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="deleteManagedRepositoryAction"
52 public class DeleteManagedRepositoryAction
53 extends AbstractManagedRepositoriesAction
56 private ManagedRepositoryConfiguration repository;
58 private String repoid;
61 * @plexus.requirement role-hint="jdo"
63 private ArchivaDAO archivaDAO;
67 if ( StringUtils.isNotBlank( repoid ) )
69 this.repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid );
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 if ( existingRepository == null )
99 addActionError( "A repository with that id does not exist" );
103 String result = SUCCESS;
107 Configuration configuration = archivaConfiguration.getConfiguration();
108 cleanupRepositoryData( existingRepository );
109 removeRepository( repoid, configuration );
110 result = saveConfiguration( configuration );
112 if ( result.equals( SUCCESS ) )
114 if ( deleteContents )
116 removeContents( existingRepository );
120 catch ( IOException e )
122 addActionError( "Unable to delete repository: " + e.getMessage() );
125 catch ( RoleManagerException e )
127 addActionError( "Unable to delete repository: " + e.getMessage() );
130 catch ( ArchivaDatabaseException e )
132 addActionError( "Unable to delete repositoy: " + e.getMessage() );
139 private void cleanupRepositoryData( ManagedRepositoryConfiguration cleanupRepository )
140 throws RoleManagerException, ArchivaDatabaseException
142 removeRepositoryRoles( cleanupRepository );
144 // TODO: [MRM-382] Remove index from artifacts of deleted managed repositories.
146 // [MRM-265] After removing a managed repository - Browse/Search still see it
147 cleanupDatabase( cleanupRepository.getId() );
148 cleanupScanStats( cleanupRepository.getId() );
150 // [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
151 List<ProxyConnectorConfiguration> proxyConnectors = getProxyConnectors();
152 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
154 if ( StringUtils.equals( proxyConnector.getSourceRepoId(), cleanupRepository.getId() ) )
156 archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector );
161 private void cleanupDatabase( String repoId )
162 throws ArchivaDatabaseException
164 Constraint constraint = new ArtifactsByRepositoryConstraint( repoId );
166 List<ArchivaArtifact> artifacts = archivaDAO.getArtifactDAO().queryArtifacts( constraint );
168 for ( ArchivaArtifact artifact : artifacts )
170 getLogger().info( "Removing artifact " + artifact + " from the database." );
173 archivaDAO.getArtifactDAO().deleteArtifact( artifact );
175 ArchivaProjectModel projectModel =
176 archivaDAO.getProjectModelDAO().getProjectModel( artifact.getGroupId(), artifact.getArtifactId(),
177 artifact.getVersion() );
179 archivaDAO.getProjectModelDAO().deleteProjectModel( projectModel );
181 catch ( ObjectNotFoundException oe )
183 getLogger().info( "Project model of artifact " + artifact + " does not exist in the database. " +
184 "Moving on to the next artifact." );
186 catch ( ArchivaDatabaseException ae )
188 getLogger().info( "Unable to delete artifact " + artifact + " from the database. " +
189 "Moving on to the next artifact." );
194 private void cleanupScanStats( String repoId )
195 throws ArchivaDatabaseException
197 List<RepositoryContentStatistics> results =
198 archivaDAO.getRepositoryContentStatisticsDAO().queryRepositoryContentStatistics(
199 new RepositoryContentStatisticsByRepositoryConstraint( repoId ) );
201 for ( RepositoryContentStatistics stats : results )
203 archivaDAO.getRepositoryContentStatisticsDAO().deleteRepositoryContentStatistics( stats );
207 public ManagedRepositoryConfiguration getRepository()
212 public void setRepository( ManagedRepositoryConfiguration repository )
214 this.repository = repository;
217 public String getRepoid()
222 public void setRepoid( String repoid )
224 this.repoid = repoid;
227 public void setArchivaDAO( ArchivaDAO archivaDAO )
229 this.archivaDAO = archivaDAO;
232 public ArchivaDAO getArchivaDAO()