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