001package org.apache.archiva.consumers.metadata; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.admin.model.beans.ManagedRepository; 023import org.apache.archiva.common.utils.VersionUtil; 024import org.apache.archiva.configuration.ArchivaConfiguration; 025import org.apache.archiva.configuration.ConfigurationNames; 026import org.apache.archiva.configuration.FileTypes; 027import org.apache.archiva.consumers.AbstractMonitoredConsumer; 028import org.apache.archiva.consumers.ConsumerException; 029import org.apache.archiva.consumers.KnownRepositoryContentConsumer; 030import org.apache.archiva.metadata.model.ArtifactMetadata; 031import org.apache.archiva.metadata.model.ProjectMetadata; 032import org.apache.archiva.metadata.model.ProjectVersionMetadata; 033import org.apache.archiva.metadata.repository.MetadataRepository; 034import org.apache.archiva.metadata.repository.MetadataRepositoryException; 035import org.apache.archiva.metadata.repository.RepositorySession; 036import org.apache.archiva.metadata.repository.RepositorySessionFactory; 037import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest; 038import org.apache.archiva.metadata.repository.storage.RepositoryStorage; 039import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException; 040import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException; 041import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException; 042import org.apache.archiva.redback.components.registry.Registry; 043import org.apache.archiva.redback.components.registry.RegistryListener; 044import org.slf4j.Logger; 045import org.slf4j.LoggerFactory; 046import org.springframework.context.annotation.Scope; 047import org.springframework.stereotype.Service; 048 049import javax.annotation.PostConstruct; 050import javax.inject.Inject; 051import javax.inject.Named; 052import java.util.ArrayList; 053import java.util.Date; 054import java.util.List; 055 056/** 057 * Take an artifact off of disk and put it into the metadata repository. 058 */ 059@Service ("knownRepositoryContentConsumer#create-archiva-metadata") 060@Scope ("prototype") 061public class ArchivaMetadataCreationConsumer 062 extends AbstractMonitoredConsumer 063 implements KnownRepositoryContentConsumer, RegistryListener 064{ 065 private String id = "create-archiva-metadata"; 066 067 private String description = "Create basic metadata for Archiva to be able to reference the artifact"; 068 069 @Inject 070 private ArchivaConfiguration configuration; 071 072 @Inject 073 private FileTypes filetypes; 074 075 private Date whenGathered; 076 077 private List<String> includes = new ArrayList<>( 0 ); 078 079 /** 080 * FIXME: this could be multiple implementations and needs to be configured. 081 */ 082 @Inject 083 private RepositorySessionFactory repositorySessionFactory; 084 085 /** 086 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a 087 * factory. 088 */ 089 @Inject 090 @Named (value = "repositoryStorage#maven2") 091 private RepositoryStorage repositoryStorage; 092 093 private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class ); 094 095 private String repoId; 096 097 @Override 098 public String getId() 099 { 100 return this.id; 101 } 102 103 @Override 104 public String getDescription() 105 { 106 return this.description; 107 } 108 109 @Override 110 public List<String> getExcludes() 111 { 112 return getDefaultArtifactExclusions(); 113 } 114 115 @Override 116 public List<String> getIncludes() 117 { 118 return this.includes; 119 } 120 121 @Override 122 public void beginScan( ManagedRepository repo, Date whenGathered ) 123 throws ConsumerException 124 { 125 repoId = repo.getId(); 126 this.whenGathered = whenGathered; 127 } 128 129 @Override 130 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo ) 131 throws ConsumerException 132 { 133 beginScan( repository, whenGathered ); 134 } 135 136 @Override 137 public void processFile( String path ) 138 throws ConsumerException 139 { 140 141 RepositorySession repositorySession = repositorySessionFactory.createSession(); 142 try 143 { 144 // note that we do minimal processing including checksums and POM information for performance of 145 // the initial scan. Any request for this information will be intercepted and populated on-demand 146 // or picked up by subsequent scans 147 148 ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path ); 149 150 ProjectMetadata project = new ProjectMetadata(); 151 project.setNamespace( artifact.getNamespace() ); 152 project.setId( artifact.getProject() ); 153 154 String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() ); 155 156 MetadataRepository metadataRepository = repositorySession.getRepository(); 157 158 boolean createVersionMetadata = false; 159 160 // FIXME: maybe not too efficient since it may have already been read and stored for this artifact 161 ProjectVersionMetadata versionMetadata = null; 162 try 163 { 164 ReadMetadataRequest readMetadataRequest = 165 new ReadMetadataRequest().repositoryId( repoId ).namespace( artifact.getNamespace() ).projectId( 166 artifact.getProject() ).projectVersion( projectVersion ); 167 versionMetadata = repositoryStorage.readProjectVersionMetadata( readMetadataRequest ); 168 createVersionMetadata = true; 169 } 170 catch ( RepositoryStorageMetadataNotFoundException e ) 171 { 172 log.warn( "Missing or invalid POM for artifact:{} (repository:{}); creating empty metadata", path, 173 repoId ); 174 175 versionMetadata = new ProjectVersionMetadata(); 176 versionMetadata.setId( projectVersion ); 177 versionMetadata.setIncomplete( true ); 178 createVersionMetadata = true; 179 } 180 catch ( RepositoryStorageMetadataInvalidException e ) 181 { 182 log.warn( "Error occurred resolving POM for artifact:{} (repository:{}); message: {}", 183 new Object[]{ path, repoId, e.getMessage() } ); 184 } 185 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, 189 artifact ); 190 if ( createVersionMetadata ) 191 { 192 metadataRepository.updateProjectVersion( repoId, project.getNamespace(), project.getId(), 193 versionMetadata ); 194 } 195 metadataRepository.updateProject( repoId, project ); 196 repositorySession.save(); 197 } 198 catch ( MetadataRepositoryException e ) 199 { 200 log.warn( 201 "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}" , 202 path, repoId, e.getMessage(), e ); 203 repositorySession.revert(); 204 } 205 catch ( RepositoryStorageRuntimeException e ) 206 { 207 log.warn( 208 "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}", 209 path, repoId, e.getMessage(), e ); 210 repositorySession.revert(); 211 } 212 finally 213 { 214 repositorySession.close(); 215 } 216 } 217 218 @Override 219 public void processFile( String path, boolean executeOnEntireRepo ) 220 throws ConsumerException 221 { 222 processFile( path ); 223 } 224 225 @Override 226 public void completeScan() 227 { 228 /* do nothing */ 229 } 230 231 @Override 232 public void completeScan( boolean executeOnEntireRepo ) 233 { 234 completeScan(); 235 } 236 237 @Override 238 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) 239 { 240 if ( ConfigurationNames.isRepositoryScanning( propertyName ) ) 241 { 242 initIncludes(); 243 } 244 } 245 246 @Override 247 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) 248 { 249 /* do nothing */ 250 } 251 252 private void initIncludes() 253 { 254 includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) ); 255 } 256 257 @PostConstruct 258 public void initialize() 259 { 260 configuration.addChangeListener( this ); 261 262 initIncludes(); 263 } 264}