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.Date;
24 import java.util.List;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.ConfigurationNames;
28 import org.apache.maven.archiva.configuration.FileTypes;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
31 import org.apache.maven.archiva.consumers.ConsumerException;
32 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
33 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
34 import org.apache.maven.archiva.repository.RepositoryContentFactory;
35 import org.apache.maven.archiva.repository.RepositoryException;
36 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
37 import org.apache.maven.archiva.repository.events.RepositoryListener;
38 import org.apache.maven.archiva.repository.metadata.MetadataTools;
39 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
41 import org.codehaus.plexus.registry.Registry;
42 import org.codehaus.plexus.registry.RegistryListener;
44 import edu.emory.mathcs.backport.java.util.Collections;
47 * Consumer for removing old snapshots in the repository based on the criteria
48 * specified by the user.
52 * role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
53 * role-hint="repository-purge"
54 * instantiation-strategy="per-lookup"
56 public class RepositoryPurgeConsumer
57 extends AbstractMonitoredConsumer
58 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
61 * @plexus.configuration default-value="repository-purge"
66 * @plexus.configuration default-value="Purge repository of old snapshots"
68 private String description;
73 private ArchivaConfiguration configuration;
78 private RepositoryContentFactory repositoryFactory;
83 private MetadataTools metadataTools;
88 private FileTypes filetypes;
90 private List<String> includes = new ArrayList<String>();
92 private RepositoryPurge repoPurge;
94 private RepositoryPurge cleanUp;
96 private boolean deleteReleasedSnapshots;
98 /** @plexus.requirement role="org.apache.maven.archiva.repository.events.RepositoryListener" */
99 private List<RepositoryListener> listeners = Collections.emptyList();
101 public String getId()
106 public String getDescription()
108 return this.description;
111 public boolean isPermanent()
116 public List<String> getExcludes()
118 return getDefaultArtifactExclusions();
121 public List<String> getIncludes()
123 return this.includes;
126 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
127 throws ConsumerException
131 ManagedRepositoryContent repositoryContent = repositoryFactory.getManagedRepositoryContent( repository
134 if ( repository.getDaysOlder() != 0 )
136 repoPurge = new DaysOldRepositoryPurge( repositoryContent, repository.getDaysOlder(),
137 repository.getRetentionCount(), listeners );
141 repoPurge = new RetentionCountRepositoryPurge( repositoryContent, repository.getRetentionCount(),
146 new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, metadataTools, configuration,
147 repositoryFactory, listeners );
149 deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
151 catch ( RepositoryNotFoundException e )
153 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
155 catch ( RepositoryException e )
157 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
161 public void processFile( String path )
162 throws ConsumerException
166 if ( deleteReleasedSnapshots )
168 cleanUp.process( path );
171 repoPurge.process( path );
173 catch ( RepositoryPurgeException rpe )
175 throw new ConsumerException( rpe.getMessage(), rpe );
179 public void completeScan()
184 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
186 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
192 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
197 private void initIncludes()
201 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
204 public void initialize()
205 throws InitializationException
207 configuration.addChangeListener( this );
212 public boolean isProcessUnmodified()
214 // we need to check all files for deletion, especially if not modified