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.
61 @Service("knownRepositoryContentConsumer#metadata-updater")
63 public class MetadataUpdaterConsumer
64 extends AbstractMonitoredConsumer
65 implements KnownRepositoryContentConsumer, RegistryListener
67 private Logger log = LoggerFactory.getLogger( MetadataUpdaterConsumer.class );
70 * default-value="metadata-updater"
72 private String id = "metadata-updater";
75 * default-value="Update / Create maven-metadata.xml files"
77 private String description = "Update / Create maven-metadata.xml files";
83 private RepositoryContentFactory repositoryFactory;
89 private MetadataTools metadataTools;
95 private ArchivaConfiguration configuration;
101 private FileTypes filetypes;
103 private static final String TYPE_METADATA_BAD_INTERNAL_REF = "metadata-bad-internal-ref";
105 private static final String TYPE_METADATA_WRITE_FAILURE = "metadata-write-failure";
107 private static final String TYPE_METADATA_IO = "metadata-io-warning";
109 private ManagedRepositoryContent repository;
111 private File repositoryDir;
113 private List<String> includes = new ArrayList<String>();
115 private long scanStartTimestamp = 0;
117 public String getDescription()
122 public String getId()
127 public void setIncludes( List<String> includes )
129 this.includes = includes;
132 public void beginScan( ManagedRepositoryConfiguration repoConfig, Date whenGathered )
133 throws ConsumerException
137 this.repository = repositoryFactory.getManagedRepositoryContent( repoConfig.getId() );
138 this.repositoryDir = new File( repository.getRepoRoot() );
139 this.scanStartTimestamp = System.currentTimeMillis();
141 catch ( RepositoryNotFoundException e )
143 throw new ConsumerException( e.getMessage(), e );
145 catch ( RepositoryException e )
147 throw new ConsumerException( e.getMessage(), e );
151 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
152 throws ConsumerException
154 beginScan( repository, whenGathered );
157 public void completeScan()
159 /* do nothing here */
162 public void completeScan( boolean executeOnEntireRepo )
167 public List<String> getExcludes()
169 return getDefaultArtifactExclusions();
172 public List<String> getIncludes()
174 return this.includes;
177 public void processFile( String path )
178 throws ConsumerException
180 // Ignore paths like .index etc
181 if ( !path.startsWith( "." ) )
185 ArtifactReference artifact = repository.toArtifactReference( path );
186 updateVersionMetadata( artifact, path );
187 updateProjectMetadata( artifact, path );
189 catch ( LayoutException e )
191 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 File projectMetadata = new File( this.repositoryDir, metadataPath );
214 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= 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 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
227 + "] to an internal project reference: " + e.getMessage() );
229 catch ( RepositoryMetadataException e )
231 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write project metadata for artifact [" + path
232 + "]: " + e.getMessage() );
234 catch ( IOException e )
236 triggerConsumerWarning( TYPE_METADATA_IO, "Project metadata not written due to IO warning: "
239 catch ( ContentNotFoundException e )
241 triggerConsumerWarning( TYPE_METADATA_IO,
242 "Project metadata not written because no versions were found to update: "
247 private void updateVersionMetadata( ArtifactReference artifact, String path )
249 VersionedReference versionRef = new VersionedReference();
250 versionRef.setGroupId( artifact.getGroupId() );
251 versionRef.setArtifactId( artifact.getArtifactId() );
252 versionRef.setVersion( artifact.getVersion() );
256 String metadataPath = this.metadataTools.toPath( versionRef );
258 File projectMetadata = new File( this.repositoryDir, metadataPath );
260 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
262 // This metadata is up to date. skip it.
263 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( versionRef ) );
267 metadataTools.updateMetadata( this.repository, versionRef );
268 log.debug( "Updated metadata: {}", this.metadataTools.toPath( versionRef ) );
270 catch ( LayoutException e )
272 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
273 + "] to an internal version reference: " + e.getMessage() );
275 catch ( RepositoryMetadataException e )
277 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write version metadata for artifact [" + path
278 + "]: " + e.getMessage() );
280 catch ( IOException e )
282 triggerConsumerWarning( TYPE_METADATA_IO, "Version metadata not written due to IO warning: "
285 catch ( ContentNotFoundException e )
287 triggerConsumerWarning( TYPE_METADATA_IO,
288 "Version metadata not written because no versions were found to update: "
293 public boolean isPermanent()
298 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
300 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
306 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
308 /* do nothing here */
311 private void initIncludes()
315 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
319 public void initialize()
321 configuration.addChangeListener( this );