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.
38 public abstract class AbstractRepositoryPurge
39 implements RepositoryPurge
41 protected Logger log = LoggerFactory.getLogger( AbstractRepositoryPurge.class );
43 protected final ManagedRepositoryContent repository;
45 protected final List<RepositoryListener> listeners;
47 private Logger logger = LoggerFactory.getLogger( "org.apache.archiva.AuditLog" );
49 private static final char DELIM = ' ';
51 public AbstractRepositoryPurge( ManagedRepositoryContent repository, List<RepositoryListener> listeners )
53 this.repository = repository;
54 this.listeners = listeners;
58 * Purge the repo. Update db and index of removed artifacts.
62 protected void purge( Set<ArtifactReference> references )
64 if( references != null && !references.isEmpty() )
66 for ( ArtifactReference reference : references )
68 File artifactFile = repository.toFile( reference );
70 // TODO: looks incomplete, might not delete related metadata?
71 for ( RepositoryListener listener : listeners )
73 listener.deleteArtifact( repository.getId(), reference.getGroupId(), reference.getArtifactId(),
74 reference.getVersion(), artifactFile.getName() );
77 // TODO: this needs to be logged
78 artifactFile.delete();
79 triggerAuditEvent( repository.getRepository().getId(), ArtifactReference.toKey( reference ), 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 )
122 String msg = repoId + DELIM + "<system-purge>" + DELIM + "<system>" + DELIM + '\"' + resource + '\"' +
123 DELIM + '\"' + action + '\"';