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.personality.plexus.lifecycle.phase.Initializable;
47 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
48 import org.codehaus.plexus.registry.Registry;
49 import org.codehaus.plexus.registry.RegistryListener;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * MetadataUpdaterConsumer will create and update the metadata present within the repository.
57 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
58 * role-hint="metadata-updater"
59 * instantiation-strategy="per-lookup"
61 public class MetadataUpdaterConsumer
62 extends AbstractMonitoredConsumer
63 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
65 private Logger log = LoggerFactory.getLogger( MetadataUpdaterConsumer.class );
68 * @plexus.configuration default-value="metadata-updater"
73 * @plexus.configuration default-value="Update / Create maven-metadata.xml files"
75 private String description;
80 private RepositoryContentFactory repositoryFactory;
85 private MetadataTools metadataTools;
90 private ArchivaConfiguration configuration;
95 private FileTypes filetypes;
97 private static final String TYPE_METADATA_BAD_INTERNAL_REF = "metadata-bad-internal-ref";
99 private static final String TYPE_METADATA_WRITE_FAILURE = "metadata-write-failure";
101 private static final String TYPE_METADATA_IO = "metadata-io-warning";
103 private ManagedRepositoryContent repository;
105 private File repositoryDir;
107 private List<String> includes = new ArrayList<String>();
109 private long scanStartTimestamp = 0;
111 public String getDescription()
116 public String getId()
121 public void setIncludes( List<String> includes )
123 this.includes = includes;
126 public void beginScan( ManagedRepositoryConfiguration repoConfig, Date whenGathered )
127 throws ConsumerException
131 this.repository = repositoryFactory.getManagedRepositoryContent( repoConfig.getId() );
132 this.repositoryDir = new File( repository.getRepoRoot() );
133 this.scanStartTimestamp = System.currentTimeMillis();
135 catch ( RepositoryNotFoundException e )
137 throw new ConsumerException( e.getMessage(), e );
139 catch ( RepositoryException e )
141 throw new ConsumerException( e.getMessage(), e );
145 public void completeScan()
147 /* do nothing here */
150 public List<String> getExcludes()
152 return getDefaultArtifactExclusions();
155 public List<String> getIncludes()
157 return this.includes;
160 public void processFile( String path )
161 throws ConsumerException
163 //Ignore paths like .indexer etc
164 if (path.startsWith("."))
169 ArtifactReference artifact = repository.toArtifactReference( path );
170 updateVersionMetadata( artifact, path );
171 updateProjectMetadata( artifact, path );
173 catch ( LayoutException e )
175 throw new ConsumerException( "Unable to convert to artifact reference: " + path, e );
179 private void updateProjectMetadata( ArtifactReference artifact, String path )
181 ProjectReference projectRef = new ProjectReference();
182 projectRef.setGroupId( artifact.getGroupId() );
183 projectRef.setArtifactId( artifact.getArtifactId() );
187 String metadataPath = this.metadataTools.toPath( projectRef );
189 File projectMetadata = new File( this.repositoryDir, metadataPath );
191 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
193 // This metadata is up to date. skip it.
194 log.debug( "Skipping uptodate metadata: " + this.metadataTools.toPath( projectRef ) );
198 metadataTools.updateMetadata( this.repository, projectRef );
199 log.debug( "Updated metadata: " + this.metadataTools.toPath( projectRef ) );
201 catch ( LayoutException e )
203 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
204 + "] to an internal project reference: " + e.getMessage() );
206 catch ( RepositoryMetadataException e )
208 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write project metadata for artifact [" + path
209 + "]: " + e.getMessage() );
211 catch ( IOException e )
213 triggerConsumerWarning( TYPE_METADATA_IO, "Project metadata not written due to IO warning: "
216 catch ( ContentNotFoundException e )
218 triggerConsumerWarning( TYPE_METADATA_IO,
219 "Project metadata not written because no versions were found to update: "
224 private void updateVersionMetadata( ArtifactReference artifact, String path )
226 VersionedReference versionRef = new VersionedReference();
227 versionRef.setGroupId( artifact.getGroupId() );
228 versionRef.setArtifactId( artifact.getArtifactId() );
229 versionRef.setVersion( artifact.getVersion() );
233 String metadataPath = this.metadataTools.toPath( versionRef );
235 File projectMetadata = new File( this.repositoryDir, metadataPath );
237 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
239 // This metadata is up to date. skip it.
240 log.debug( "Skipping uptodate metadata: " + this.metadataTools.toPath( versionRef ) );
244 metadataTools.updateMetadata( this.repository, versionRef );
245 log.debug( "Updated metadata: " + this.metadataTools.toPath( versionRef ) );
247 catch ( LayoutException e )
249 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
250 + "] to an internal version reference: " + e.getMessage() );
252 catch ( RepositoryMetadataException e )
254 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write version metadata for artifact [" + path
255 + "]: " + e.getMessage() );
257 catch ( IOException e )
259 triggerConsumerWarning( TYPE_METADATA_IO, "Version metadata not written due to IO warning: "
262 catch ( ContentNotFoundException e )
264 triggerConsumerWarning( TYPE_METADATA_IO,
265 "Version metadata not written because no versions were found to update: "
270 public boolean isPermanent()
275 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
277 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
283 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
285 /* do nothing here */
288 private void initIncludes()
292 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
295 public void initialize()
296 throws InitializationException
298 configuration.addChangeListener( this );