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