]> source.dussan.org Git - archiva.git/blob
954ac6bf3e7af6e9bb55bc477f2584d11a336dee
[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.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.springframework.context.annotation.Scope;
31 import org.springframework.stereotype.Service;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import javax.annotation.PostConstruct;
36 import javax.inject.Inject;
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  *
47  */
48 @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
49 @Scope( "prototype" )
50 public class ArtifactMissingChecksumsConsumer
51     extends AbstractMonitoredConsumer
52     implements KnownRepositoryContentConsumer
53     // it's prototype bean so we assume configuration won't change during a run
54     //, RegistryListener
55 {
56
57     private Logger log = LoggerFactory.getLogger( ArtifactMissingChecksumsConsumer.class );
58
59     private String id = "create-missing-checksums";
60
61     private String description = "Create Missing and/or Fix Invalid Checksums (.sha1, .md5)";
62
63     private ArchivaConfiguration configuration;
64
65     private FileTypes filetypes;
66
67     private ChecksummedFile checksum;
68
69     private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
70
71     private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
72
73     private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
74
75     private File repositoryDir;
76
77     private List<String> includes = new ArrayList<>( 0 );
78
79     @Inject
80     public ArtifactMissingChecksumsConsumer( ArchivaConfiguration configuration, FileTypes filetypes )
81     {
82         this.configuration = configuration;
83         this.filetypes = filetypes;
84
85         //configuration.addChangeListener( this );
86
87         initIncludes();
88     }
89
90     @Override
91     public String getId()
92     {
93         return this.id;
94     }
95
96     @Override
97     public String getDescription()
98     {
99         return this.description;
100     }
101     
102     @Override
103     public void beginScan( ManagedRepository repo, Date whenGathered )
104         throws ConsumerException
105     {
106         this.repositoryDir = new File( repo.getLocation() );
107     }
108
109     @Override
110     public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
111         throws ConsumerException
112     {
113         beginScan( repo, whenGathered );
114     }
115
116     @Override
117     public void completeScan()
118     {
119         /* do nothing */
120     }
121
122     @Override
123     public void completeScan( boolean executeOnEntireRepo )
124     {
125         completeScan();
126     }
127
128     @Override
129     public List<String> getExcludes()
130     {
131         return getDefaultArtifactExclusions();
132     }
133
134     @Override
135     public List<String> getIncludes()
136     {
137         return includes;
138     }
139
140     @Override
141     public void processFile( String path )
142         throws ConsumerException
143     {
144         createFixChecksum( path, ChecksumAlgorithm.SHA1 );
145         createFixChecksum( path, ChecksumAlgorithm.MD5 );
146     }
147
148     @Override
149     public void processFile( String path, boolean executeOnEntireRepo )
150         throws ConsumerException
151     {
152         processFile( path );
153     }
154
155     private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm )
156     {
157         File artifactFile = new File( this.repositoryDir, path );
158         File checksumFile = new File( this.repositoryDir, path + "."  + checksumAlgorithm.getExt() );
159
160         if ( checksumFile.exists() )
161         {
162             checksum = new ChecksummedFile( artifactFile );
163             try
164             {
165                 if ( !checksum.isValidChecksum( checksumAlgorithm ) )
166                 {
167                     checksum.fixChecksums( new ChecksumAlgorithm[]{ checksumAlgorithm } );
168                     log.info( "Fixed checksum file {}", checksumFile.getAbsolutePath() );
169                     triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
170                 }
171             }
172             catch ( IOException e )
173             {
174                 log.error( "Cannot calculate checksum for file {} :", checksumFile, e );
175                 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
176                     ": " + e.getMessage() );
177             }
178         }
179         else if ( !checksumFile.exists() )
180         {
181             checksum = new ChecksummedFile( artifactFile );
182             try
183             {
184                 checksum.createChecksum( checksumAlgorithm );
185                 log.info( "Created missing checksum file {}", checksumFile.getAbsolutePath() ); 
186                 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
187             }
188             catch ( IOException e )
189             {
190                 log.error( "Cannot create checksum for file {} :", checksumFile, e );
191                 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
192                     ": " + e.getMessage() );
193             }
194         }
195         else
196         {
197             log.warn( "Checksum file {} is not a file. ", checksumFile.getAbsolutePath() );
198             triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
199                                     "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
200         }
201     }
202
203     /*
204     @Override
205     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
206     {
207         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
208         {
209             initIncludes();
210         }
211     }
212
213
214     @Override
215     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
216     {
217         // do nothing
218     }
219
220     */
221
222     private void initIncludes()
223     {
224         includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
225
226     }
227
228     @PostConstruct
229     public void initialize()
230     {
231         //configuration.addChangeListener( this );
232
233         initIncludes();
234     }
235 }