1 package org.apache.maven.archiva.consumers.core;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.ConfigurationNames;
30 import org.apache.maven.archiva.configuration.FileTypes;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
33 import org.apache.maven.archiva.consumers.ConsumerException;
34 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
35 import org.apache.maven.archiva.model.ArtifactReference;
36 import org.apache.maven.archiva.model.ProjectReference;
37 import org.apache.maven.archiva.model.VersionedReference;
38 import org.apache.maven.archiva.repository.ContentNotFoundException;
39 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
40 import org.apache.maven.archiva.repository.RepositoryContentFactory;
41 import org.apache.maven.archiva.repository.RepositoryException;
42 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
43 import org.apache.maven.archiva.repository.layout.LayoutException;
44 import org.apache.maven.archiva.repository.metadata.MetadataTools;
45 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
46 import org.codehaus.plexus.registry.Registry;
47 import org.codehaus.plexus.registry.RegistryListener;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.context.annotation.Scope;
51 import org.springframework.stereotype.Service;
53 import javax.annotation.PostConstruct;
54 import javax.inject.Inject;
57 * MetadataUpdaterConsumer will create and update the metadata present within the repository.
60 * plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
61 * role-hint="metadata-updater"
62 * instantiation-strategy="per-lookup"
64 @Service("knownRepositoryContentConsumer#metadata-updater")
66 public class MetadataUpdaterConsumer
67 extends AbstractMonitoredConsumer
68 implements KnownRepositoryContentConsumer, RegistryListener
70 private Logger log = LoggerFactory.getLogger( MetadataUpdaterConsumer.class );
73 * plexus.configuration default-value="metadata-updater"
75 private String id = "metadata-updater";
78 * plexus.configuration default-value="Update / Create maven-metadata.xml files"
80 private String description = "Update / Create maven-metadata.xml files";
86 private RepositoryContentFactory repositoryFactory;
92 private MetadataTools metadataTools;
98 private ArchivaConfiguration configuration;
104 private FileTypes filetypes;
106 private static final String TYPE_METADATA_BAD_INTERNAL_REF = "metadata-bad-internal-ref";
108 private static final String TYPE_METADATA_WRITE_FAILURE = "metadata-write-failure";
110 private static final String TYPE_METADATA_IO = "metadata-io-warning";
112 private ManagedRepositoryContent repository;
114 private File repositoryDir;
116 private List<String> includes = new ArrayList<String>();
118 private long scanStartTimestamp = 0;
120 public String getDescription()
125 public String getId()
130 public void setIncludes( List<String> includes )
132 this.includes = includes;
135 public void beginScan( ManagedRepositoryConfiguration repoConfig, Date whenGathered )
136 throws ConsumerException
140 this.repository = repositoryFactory.getManagedRepositoryContent( repoConfig.getId() );
141 this.repositoryDir = new File( repository.getRepoRoot() );
142 this.scanStartTimestamp = System.currentTimeMillis();
144 catch ( RepositoryNotFoundException e )
146 throw new ConsumerException( e.getMessage(), e );
148 catch ( RepositoryException e )
150 throw new ConsumerException( e.getMessage(), e );
154 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
155 throws ConsumerException
157 beginScan( repository, whenGathered );
160 public void completeScan()
162 /* do nothing here */
165 public void completeScan( boolean executeOnEntireRepo )
170 public List<String> getExcludes()
172 return getDefaultArtifactExclusions();
175 public List<String> getIncludes()
177 return this.includes;
180 public void processFile( String path )
181 throws ConsumerException
183 // Ignore paths like .index etc
184 if ( !path.startsWith( "." ) )
188 ArtifactReference artifact = repository.toArtifactReference( path );
189 updateVersionMetadata( artifact, path );
190 updateProjectMetadata( artifact, path );
192 catch ( LayoutException e )
194 log.info( "Not processing path that is not an artifact: " + path + " (" + e.getMessage() + ")" );
199 public void processFile( String path, boolean executeOnEntireRepo )
205 private void updateProjectMetadata( ArtifactReference artifact, String path )
207 ProjectReference projectRef = new ProjectReference();
208 projectRef.setGroupId( artifact.getGroupId() );
209 projectRef.setArtifactId( artifact.getArtifactId() );
213 String metadataPath = this.metadataTools.toPath( projectRef );
215 File projectMetadata = new File( this.repositoryDir, metadataPath );
217 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
219 // This metadata is up to date. skip it.
220 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( projectRef ) );
224 metadataTools.updateMetadata( this.repository, projectRef );
225 log.debug( "Updated metadata: {}", this.metadataTools.toPath( projectRef ) );
227 catch ( LayoutException e )
229 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
230 + "] to an internal project reference: " + e.getMessage() );
232 catch ( RepositoryMetadataException e )
234 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write project metadata for artifact [" + path
235 + "]: " + e.getMessage() );
237 catch ( IOException e )
239 triggerConsumerWarning( TYPE_METADATA_IO, "Project metadata not written due to IO warning: "
242 catch ( ContentNotFoundException e )
244 triggerConsumerWarning( TYPE_METADATA_IO,
245 "Project metadata not written because no versions were found to update: "
250 private void updateVersionMetadata( ArtifactReference artifact, String path )
252 VersionedReference versionRef = new VersionedReference();
253 versionRef.setGroupId( artifact.getGroupId() );
254 versionRef.setArtifactId( artifact.getArtifactId() );
255 versionRef.setVersion( artifact.getVersion() );
259 String metadataPath = this.metadataTools.toPath( versionRef );
261 File projectMetadata = new File( this.repositoryDir, metadataPath );
263 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
265 // This metadata is up to date. skip it.
266 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( versionRef ) );
270 metadataTools.updateMetadata( this.repository, versionRef );
271 log.debug( "Updated metadata: {}", this.metadataTools.toPath( versionRef ) );
273 catch ( LayoutException e )
275 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
276 + "] to an internal version reference: " + e.getMessage() );
278 catch ( RepositoryMetadataException e )
280 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write version metadata for artifact [" + path
281 + "]: " + e.getMessage() );
283 catch ( IOException e )
285 triggerConsumerWarning( TYPE_METADATA_IO, "Version metadata not written due to IO warning: "
288 catch ( ContentNotFoundException e )
290 triggerConsumerWarning( TYPE_METADATA_IO,
291 "Version metadata not written because no versions were found to update: "
296 public boolean isPermanent()
301 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
303 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
309 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
311 /* do nothing here */
314 private void initIncludes()
318 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
322 public void initialize()
324 configuration.addChangeListener( this );