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.configuration.Configuration;
23 import org.apache.maven.archiva.model.ArchivaRepository;
24 import org.apache.maven.archiva.model.ArchivaArtifact;
25 import org.apache.maven.archiva.repository.layout.FilenameParts;
26 import org.apache.maven.archiva.repository.layout.RepositoryLayoutUtils;
27 import org.apache.maven.archiva.repository.layout.LayoutException;
28 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
29 import org.apache.maven.archiva.database.ArchivaDatabaseException;
30 import org.apache.maven.archiva.database.ArtifactDAO;
31 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
32 import org.apache.maven.archiva.indexer.RepositoryIndexException;
33 import org.apache.maven.archiva.indexer.filecontent.FileContentRecord;
34 import org.apache.commons.lang.StringUtils;
37 import java.io.FilenameFilter;
38 import java.util.ArrayList;
39 import java.util.List;
42 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
44 public abstract class AbstractRepositoryPurge
45 implements RepositoryPurge
47 private ArchivaRepository repository;
49 private BidirectionalRepositoryLayout layout;
51 private RepositoryContentIndex index;
53 private ArtifactDAO artifactDao;
56 * Get all files from the directory that matches the specified filename.
58 * @param dir the directory to be scanned
59 * @param filename the filename to be matched
62 protected File[] getFiles( File dir, String filename )
63 throws RepositoryPurgeException
65 FilenameFilter filter = new ArtifactFilenameFilter( filename );
67 if ( !dir.isDirectory() )
69 throw new RepositoryPurgeException( "Parent file " + dir.getPath() + " is not a directory." );
72 File[] files = dir.listFiles( filter );
77 public abstract void process( String path, Configuration configuration )
78 throws RepositoryPurgeException;
81 * Purge the repo. Update db and index of removed artifacts.
83 * @param artifactFiles
84 * @throws RepositoryIndexException
86 protected void purge( File[] artifactFiles )
87 throws RepositoryIndexException
89 List records = new ArrayList();
91 for ( int i = 0; i < artifactFiles.length; i++ )
93 artifactFiles[i].delete();
95 String[] artifactPathParts = artifactFiles[i].getAbsolutePath().split( getRepository().getUrl().getPath() );
96 String artifactPath = artifactPathParts[artifactPathParts.length - 1];
97 if ( !artifactPath.toUpperCase().endsWith( "SHA1" ) && !artifactPath.toUpperCase().endsWith( "MD5" ) )
99 updateDatabase( artifactPath );
102 FileContentRecord record = new FileContentRecord();
103 record.setRepositoryId( this.repository.getId() );
104 record.setFilename( artifactPath );
105 records.add( record );
108 //index.deleteRecords( records );
111 private void updateDatabase( String path )
115 ArchivaArtifact artifact = layout.toArtifact( path );
116 ArchivaArtifact queriedArtifact = artifactDao.getArtifact( artifact.getGroupId(), artifact.getArtifactId(),
117 artifact.getVersion(), artifact.getClassifier(),
118 artifact.getType() );
120 artifactDao.deleteArtifact( queriedArtifact );
122 catch ( ArchivaDatabaseException ae )
126 catch ( LayoutException le )
132 protected void updateMetadata()
138 * Get the artifactId, version, extension and classifier from the path parameter
142 * @throws LayoutException
144 protected FilenameParts getFilenameParts( String path )
145 throws LayoutException
147 String normalizedPath = StringUtils.replace( path, "\\", "/" );
148 String pathParts[] = StringUtils.split( normalizedPath, '/' );
150 FilenameParts parts = RepositoryLayoutUtils.splitFilename( pathParts[pathParts.length - 1], null );
155 public void setRepository( ArchivaRepository repository )
157 this.repository = repository;
160 public void setLayout( BidirectionalRepositoryLayout layout )
162 this.layout = layout;
165 public void setIndex( RepositoryContentIndex index )
170 public void setArtifactDao( ArtifactDAO artifactDao )
172 this.artifactDao = artifactDao;
175 protected ArchivaRepository getRepository()
180 protected BidirectionalRepositoryLayout getLayout()