]> source.dussan.org Git - archiva.git/blob
e9a66545166588a502117ac81fd48ada7cd6faec
[archiva.git] /
1 package org.apache.archiva.consumers.core;
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.checksum.ChecksumAlgorithm;
24 import org.apache.archiva.checksum.ChecksummedFile;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.ConfigurationNames;
27 import org.apache.archiva.configuration.FileTypes;
28 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
29 import org.apache.archiva.consumers.ConsumerException;
30 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.codehaus.plexus.registry.Registry;
32 import org.codehaus.plexus.registry.RegistryListener;
33 import org.springframework.context.annotation.Scope;
34 import org.springframework.stereotype.Service;
35
36 import javax.annotation.PostConstruct;
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.Date;
41 import java.util.List;
42
43 /**
44  * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
45  *
46  * @version $Id$
47  */
48 @Service("knownRepositoryContentConsumer#create-missing-checksums")
49 @Scope("prototype")
50 public class ArtifactMissingChecksumsConsumer
51     extends AbstractMonitoredConsumer
52     implements KnownRepositoryContentConsumer, RegistryListener
53 {   
54     private String id;
55
56     private String description;
57
58     private ArchivaConfiguration configuration;
59
60     private FileTypes filetypes;
61
62     private ChecksummedFile checksum;
63
64     private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
65     
66     private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
67     
68     private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
69
70     private File repositoryDir;
71     
72     private List<String> includes = new ArrayList<String>();
73     
74     public ArtifactMissingChecksumsConsumer(String id,
75             String description,
76             ArchivaConfiguration configuration,
77             FileTypes filetypes) {
78         this.id = id;
79         this.description = description;
80         this.configuration = configuration;
81         this.filetypes = filetypes;
82
83         configuration.addChangeListener( this );
84
85         initIncludes();
86     }
87
88     public String getId()
89     {
90         return this.id;
91     }
92
93     public String getDescription()
94     {
95         return this.description;
96     }
97
98     public boolean isPermanent()
99     {
100         return false;
101     }
102
103     public void beginScan( ManagedRepository repo, Date whenGathered )
104         throws ConsumerException
105     {
106         this.repositoryDir = new File( repo.getLocation() );
107     }
108
109     public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
110         throws ConsumerException
111     {
112         beginScan( repo, whenGathered );
113     }
114
115     public void completeScan()
116     {
117         /* do nothing */
118     }
119
120     public void completeScan( boolean executeOnEntireRepo )
121     {
122         completeScan();
123     }
124
125     public List<String> getExcludes()
126     {
127         return getDefaultArtifactExclusions();
128     }
129
130     public List<String> getIncludes()
131     {
132         return includes;
133     }
134
135     public void processFile( String path )
136         throws ConsumerException
137     {
138         createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } );
139         createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5 } );        
140     }
141
142     public void processFile( String path, boolean executeOnEntireRepo )
143         throws ConsumerException
144     {
145         processFile( path );   
146     }
147     
148     private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
149     {
150         File artifactFile = new File( this.repositoryDir, path );
151         File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() );
152                 
153         if( checksumFile.exists() )
154         {
155             checksum = new ChecksummedFile( artifactFile );
156             try
157             {
158                 if( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
159                 {   
160                     checksum.fixChecksums( checksumAlgorithm );
161                     triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
162                 }
163             }
164             catch ( IOException e )
165             {
166                 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
167                     ": " + e.getMessage() );
168             }
169         }
170         else if( !checksumFile.exists() )
171         {   
172             checksum = new ChecksummedFile( artifactFile );
173             try
174             {
175                 checksum.createChecksum( checksumAlgorithm[0] );
176                 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
177             }
178             catch ( IOException e )
179             {
180                 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
181                     ": " + e.getMessage() );
182             }
183         }
184         else
185         {
186             triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
187                                     "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
188         }
189     }
190
191     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
192     {             
193         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
194         {
195             initIncludes();
196         }
197     }
198
199     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
200     {
201         /* do nothing */
202     }
203
204     private void initIncludes()
205     {
206         includes.clear();
207
208         includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
209     }
210
211     @PostConstruct
212     public void initialize()
213     {
214         configuration.addChangeListener( this );
215         
216         initIncludes();
217     }
218 }