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 java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.List;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.ConfigurationNames;
29 import org.apache.maven.archiva.configuration.FileTypes;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
32 import org.apache.maven.archiva.consumers.ConsumerException;
33 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
34 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
35 import org.apache.maven.archiva.repository.RepositoryContentFactory;
36 import org.apache.maven.archiva.repository.RepositoryException;
37 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
38 import org.apache.maven.archiva.repository.events.RepositoryListener;
39 import org.apache.maven.archiva.repository.metadata.MetadataTools;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
41 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
42 import org.codehaus.plexus.registry.Registry;
43 import org.codehaus.plexus.registry.RegistryListener;
46 * Consumer for removing old snapshots in the repository based on the criteria
47 * specified by the user.
51 * role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
52 * role-hint="repository-purge"
53 * instantiation-strategy="per-lookup"
55 public class RepositoryPurgeConsumer
56 extends AbstractMonitoredConsumer
57 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
60 * @plexus.configuration default-value="repository-purge"
65 * @plexus.configuration default-value="Purge repository of old snapshots"
67 private String description;
72 private ArchivaConfiguration configuration;
77 private RepositoryContentFactory repositoryFactory;
82 private MetadataTools metadataTools;
87 private FileTypes filetypes;
89 private List<String> includes = new ArrayList<String>();
91 private RepositoryPurge repoPurge;
93 private RepositoryPurge cleanUp;
95 private boolean deleteReleasedSnapshots;
97 /** @plexus.requirement role="org.apache.maven.archiva.repository.events.RepositoryListener" */
98 private List<RepositoryListener> listeners = Collections.emptyList();
100 public String getId()
105 public String getDescription()
107 return this.description;
110 public boolean isPermanent()
115 public List<String> getExcludes()
117 return getDefaultArtifactExclusions();
120 public List<String> getIncludes()
122 return this.includes;
125 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
126 throws ConsumerException
130 ManagedRepositoryContent repositoryContent = repositoryFactory.getManagedRepositoryContent( repository
133 if ( repository.getDaysOlder() != 0 )
135 repoPurge = new DaysOldRepositoryPurge( repositoryContent, repository.getDaysOlder(),
136 repository.getRetentionCount(), listeners );
140 repoPurge = new RetentionCountRepositoryPurge( repositoryContent, repository.getRetentionCount(),
145 new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, metadataTools, configuration,
146 repositoryFactory, listeners );
148 deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
150 catch ( RepositoryNotFoundException e )
152 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
154 catch ( RepositoryException e )
156 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
160 public void processFile( String path )
161 throws ConsumerException
165 if ( deleteReleasedSnapshots )
167 cleanUp.process( path );
170 repoPurge.process( path );
172 catch ( RepositoryPurgeException rpe )
174 throw new ConsumerException( rpe.getMessage(), rpe );
178 public void completeScan()
183 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
185 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
191 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
196 private void initIncludes()
200 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
203 public void initialize()
204 throws InitializationException
206 configuration.addChangeListener( this );
211 public boolean isProcessUnmodified()
213 // we need to check all files for deletion, especially if not modified