1 package org.apache.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.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.ConfigurationNames;
24 import org.apache.archiva.configuration.FileTypes;
25 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
26 import org.apache.archiva.consumers.ConsumerException;
27 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.apache.archiva.metadata.repository.RepositorySession;
29 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
30 import org.apache.archiva.redback.components.registry.Registry;
31 import org.apache.archiva.redback.components.registry.RegistryListener;
32 import org.apache.archiva.repository.ManagedRepository;
33 import org.apache.archiva.repository.ManagedRepositoryContent;
34 import org.apache.archiva.repository.RepositoryContentFactory;
35 import org.apache.archiva.repository.RepositoryRegistry;
36 import org.apache.archiva.repository.events.RepositoryListener;
37 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
38 import org.apache.archiva.repository.metadata.MetadataTools;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.context.annotation.Scope;
41 import org.springframework.stereotype.Service;
43 import javax.annotation.PostConstruct;
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.Date;
49 import java.util.List;
52 * Consumer for removing old snapshots in the repository based on the criteria
53 * specified by the user.
55 @Service( "knownRepositoryContentConsumer#repository-purge" )
57 public class RepositoryPurgeConsumer
58 extends AbstractMonitoredConsumer
59 implements KnownRepositoryContentConsumer, RegistryListener
62 * default-value="repository-purge"
64 private String id = "repository-purge";
67 * default-value="Purge repository of old snapshots"
69 private String description = "Purge repository of old snapshots";
72 @Named( value = "archivaConfiguration#default" )
73 private ArchivaConfiguration configuration;
76 private RepositoryRegistry repositoryRegistry;
79 @Named( value = "repositoryContentFactory#default" )
80 private RepositoryContentFactory repositoryContentFactory;
83 private MetadataTools metadataTools;
86 @Named( value = "fileTypes" )
87 private FileTypes filetypes;
89 private List<String> includes = new ArrayList<>( );
91 private RepositoryPurge repoPurge;
93 private RepositoryPurge cleanUp;
95 private boolean deleteReleasedSnapshots;
101 @Autowired( required = false )
102 private List<RepositoryListener> listeners = Collections.emptyList( );
105 private RepositorySessionFactory repositorySessionFactory;
107 private RepositorySession repositorySession;
110 public String getId( )
116 public String getDescription( )
118 return this.description;
122 public List<String> getExcludes( )
124 return getDefaultArtifactExclusions( );
128 public List<String> getIncludes( )
130 return this.includes;
134 public void beginScan( ManagedRepository repository, Date whenGathered )
135 throws ConsumerException
137 ManagedRepositoryContent repositoryContent;
138 repositoryContent = repository.getContent();
142 repositorySession = repositorySessionFactory.createSession( );
144 catch ( org.apache.archiva.metadata.repository.MetadataRepositoryException e )
146 throw new ConsumerException( "Could not create session: " + e.getMessage( ), e );
149 if (repository.supportsFeature( ArtifactCleanupFeature.class ))
151 ArtifactCleanupFeature acf = repository.getFeature( ArtifactCleanupFeature.class ).get();
152 int retentionPeriodInDays = acf.getRetentionPeriod( ).getDays( );
153 int retentionCount = acf.getRetentionCount();
154 if ( retentionPeriodInDays != 0 )
156 repoPurge = new DaysOldRepositoryPurge( repositoryContent, retentionPeriodInDays,
157 retentionCount, repositorySession, listeners );
162 new RetentionCountRepositoryPurge( repositoryContent, retentionCount, repositorySession,
165 deleteReleasedSnapshots = acf.isDeleteReleasedSnapshots( );
167 throw new ConsumerException( "The repository does not support the ArtifactCleanup feature "+repository.getId() );
171 cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, metadataTools, repositoryRegistry,
172 repositorySession, listeners );
177 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
178 throws ConsumerException
180 beginScan( repository, whenGathered );
184 public void processFile( String path )
185 throws ConsumerException
189 if ( deleteReleasedSnapshots )
191 cleanUp.process( path );
193 repoPurge.process( path );
195 catch ( RepositoryPurgeException rpe )
197 throw new ConsumerException( rpe.getMessage( ), rpe );
202 public void processFile( String path, boolean executeOnEntireRepo )
209 public void completeScan( )
211 repositorySession.close( );
215 public void completeScan( boolean executeOnEntireRepo )
221 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
223 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
230 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
235 private void initIncludes( )
237 includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
241 public void initialize( )
243 configuration.addChangeListener( this );
249 public boolean isProcessUnmodified( )
251 // we need to check all files for deletion, especially if not modified
255 public ArchivaConfiguration getConfiguration( )
257 return configuration;
260 public void setConfiguration( ArchivaConfiguration configuration )
262 this.configuration = configuration;
265 public RepositoryContentFactory getRepositoryContentFactory( )
267 return repositoryContentFactory;
270 public void setRepositoryContentFactory( RepositoryContentFactory repositoryContentFactory )
272 this.repositoryContentFactory = repositoryContentFactory;
275 public MetadataTools getMetadataTools( )
277 return metadataTools;
280 public void setMetadataTools( MetadataTools metadataTools )
282 this.metadataTools = metadataTools;
285 public FileTypes getFiletypes( )
290 public void setFiletypes( FileTypes filetypes )
292 this.filetypes = filetypes;
295 public RepositoryPurge getRepoPurge( )
300 public void setRepoPurge( RepositoryPurge repoPurge )
302 this.repoPurge = repoPurge;
305 public RepositoryPurge getCleanUp( )
310 public void setCleanUp( RepositoryPurge cleanUp )
312 this.cleanUp = cleanUp;
315 public boolean isDeleteReleasedSnapshots( )
317 return deleteReleasedSnapshots;
320 public void setDeleteReleasedSnapshots( boolean deleteReleasedSnapshots )
322 this.deleteReleasedSnapshots = deleteReleasedSnapshots;
325 public RepositorySessionFactory getRepositorySessionFactory( )
327 return repositorySessionFactory;
330 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
332 this.repositorySessionFactory = repositorySessionFactory;