1 package org.apache.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
22 import org.apache.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.FileTypes;
24 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
25 import org.apache.archiva.consumers.ConsumerException;
26 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.archiva.model.ArtifactReference;
28 import org.apache.archiva.model.ProjectReference;
29 import org.apache.archiva.model.VersionedReference;
30 import org.apache.archiva.repository.ContentNotFoundException;
31 import org.apache.archiva.repository.ManagedRepository;
32 import org.apache.archiva.repository.ManagedRepositoryContent;
33 import org.apache.archiva.repository.RepositoryException;
34 import org.apache.archiva.repository.RepositoryNotFoundException;
35 import org.apache.archiva.repository.RepositoryRegistry;
36 import org.apache.archiva.repository.layout.LayoutException;
37 import org.apache.archiva.repository.metadata.MetadataTools;
38 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.stereotype.Service;
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import java.io.IOException;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.nio.file.Paths;
50 import java.util.ArrayList;
51 import java.util.Date;
52 import java.util.List;
55 * MetadataUpdaterConsumer will create and update the metadata present within the repository.
57 @Service( "knownRepositoryContentConsumer#metadata-updater" )
59 public class MetadataUpdaterConsumer
60 extends AbstractMonitoredConsumer
61 implements KnownRepositoryContentConsumer
62 // it's prototype bean so we assume configuration won't change during a run
65 private Logger log = LoggerFactory.getLogger( MetadataUpdaterConsumer.class );
68 * default-value="metadata-updater"
70 private String id = "metadata-updater";
72 private String description = "Update / Create maven-metadata.xml files";
75 private RepositoryRegistry repositoryRegistry;
78 private MetadataTools metadataTools;
81 private ArchivaConfiguration configuration;
84 private FileTypes filetypes;
86 private static final String TYPE_METADATA_BAD_INTERNAL_REF = "metadata-bad-internal-ref";
88 private static final String TYPE_METADATA_WRITE_FAILURE = "metadata-write-failure";
90 private static final String TYPE_METADATA_IO = "metadata-io-warning";
92 private ManagedRepositoryContent repository;
94 private Path repositoryDir;
96 private List<String> includes = new ArrayList<>( 0 );
98 private long scanStartTimestamp = 0;
101 public String getDescription( )
107 public String getId( )
112 public void setIncludes( List<String> includes )
114 this.includes = includes;
118 public void beginScan( ManagedRepository repoConfig, Date whenGathered )
119 throws ConsumerException
123 ManagedRepository repo = repositoryRegistry.getManagedRepository( repoConfig.getId( ) );
125 throw new RepositoryNotFoundException( "Repository not found: "+repoConfig.getId() );
127 this.repository = repo.getContent();
128 if (this.repository==null) {
129 throw new RepositoryNotFoundException( "Repository content not found: "+repoConfig.getId() );
131 this.repositoryDir = Paths.get( repository.getRepoRoot( ) );
132 this.scanStartTimestamp = System.currentTimeMillis( );
134 catch ( RepositoryNotFoundException e )
136 throw new ConsumerException( e.getMessage( ), e );
138 catch ( RepositoryException e )
140 throw new ConsumerException( e.getMessage( ), e );
145 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
146 throws ConsumerException
148 beginScan( repository, whenGathered );
152 public void completeScan( )
154 /* do nothing here */
158 public void completeScan( boolean executeOnEntireRepo )
164 public List<String> getExcludes( )
166 return getDefaultArtifactExclusions( );
170 public List<String> getIncludes( )
172 return this.includes;
176 public void processFile( String path )
177 throws ConsumerException
179 // Ignore paths like .index etc
180 if ( !path.startsWith( "." ) )
184 ArtifactReference artifact = repository.toArtifactReference( path );
185 updateVersionMetadata( artifact, path );
186 updateProjectMetadata( artifact, path );
188 catch ( LayoutException e )
190 log.info( "Not processing path that is not an artifact: {} ({})", path, e.getMessage( ) );
196 public void processFile( String path, boolean executeOnEntireRepo )
202 private void updateProjectMetadata( ArtifactReference artifact, String path )
204 ProjectReference projectRef = new ProjectReference( );
205 projectRef.setGroupId( artifact.getGroupId( ) );
206 projectRef.setArtifactId( artifact.getArtifactId( ) );
210 String metadataPath = this.metadataTools.toPath( projectRef );
212 Path projectMetadata = this.repositoryDir.resolve( metadataPath );
214 if ( Files.exists(projectMetadata) && ( Files.getLastModifiedTime( projectMetadata).toMillis() >= this.scanStartTimestamp ) )
216 // This metadata is up to date. skip it.
217 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( projectRef ) );
221 metadataTools.updateMetadata( this.repository, projectRef );
222 log.debug( "Updated metadata: {}", this.metadataTools.toPath( projectRef ) );
224 catch ( LayoutException e )
226 log.warn( "Unable to convert path [{}] to an internal project reference: ", path, e );
227 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF,
228 "Unable to convert path [" + path + "] to an internal project reference: "
231 catch ( RepositoryMetadataException e )
233 log.error( "Unable to write project metadat for artifact [{}]:", path, e );
234 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE,
235 "Unable to write project metadata for artifact [" + path + "]: " + e.getMessage( ) );
237 catch ( IOException e )
239 log.warn( "Project metadata not written due to IO warning: ", e );
240 triggerConsumerWarning( TYPE_METADATA_IO,
241 "Project metadata not written due to IO warning: " + e.getMessage( ) );
243 catch ( ContentNotFoundException e )
245 log.warn( "Project metadata not written because no versions were found to update: ", e );
246 triggerConsumerWarning( TYPE_METADATA_IO,
247 "Project metadata not written because no versions were found to update: "
252 private void updateVersionMetadata( ArtifactReference artifact, String path )
254 VersionedReference versionRef = new VersionedReference( );
255 versionRef.setGroupId( artifact.getGroupId( ) );
256 versionRef.setArtifactId( artifact.getArtifactId( ) );
257 versionRef.setVersion( artifact.getVersion( ) );
261 String metadataPath = this.metadataTools.toPath( versionRef );
263 Path projectMetadata = this.repositoryDir.resolve( metadataPath );
265 if ( Files.exists(projectMetadata) && ( Files.getLastModifiedTime( projectMetadata ).toMillis() >= this.scanStartTimestamp ) )
267 // This metadata is up to date. skip it.
268 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( versionRef ) );
272 metadataTools.updateMetadata( this.repository, versionRef );
273 log.debug( "Updated metadata: {}", this.metadataTools.toPath( versionRef ) );
275 catch ( LayoutException e )
277 log.warn( "Unable to convert path [{}] to an internal version reference: ", path, e );
278 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF,
279 "Unable to convert path [" + path + "] to an internal version reference: "
282 catch ( RepositoryMetadataException e )
284 log.error( "Unable to write version metadata for artifact [{}]: ", path, e );
285 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE,
286 "Unable to write version metadata for artifact [" + path + "]: " + e.getMessage( ) );
288 catch ( IOException e )
290 log.warn( "Version metadata not written due to IO warning: ", e );
291 triggerConsumerWarning( TYPE_METADATA_IO,
292 "Version metadata not written due to IO warning: " + e.getMessage( ) );
294 catch ( ContentNotFoundException e )
296 log.warn( "Version metadata not written because no versions were found to update: ", e );
297 triggerConsumerWarning( TYPE_METADATA_IO,
298 "Version metadata not written because no versions were found to update: "
305 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
307 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
314 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
320 private void initIncludes( )
322 includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
326 public void initialize( )
328 //configuration.addChangeListener( this );