]> source.dussan.org Git - archiva.git/blob
6b56549005b4add0f53e34695b0b3c4bd86d77da
[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.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.FileTypes;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
26 import org.apache.maven.archiva.consumers.ConsumerException;
27 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.codehaus.plexus.digest.ChecksumFile;
29 import org.codehaus.plexus.digest.Digester;
30 import org.codehaus.plexus.digest.DigesterException;
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.List;
40
41 /**
42  * ArtifactMissingChecksumsConsumer - Create missing checksums for the artifact.
43  *
44  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
45  * @version $Id$
46  * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
47  * role-hint="create-missing-checksums"
48  * instantiation-strategy="per-lookup"
49  */
50 public class ArtifactMissingChecksumsConsumer
51     extends AbstractMonitoredConsumer
52     implements KnownRepositoryContentConsumer, RegistryListener, Initializable
53 {
54     /**
55      * @plexus.configuration default-value="create-missing-checksums"
56      */
57     private String id;
58
59     /**
60      * @plexus.configuration default-value="Create Missing Checksums (.sha1 & .md5)"
61      */
62     private String description;
63
64     /**
65      * @plexus.requirement
66      */
67     private ArchivaConfiguration configuration;
68
69     /**
70      * @plexus.requirement
71      */
72     private FileTypes filetypes;
73
74     /**
75      * @plexus.requirement role-hint="sha1"
76      */
77     private Digester digestSha1;
78
79     /**
80      * @plexus.requirement role-hint="md5";
81      */
82     private Digester digestMd5;
83
84     /**
85      * @plexus.requirement
86      */
87     private ChecksumFile checksum;
88
89     private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
90
91     private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
92
93     private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
94
95     private File repositoryDir;
96
97     private List<String> propertyNameTriggers = new ArrayList<String>();
98
99     private List<String> includes = new ArrayList<String>();
100
101     public String getId()
102     {
103         return this.id;
104     }
105
106     public String getDescription()
107     {
108         return this.description;
109     }
110
111     public boolean isPermanent()
112     {
113         return false;
114     }
115
116     public void beginScan( ManagedRepositoryConfiguration repo )
117         throws ConsumerException
118     {
119         this.repositoryDir = new File( repo.getLocation() );
120     }
121
122     public void completeScan()
123     {
124         /* do nothing */
125     }
126
127     public List<String> getExcludes()
128     {
129         return null;
130     }
131
132     public List<String> getIncludes()
133     {
134         return includes;
135     }
136
137     public void processFile( String path )
138         throws ConsumerException
139     {
140         createIfMissing( path, digestSha1 );
141         createIfMissing( path, digestMd5 );
142     }
143
144     private void createIfMissing( String path, Digester digester )
145     {
146         File checksumFile = new File( this.repositoryDir, path + digester.getFilenameExtension() );
147         if ( !checksumFile.exists() )
148         {
149             try
150             {
151                 checksum.createChecksum( new File( this.repositoryDir, path ), digester );
152                 triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
153             }
154             catch ( DigesterException e )
155             {
156                 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC,
157                                       "Cannot calculate checksum for file " + checksumFile + ": " + e.getMessage() );
158             }
159             catch ( IOException e )
160             {
161                 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE,
162                                       "Cannot create checksum for file " + checksumFile + ": " + e.getMessage() );
163             }
164         }
165         else if ( !checksumFile.isFile() )
166         {
167             triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
168                                     "Checksum file " + checksumFile.getAbsolutePath() + " is not a file." );
169         }
170
171     }
172
173     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
174     {
175         if ( propertyNameTriggers.contains( propertyName ) )
176         {
177             initIncludes();
178         }
179     }
180
181     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
182     {
183         /* do nothing */
184     }
185
186     private void initIncludes()
187     {
188         includes.clear();
189
190         includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
191     }
192
193     public void initialize()
194         throws InitializationException
195     {
196         propertyNameTriggers = new ArrayList<String>();
197         propertyNameTriggers.add( "repositoryScanning" );
198         propertyNameTriggers.add( "fileTypes" );
199         propertyNameTriggers.add( "fileType" );
200         propertyNameTriggers.add( "patterns" );
201         propertyNameTriggers.add( "pattern" );
202
203         configuration.addChangeListener( this );
204
205         initIncludes();
206     }
207 }