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.registry.Registry;
41 import org.codehaus.plexus.registry.RegistryListener;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.context.annotation.Scope;
45 import org.springframework.stereotype.Service;
47 import javax.annotation.PostConstruct;
48 import javax.inject.Inject;
49 import javax.inject.Named;
50 import java.util.ArrayList;
51 import java.util.Date;
52 import java.util.List;
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 $
59 @Service("knownRepositoryContentConsumer#create-archiva-metadata")
61 public class ArchivaMetadataCreationConsumer
62 extends AbstractMonitoredConsumer
63 implements KnownRepositoryContentConsumer, RegistryListener
66 * plexus.configuration default-value="create-archiva-metadata"
68 private String id = "create-archiva-metadata";
71 * plexus.configuration default-value="Create basic metadata for Archiva to be able to reference the artifact"
73 private String description = "Create basic metadata for Archiva to be able to reference the artifact";
79 private ArchivaConfiguration configuration;
85 private FileTypes filetypes;
87 private Date whenGathered;
89 private List<String> includes = new ArrayList<String>();
92 * FIXME: can be of other types
97 private RepositorySessionFactory repositorySessionFactory;
100 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
103 * plexus.requirement role-hint="maven2"
106 @Named(value = "repositoryStorage#maven2")
107 private RepositoryStorage repositoryStorage;
109 private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
111 private String repoId;
113 public String getId()
118 public String getDescription()
120 return this.description;
123 public boolean isPermanent()
128 public List<String> getExcludes()
130 return getDefaultArtifactExclusions();
133 public List<String> getIncludes()
135 return this.includes;
138 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
139 throws ConsumerException
141 repoId = repo.getId();
142 this.whenGathered = whenGathered;
145 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
146 throws ConsumerException
148 beginScan( repository, whenGathered );
151 public void processFile( String path )
152 throws ConsumerException
154 // note that we do minimal processing including checksums and POM information for performance of
155 // the initial scan. Any request for this information will be intercepted and populated on-demand
156 // or picked up by subsequent scans
158 ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path );
160 ProjectMetadata project = new ProjectMetadata();
161 project.setNamespace( artifact.getNamespace() );
162 project.setId( artifact.getProject() );
164 String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
166 RepositorySession repositorySession = repositorySessionFactory.createSession();
169 MetadataRepository metadataRepository = repositorySession.getRepository();
171 boolean createVersionMetadata = false;
173 // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
174 ProjectVersionMetadata versionMetadata = null;
177 versionMetadata = repositoryStorage.readProjectVersionMetadata( repoId, artifact.getNamespace(),
178 artifact.getProject(), projectVersion );
179 createVersionMetadata = true;
181 catch ( RepositoryStorageMetadataNotFoundException e )
183 log.warn( "Missing or invalid POM for artifact: " + path + "; creating empty metadata" );
185 versionMetadata = new ProjectVersionMetadata();
186 versionMetadata.setId( projectVersion );
187 versionMetadata.setIncomplete( true );
188 createVersionMetadata = true;
190 catch ( RepositoryStorageMetadataInvalidException e )
192 log.warn( "Error occurred resolving POM for artifact: " + path + "; message: " + e.getMessage() );
195 // read the metadata and update it if it is newer or doesn't exist
196 artifact.setWhenGathered( whenGathered );
197 metadataRepository.updateArtifact( repoId, project.getNamespace(), project.getId(), projectVersion,
199 if ( createVersionMetadata )
201 metadataRepository.updateProjectVersion( repoId, project.getNamespace(), project.getId(),
204 metadataRepository.updateProject( repoId, project );
205 repositorySession.save();
207 catch ( MetadataRepositoryException e )
209 log.warn( "Error occurred persisting metadata for artifact: " + path + "; message: " + e.getMessage(), e );
210 repositorySession.revert();
214 repositorySession.close();
218 public void processFile( String path, boolean executeOnEntireRepo )
219 throws ConsumerException
224 public void completeScan()
229 public void completeScan( boolean executeOnEntireRepo )
234 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
236 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
242 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
247 private void initIncludes()
251 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
255 public void initialize()
257 configuration.addChangeListener( this );