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.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.ConfigurationNames;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
28 import org.apache.archiva.consumers.ConsumerException;
29 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.apache.archiva.metadata.repository.RepositorySession;
31 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
32 import org.apache.archiva.redback.components.registry.Registry;
33 import org.apache.archiva.repository.ManagedRepositoryContent;
34 import org.apache.archiva.repository.RepositoryContentFactory;
35 import org.apache.archiva.repository.RepositoryException;
36 import org.apache.archiva.repository.RepositoryNotFoundException;
37 import org.apache.archiva.repository.events.RepositoryListener;
38 import org.apache.archiva.repository.metadata.MetadataTools;
39 import org.apache.archiva.redback.components.registry.RegistryListener;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.stereotype.Service;
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import javax.inject.Named;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.Date;
50 import java.util.List;
53 * Consumer for removing old snapshots in the repository based on the criteria
54 * specified by the user.
56 @Service( "knownRepositoryContentConsumer#repository-purge" )
58 public class RepositoryPurgeConsumer
59 extends AbstractMonitoredConsumer
60 implements KnownRepositoryContentConsumer, RegistryListener
63 * default-value="repository-purge"
65 private String id = "repository-purge";
68 * default-value="Purge repository of old snapshots"
70 private String description = "Purge repository of old snapshots";
73 @Named( value = "archivaConfiguration#default" )
74 private ArchivaConfiguration configuration;
77 private ManagedRepositoryAdmin managedRepositoryAdmin;
80 @Named( value = "repositoryContentFactory#default" )
81 private RepositoryContentFactory repositoryContentFactory;
84 private MetadataTools metadataTools;
87 @Named( value = "fileTypes" )
88 private FileTypes filetypes;
90 private List<String> includes = new ArrayList<>();
92 private RepositoryPurge repoPurge;
94 private RepositoryPurge cleanUp;
96 private boolean deleteReleasedSnapshots;
102 @Autowired(required = false)
103 private List<RepositoryListener> listeners = Collections.emptyList();
106 private RepositorySessionFactory repositorySessionFactory;
108 private RepositorySession repositorySession;
111 public String getId()
117 public String getDescription()
119 return this.description;
123 public List<String> getExcludes()
125 return getDefaultArtifactExclusions();
129 public List<String> getIncludes()
131 return this.includes;
135 public void beginScan( ManagedRepository repository, Date whenGathered )
136 throws ConsumerException
138 ManagedRepositoryContent repositoryContent;
141 repositoryContent = repositoryContentFactory.getManagedRepositoryContent( repository.getId() );
143 catch ( RepositoryNotFoundException e )
145 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
147 catch ( RepositoryException e )
149 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
152 repositorySession = repositorySessionFactory.createSession();
154 if ( repository.getDaysOlder() != 0 )
156 repoPurge = new DaysOldRepositoryPurge( repositoryContent, repository.getDaysOlder(),
157 repository.getRetentionCount(), repositorySession, listeners );
162 new RetentionCountRepositoryPurge( repositoryContent, repository.getRetentionCount(), repositorySession,
166 cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, metadataTools, managedRepositoryAdmin,
167 repositoryContentFactory, repositorySession, listeners );
169 deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
173 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
174 throws ConsumerException
176 beginScan( repository, whenGathered );
180 public void processFile( String path )
181 throws ConsumerException
185 if ( deleteReleasedSnapshots )
187 cleanUp.process( path );
189 repoPurge.process( path );
191 catch ( RepositoryPurgeException rpe )
193 throw new ConsumerException( rpe.getMessage(), rpe );
198 public void processFile( String path, boolean executeOnEntireRepo )
205 public void completeScan()
207 repositorySession.close();
211 public void completeScan( boolean executeOnEntireRepo )
217 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
219 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
226 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
231 private void initIncludes()
233 includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
237 public void initialize()
239 configuration.addChangeListener( this );
245 public boolean isProcessUnmodified()
247 // we need to check all files for deletion, especially if not modified
251 public ArchivaConfiguration getConfiguration()
253 return configuration;
256 public void setConfiguration( ArchivaConfiguration configuration )
258 this.configuration = configuration;
261 public RepositoryContentFactory getRepositoryContentFactory()
263 return repositoryContentFactory;
266 public void setRepositoryContentFactory( RepositoryContentFactory repositoryContentFactory )
268 this.repositoryContentFactory = repositoryContentFactory;
271 public MetadataTools getMetadataTools()
273 return metadataTools;
276 public void setMetadataTools( MetadataTools metadataTools )
278 this.metadataTools = metadataTools;
281 public FileTypes getFiletypes()
286 public void setFiletypes( FileTypes filetypes )
288 this.filetypes = filetypes;
291 public RepositoryPurge getRepoPurge()
296 public void setRepoPurge( RepositoryPurge repoPurge )
298 this.repoPurge = repoPurge;
301 public RepositoryPurge getCleanUp()
306 public void setCleanUp( RepositoryPurge cleanUp )
308 this.cleanUp = cleanUp;
311 public boolean isDeleteReleasedSnapshots()
313 return deleteReleasedSnapshots;
316 public void setDeleteReleasedSnapshots( boolean deleteReleasedSnapshots )
318 this.deleteReleasedSnapshots = deleteReleasedSnapshots;
321 public RepositorySessionFactory getRepositorySessionFactory()
323 return repositorySessionFactory;
326 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
328 this.repositorySessionFactory = repositorySessionFactory;
331 public RepositorySession getRepositorySession()
333 return repositorySession;
336 public void setRepositorySession( RepositorySession repositorySession )
338 this.repositorySession = repositorySession;