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.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.ConfigurationNames;
24 import org.apache.maven.archiva.configuration.FileTypes;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.maven.archiva.consumers.ConsumerException;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.maven.archiva.database.ArchivaDAO;
30 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
31 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
32 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
33 import org.apache.maven.archiva.repository.RepositoryContentFactory;
34 import org.apache.maven.archiva.repository.RepositoryException;
35 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
36 import org.apache.maven.archiva.repository.metadata.MetadataTools;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
38 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
39 import org.codehaus.plexus.registry.Registry;
40 import org.codehaus.plexus.registry.RegistryListener;
42 import java.util.ArrayList;
43 import java.util.Date;
44 import java.util.HashMap;
45 import java.util.List;
49 * Consumer for removing old snapshots in the repository based on the criteria
50 * specified by the user.
52 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
55 * role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
56 * role-hint="repository-purge"
57 * instantiation-strategy="per-lookup"
59 public class RepositoryPurgeConsumer
60 extends AbstractMonitoredConsumer
61 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
64 * @plexus.configuration default-value="repository-purge"
69 * @plexus.configuration default-value="Purge repository of old snapshots"
71 private String description;
76 private ArchivaConfiguration configuration;
79 * @plexus.requirement role-hint="jdo"
81 private ArchivaDAO dao;
86 private RepositoryContentFactory repositoryFactory;
91 private MetadataTools metadataTools;
96 private FileTypes filetypes;
98 private List<String> includes = new ArrayList<String>();
100 private List<String> propertyNameTriggers = new ArrayList<String>();
102 private RepositoryPurge repoPurge;
104 private RepositoryPurge cleanUp;
106 private boolean deleteReleasedSnapshots;
109 * @plexus.requirement role-hint="lucene"
111 private RepositoryContentIndexFactory indexFactory;
113 public String getId()
118 public String getDescription()
120 return this.description;
123 public boolean isPermanent()
128 public List<String> getExcludes()
130 return getDefaultArtifactExclusions();
133 public List<String> getIncludes()
135 return this.includes;
138 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
139 throws ConsumerException
143 Map<String, RepositoryContentIndex> indices = new HashMap<String, RepositoryContentIndex>();
144 indices.put( "bytecode", indexFactory.createBytecodeIndex( repository ) );
145 indices.put( "hashcodes", indexFactory.createHashcodeIndex( repository ) );
146 indices.put( "filecontent", indexFactory.createFileContentIndex( repository ) );
148 ManagedRepositoryContent repositoryContent = repositoryFactory.getManagedRepositoryContent( repository
151 if ( repository.getDaysOlder() != 0 )
153 repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
154 .getDaysOlder(), repository.getRetentionCount(), indices );
158 repoPurge = new RetentionCountRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
159 .getRetentionCount(), indices );
162 cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, dao.getArtifactDAO(),
163 metadataTools, indices, configuration, repositoryFactory );
165 deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
167 catch ( RepositoryNotFoundException e )
169 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
171 catch ( RepositoryException e )
173 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
177 public void processFile( String path )
178 throws ConsumerException
182 if ( deleteReleasedSnapshots )
184 cleanUp.process( path );
187 repoPurge.process( path );
189 catch ( RepositoryPurgeException rpe )
191 throw new ConsumerException( rpe.getMessage(), rpe );
195 public void completeScan()
200 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
202 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
208 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
213 private void initIncludes()
217 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
220 public void initialize()
221 throws InitializationException
223 configuration.addChangeListener( this );
228 public boolean isProcessUnmodified()
230 // we need to check all files for deletion, especially if not modified
234 public void setRepositoryContentIndexFactory( RepositoryContentIndexFactory indexFactory )
236 this.indexFactory = indexFactory;