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.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.FileTypes;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
26 import org.apache.maven.archiva.consumers.ConsumerException;
27 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.apache.maven.archiva.database.ArchivaDAO;
29 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
30 import org.apache.maven.archiva.repository.RepositoryContentFactory;
31 import org.apache.maven.archiva.repository.RepositoryException;
32 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
33 import org.apache.maven.archiva.repository.metadata.MetadataTools;
34 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
36 import org.codehaus.plexus.registry.Registry;
37 import org.codehaus.plexus.registry.RegistryListener;
39 import java.util.ArrayList;
40 import java.util.List;
43 * Consumer for removing old snapshots in the repository based on the criteria
44 * specified by the user.
46 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
49 * role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
50 * role-hint="repository-purge"
51 * instantiation-strategy="per-lookup
53 public class RepositoryPurgeConsumer
54 extends AbstractMonitoredConsumer
55 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
58 * @plexus.configuration default-value="repository-purge"
63 * @plexus.configuration default-value="Purge repository of old snapshots"
65 private String description;
70 private ArchivaConfiguration configuration;
73 * @plexus.requirement role-hint="jdo"
75 private ArchivaDAO dao;
80 private RepositoryContentFactory repositoryFactory;
85 private MetadataTools metadataTools;
90 private FileTypes filetypes;
92 private List<String> includes = new ArrayList<String>();
94 private List<String> propertyNameTriggers = new ArrayList<String>();
96 private RepositoryPurge repoPurge;
98 private RepositoryPurge cleanUp;
100 private boolean deleteReleasedSnapshots;
102 public String getId()
107 public String getDescription()
109 return this.description;
112 public boolean isPermanent()
117 public List<String> getExcludes()
122 public List<String> getIncludes()
124 return this.includes;
127 public void beginScan( ManagedRepositoryConfiguration repository )
128 throws ConsumerException
132 ManagedRepositoryContent repositoryContent = repositoryFactory.getManagedRepositoryContent( repository
135 if ( repository.getDaysOlder() != 0 )
137 repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
142 repoPurge = new RetentionCountRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
143 .getRetentionCount() );
146 cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, dao.getArtifactDAO(),
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 ( propertyNameTriggers.contains( 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 propertyNameTriggers = new ArrayList<String>();
208 propertyNameTriggers.add( "repositoryScanning" );
209 propertyNameTriggers.add( "fileTypes" );
210 propertyNameTriggers.add( "fileType" );
211 propertyNameTriggers.add( "patterns" );
212 propertyNameTriggers.add( "pattern" );
214 configuration.addChangeListener( this );
219 public boolean isProcessUnmodified()
221 // we need to check all files for deletion, especially if not modified