]> source.dussan.org Git - archiva.git/blob
f7711e2a91f53a9cdd6be1b3ebf65843807d1ae6
[archiva.git] /
1 package org.apache.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.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.ConfigurationNames;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
28 import org.apache.archiva.consumers.ConsumerException;
29 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.apache.archiva.metadata.repository.RepositorySession;
31 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
32 import org.apache.archiva.redback.components.registry.Registry;
33 import org.apache.archiva.redback.components.registry.RegistryListener;
34 import org.apache.archiva.repository.ManagedRepositoryContent;
35 import org.apache.archiva.repository.RepositoryContentFactory;
36 import org.apache.archiva.repository.RepositoryException;
37 import org.apache.archiva.repository.RepositoryNotFoundException;
38 import org.apache.archiva.repository.events.RepositoryListener;
39 import org.apache.archiva.repository.metadata.MetadataTools;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.stereotype.Service;
43
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import javax.inject.Named;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.Date;
50 import java.util.List;
51
52 /**
53  * Consumer for removing old snapshots in the repository based on the criteria
54  * specified by the user.
55  */
56 @Service( "knownRepositoryContentConsumer#repository-purge" )
57 @Scope( "prototype" )
58 public class RepositoryPurgeConsumer
59     extends AbstractMonitoredConsumer
60     implements KnownRepositoryContentConsumer, RegistryListener
61 {
62     /**
63      * default-value="repository-purge"
64      */
65     private String id = "repository-purge";
66
67     /**
68      * default-value="Purge repository of old snapshots"
69      */
70     private String description = "Purge repository of old snapshots";
71
72     @Inject
73     @Named( value = "archivaConfiguration#default" )
74     private ArchivaConfiguration configuration;
75
76     @Inject
77     private ManagedRepositoryAdmin managedRepositoryAdmin;
78
79     @Inject
80     @Named( value = "repositoryContentFactory#default" )
81     private RepositoryContentFactory repositoryContentFactory;
82
83     @Inject
84     private MetadataTools metadataTools;
85
86     @Inject
87     @Named( value = "fileTypes" )
88     private FileTypes filetypes;
89
90     private List<String> includes = new ArrayList<>();
91
92     private RepositoryPurge repoPurge;
93
94     private RepositoryPurge cleanUp;
95
96     private boolean deleteReleasedSnapshots;
97
98     /**
99      *
100      */
101     @Inject
102     @Autowired(required = false)
103     private List<RepositoryListener> listeners = Collections.emptyList();
104
105     @Inject
106     private RepositorySessionFactory repositorySessionFactory;
107
108     private RepositorySession repositorySession;
109
110     @Override
111     public String getId()
112     {
113         return this.id;
114     }
115
116     @Override
117     public String getDescription()
118     {
119         return this.description;
120     }
121
122     @Override
123     public List<String> getExcludes()
124     {
125         return getDefaultArtifactExclusions();
126     }
127
128     @Override
129     public List<String> getIncludes()
130     {
131         return this.includes;
132     }
133
134     @Override
135     public void beginScan( ManagedRepository repository, Date whenGathered )
136         throws ConsumerException
137     {
138         ManagedRepositoryContent repositoryContent;
139         try
140         {
141             repositoryContent = repositoryContentFactory.getManagedRepositoryContent( repository.getId() );
142         }
143         catch ( RepositoryNotFoundException e )
144         {
145             throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
146         }
147         catch ( RepositoryException e )
148         {
149             throw new ConsumerException( "Can't run repository purge: " + e.getMessage(), e );
150         }
151
152         repositorySession = repositorySessionFactory.createSession();
153
154         if ( repository.getDaysOlder() != 0 )
155         {
156             repoPurge = new DaysOldRepositoryPurge( repositoryContent, repository.getDaysOlder(),
157                                                     repository.getRetentionCount(), repositorySession, listeners );
158         }
159         else
160         {
161             repoPurge =
162                 new RetentionCountRepositoryPurge( repositoryContent, repository.getRetentionCount(), repositorySession,
163                                                    listeners );
164         }
165
166         cleanUp = new CleanupReleasedSnapshotsRepositoryPurge( repositoryContent, metadataTools, managedRepositoryAdmin,
167                                                                repositoryContentFactory, repositorySession, listeners );
168
169         deleteReleasedSnapshots = repository.isDeleteReleasedSnapshots();
170     }
171
172     @Override
173     public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
174         throws ConsumerException
175     {
176         beginScan( repository, whenGathered );
177     }
178
179     @Override
180     public void processFile( String path )
181         throws ConsumerException
182     {
183         try
184         {
185             if ( deleteReleasedSnapshots )
186             {
187                 cleanUp.process( path );
188             }
189             repoPurge.process( path );
190         }
191         catch ( RepositoryPurgeException rpe )
192         {
193             throw new ConsumerException( rpe.getMessage(), rpe );
194         }
195     }
196
197     @Override
198     public void processFile( String path, boolean executeOnEntireRepo )
199         throws Exception
200     {
201         processFile( path );
202     }
203
204     @Override
205     public void completeScan()
206     {
207         repositorySession.close();
208     }
209
210     @Override
211     public void completeScan( boolean executeOnEntireRepo )
212     {
213         completeScan();
214     }
215
216     @Override
217     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
218     {
219         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
220         {
221             initIncludes();
222         }
223     }
224
225     @Override
226     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
227     {
228         /* do nothing */
229     }
230
231     private void initIncludes()
232     {
233         includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
234     }
235
236     @PostConstruct
237     public void initialize()
238     {
239         configuration.addChangeListener( this );
240
241         initIncludes();
242     }
243
244     @Override
245     public boolean isProcessUnmodified()
246     {
247         // we need to check all files for deletion, especially if not modified
248         return true;
249     }
250
251     public ArchivaConfiguration getConfiguration()
252     {
253         return configuration;
254     }
255
256     public void setConfiguration( ArchivaConfiguration configuration )
257     {
258         this.configuration = configuration;
259     }
260
261     public RepositoryContentFactory getRepositoryContentFactory()
262     {
263         return repositoryContentFactory;
264     }
265
266     public void setRepositoryContentFactory( RepositoryContentFactory repositoryContentFactory )
267     {
268         this.repositoryContentFactory = repositoryContentFactory;
269     }
270
271     public MetadataTools getMetadataTools()
272     {
273         return metadataTools;
274     }
275
276     public void setMetadataTools( MetadataTools metadataTools )
277     {
278         this.metadataTools = metadataTools;
279     }
280
281     public FileTypes getFiletypes()
282     {
283         return filetypes;
284     }
285
286     public void setFiletypes( FileTypes filetypes )
287     {
288         this.filetypes = filetypes;
289     }
290
291     public RepositoryPurge getRepoPurge()
292     {
293         return repoPurge;
294     }
295
296     public void setRepoPurge( RepositoryPurge repoPurge )
297     {
298         this.repoPurge = repoPurge;
299     }
300
301     public RepositoryPurge getCleanUp()
302     {
303         return cleanUp;
304     }
305
306     public void setCleanUp( RepositoryPurge cleanUp )
307     {
308         this.cleanUp = cleanUp;
309     }
310
311     public boolean isDeleteReleasedSnapshots()
312     {
313         return deleteReleasedSnapshots;
314     }
315
316     public void setDeleteReleasedSnapshots( boolean deleteReleasedSnapshots )
317     {
318         this.deleteReleasedSnapshots = deleteReleasedSnapshots;
319     }
320
321     public RepositorySessionFactory getRepositorySessionFactory()
322     {
323         return repositorySessionFactory;
324     }
325
326     public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
327     {
328         this.repositorySessionFactory = repositorySessionFactory;
329     }
330
331     public RepositorySession getRepositorySession()
332     {
333         return repositorySession;
334     }
335
336     public void setRepositorySession( RepositorySession repositorySession )
337     {
338         this.repositorySession = repositorySession;
339     }
340 }