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 * plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
56 * role-hint="repository-purge"
57 * instantiation-strategy="per-lookup"
59 @Service( "knownRepositoryContentConsumer#repository-purge" )
61 public class RepositoryPurgeConsumer
62 extends AbstractMonitoredConsumer
63 implements KnownRepositoryContentConsumer, RegistryListener
66 * plexus.configuration default-value="repository-purge"
68 private String id = "repository-purge";
71 * plexus.configuration default-value="Purge repository of old snapshots"
73 private String description = "Purge repository of old snapshots";
79 @Named( value = "archivaConfiguration#default" )
80 private ArchivaConfiguration configuration;
86 @Named(value = "repositoryContentFactory#default")
87 private RepositoryContentFactory repositoryContentFactory;
93 private MetadataTools metadataTools;
99 @Named(value = "fileTypes")
100 private FileTypes filetypes;
102 private List<String> includes = new ArrayList<String>();
104 private RepositoryPurge repoPurge;
106 private RepositoryPurge cleanUp;
108 private boolean deleteReleasedSnapshots;
111 //private ApplicationContext applicationContext;
114 * plexus.requirement role="org.apache.archiva.repository.events.RepositoryListener"
117 private List<RepositoryListener> listeners = Collections.emptyList();
120 * TODO: this could be multiple implementations and needs to be configured.
125 private RepositorySessionFactory repositorySessionFactory;
127 private RepositorySession repositorySession;
129 public String getId()
134 public String getDescription()
136 return this.description;
139 public boolean isPermanent()
144 public List<String> getExcludes()
146 return getDefaultArtifactExclusions();
149 public List<String> getIncludes()
151 return this.includes;
154 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
155 throws ConsumerException
157 ManagedRepositoryContent repositoryContent;
160 repositoryContent = repositoryContentFactory.getManagedRepositoryContent( repository.getId() );
162 catch ( RepositoryNotFoundException e )
164 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
166 catch ( RepositoryException e )
168 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
171 repositorySession = repositorySessionFactory.createSession();
173 if ( repository.getDaysOlder() != 0 )
175 repoPurge = new DaysOldRepositoryPurge( repositoryContent, repository.getDaysOlder(),
176 repository.getRetentionCount(), repositorySession, listeners );
181 new RetentionCountRepositoryPurge( repositoryContent, repository.getRetentionCount(), repositorySession,
185 cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, metadataTools, configuration,
186 repositoryContentFactory, repositorySession, listeners );
188 deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
191 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
192 throws ConsumerException
194 beginScan( repository, whenGathered );
197 public void processFile( String path )
198 throws ConsumerException
202 if ( deleteReleasedSnapshots )
204 cleanUp.process( path );
207 repoPurge.process( path );
209 catch ( RepositoryPurgeException rpe )
211 throw new ConsumerException( rpe.getMessage(), rpe );
215 public void processFile( String path, boolean executeOnEntireRepo )
221 public void completeScan()
223 repositorySession.close();
226 public void completeScan( boolean executeOnEntireRepo )
231 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
233 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
239 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
244 private void initIncludes()
248 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
252 public void initialize()
255 // new ArrayList<RepositoryListener>( applicationContext.getBeansOfType( RepositoryListener.class ).values() );
256 configuration.addChangeListener( this );
261 public boolean isProcessUnmodified()
263 // we need to check all files for deletion, especially if not modified
267 public ArchivaConfiguration getConfiguration()
269 return configuration;
272 public void setConfiguration( ArchivaConfiguration configuration )
274 this.configuration = configuration;
277 public RepositoryContentFactory getRepositoryContentFactory()
279 return repositoryContentFactory;
282 public void setRepositoryContentFactory( RepositoryContentFactory repositoryContentFactory )
284 this.repositoryContentFactory = repositoryContentFactory;
287 public MetadataTools getMetadataTools()
289 return metadataTools;
292 public void setMetadataTools( MetadataTools metadataTools )
294 this.metadataTools = metadataTools;
297 public FileTypes getFiletypes()
302 public void setFiletypes( FileTypes filetypes )
304 this.filetypes = filetypes;
307 public RepositoryPurge getRepoPurge()
312 public void setRepoPurge( RepositoryPurge repoPurge )
314 this.repoPurge = repoPurge;
317 public RepositoryPurge getCleanUp()
322 public void setCleanUp( RepositoryPurge cleanUp )
324 this.cleanUp = cleanUp;
327 public boolean isDeleteReleasedSnapshots()
329 return deleteReleasedSnapshots;
332 public void setDeleteReleasedSnapshots( boolean deleteReleasedSnapshots )
334 this.deleteReleasedSnapshots = deleteReleasedSnapshots;
337 public RepositorySessionFactory getRepositorySessionFactory()
339 return repositorySessionFactory;
342 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
344 this.repositorySessionFactory = repositorySessionFactory;
347 public RepositorySession getRepositorySession()
349 return repositorySession;
352 public void setRepositorySession( RepositorySession repositorySession )
354 this.repositorySession = repositorySession;