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