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.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.ConfigurationNames;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.archiva.consumers.ConsumerException;
28 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.archiva.model.ArtifactReference;
30 import org.apache.archiva.model.ProjectReference;
31 import org.apache.archiva.model.VersionedReference;
32 import org.apache.archiva.repository.ContentNotFoundException;
33 import org.apache.archiva.repository.ManagedRepositoryContent;
34 import org.apache.archiva.repository.RepositoryContentFactory;
35 import org.apache.archiva.repository.RepositoryException;
36 import org.apache.archiva.repository.RepositoryNotFoundException;
37 import org.apache.archiva.repository.layout.LayoutException;
38 import org.apache.archiva.repository.metadata.MetadataTools;
39 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
40 import org.apache.archiva.redback.components.registry.Registry;
41 import org.apache.archiva.redback.components.registry.RegistryListener;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.context.annotation.Scope;
45 import org.springframework.stereotype.Service;
47 import javax.annotation.PostConstruct;
48 import javax.inject.Inject;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.Date;
53 import java.util.List;
56 * MetadataUpdaterConsumer will create and update the metadata present within the repository.
60 @Service( "knownRepositoryContentConsumer#metadata-updater" )
62 public class MetadataUpdaterConsumer
63 extends AbstractMonitoredConsumer
64 implements KnownRepositoryContentConsumer, RegistryListener
66 private Logger log = LoggerFactory.getLogger( MetadataUpdaterConsumer.class );
69 * default-value="metadata-updater"
71 private String id = "metadata-updater";
74 * default-value="Update / Create maven-metadata.xml files"
76 private String description = "Update / Create maven-metadata.xml files";
82 private RepositoryContentFactory repositoryFactory;
88 private MetadataTools metadataTools;
94 private ArchivaConfiguration configuration;
100 private FileTypes filetypes;
102 private static final String TYPE_METADATA_BAD_INTERNAL_REF = "metadata-bad-internal-ref";
104 private static final String TYPE_METADATA_WRITE_FAILURE = "metadata-write-failure";
106 private static final String TYPE_METADATA_IO = "metadata-io-warning";
108 private ManagedRepositoryContent repository;
110 private File repositoryDir;
112 private List<String> includes = new ArrayList<>( 0 );
114 private long scanStartTimestamp = 0;
117 public String getDescription()
123 public String getId()
128 public void setIncludes( List<String> includes )
130 this.includes = includes;
134 public void beginScan( ManagedRepository repoConfig, Date whenGathered )
135 throws ConsumerException
139 this.repository = repositoryFactory.getManagedRepositoryContent( repoConfig.getId() );
140 this.repositoryDir = new File( repository.getRepoRoot() );
141 this.scanStartTimestamp = System.currentTimeMillis();
143 catch ( RepositoryNotFoundException e )
145 throw new ConsumerException( e.getMessage(), e );
147 catch ( RepositoryException e )
149 throw new ConsumerException( e.getMessage(), e );
154 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
155 throws ConsumerException
157 beginScan( repository, whenGathered );
161 public void completeScan()
163 /* do nothing here */
167 public void completeScan( boolean executeOnEntireRepo )
173 public List<String> getExcludes()
175 return getDefaultArtifactExclusions();
179 public List<String> getIncludes()
181 return this.includes;
185 public void processFile( String path )
186 throws ConsumerException
188 // Ignore paths like .index etc
189 if ( !path.startsWith( "." ) )
193 ArtifactReference artifact = repository.toArtifactReference( path );
194 updateVersionMetadata( artifact, path );
195 updateProjectMetadata( artifact, path );
197 catch ( LayoutException e )
199 log.info( "Not processing path that is not an artifact: {} ({})", path, e.getMessage() );
205 public void processFile( String path, boolean executeOnEntireRepo )
211 private void updateProjectMetadata( ArtifactReference artifact, String path )
213 ProjectReference projectRef = new ProjectReference();
214 projectRef.setGroupId( artifact.getGroupId() );
215 projectRef.setArtifactId( artifact.getArtifactId() );
219 String metadataPath = this.metadataTools.toPath( projectRef );
221 File projectMetadata = new File( this.repositoryDir, metadataPath );
223 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
225 // This metadata is up to date. skip it.
226 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( projectRef ) );
230 metadataTools.updateMetadata( this.repository, projectRef );
231 log.debug( "Updated metadata: {}", this.metadataTools.toPath( projectRef ) );
233 catch ( LayoutException e )
235 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF,
236 "Unable to convert path [" + path + "] to an internal project reference: "
239 catch ( RepositoryMetadataException e )
241 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE,
242 "Unable to write project metadata for artifact [" + path + "]: " + e.getMessage() );
244 catch ( IOException e )
246 triggerConsumerWarning( TYPE_METADATA_IO,
247 "Project metadata not written due to IO warning: " + e.getMessage() );
249 catch ( ContentNotFoundException e )
251 triggerConsumerWarning( TYPE_METADATA_IO,
252 "Project metadata not written because no versions were found to update: "
257 private void updateVersionMetadata( ArtifactReference artifact, String path )
259 VersionedReference versionRef = new VersionedReference();
260 versionRef.setGroupId( artifact.getGroupId() );
261 versionRef.setArtifactId( artifact.getArtifactId() );
262 versionRef.setVersion( artifact.getVersion() );
266 String metadataPath = this.metadataTools.toPath( versionRef );
268 File projectMetadata = new File( this.repositoryDir, metadataPath );
270 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
272 // This metadata is up to date. skip it.
273 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( versionRef ) );
277 metadataTools.updateMetadata( this.repository, versionRef );
278 log.debug( "Updated metadata: {}", this.metadataTools.toPath( versionRef ) );
280 catch ( LayoutException e )
282 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF,
283 "Unable to convert path [" + path + "] to an internal version reference: "
286 catch ( RepositoryMetadataException e )
288 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE,
289 "Unable to write version metadata for artifact [" + path + "]: " + e.getMessage() );
291 catch ( IOException e )
293 triggerConsumerWarning( TYPE_METADATA_IO,
294 "Version metadata not written due to IO warning: " + e.getMessage() );
296 catch ( ContentNotFoundException e )
298 triggerConsumerWarning( TYPE_METADATA_IO,
299 "Version metadata not written because no versions were found to update: "
305 public boolean isPermanent()
311 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
313 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
320 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
322 /* do nothing here */
325 private void initIncludes()
327 includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
331 public void initialize()
333 configuration.addChangeListener( this );