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.MetadataResolverException;
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;
170 storageResolver.getProjectVersion( repository.getId(), artifact.getGroupId(), artifact.getArtifactId(),
173 catch ( MetadataResolverException e )
175 throw new ConsumerException( e.getMessage(), e );
178 if ( versionMetadata == null )
180 log.warn( "Missing POM for artifact: " + artifact + "; creating empty metadata" );
181 versionMetadata = new ProjectVersionMetadata();
182 versionMetadata.setId( projectVersion );
185 ArtifactMetadata artifactMeta = new ArtifactMetadata();
186 artifactMeta.setRepositoryId( repository.getId() );
187 artifactMeta.setNamespace( artifact.getGroupId() );
188 artifactMeta.setProject( artifact.getArtifactId() );
189 artifactMeta.setId( file.getName() );
190 artifactMeta.setFileLastModified( file.lastModified() );
191 artifactMeta.setSize( file.length() );
192 artifactMeta.setVersion( artifact.getVersion() );
193 artifactMeta.setWhenGathered( whenGathered );
195 ChecksummedFile checksummedFile = new ChecksummedFile( file );
198 artifactMeta.setMd5( checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 ) );
200 catch ( IOException e )
202 log.error( "Error attempting to get MD5 checksum for " + file + ": " + e.getMessage() );
206 artifactMeta.setSha1( checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 ) );
208 catch ( IOException e )
210 log.error( "Error attempting to get SHA-1 checksum for " + file + ": " + e.getMessage() );
214 // read the metadata and update it if it is newer or doesn't exist
215 metadataRepository.updateArtifact( repository.getId(), project.getNamespace(), project.getId(), projectVersion,
217 metadataRepository.updateProjectVersion( repository.getId(), project.getNamespace(), project.getId(),
219 metadataRepository.updateProject( repository.getId(), project );
222 public void completeScan()
227 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
229 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
235 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
240 private void initIncludes()
244 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
247 public void initialize()
248 throws InitializationException
250 configuration.addChangeListener( this );