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
22 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.ConfigurationNames;
24 import org.apache.maven.archiva.configuration.FileTypes;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.maven.archiva.consumers.ConsumerException;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.maven.archiva.model.ArtifactReference;
30 import org.apache.maven.archiva.model.ProjectReference;
31 import org.apache.maven.archiva.model.VersionedReference;
32 import org.apache.maven.archiva.repository.ContentNotFoundException;
33 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
34 import org.apache.maven.archiva.repository.RepositoryContentFactory;
35 import org.apache.maven.archiva.repository.RepositoryException;
36 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
37 import org.apache.maven.archiva.repository.layout.LayoutException;
38 import org.apache.maven.archiva.repository.metadata.MetadataTools;
39 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
41 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
42 import org.codehaus.plexus.registry.Registry;
43 import org.codehaus.plexus.registry.RegistryListener;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.List;
51 * MetadataUpdaterConsumer will create and update the metadata present within the repository.
53 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
55 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
56 * role-hint="metadata-updater"
57 * instantiation-strategy="per-lookup"
59 public class MetadataUpdaterConsumer
60 extends AbstractMonitoredConsumer
61 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
64 * @plexus.configuration default-value="metadata-updater"
69 * @plexus.configuration default-value="Update / Create maven-metadata.xml files"
71 private String description;
76 private RepositoryContentFactory repositoryFactory;
81 private MetadataTools metadataTools;
86 private ArchivaConfiguration configuration;
91 private FileTypes filetypes;
93 private static final String TYPE_METADATA_BAD_INTERNAL_REF = "metadata-bad-internal-ref";
95 private static final String TYPE_METADATA_WRITE_FAILURE = "metadata-write-failure";
97 private static final String TYPE_METADATA_IO = "metadata-io-warning";
99 private ManagedRepositoryContent repository;
101 private File repositoryDir;
103 private List<String> includes = new ArrayList<String>();
105 private long scanStartTimestamp = 0;
107 public String getDescription()
112 public String getId()
117 public void setIncludes( List<String> includes )
119 this.includes = includes;
122 public void beginScan( ManagedRepositoryConfiguration repoConfig )
123 throws ConsumerException
127 this.repository = repositoryFactory.getManagedRepositoryContent( repoConfig.getId() );
128 this.repositoryDir = new File( repository.getRepoRoot() );
129 this.scanStartTimestamp = System.currentTimeMillis();
131 catch ( RepositoryNotFoundException e )
133 throw new ConsumerException( e.getMessage(), e );
135 catch ( RepositoryException e )
137 throw new ConsumerException( e.getMessage(), e );
141 public void completeScan()
143 /* do nothing here */
146 public List<String> getExcludes()
151 public List<String> getIncludes()
153 return this.includes;
156 public void processFile( String path )
157 throws ConsumerException
161 ArtifactReference artifact = repository.toArtifactReference( path );
162 updateVersionMetadata( artifact, path );
163 updateProjectMetadata( artifact, path );
165 catch ( LayoutException e )
167 throw new ConsumerException( "Unable to convert to artifact reference: " + path, e );
171 private void updateProjectMetadata( ArtifactReference artifact, String path )
173 ProjectReference projectRef = new ProjectReference();
174 projectRef.setGroupId( artifact.getGroupId() );
175 projectRef.setArtifactId( artifact.getArtifactId() );
179 String metadataPath = this.metadataTools.toPath( projectRef );
181 File projectMetadata = new File( this.repositoryDir, metadataPath );
183 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
185 // This metadata is up to date. skip it.
186 getLogger().debug( "Skipping uptodate metadata: " + this.metadataTools.toPath( projectRef ) );
190 metadataTools.updateMetadata( this.repository, projectRef );
191 getLogger().debug( "Updated metadata: " + this.metadataTools.toPath( projectRef ) );
193 catch ( LayoutException e )
195 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
196 + "] to an internal project reference: " + e.getMessage() );
198 catch ( RepositoryMetadataException e )
200 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write project metadata for artifact [" + path
201 + "]: " + e.getMessage() );
203 catch ( IOException e )
205 triggerConsumerWarning( TYPE_METADATA_IO, "Project metadata not written due to IO warning: "
208 catch ( ContentNotFoundException e )
210 triggerConsumerWarning( TYPE_METADATA_IO,
211 "Project metadata not written because no versions were found to update: "
216 private void updateVersionMetadata( ArtifactReference artifact, String path )
218 VersionedReference versionRef = new VersionedReference();
219 versionRef.setGroupId( artifact.getGroupId() );
220 versionRef.setArtifactId( artifact.getArtifactId() );
221 versionRef.setVersion( artifact.getVersion() );
225 String metadataPath = this.metadataTools.toPath( versionRef );
227 File projectMetadata = new File( this.repositoryDir, metadataPath );
229 if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
231 // This metadata is up to date. skip it.
232 getLogger().debug( "Skipping uptodate metadata: " + this.metadataTools.toPath( versionRef ) );
236 metadataTools.updateMetadata( this.repository, versionRef );
237 getLogger().debug( "Updated metadata: " + this.metadataTools.toPath( versionRef ) );
239 catch ( LayoutException e )
241 triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
242 + "] to an internal version reference: " + e.getMessage() );
244 catch ( RepositoryMetadataException e )
246 triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write version metadata for artifact [" + path
247 + "]: " + e.getMessage() );
249 catch ( IOException e )
251 triggerConsumerWarning( TYPE_METADATA_IO, "Version metadata not written due to IO warning: "
254 catch ( ContentNotFoundException e )
256 triggerConsumerWarning( TYPE_METADATA_IO,
257 "Version metadata not written because no versions were found to update: "
262 public boolean isPermanent()
267 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
269 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
275 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
277 /* do nothing here */
280 private void initIncludes()
284 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
287 public void initialize()
288 throws InitializationException
290 configuration.addChangeListener( this );