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