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.maven.archiva.database.ArchivaDatabaseException;
23 import org.apache.maven.archiva.database.ArtifactDAO;
24 import org.apache.maven.archiva.indexer.RepositoryIndexException;
25 import org.apache.maven.archiva.model.ArchivaArtifact;
26 import org.apache.maven.archiva.model.ArtifactReference;
27 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
28 import org.apache.maven.archiva.repository.layout.LayoutException;
31 import java.io.FilenameFilter;
35 * Base class for all repository purge tasks.
37 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
39 public abstract class AbstractRepositoryPurge
40 implements RepositoryPurge
42 protected ManagedRepositoryContent repository;
44 protected ArtifactDAO artifactDao;
46 public AbstractRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao )
48 this.repository = repository;
49 this.artifactDao = artifactDao;
53 * Get all files from the directory that matches the specified filename.
55 * @param dir the directory to be scanned
56 * @param filename the filename to be matched
59 protected File[] getFiles( File dir, String filename )
61 FilenameFilter filter = new ArtifactFilenameFilter( filename );
63 File[] files = dir.listFiles( filter );
68 protected String toRelativePath( File artifactFile )
70 String artifactPath = artifactFile.getAbsolutePath();
71 if ( artifactPath.startsWith( repository.getRepoRoot() ) )
73 artifactPath = artifactPath.substring( repository.getRepoRoot().length() );
80 * Purge the repo. Update db and index of removed artifacts.
82 * @param artifactFiles
83 * @throws RepositoryIndexException
85 protected void purge( Set<ArtifactReference> references )
87 for ( ArtifactReference reference : references )
89 File artifactFile = repository.toFile( reference );
91 artifactFile.delete();
92 purgeSupportFiles( artifactFile );
94 // intended to be swallowed
95 // continue updating the database for all artifacts
98 String artifactPath = toRelativePath( artifactFile );
99 updateDatabase( artifactPath );
101 catch ( ArchivaDatabaseException ae )
103 // TODO: determine logging to be used
105 catch ( LayoutException le )
114 * This find support files for the artifactFile and deletes them.
118 * Support Files are things like ".sha1", ".md5", ".asc", etc.
121 * @param artifactFile the file to base off of.
123 private void purgeSupportFiles( File artifactFile )
125 File parentDir = artifactFile.getParentFile();
127 if ( !parentDir.exists() )
132 FilenameFilter filter = new ArtifactFilenameFilter( artifactFile.getName() );
134 File[] files = parentDir.listFiles( filter );
136 for ( File file : files )
138 if ( file.exists() && file.isFile() )
141 // TODO: log that it was deleted?
146 private void updateDatabase( String path )
147 throws ArchivaDatabaseException, LayoutException
149 ArtifactReference artifact = repository.toArtifactReference( path );
150 ArchivaArtifact queriedArtifact = artifactDao.getArtifact( artifact.getGroupId(), artifact.getArtifactId(),
151 artifact.getVersion(), artifact.getClassifier(),
152 artifact.getType() );
154 artifactDao.deleteArtifact( queriedArtifact );
156 // TODO [MRM-37]: re-run the database consumers to clean up