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.FileTypes;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
26 import org.apache.maven.archiva.consumers.ConsumerException;
27 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.apache.maven.archiva.database.ArchivaDAO;
29 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
30 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
31 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
32 import org.apache.maven.archiva.repository.RepositoryContentFactory;
33 import org.apache.maven.archiva.repository.RepositoryException;
34 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
35 import org.apache.maven.archiva.repository.metadata.MetadataTools;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
38 import org.codehaus.plexus.registry.Registry;
39 import org.codehaus.plexus.registry.RegistryListener;
41 import java.util.ArrayList;
42 import java.util.Date;
43 import java.util.HashMap;
44 import java.util.List;
48 * Consumer for removing old snapshots in the repository based on the criteria
49 * specified by the user.
51 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
54 * role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
55 * role-hint="repository-purge"
56 * instantiation-strategy="per-lookup"
58 public class RepositoryPurgeConsumer
59 extends AbstractMonitoredConsumer
60 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
63 * @plexus.configuration default-value="repository-purge"
68 * @plexus.configuration default-value="Purge repository of old snapshots"
70 private String description;
75 private ArchivaConfiguration configuration;
78 * @plexus.requirement role-hint="jdo"
80 private ArchivaDAO dao;
85 private RepositoryContentFactory repositoryFactory;
90 private MetadataTools metadataTools;
95 private FileTypes filetypes;
97 private List<String> includes = new ArrayList<String>();
99 private List<String> propertyNameTriggers = new ArrayList<String>();
101 private RepositoryPurge repoPurge;
103 private RepositoryPurge cleanUp;
105 private boolean deleteReleasedSnapshots;
108 * @plexus.requirement role-hint="lucene"
110 private RepositoryContentIndexFactory indexFactory;
112 public String getId()
117 public String getDescription()
119 return this.description;
122 public boolean isPermanent()
127 public List<String> getExcludes()
129 return getDefaultArtifactExclusions();
132 public List<String> getIncludes()
134 return this.includes;
137 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
138 throws ConsumerException
142 Map<String, RepositoryContentIndex> indices = new HashMap<String, RepositoryContentIndex>();
143 indices.put( "bytecode", indexFactory.createBytecodeIndex( repository ) );
144 indices.put( "hashcodes", indexFactory.createHashcodeIndex( repository ) );
145 indices.put( "filecontent", indexFactory.createFileContentIndex( repository ) );
147 ManagedRepositoryContent repositoryContent = repositoryFactory.getManagedRepositoryContent( repository
150 if ( repository.getDaysOlder() != 0 )
152 repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
153 .getDaysOlder(), repository.getRetentionCount(), indices );
157 repoPurge = new RetentionCountRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
158 .getRetentionCount(), indices );
161 cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, dao.getArtifactDAO(),
162 metadataTools, indices );
164 deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
166 catch ( RepositoryNotFoundException e )
168 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
170 catch ( RepositoryException e )
172 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
176 public void processFile( String path )
177 throws ConsumerException
181 if ( deleteReleasedSnapshots )
183 cleanUp.process( path );
186 repoPurge.process( path );
188 catch ( RepositoryPurgeException rpe )
190 throw new ConsumerException( rpe.getMessage(), rpe );
194 public void completeScan()
199 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
201 if ( propertyNameTriggers.contains( propertyName ) )
207 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
212 private void initIncludes()
216 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
219 public void initialize()
220 throws InitializationException
222 propertyNameTriggers = new ArrayList<String>();
223 propertyNameTriggers.add( "repositoryScanning" );
224 propertyNameTriggers.add( "fileTypes" );
225 propertyNameTriggers.add( "fileType" );
226 propertyNameTriggers.add( "patterns" );
227 propertyNameTriggers.add( "pattern" );
229 configuration.addChangeListener( this );
234 public boolean isProcessUnmodified()
236 // we need to check all files for deletion, especially if not modified
240 public void setRepositoryContentIndexFactory( RepositoryContentIndexFactory indexFactory )
242 this.indexFactory = indexFactory;