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.archiva.metadata.repository.RepositorySession;
23 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
24 import org.apache.archiva.repository.events.RepositoryListener;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ConfigurationNames;
27 import org.apache.maven.archiva.configuration.FileTypes;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
30 import org.apache.maven.archiva.consumers.ConsumerException;
31 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
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.registry.Registry;
38 import org.codehaus.plexus.registry.RegistryListener;
39 import org.springframework.context.ApplicationContext;
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 * plexus.configuration default-value="repository-purge"
64 private String id = "repository-purge";
67 * plexus.configuration default-value="Purge repository of old snapshots"
69 private String description = "Purge repository of old snapshots";
75 @Named( value = "archivaConfiguration#default" )
76 private ArchivaConfiguration configuration;
82 @Named(value = "repositoryContentFactory#default")
83 private RepositoryContentFactory repositoryContentFactory;
89 private MetadataTools metadataTools;
95 @Named(value = "fileTypes")
96 private FileTypes filetypes;
98 private List<String> includes = new ArrayList<String>();
100 private RepositoryPurge repoPurge;
102 private RepositoryPurge cleanUp;
104 private boolean deleteReleasedSnapshots;
107 //private ApplicationContext applicationContext;
110 * plexus.requirement role="org.apache.archiva.repository.events.RepositoryListener"
113 private List<RepositoryListener> listeners = Collections.emptyList();
116 * TODO: this could be multiple implementations and needs to be configured.
121 private RepositorySessionFactory repositorySessionFactory;
123 private RepositorySession repositorySession;
125 public String getId()
130 public String getDescription()
132 return this.description;
135 public boolean isPermanent()
140 public List<String> getExcludes()
142 return getDefaultArtifactExclusions();
145 public List<String> getIncludes()
147 return this.includes;
150 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
151 throws ConsumerException
153 ManagedRepositoryContent repositoryContent;
156 repositoryContent = repositoryContentFactory.getManagedRepositoryContent( repository.getId() );
158 catch ( RepositoryNotFoundException e )
160 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
162 catch ( RepositoryException e )
164 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
167 repositorySession = repositorySessionFactory.createSession();
169 if ( repository.getDaysOlder() != 0 )
171 repoPurge = new DaysOldRepositoryPurge( repositoryContent, repository.getDaysOlder(),
172 repository.getRetentionCount(), repositorySession, listeners );
177 new RetentionCountRepositoryPurge( repositoryContent, repository.getRetentionCount(), repositorySession,
181 cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, metadataTools, configuration,
182 repositoryContentFactory, repositorySession, listeners );
184 deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
187 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
188 throws ConsumerException
190 beginScan( repository, whenGathered );
193 public void processFile( String path )
194 throws ConsumerException
198 if ( deleteReleasedSnapshots )
200 cleanUp.process( path );
203 repoPurge.process( path );
205 catch ( RepositoryPurgeException rpe )
207 throw new ConsumerException( rpe.getMessage(), rpe );
211 public void processFile( String path, boolean executeOnEntireRepo )
217 public void completeScan()
219 repositorySession.close();
222 public void completeScan( boolean executeOnEntireRepo )
227 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
229 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
235 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
240 private void initIncludes()
244 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
248 public void initialize()
251 // new ArrayList<RepositoryListener>( applicationContext.getBeansOfType( RepositoryListener.class ).values() );
252 configuration.addChangeListener( this );
257 public boolean isProcessUnmodified()
259 // we need to check all files for deletion, especially if not modified
263 public ArchivaConfiguration getConfiguration()
265 return configuration;
268 public void setConfiguration( ArchivaConfiguration configuration )
270 this.configuration = configuration;
273 public RepositoryContentFactory getRepositoryContentFactory()
275 return repositoryContentFactory;
278 public void setRepositoryContentFactory( RepositoryContentFactory repositoryContentFactory )
280 this.repositoryContentFactory = repositoryContentFactory;
283 public MetadataTools getMetadataTools()
285 return metadataTools;
288 public void setMetadataTools( MetadataTools metadataTools )
290 this.metadataTools = metadataTools;
293 public FileTypes getFiletypes()
298 public void setFiletypes( FileTypes filetypes )
300 this.filetypes = filetypes;
303 public RepositoryPurge getRepoPurge()
308 public void setRepoPurge( RepositoryPurge repoPurge )
310 this.repoPurge = repoPurge;
313 public RepositoryPurge getCleanUp()
318 public void setCleanUp( RepositoryPurge cleanUp )
320 this.cleanUp = cleanUp;
323 public boolean isDeleteReleasedSnapshots()
325 return deleteReleasedSnapshots;
328 public void setDeleteReleasedSnapshots( boolean deleteReleasedSnapshots )
330 this.deleteReleasedSnapshots = deleteReleasedSnapshots;
333 public RepositorySessionFactory getRepositorySessionFactory()
335 return repositorySessionFactory;
338 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
340 this.repositorySessionFactory = repositorySessionFactory;
343 public RepositorySession getRepositorySession()
345 return repositorySession;
348 public void setRepositorySession( RepositorySession repositorySession )
350 this.repositorySession = repositorySession;