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
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.ProjectMetadata;
24 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.MetadataResolutionException;
28 import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
29 import org.apache.maven.archiva.common.utils.VersionUtil;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.ConfigurationNames;
32 import org.apache.maven.archiva.configuration.FileTypes;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
35 import org.apache.maven.archiva.consumers.ConsumerException;
36 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
38 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
39 import org.codehaus.plexus.registry.Registry;
40 import org.codehaus.plexus.registry.RegistryListener;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import java.util.ArrayList;
45 import java.util.Date;
46 import java.util.List;
49 * Take an artifact off of disk and put it into the metadata repository.
51 * @version $Id: ArtifactUpdateDatabaseConsumer.java 718864 2008-11-19 06:33:35Z brett $
52 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
53 * role-hint="create-archiva-metadata" instantiation-strategy="per-lookup"
55 public class ArchivaMetadataCreationConsumer
56 extends AbstractMonitoredConsumer
57 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
60 * @plexus.configuration default-value="create-archiva-metadata"
65 * @plexus.configuration default-value="Create basic metadata for Archiva to be able to reference the artifact"
67 private String description;
72 private ArchivaConfiguration configuration;
77 private FileTypes filetypes;
79 private Date whenGathered;
81 private List<String> includes = new ArrayList<String>();
86 private MetadataRepository metadataRepository;
89 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
92 * @plexus.requirement role-hint="maven2"
94 private RepositoryStorage repositoryStorage;
96 private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
98 private String repoId;
100 public String getId()
105 public String getDescription()
107 return this.description;
110 public boolean isPermanent()
115 public List<String> getExcludes()
117 return getDefaultArtifactExclusions();
120 public List<String> getIncludes()
122 return this.includes;
125 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
126 throws ConsumerException
128 repoId = repo.getId();
129 this.whenGathered = whenGathered;
132 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
133 throws ConsumerException
135 beginScan( repository, whenGathered );
138 public void processFile( String path )
139 throws ConsumerException
141 // note that we do minimal processing including checksums and POM information for performance of
142 // the initial scan. Any request for this information will be intercepted and populated on-demand
143 // or picked up by subsequent scans
145 ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path );
147 ProjectMetadata project = new ProjectMetadata();
148 project.setNamespace( artifact.getNamespace() );
149 project.setId( artifact.getProject() );
151 String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
152 // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
153 ProjectVersionMetadata versionMetadata = null;
156 versionMetadata = repositoryStorage.readProjectVersionMetadata( repoId, artifact.getNamespace(),
157 artifact.getProject(), projectVersion );
159 catch ( MetadataResolutionException e )
161 log.warn( "Error occurred resolving POM for artifact: " + path + "; message: " + e.getMessage() );
164 boolean createVersionMetadata = false;
165 if ( versionMetadata == null )
167 log.warn( "Missing or invalid POM for artifact: " + path + "; creating empty metadata" );
168 versionMetadata = new ProjectVersionMetadata();
169 versionMetadata.setId( projectVersion );
170 versionMetadata.setIncomplete( true );
171 createVersionMetadata = true;
176 // FIXME: transaction
177 // read the metadata and update it if it is newer or doesn't exist
178 artifact.setWhenGathered( whenGathered );
179 metadataRepository.updateArtifact( repoId, project.getNamespace(), project.getId(), projectVersion,
181 if ( createVersionMetadata )
183 metadataRepository.updateProjectVersion( repoId, project.getNamespace(), project.getId(),
186 metadataRepository.updateProject( repoId, project );
188 catch ( MetadataRepositoryException e )
190 log.warn( "Error occurred persisting metadata for artifact: " + path + "; message: " + e.getMessage(), e );
194 public void processFile( String path, boolean executeOnEntireRepo )
195 throws ConsumerException
200 public void completeScan()
205 public void completeScan( boolean executeOnEntireRepo )
210 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
212 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
218 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
223 private void initIncludes()
227 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
230 public void initialize()
231 throws InitializationException
233 configuration.addChangeListener( this );