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.RepositoryContentIndex;
25 import org.apache.maven.archiva.indexer.RepositoryIndexException;
26 import org.apache.maven.archiva.indexer.bytecode.BytecodeRecord;
27 import org.apache.maven.archiva.indexer.filecontent.FileContentRecord;
28 import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord;
29 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
30 import org.apache.maven.archiva.model.ArchivaArtifact;
31 import org.apache.maven.archiva.model.ArtifactReference;
32 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
33 import org.apache.maven.archiva.repository.layout.LayoutException;
36 import java.io.FilenameFilter;
37 import java.util.ArrayList;
38 import java.util.List;
43 * Base class for all repository purge tasks.
45 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
47 public abstract class AbstractRepositoryPurge
48 implements RepositoryPurge
50 protected ManagedRepositoryContent repository;
52 protected ArtifactDAO artifactDao;
54 private Map<String, RepositoryContentIndex> indices;
56 public AbstractRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao,
57 Map<String, RepositoryContentIndex> indices )
59 this.repository = repository;
60 this.artifactDao = artifactDao;
61 this.indices = indices;
65 * Get all files from the directory that matches the specified filename.
67 * @param dir the directory to be scanned
68 * @param filename the filename to be matched
71 protected File[] getFiles( File dir, String filename )
73 FilenameFilter filter = new ArtifactFilenameFilter( filename );
75 File[] files = dir.listFiles( filter );
80 protected String toRelativePath( File artifactFile )
82 String artifactPath = artifactFile.getAbsolutePath();
83 if ( artifactPath.startsWith( repository.getRepoRoot() ) )
85 artifactPath = artifactPath.substring( repository.getRepoRoot().length() );
92 * Purge the repo. Update db and index of removed artifacts.
94 * @param artifactFiles
95 * @throws RepositoryIndexException
97 protected void purge( Set<ArtifactReference> references )
99 List<LuceneRepositoryContentRecord> fileContentRecords = new ArrayList<LuceneRepositoryContentRecord>();
100 List<LuceneRepositoryContentRecord> hashcodeRecords = new ArrayList<LuceneRepositoryContentRecord>();
101 List<LuceneRepositoryContentRecord> bytecodeRecords = new ArrayList<LuceneRepositoryContentRecord>();
103 for ( ArtifactReference reference : references )
105 File artifactFile = repository.toFile( reference );
107 ArchivaArtifact artifact =
108 new ArchivaArtifact( reference.getGroupId(), reference.getArtifactId(), reference.getVersion(),
109 reference.getClassifier(), reference.getType() );
111 FileContentRecord fileContentRecord = new FileContentRecord();
112 fileContentRecord.setFilename( repository.toPath( artifact ) );
113 fileContentRecords.add( fileContentRecord );
115 HashcodesRecord hashcodesRecord = new HashcodesRecord();
116 hashcodesRecord.setArtifact( artifact );
117 hashcodeRecords.add( hashcodesRecord );
119 BytecodeRecord bytecodeRecord = new BytecodeRecord();
120 bytecodeRecord.setArtifact( artifact );
121 bytecodeRecords.add( bytecodeRecord );
123 // TODO: this needs to be logged
124 artifactFile.delete();
125 purgeSupportFiles( artifactFile );
127 // intended to be swallowed
128 // continue updating the database for all artifacts
131 String artifactPath = toRelativePath( artifactFile );
132 updateDatabase( artifactPath );
134 catch ( ArchivaDatabaseException ae )
136 // TODO: determine logging to be used
138 catch ( LayoutException le )
146 updateIndices( fileContentRecords, hashcodeRecords, bytecodeRecords );
148 catch ( RepositoryIndexException e )
156 * This find support files for the artifactFile and deletes them.
159 * Support Files are things like ".sha1", ".md5", ".asc", etc.
162 * @param artifactFile the file to base off of.
164 private void purgeSupportFiles( File artifactFile )
166 File parentDir = artifactFile.getParentFile();
168 if ( !parentDir.exists() )
173 FilenameFilter filter = new ArtifactFilenameFilter( artifactFile.getName() );
175 File[] files = parentDir.listFiles( filter );
177 for ( File file : files )
179 if ( file.exists() && file.isFile() )
182 // TODO: log that it was deleted
187 private void updateDatabase( String path )
188 throws ArchivaDatabaseException, LayoutException
190 ArtifactReference artifact = repository.toArtifactReference( path );
191 ArchivaArtifact queriedArtifact =
192 artifactDao.getArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
193 artifact.getClassifier(), artifact.getType() );
195 artifactDao.deleteArtifact( queriedArtifact );
197 // TODO [MRM-37]: re-run the database consumers to clean up
200 private void updateIndices( List<LuceneRepositoryContentRecord> fileContentRecords,
201 List<LuceneRepositoryContentRecord> hashcodeRecords,
202 List<LuceneRepositoryContentRecord> bytecodeRecords )
203 throws RepositoryIndexException
205 RepositoryContentIndex index = indices.get( "filecontent" );
206 index.deleteRecords( fileContentRecords );
208 index = indices.get( "hashcodes" );
209 index.deleteRecords( hashcodeRecords );
211 index = indices.get( "bytecode" );
212 index.deleteRecords( bytecodeRecords );