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.RepositorySession;
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
29 import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
30 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
31 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
32 import org.apache.maven.archiva.common.utils.VersionUtil;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.ConfigurationNames;
35 import org.apache.maven.archiva.configuration.FileTypes;
36 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
37 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
38 import org.apache.maven.archiva.consumers.ConsumerException;
39 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
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;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import java.util.ArrayList;
48 import java.util.Date;
49 import java.util.List;
52 * Take an artifact off of disk and put it into the metadata repository.
54 * @version $Id: ArtifactUpdateDatabaseConsumer.java 718864 2008-11-19 06:33:35Z brett $
55 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
56 * role-hint="create-archiva-metadata" instantiation-strategy="per-lookup"
58 public class ArchivaMetadataCreationConsumer
59 extends AbstractMonitoredConsumer
60 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
63 * @plexus.configuration default-value="create-archiva-metadata"
68 * @plexus.configuration default-value="Create basic metadata for Archiva to be able to reference the artifact"
70 private String description;
75 private ArchivaConfiguration configuration;
80 private FileTypes filetypes;
82 private Date whenGathered;
84 private List<String> includes = new ArrayList<String>();
87 * FIXME: can be of other types
91 private RepositorySessionFactory repositorySessionFactory;
94 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
97 * @plexus.requirement role-hint="maven2"
99 private RepositoryStorage repositoryStorage;
101 private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
103 private String repoId;
105 public String getId()
110 public String getDescription()
112 return this.description;
115 public boolean isPermanent()
120 public List<String> getExcludes()
122 return getDefaultArtifactExclusions();
125 public List<String> getIncludes()
127 return this.includes;
130 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
131 throws ConsumerException
133 repoId = repo.getId();
134 this.whenGathered = whenGathered;
137 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
138 throws ConsumerException
140 beginScan( repository, whenGathered );
143 public void processFile( String path )
144 throws ConsumerException
146 // note that we do minimal processing including checksums and POM information for performance of
147 // the initial scan. Any request for this information will be intercepted and populated on-demand
148 // or picked up by subsequent scans
150 ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path );
152 ProjectMetadata project = new ProjectMetadata();
153 project.setNamespace( artifact.getNamespace() );
154 project.setId( artifact.getProject() );
156 String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
158 RepositorySession repositorySession = repositorySessionFactory.createSession();
161 MetadataRepository metadataRepository = repositorySession.getRepository();
163 boolean createVersionMetadata = false;
165 // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
166 ProjectVersionMetadata versionMetadata = null;
169 versionMetadata = repositoryStorage.readProjectVersionMetadata( repoId, artifact.getNamespace(),
170 artifact.getProject(), projectVersion );
172 catch ( RepositoryStorageMetadataNotFoundException e )
174 log.warn( "Missing or invalid POM for artifact: " + path + "; creating empty metadata" );
176 versionMetadata = new ProjectVersionMetadata();
177 versionMetadata.setId( projectVersion );
178 versionMetadata.setIncomplete( true );
179 createVersionMetadata = true;
181 catch ( RepositoryStorageMetadataInvalidException e )
183 log.warn( "Error occurred resolving POM for artifact: " + path + "; message: " + e.getMessage() );
186 // read the metadata and update it if it is newer or doesn't exist
187 artifact.setWhenGathered( whenGathered );
188 metadataRepository.updateArtifact( repoId, project.getNamespace(), project.getId(), projectVersion,
190 if ( createVersionMetadata )
192 metadataRepository.updateProjectVersion( repoId, project.getNamespace(), project.getId(),
195 metadataRepository.updateProject( repoId, project );
196 repositorySession.save();
198 catch ( MetadataRepositoryException e )
200 log.warn( "Error occurred persisting metadata for artifact: " + path + "; message: " + e.getMessage(), e );
201 repositorySession.revert();
205 repositorySession.close();
209 public void processFile( String path, boolean executeOnEntireRepo )
210 throws ConsumerException
215 public void completeScan()
220 public void completeScan( boolean executeOnEntireRepo )
225 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
227 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
233 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
238 private void initIncludes()
242 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
245 public void initialize()
246 throws InitializationException
248 configuration.addChangeListener( this );