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