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;
45 import java.util.Properties;
48 * DeleteManagedRepositoryAction
50 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
52 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="deleteManagedRepositoryAction"
54 public class DeleteManagedRepositoryAction
55 extends AbstractManagedRepositoriesAction
58 private ManagedRepositoryConfiguration repository;
60 private String repoid;
63 * @plexus.requirement role-hint="jdo"
65 private ArchivaDAO archivaDAO;
69 if ( StringUtils.isNotBlank( repoid ) )
71 this.repository = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoid );
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 if ( existingRepository == null )
101 addActionError( "A repository with that id does not exist" );
107 // [MRM-789] Archiva may delete your app server installation
108 Properties props = System.getProperties();
109 for( Object value : props.values() )
111 if( StringUtils.equalsIgnoreCase( ( (String) value ).trim(), existingRepository.getLocation().trim() ) )
113 addActionError( "Unable to delete repository. The location is being referenced in the system properties." );
119 String result = SUCCESS;
123 Configuration configuration = archivaConfiguration.getConfiguration();
124 cleanupRepositoryData( existingRepository );
125 removeRepository( repoid, configuration );
126 result = saveConfiguration( configuration );
128 if ( result.equals( SUCCESS ) )
130 if ( deleteContents )
132 removeContents( existingRepository );
136 catch ( IOException e )
138 addActionError( "Unable to delete repository: " + e.getMessage() );
141 catch ( RoleManagerException e )
143 addActionError( "Unable to delete repository: " + e.getMessage() );
146 catch ( ArchivaDatabaseException e )
148 addActionError( "Unable to delete repositoy: " + e.getMessage() );
155 private void cleanupRepositoryData( ManagedRepositoryConfiguration cleanupRepository )
156 throws RoleManagerException, ArchivaDatabaseException
158 removeRepositoryRoles( cleanupRepository );
159 cleanupDatabase( cleanupRepository.getId() );
160 cleanupScanStats( cleanupRepository.getId() );
162 List<ProxyConnectorConfiguration> proxyConnectors = getProxyConnectors();
163 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
165 if ( StringUtils.equals( proxyConnector.getSourceRepoId(), cleanupRepository.getId() ) )
167 archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector );
171 Map<String, List<String>> repoToGroupMap = archivaConfiguration.getConfiguration().getRepositoryToGroupMap();
172 if( repoToGroupMap != null )
174 if( repoToGroupMap.containsKey( cleanupRepository.getId() ) )
176 List<String> repoGroups = repoToGroupMap.get( cleanupRepository.getId() );
177 for( String repoGroup : repoGroups )
179 archivaConfiguration.getConfiguration().findRepositoryGroupById( repoGroup ).removeRepository( cleanupRepository.getId() );
185 private void cleanupDatabase( String repoId )
186 throws ArchivaDatabaseException
188 Constraint constraint = new ArtifactsByRepositoryConstraint( repoId );
190 List<ArchivaArtifact> artifacts = archivaDAO.getArtifactDAO().queryArtifacts( constraint );
192 for ( ArchivaArtifact artifact : artifacts )
194 getLogger().info( "Removing artifact " + artifact + " from the database." );
197 archivaDAO.getArtifactDAO().deleteArtifact( artifact );
199 ArchivaProjectModel projectModel =
200 archivaDAO.getProjectModelDAO().getProjectModel( artifact.getGroupId(), artifact.getArtifactId(),
201 artifact.getVersion() );
203 archivaDAO.getProjectModelDAO().deleteProjectModel( projectModel );
205 catch ( ObjectNotFoundException oe )
207 getLogger().info( "Project model of artifact " + artifact + " does not exist in the database. " +
208 "Moving on to the next artifact." );
210 catch ( ArchivaDatabaseException ae )
212 getLogger().info( "Unable to delete artifact " + artifact + " from the database. " +
213 "Moving on to the next artifact." );
218 private void cleanupScanStats( String repoId )
219 throws ArchivaDatabaseException
221 List<RepositoryContentStatistics> results =
222 archivaDAO.getRepositoryContentStatisticsDAO().queryRepositoryContentStatistics(
223 new RepositoryContentStatisticsByRepositoryConstraint( repoId ) );
225 for ( RepositoryContentStatistics stats : results )
227 archivaDAO.getRepositoryContentStatisticsDAO().deleteRepositoryContentStatistics( stats );
231 public ManagedRepositoryConfiguration getRepository()
236 public void setRepository( ManagedRepositoryConfiguration repository )
238 this.repository = repository;
241 public String getRepoid()
246 public void setRepoid( String repoid )
248 this.repoid = repoid;
251 public void setArchivaDAO( ArchivaDAO archivaDAO )
253 this.archivaDAO = archivaDAO;
256 public ArchivaDAO getArchivaDAO()