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