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.storage.StorageMetadataResolver;
35 import org.apache.maven.archiva.common.utils.VersionUtil;
36 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
37 import org.apache.maven.archiva.configuration.ConfigurationNames;
38 import org.apache.maven.archiva.configuration.FileTypes;
39 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
40 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
41 import org.apache.maven.archiva.consumers.ConsumerException;
42 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
43 import org.apache.maven.archiva.model.ArtifactReference;
44 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
45 import org.apache.maven.archiva.repository.layout.LayoutException;
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 * Take an artifact off of disk and put it into the metadata repository.
56 * @version $Id: ArtifactUpdateDatabaseConsumer.java 718864 2008-11-19 06:33:35Z brett $
57 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
58 * role-hint="create-archiva-metadata" instantiation-strategy="per-lookup"
60 public class ArchivaMetadataCreationConsumer
61 extends AbstractMonitoredConsumer
62 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
65 * @plexus.configuration default-value="create-archiva-metadata"
70 * @plexus.configuration default-value="Create basic metadata for Archiva to be able to reference the artifact"
72 private String description;
77 private ArchivaConfiguration configuration;
82 private FileTypes filetypes;
84 private Date whenGathered;
89 private ManagedRepositoryContent repository;
91 private List<String> includes = new ArrayList<String>();
96 private MetadataRepository metadataRepository;
99 * FIXME: this needs to be configurable based on storage type, and availability of proxy module
100 * ... could be a different type since we need methods to modify the storage metadata, which would also allow more
101 * appropriate methods to pass in the already determined repository configuration, for example, instead of the ID
103 * @plexus.requirement role-hint="maven2"
105 private StorageMetadataResolver storageResolver;
107 private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
109 public String getId()
114 public String getDescription()
116 return this.description;
119 public boolean isPermanent()
124 public List<String> getExcludes()
126 return getDefaultArtifactExclusions();
129 public List<String> getIncludes()
131 return this.includes;
134 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
135 throws ConsumerException
137 this.repository.setRepository( repo );
138 this.whenGathered = whenGathered;
141 public void processFile( String path )
142 throws ConsumerException
144 // note that we do minimal processing including checksums and POM information for performance of
145 // the initial scan. Any request for this information will be intercepted and populated on-demand
146 // or picked up by subsequent scans
147 ArtifactReference artifact;
150 artifact = repository.toArtifactReference( path );
152 catch ( LayoutException e )
154 throw new ConsumerException( e.getMessage(), e );
157 File file = new File( repository.getRepoRoot(), path );
159 ProjectMetadata project = new ProjectMetadata();
160 project.setNamespace( artifact.getGroupId() );
161 project.setId( artifact.getArtifactId() );
163 String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
164 // TODO: maybe not too efficient since it may have already been read and stored for this artifact
165 ProjectVersionMetadata versionMetadata =
166 storageResolver.getProjectVersion( repository.getId(), artifact.getGroupId(), artifact.getArtifactId(),
169 if ( versionMetadata == null )
171 log.warn( "Missing POM for artifact: " + artifact + "; creating empty metadata" );
172 versionMetadata = new ProjectVersionMetadata();
173 versionMetadata.setId( projectVersion );
176 ArtifactMetadata artifactMeta = new ArtifactMetadata();
177 artifactMeta.setRepositoryId( repository.getId() );
178 artifactMeta.setNamespace( artifact.getGroupId() );
179 artifactMeta.setProject( artifact.getArtifactId() );
180 artifactMeta.setId( file.getName() );
181 artifactMeta.setFileLastModified( file.lastModified() );
182 artifactMeta.setSize( file.length() );
183 artifactMeta.setVersion( artifact.getVersion() );
184 artifactMeta.setWhenGathered( whenGathered );
186 ChecksummedFile checksummedFile = new ChecksummedFile( file );
189 artifactMeta.setMd5( checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 ) );
191 catch ( IOException e )
193 log.error( "Error attempting to get MD5 checksum for " + file + ": " + e.getMessage() );
197 artifactMeta.setSha1( checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 ) );
199 catch ( IOException e )
201 log.error( "Error attempting to get SHA-1 checksum for " + file + ": " + e.getMessage() );
205 // read the metadata and update it if it is newer or doesn't exist
206 metadataRepository.updateArtifact( repository.getId(), project.getNamespace(), project.getId(), projectVersion,
208 metadataRepository.updateProjectVersion( repository.getId(), project.getNamespace(), project.getId(),
210 metadataRepository.updateProject( repository.getId(), project );
213 public void completeScan()
218 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
220 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
226 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
231 private void initIncludes()
235 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
238 public void initialize()
239 throws InitializationException
241 configuration.addChangeListener( this );