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 * 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";
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;
110 private List<RepositoryListener> listeners = Collections.emptyList();
113 * TODO: this could be multiple implementations and needs to be configured.
117 private RepositorySessionFactory repositorySessionFactory;
119 private RepositorySession repositorySession;
121 public String getId()
126 public String getDescription()
128 return this.description;
131 public boolean isPermanent()
136 public List<String> getExcludes()
138 return getDefaultArtifactExclusions();
141 public List<String> getIncludes()
143 return this.includes;
146 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
147 throws ConsumerException
149 ManagedRepositoryContent repositoryContent;
152 repositoryContent = repositoryContentFactory.getManagedRepositoryContent( repository.getId() );
154 catch ( RepositoryNotFoundException e )
156 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
158 catch ( RepositoryException e )
160 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
163 repositorySession = repositorySessionFactory.createSession();
165 if ( repository.getDaysOlder() != 0 )
167 repoPurge = new DaysOldRepositoryPurge( repositoryContent, repository.getDaysOlder(),
168 repository.getRetentionCount(), repositorySession, listeners );
173 new RetentionCountRepositoryPurge( repositoryContent, repository.getRetentionCount(), repositorySession,
177 cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, metadataTools, configuration,
178 repositoryContentFactory, repositorySession, listeners );
180 deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
183 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
184 throws ConsumerException
186 beginScan( repository, whenGathered );
189 public void processFile( String path )
190 throws ConsumerException
194 if ( deleteReleasedSnapshots )
196 cleanUp.process( path );
199 repoPurge.process( path );
201 catch ( RepositoryPurgeException rpe )
203 throw new ConsumerException( rpe.getMessage(), rpe );
207 public void processFile( String path, boolean executeOnEntireRepo )
213 public void completeScan()
215 repositorySession.close();
218 public void completeScan( boolean executeOnEntireRepo )
223 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
225 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
231 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
236 private void initIncludes()
240 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
244 public void initialize()
247 // new ArrayList<RepositoryListener>( applicationContext.getBeansOfType( RepositoryListener.class ).values() );
248 configuration.addChangeListener( this );
253 public boolean isProcessUnmodified()
255 // we need to check all files for deletion, especially if not modified
259 public ArchivaConfiguration getConfiguration()
261 return configuration;
264 public void setConfiguration( ArchivaConfiguration configuration )
266 this.configuration = configuration;
269 public RepositoryContentFactory getRepositoryContentFactory()
271 return repositoryContentFactory;
274 public void setRepositoryContentFactory( RepositoryContentFactory repositoryContentFactory )
276 this.repositoryContentFactory = repositoryContentFactory;
279 public MetadataTools getMetadataTools()
281 return metadataTools;
284 public void setMetadataTools( MetadataTools metadataTools )
286 this.metadataTools = metadataTools;
289 public FileTypes getFiletypes()
294 public void setFiletypes( FileTypes filetypes )
296 this.filetypes = filetypes;
299 public RepositoryPurge getRepoPurge()
304 public void setRepoPurge( RepositoryPurge repoPurge )
306 this.repoPurge = repoPurge;
309 public RepositoryPurge getCleanUp()
314 public void setCleanUp( RepositoryPurge cleanUp )
316 this.cleanUp = cleanUp;
319 public boolean isDeleteReleasedSnapshots()
321 return deleteReleasedSnapshots;
324 public void setDeleteReleasedSnapshots( boolean deleteReleasedSnapshots )
326 this.deleteReleasedSnapshots = deleteReleasedSnapshots;
329 public RepositorySessionFactory getRepositorySessionFactory()
331 return repositorySessionFactory;
334 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
336 this.repositorySessionFactory = repositorySessionFactory;
339 public RepositorySession getRepositorySession()
341 return repositorySession;
344 public void setRepositorySession( RepositorySession repositorySession )
346 this.repositorySession = repositorySession;