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.repository.events.RepositoryListener;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ConfigurationNames;
25 import org.apache.maven.archiva.configuration.FileTypes;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
28 import org.apache.maven.archiva.consumers.ConsumerException;
29 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
31 import org.apache.maven.archiva.repository.RepositoryContentFactory;
32 import org.apache.maven.archiva.repository.RepositoryException;
33 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
34 import org.apache.maven.archiva.repository.metadata.MetadataTools;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
37 import org.codehaus.plexus.registry.Registry;
38 import org.codehaus.plexus.registry.RegistryListener;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.Date;
43 import java.util.List;
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.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 beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
161 throws ConsumerException
163 beginScan( repository, whenGathered );
166 public void processFile( String path )
167 throws ConsumerException
171 if ( deleteReleasedSnapshots )
173 cleanUp.process( path );
176 repoPurge.process( path );
178 catch ( RepositoryPurgeException rpe )
180 throw new ConsumerException( rpe.getMessage(), rpe );
184 public void processFile( String path, boolean executeOnEntireRepo )
190 public void completeScan()
195 public void completeScan( boolean executeOnEntireRepo )
200 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
202 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
208 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
213 private void initIncludes()
217 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
220 public void initialize()
221 throws InitializationException
223 configuration.addChangeListener( this );
228 public boolean isProcessUnmodified()
230 // we need to check all files for deletion, especially if not modified