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