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.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.VersionUtil;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.ConfigurationNames;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
28 import org.apache.archiva.consumers.ConsumerException;
29 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
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.MetadataRepositoryException;
35 import org.apache.archiva.metadata.repository.RepositorySession;
36 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
37 import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
38 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
39 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
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 * default-value="create-archiva-metadata"
68 private String id = "create-archiva-metadata";
71 * 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";
78 private ArchivaConfiguration configuration;
83 private FileTypes filetypes;
85 private Date whenGathered;
87 private List<String> includes = new ArrayList<String>( 0 );
90 * FIXME: can be of other types
93 private RepositorySessionFactory repositorySessionFactory;
96 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
100 @Named( value = "repositoryStorage#maven2" )
101 private RepositoryStorage repositoryStorage;
103 private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
105 private String repoId;
107 public String getId()
112 public String getDescription()
114 return this.description;
117 public boolean isPermanent()
122 public List<String> getExcludes()
124 return getDefaultArtifactExclusions();
127 public List<String> getIncludes()
129 return this.includes;
132 public void beginScan( ManagedRepository repo, Date whenGathered )
133 throws ConsumerException
135 repoId = repo.getId();
136 this.whenGathered = whenGathered;
139 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
140 throws ConsumerException
142 beginScan( repository, whenGathered );
145 public void processFile( String path )
146 throws ConsumerException
148 // note that we do minimal processing including checksums and POM information for performance of
149 // the initial scan. Any request for this information will be intercepted and populated on-demand
150 // or picked up by subsequent scans
152 ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path );
154 ProjectMetadata project = new ProjectMetadata();
155 project.setNamespace( artifact.getNamespace() );
156 project.setId( artifact.getProject() );
158 String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
160 RepositorySession repositorySession = repositorySessionFactory.createSession();
163 MetadataRepository metadataRepository = repositorySession.getRepository();
165 boolean createVersionMetadata = false;
167 // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
168 ProjectVersionMetadata versionMetadata = null;
171 versionMetadata = repositoryStorage.readProjectVersionMetadata( repoId, artifact.getNamespace(),
172 artifact.getProject(), projectVersion );
173 createVersionMetadata = true;
175 catch ( RepositoryStorageMetadataNotFoundException e )
177 log.warn( "Missing or invalid POM for artifact: " + path + "; creating empty metadata" );
179 versionMetadata = new ProjectVersionMetadata();
180 versionMetadata.setId( projectVersion );
181 versionMetadata.setIncomplete( true );
182 createVersionMetadata = true;
184 catch ( RepositoryStorageMetadataInvalidException e )
186 log.warn( "Error occurred resolving POM for artifact: " + path + "; message: " + e.getMessage() );
189 // read the metadata and update it if it is newer or doesn't exist
190 artifact.setWhenGathered( whenGathered );
191 metadataRepository.updateArtifact( repoId, project.getNamespace(), project.getId(), projectVersion,
193 if ( createVersionMetadata )
195 metadataRepository.updateProjectVersion( repoId, project.getNamespace(), project.getId(),
198 metadataRepository.updateProject( repoId, project );
199 repositorySession.save();
201 catch ( MetadataRepositoryException e )
203 log.warn( "Error occurred persisting metadata for artifact: " + path + "; message: " + e.getMessage(), e );
204 repositorySession.revert();
208 repositorySession.close();
212 public void processFile( String path, boolean executeOnEntireRepo )
213 throws ConsumerException
218 public void completeScan()
223 public void completeScan( boolean executeOnEntireRepo )
228 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
230 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
236 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
241 private void initIncludes()
243 includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
247 public void initialize()
249 configuration.addChangeListener( this );