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<String>( 0 );
114 private long scanStartTimestamp = 0;
116 public String getDescription()
121 public String getId()
126 public void setIncludes( List<String> includes )
128 this.includes = includes;
131 public void beginScan( ManagedRepository repoConfig, Date whenGathered )
132 throws ConsumerException
136 this.repository = repositoryFactory.getManagedRepositoryContent( repoConfig.getId() );
137 this.repositoryDir = new File( repository.getRepoRoot() );
138 this.scanStartTimestamp = System.currentTimeMillis();
140 catch ( RepositoryNotFoundException e )
142 throw new ConsumerException( e.getMessage(), e );
144 catch ( RepositoryException e )
146 throw new ConsumerException( e.getMessage(), e );
150 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
151 throws ConsumerException
153 beginScan( repository, whenGathered );
156 public void completeScan()
158 /* do nothing here */
161 public void completeScan( boolean executeOnEntireRepo )
166 public List<String> getExcludes()
168 return getDefaultArtifactExclusions();
171 public List<String> getIncludes()
173 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() + ")" );
195 public void processFile( String path, boolean executeOnEntireRepo )
201 private void updateProjectMetadata( ArtifactReference artifact, String path )
203 ProjectReference projectRef = new ProjectReference();
204 projectRef.setGroupId( artifact.getGroupId() );
205 projectRef.setArtifactId( artifact.getArtifactId() );
209 String metadataPath = this.metadataTools.toPath( projectRef );
211 File projectMetadata = new File( this.repositoryDir, metadataPath );
213 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
215 // This metadata is up to date. skip it.
216 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( projectRef ) );
220 metadataTools.updateMetadata( this.repository, projectRef );
221 log.debug( "Updated metadata: {}", this.metadataTools.toPath( projectRef ) );
223 catch ( LayoutException e )
225 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF,
226 "Unable to convert path [" + path + "] to an internal project reference: "
229 catch ( RepositoryMetadataException e )
231 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE,
232 "Unable to write project metadata for artifact [" + path + "]: " + e.getMessage() );
234 catch ( IOException e )
236 triggerConsumerWarning( TYPE_METADATA_IO,
237 "Project metadata not written due to IO warning: " + e.getMessage() );
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,
273 "Unable to convert path [" + path + "] to an internal version reference: "
276 catch ( RepositoryMetadataException e )
278 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE,
279 "Unable to write version metadata for artifact [" + path + "]: " + e.getMessage() );
281 catch ( IOException e )
283 triggerConsumerWarning( TYPE_METADATA_IO,
284 "Version metadata not written due to IO warning: " + e.getMessage() );
286 catch ( ContentNotFoundException e )
288 triggerConsumerWarning( TYPE_METADATA_IO,
289 "Version metadata not written because no versions were found to update: "
294 public boolean isPermanent()
299 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
301 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
307 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
309 /* do nothing here */
312 private void initIncludes()
314 includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
318 public void initialize()
320 configuration.addChangeListener( this );