1 package org.apache.maven.archiva.consumers.core.repository;
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 org.apache.archiva.audit.AuditEvent;
23 import org.apache.archiva.repository.events.RepositoryListener;
24 import org.apache.maven.archiva.model.ArtifactReference;
25 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 import java.io.FilenameFilter;
31 import java.util.List;
35 * Base class for all repository purge tasks.
37 public abstract class AbstractRepositoryPurge
38 implements RepositoryPurge
40 protected Logger log = LoggerFactory.getLogger( AbstractRepositoryPurge.class );
42 protected final ManagedRepositoryContent repository;
44 protected final List<RepositoryListener> listeners;
46 private Logger logger = LoggerFactory.getLogger( "org.apache.archiva.AuditLog" );
48 private static final char DELIM = ' ';
50 public AbstractRepositoryPurge( ManagedRepositoryContent repository, List<RepositoryListener> listeners )
52 this.repository = repository;
53 this.listeners = listeners;
57 * Purge the repo. Update db and index of removed artifacts.
61 protected void purge( Set<ArtifactReference> references )
63 if ( references != null && !references.isEmpty() )
65 for ( ArtifactReference reference : references )
67 File artifactFile = repository.toFile( reference );
69 // FIXME: looks incomplete, might not delete related metadata?
70 for ( RepositoryListener listener : listeners )
72 listener.deleteArtifact( repository.getId(), reference.getGroupId(), reference.getArtifactId(),
73 reference.getVersion(), artifactFile.getName() );
76 // TODO: this needs to be logged
77 artifactFile.delete();
78 triggerAuditEvent( repository.getRepository().getId(), ArtifactReference.toKey( reference ),
79 AuditEvent.PURGE_ARTIFACT );
80 purgeSupportFiles( artifactFile );
87 * This find support files for the artifactFile and deletes them.
90 * Support Files are things like ".sha1", ".md5", ".asc", etc.
93 * @param artifactFile the file to base off of.
95 private void purgeSupportFiles( File artifactFile )
97 File parentDir = artifactFile.getParentFile();
99 if ( !parentDir.exists() )
104 FilenameFilter filter = new ArtifactFilenameFilter( artifactFile.getName() );
106 File[] files = parentDir.listFiles( filter );
108 for ( File file : files )
110 if ( file.exists() && file.isFile() )
112 String fileName = file.getName();
114 // TODO: log that it was deleted
115 triggerAuditEvent( repository.getRepository().getId(), fileName, AuditEvent.PURGE_FILE );
120 private void triggerAuditEvent( String repoId, String resource, String action )
123 repoId + DELIM + "<system-purge>" + DELIM + "<system>" + DELIM + '\"' + resource + '\"' + DELIM + '\"' +