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