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.indexer.RepositoryContentIndex;
30 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
31 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
32 import org.apache.maven.archiva.repository.RepositoryContentFactory;
33 import org.apache.maven.archiva.repository.RepositoryException;
34 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
35 import org.apache.maven.archiva.repository.metadata.MetadataTools;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
38 import org.codehaus.plexus.registry.Registry;
39 import org.codehaus.plexus.registry.RegistryListener;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
47 * Consumer for removing old snapshots in the repository based on the criteria
48 * specified by the user.
50 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
53 * role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
54 * role-hint="repository-purge"
55 * instantiation-strategy="per-lookup
57 public class RepositoryPurgeConsumer
58 extends AbstractMonitoredConsumer
59 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
62 * @plexus.configuration default-value="repository-purge"
67 * @plexus.configuration default-value="Purge repository of old snapshots"
69 private String description;
74 private ArchivaConfiguration configuration;
77 * @plexus.requirement role-hint="jdo"
79 private ArchivaDAO dao;
84 private RepositoryContentFactory repositoryFactory;
89 private MetadataTools metadataTools;
94 private FileTypes filetypes;
96 private List<String> includes = new ArrayList<String>();
98 private List<String> propertyNameTriggers = new ArrayList<String>();
100 private RepositoryPurge repoPurge;
102 private RepositoryPurge cleanUp;
104 private boolean deleteReleasedSnapshots;
107 * @plexus.requirement role-hint="lucene"
109 private RepositoryContentIndexFactory indexFactory;
111 public String getId()
116 public String getDescription()
118 return this.description;
121 public boolean isPermanent()
126 public List<String> getExcludes()
131 public List<String> getIncludes()
133 return this.includes;
136 public void beginScan( ManagedRepositoryConfiguration repository )
137 throws ConsumerException
141 Map<String, RepositoryContentIndex> indices = new HashMap<String, RepositoryContentIndex>();
142 indices.put( "bytecode", indexFactory.createBytecodeIndex( repository ) );
143 indices.put( "hashcodes", indexFactory.createHashcodeIndex( repository ) );
144 indices.put( "filecontent", indexFactory.createFileContentIndex( repository ) );
146 ManagedRepositoryContent repositoryContent = repositoryFactory.getManagedRepositoryContent( repository
149 if ( repository.getDaysOlder() != 0 )
151 repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
152 .getDaysOlder(), repository.getRetentionCount(), indices );
156 repoPurge = new RetentionCountRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
157 .getRetentionCount(), indices );
160 cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, dao.getArtifactDAO(),
161 metadataTools, indices );
163 deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
165 catch ( RepositoryNotFoundException e )
167 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
169 catch ( RepositoryException e )
171 throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
175 public void processFile( String path )
176 throws ConsumerException
180 if ( deleteReleasedSnapshots )
182 cleanUp.process( path );
185 repoPurge.process( path );
187 catch ( RepositoryPurgeException rpe )
189 throw new ConsumerException( rpe.getMessage(), rpe );
193 public void completeScan()
198 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
200 if ( propertyNameTriggers.contains( propertyName ) )
206 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
211 private void initIncludes()
215 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
218 public void initialize()
219 throws InitializationException
221 propertyNameTriggers = new ArrayList<String>();
222 propertyNameTriggers.add( "repositoryScanning" );
223 propertyNameTriggers.add( "fileTypes" );
224 propertyNameTriggers.add( "fileType" );
225 propertyNameTriggers.add( "patterns" );
226 propertyNameTriggers.add( "pattern" );
228 configuration.addChangeListener( this );
233 public boolean isProcessUnmodified()
235 // we need to check all files for deletion, especially if not modified
239 public void setRepositoryContentIndexFactory( RepositoryContentIndexFactory indexFactory )
241 this.indexFactory = indexFactory;