1 package org.apache.archiva.consumers.metadata;
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.archiva.checksum.ChecksumAlgorithm;
29 import org.apache.archiva.checksum.ChecksummedFile;
30 import org.apache.archiva.metadata.model.ArtifactMetadata;
31 import org.apache.archiva.metadata.model.ProjectMetadata;
32 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
33 import org.apache.archiva.metadata.repository.MetadataRepository;
34 import org.apache.archiva.metadata.repository.MetadataResolutionException;
35 import org.apache.archiva.metadata.repository.storage.StorageMetadataResolver;
36 import org.apache.maven.archiva.common.utils.VersionUtil;
37 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
38 import org.apache.maven.archiva.configuration.ConfigurationNames;
39 import org.apache.maven.archiva.configuration.FileTypes;
40 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
41 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
42 import org.apache.maven.archiva.consumers.ConsumerException;
43 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
44 import org.apache.maven.archiva.model.ArtifactReference;
45 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
46 import org.apache.maven.archiva.repository.layout.LayoutException;
47 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
48 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
49 import org.codehaus.plexus.registry.Registry;
50 import org.codehaus.plexus.registry.RegistryListener;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * Take an artifact off of disk and put it into the metadata repository.
57 * @version $Id: ArtifactUpdateDatabaseConsumer.java 718864 2008-11-19 06:33:35Z brett $
58 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
59 * role-hint="create-archiva-metadata" instantiation-strategy="per-lookup"
61 public class ArchivaMetadataCreationConsumer
62 extends AbstractMonitoredConsumer
63 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
66 * @plexus.configuration default-value="create-archiva-metadata"
71 * @plexus.configuration default-value="Create basic metadata for Archiva to be able to reference the artifact"
73 private String description;
78 private ArchivaConfiguration configuration;
83 private FileTypes filetypes;
85 private Date whenGathered;
90 private ManagedRepositoryContent repository;
92 private List<String> includes = new ArrayList<String>();
97 private MetadataRepository metadataRepository;
100 * FIXME: this needs to be configurable based on storage type, and availability of proxy module
101 * ... could be a different type since we need methods to modify the storage metadata, which would also allow more
102 * appropriate methods to pass in the already determined repository configuration, for example, instead of the ID
104 * @plexus.requirement role-hint="maven2"
106 private StorageMetadataResolver storageResolver;
108 private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
110 public String getId()
115 public String getDescription()
117 return this.description;
120 public boolean isPermanent()
125 public List<String> getExcludes()
127 return getDefaultArtifactExclusions();
130 public List<String> getIncludes()
132 return this.includes;
135 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
136 throws ConsumerException
138 this.repository.setRepository( repo );
139 this.whenGathered = whenGathered;
142 public void processFile( String path )
143 throws ConsumerException
145 // note that we do minimal processing including checksums and POM information for performance of
146 // the initial scan. Any request for this information will be intercepted and populated on-demand
147 // or picked up by subsequent scans
148 ArtifactReference artifact;
151 artifact = repository.toArtifactReference( path );
153 catch ( LayoutException e )
155 throw new ConsumerException( e.getMessage(), e );
158 File file = new File( repository.getRepoRoot(), path );
160 ProjectMetadata project = new ProjectMetadata();
161 project.setNamespace( artifact.getGroupId() );
162 project.setId( artifact.getArtifactId() );
164 String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
165 // TODO: maybe not too efficient since it may have already been read and stored for this artifact
166 ProjectVersionMetadata versionMetadata = null;
170 storageResolver.getProjectVersion( repository.getId(), artifact.getGroupId(), artifact.getArtifactId(),
173 catch ( MetadataResolutionException e )
175 log.warn( "Error occurred resolving POM for artifact: " + path + "; message: " + e.getMessage() );
178 boolean createVersionMetadata = false;
179 if ( versionMetadata == null )
181 log.warn( "Missing or invalid POM for artifact: " + path + "; creating empty metadata" );
182 versionMetadata = new ProjectVersionMetadata();
183 versionMetadata.setId( projectVersion );
184 versionMetadata.setIncomplete( true );
185 createVersionMetadata = true;
188 ArtifactMetadata artifactMeta = new ArtifactMetadata();
189 artifactMeta.setRepositoryId( repository.getId() );
190 artifactMeta.setNamespace( artifact.getGroupId() );
191 artifactMeta.setProject( artifact.getArtifactId() );
192 artifactMeta.setId( file.getName() );
193 artifactMeta.setFileLastModified( file.lastModified() );
194 artifactMeta.setSize( file.length() );
195 artifactMeta.setVersion( artifact.getVersion() );
196 artifactMeta.setWhenGathered( whenGathered );
198 ChecksummedFile checksummedFile = new ChecksummedFile( file );
201 artifactMeta.setMd5( checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 ) );
203 catch ( IOException e )
205 log.error( "Error attempting to get MD5 checksum for " + file + ": " + e.getMessage() );
209 artifactMeta.setSha1( checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 ) );
211 catch ( IOException e )
213 log.error( "Error attempting to get SHA-1 checksum for " + file + ": " + e.getMessage() );
217 // read the metadata and update it if it is newer or doesn't exist
218 metadataRepository.updateArtifact( repository.getId(), project.getNamespace(), project.getId(), projectVersion,
220 if ( createVersionMetadata )
222 metadataRepository.updateProjectVersion( repository.getId(), project.getNamespace(), project.getId(),
225 metadataRepository.updateProject( repository.getId(), project );
228 public void completeScan()
233 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
235 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
241 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
246 private void initIncludes()
250 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
253 public void initialize()
254 throws InitializationException
256 configuration.addChangeListener( this );