]> source.dussan.org Git - archiva.git/blob
c29a647a81d2acd7c3c409dfb37c578b065e2f0b
[archiva.git] /
1 package org.apache.maven.archiva.consumers.core.repository;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
40
41 import java.util.ArrayList;
42 import java.util.Date;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 /**
48  * Consumer for removing old snapshots in the repository based on the criteria
49  * specified by the user.
50  *
51  * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
52  * 
53  * @plexus.component 
54  *      role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
55  *      role-hint="repository-purge"
56  *      instantiation-strategy="per-lookup"
57  */
58 public class RepositoryPurgeConsumer
59     extends AbstractMonitoredConsumer
60     implements KnownRepositoryContentConsumer, RegistryListener, Initializable
61 {
62     /**
63      * @plexus.configuration default-value="repository-purge"
64      */
65     private String id;
66
67     /**
68      * @plexus.configuration default-value="Purge repository of old snapshots"
69      */
70     private String description;
71
72     /**
73      * @plexus.requirement
74      */
75     private ArchivaConfiguration configuration;
76
77     /**
78      * @plexus.requirement role-hint="jdo"
79      */
80     private ArchivaDAO dao;
81
82     /**
83      * @plexus.requirement
84      */
85     private RepositoryContentFactory repositoryFactory;
86
87     /**
88      * @plexus.requirement
89      */
90     private MetadataTools metadataTools;
91
92     /**
93      * @plexus.requirement
94      */
95     private FileTypes filetypes;
96
97     private List<String> includes = new ArrayList<String>();
98
99     private List<String> propertyNameTriggers = new ArrayList<String>();
100
101     private RepositoryPurge repoPurge;
102
103     private RepositoryPurge cleanUp;
104
105     private boolean deleteReleasedSnapshots;
106     
107     /**
108      * @plexus.requirement role-hint="lucene"
109      */
110     private RepositoryContentIndexFactory indexFactory;
111
112     public String getId()
113     {
114         return this.id;
115     }
116
117     public String getDescription()
118     {
119         return this.description;
120     }
121
122     public boolean isPermanent()
123     {
124         return false;
125     }
126
127     public List<String> getExcludes()
128     {
129         return getDefaultArtifactExclusions();
130     }
131
132     public List<String> getIncludes()
133     {
134         return this.includes;
135     }
136
137     public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
138         throws ConsumerException
139     {
140         try
141         {
142             Map<String, RepositoryContentIndex> indices = new HashMap<String, RepositoryContentIndex>();
143             indices.put( "bytecode", indexFactory.createBytecodeIndex( repository ) );
144             indices.put( "hashcodes", indexFactory.createHashcodeIndex( repository ) );
145             indices.put( "filecontent", indexFactory.createFileContentIndex( repository ) );
146             
147             ManagedRepositoryContent repositoryContent = repositoryFactory.getManagedRepositoryContent( repository
148                 .getId() );
149
150             if ( repository.getDaysOlder() != 0 )
151             {
152                 repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
153                     .getDaysOlder(), repository.getRetentionCount(), indices );
154             }
155             else
156             {
157                 repoPurge = new RetentionCountRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
158                     .getRetentionCount(), indices );
159             }
160
161             cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, dao.getArtifactDAO(),
162                                                                    metadataTools, indices );
163
164             deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
165         }
166         catch ( RepositoryNotFoundException e )
167         {
168             throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
169         }
170         catch ( RepositoryException e )
171         {
172             throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
173         }
174     }
175
176     public void processFile( String path )
177         throws ConsumerException
178     {
179         try
180         {
181             if ( deleteReleasedSnapshots )
182             {
183                 cleanUp.process( path );
184             }
185
186             repoPurge.process( path );
187         }
188         catch ( RepositoryPurgeException rpe )
189         {
190             throw new ConsumerException( rpe.getMessage(), rpe );
191         }
192     }
193
194     public void completeScan()
195     {
196         /* do nothing */
197     }
198
199     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
200     {
201         if ( propertyNameTriggers.contains( propertyName ) )
202         {
203             initIncludes();
204         }
205     }
206
207     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
208     {
209         /* do nothing */
210     }
211
212     private void initIncludes()
213     {
214         includes.clear();
215
216         includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
217     }
218
219     public void initialize()
220         throws InitializationException
221     {
222         propertyNameTriggers = new ArrayList<String>();
223         propertyNameTriggers.add( "repositoryScanning" );
224         propertyNameTriggers.add( "fileTypes" );
225         propertyNameTriggers.add( "fileType" );
226         propertyNameTriggers.add( "patterns" );
227         propertyNameTriggers.add( "pattern" );
228
229         configuration.addChangeListener( this );
230
231         initIncludes();
232     }
233
234     public boolean isProcessUnmodified()
235     {
236         // we need to check all files for deletion, especially if not modified
237         return true;
238     }
239     
240     public void setRepositoryContentIndexFactory( RepositoryContentIndexFactory indexFactory )
241     {
242         this.indexFactory = indexFactory;
243     }
244 }