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.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
41 import org.apache.archiva.redback.components.registry.Registry;
42 import org.apache.archiva.redback.components.registry.RegistryListener;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.context.annotation.Scope;
46 import org.springframework.stereotype.Service;
48 import javax.annotation.PostConstruct;
49 import javax.inject.Inject;
50 import javax.inject.Named;
51 import java.util.ArrayList;
52 import java.util.Date;
53 import java.util.List;
56 * Take an artifact off of disk and put it into the metadata repository.
60 @Service( "knownRepositoryContentConsumer#create-archiva-metadata" )
62 public class ArchivaMetadataCreationConsumer
63 extends AbstractMonitoredConsumer
64 implements KnownRepositoryContentConsumer, RegistryListener
67 * default-value="create-archiva-metadata"
69 private String id = "create-archiva-metadata";
72 * default-value="Create basic metadata for Archiva to be able to reference the artifact"
74 private String description = "Create basic metadata for Archiva to be able to reference the artifact";
79 private ArchivaConfiguration configuration;
84 private FileTypes filetypes;
86 private Date whenGathered;
88 private List<String> includes = new ArrayList<String>( 0 );
91 * FIXME: can be of other types
94 private RepositorySessionFactory repositorySessionFactory;
97 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
101 @Named( value = "repositoryStorage#maven2" )
102 private RepositoryStorage repositoryStorage;
104 private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
106 private String repoId;
108 public String getId()
113 public String getDescription()
115 return this.description;
118 public boolean isPermanent()
123 public List<String> getExcludes()
125 return getDefaultArtifactExclusions();
128 public List<String> getIncludes()
130 return this.includes;
133 public void beginScan( ManagedRepository repo, Date whenGathered )
134 throws ConsumerException
136 repoId = repo.getId();
137 this.whenGathered = whenGathered;
140 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
141 throws ConsumerException
143 beginScan( repository, whenGathered );
146 public void processFile( String path )
147 throws ConsumerException
150 RepositorySession repositorySession = repositorySessionFactory.createSession();
153 // note that we do minimal processing including checksums and POM information for performance of
154 // the initial scan. Any request for this information will be intercepted and populated on-demand
155 // or picked up by subsequent scans
157 ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path );
159 ProjectMetadata project = new ProjectMetadata();
160 project.setNamespace( artifact.getNamespace() );
161 project.setId( artifact.getProject() );
163 String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
165 MetadataRepository metadataRepository = repositorySession.getRepository();
167 boolean createVersionMetadata = false;
169 // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
170 ProjectVersionMetadata versionMetadata = null;
173 versionMetadata = repositoryStorage.readProjectVersionMetadata( repoId, artifact.getNamespace(),
174 artifact.getProject(), projectVersion );
175 createVersionMetadata = true;
177 catch ( RepositoryStorageMetadataNotFoundException e )
179 log.warn( "Missing or invalid POM for artifact: " + path + "; creating empty metadata" );
181 versionMetadata = new ProjectVersionMetadata();
182 versionMetadata.setId( projectVersion );
183 versionMetadata.setIncomplete( true );
184 createVersionMetadata = true;
186 catch ( RepositoryStorageMetadataInvalidException e )
188 log.warn( "Error occurred resolving POM for artifact: " + path + "; message: " + e.getMessage() );
191 // read the metadata and update it if it is newer or doesn't exist
192 artifact.setWhenGathered( whenGathered );
193 metadataRepository.updateArtifact( repoId, project.getNamespace(), project.getId(), projectVersion,
195 if ( createVersionMetadata )
197 metadataRepository.updateProjectVersion( repoId, project.getNamespace(), project.getId(),
200 metadataRepository.updateProject( repoId, project );
201 repositorySession.save();
203 catch ( MetadataRepositoryException e )
205 log.warn( "Error occurred persisting metadata for artifact: " + path + "; message: " + e.getMessage(), e );
206 repositorySession.revert();
208 catch ( RepositoryStorageRuntimeException e )
210 log.warn( "Error occurred persisting metadata for artifact: " + path + "; message: " + e.getMessage(), e );
211 repositorySession.revert();
215 repositorySession.close();
219 public void processFile( String path, boolean executeOnEntireRepo )
220 throws ConsumerException
225 public void completeScan()
230 public void completeScan( boolean executeOnEntireRepo )
235 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
237 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
243 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
248 private void initIncludes()
250 includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
254 public void initialize()
256 configuration.addChangeListener( this );