You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaMetadataCreationConsumer.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package org.apache.archiva.consumers.metadata;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.utils.VersionUtil;
  21. import org.apache.archiva.configuration.provider.ArchivaConfiguration;
  22. import org.apache.archiva.configuration.model.ConfigurationNames;
  23. import org.apache.archiva.configuration.provider.FileTypes;
  24. import org.apache.archiva.consumers.AbstractMonitoredConsumer;
  25. import org.apache.archiva.consumers.ConsumerException;
  26. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  27. import org.apache.archiva.metadata.model.ArtifactMetadata;
  28. import org.apache.archiva.metadata.model.ProjectMetadata;
  29. import org.apache.archiva.metadata.model.ProjectVersionMetadata;
  30. import org.apache.archiva.metadata.repository.*;
  31. import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
  32. import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
  33. import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
  34. import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
  35. import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
  36. import org.apache.archiva.components.registry.Registry;
  37. import org.apache.archiva.components.registry.RegistryListener;
  38. import org.apache.archiva.repository.ManagedRepository;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import org.springframework.context.annotation.Scope;
  42. import org.springframework.stereotype.Service;
  43. import javax.annotation.PostConstruct;
  44. import javax.inject.Inject;
  45. import javax.inject.Named;
  46. import java.time.ZoneId;
  47. import java.time.ZonedDateTime;
  48. import java.util.ArrayList;
  49. import java.util.Date;
  50. import java.util.List;
  51. /**
  52. * Take an artifact off of disk and put it into the metadata repository.
  53. */
  54. @Service ("knownRepositoryContentConsumer#create-archiva-metadata")
  55. @Scope ("prototype")
  56. public class ArchivaMetadataCreationConsumer
  57. extends AbstractMonitoredConsumer
  58. implements KnownRepositoryContentConsumer, RegistryListener
  59. {
  60. private String id = "create-archiva-metadata";
  61. private String description = "Create basic metadata for Archiva to be able to reference the artifact";
  62. @Inject
  63. private ArchivaConfiguration configuration;
  64. @Inject
  65. private FileTypes filetypes;
  66. private ZonedDateTime whenGathered;
  67. private List<String> includes = new ArrayList<>( 0 );
  68. /**
  69. * FIXME: this could be multiple implementations and needs to be configured.
  70. */
  71. @Inject
  72. private RepositorySessionFactory repositorySessionFactory;
  73. /**
  74. * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
  75. * factory.
  76. */
  77. @Inject
  78. @Named (value = "repositoryStorage#maven2")
  79. private RepositoryStorage repositoryStorage;
  80. private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
  81. private String repoId;
  82. @Override
  83. public String getId()
  84. {
  85. return this.id;
  86. }
  87. @Override
  88. public String getDescription()
  89. {
  90. return this.description;
  91. }
  92. @Override
  93. public List<String> getExcludes()
  94. {
  95. return getDefaultArtifactExclusions();
  96. }
  97. @Override
  98. public List<String> getIncludes()
  99. {
  100. return this.includes;
  101. }
  102. @Override
  103. public void beginScan( ManagedRepository repo, Date whenGathered )
  104. throws ConsumerException
  105. {
  106. repoId = repo.getId();
  107. this.whenGathered = ZonedDateTime.ofInstant(whenGathered.toInstant(), ZoneId.of("GMT"));
  108. }
  109. @Override
  110. public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
  111. throws ConsumerException
  112. {
  113. beginScan( repository, whenGathered );
  114. }
  115. @Override
  116. public void processFile( String path )
  117. throws ConsumerException
  118. {
  119. RepositorySession repositorySession = null;
  120. try
  121. {
  122. repositorySession = repositorySessionFactory.createSession();
  123. }
  124. catch ( MetadataRepositoryException e )
  125. {
  126. e.printStackTrace( );
  127. }
  128. try
  129. {
  130. // note that we do minimal processing including checksums and POM information for performance of
  131. // the initial scan. Any request for this information will be intercepted and populated on-demand
  132. // or picked up by subsequent scans
  133. ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path );
  134. ProjectMetadata project = new ProjectMetadata();
  135. project.setNamespace( artifact.getNamespace() );
  136. project.setId( artifact.getProject() );
  137. String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
  138. MetadataRepository metadataRepository = repositorySession.getRepository();
  139. boolean createVersionMetadata = false;
  140. // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
  141. ProjectVersionMetadata versionMetadata = null;
  142. try
  143. {
  144. ReadMetadataRequest readMetadataRequest =
  145. new ReadMetadataRequest().repositoryId( repoId ).namespace( artifact.getNamespace() ).projectId(
  146. artifact.getProject() ).projectVersion( projectVersion );
  147. versionMetadata = repositoryStorage.readProjectVersionMetadata( readMetadataRequest );
  148. createVersionMetadata = true;
  149. }
  150. catch ( RepositoryStorageMetadataNotFoundException e )
  151. {
  152. log.warn( "Missing or invalid POM for artifact:{} (repository:{}); creating empty metadata", path,
  153. repoId );
  154. versionMetadata = new ProjectVersionMetadata();
  155. versionMetadata.setId( projectVersion );
  156. versionMetadata.setIncomplete( true );
  157. createVersionMetadata = true;
  158. }
  159. catch ( RepositoryStorageMetadataInvalidException e )
  160. {
  161. log.warn( "Error occurred resolving POM for artifact:{} (repository:{}); message: {}",
  162. new Object[]{ path, repoId, e.getMessage() } );
  163. }
  164. // read the metadata and update it if it is newer or doesn't exist
  165. artifact.setWhenGathered( whenGathered );
  166. metadataRepository.updateArtifact(repositorySession , repoId, project.getNamespace(), project.getId(),
  167. projectVersion, artifact );
  168. if ( createVersionMetadata )
  169. {
  170. metadataRepository.updateProjectVersion(repositorySession , repoId, project.getNamespace(),
  171. project.getId(), versionMetadata );
  172. }
  173. metadataRepository.updateProject(repositorySession , repoId, project );
  174. repositorySession.save();
  175. }
  176. catch ( MetadataRepositoryException e )
  177. {
  178. log.warn(
  179. "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}" ,
  180. path, repoId, e.getMessage(), e );
  181. try {
  182. repositorySession.revert();
  183. } catch (MetadataSessionException ex) {
  184. log.error("Reverting failed {}", ex.getMessage());
  185. }
  186. }
  187. catch ( RepositoryStorageRuntimeException e )
  188. {
  189. log.warn(
  190. "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}",
  191. path, repoId, e.getMessage(), e );
  192. try {
  193. repositorySession.revert();
  194. } catch (MetadataSessionException ex) {
  195. log.error("Reverting failed {}", ex.getMessage());
  196. }
  197. } catch (MetadataSessionException e) {
  198. throw new ConsumerException(e.getMessage(), e);
  199. } finally
  200. {
  201. repositorySession.close();
  202. }
  203. }
  204. @Override
  205. public void processFile( String path, boolean executeOnEntireRepo )
  206. throws ConsumerException
  207. {
  208. processFile( path );
  209. }
  210. @Override
  211. public void completeScan()
  212. {
  213. /* do nothing */
  214. }
  215. @Override
  216. public void completeScan( boolean executeOnEntireRepo )
  217. {
  218. completeScan();
  219. }
  220. @Override
  221. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  222. {
  223. if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
  224. {
  225. initIncludes();
  226. }
  227. }
  228. @Override
  229. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  230. {
  231. /* do nothing */
  232. }
  233. private void initIncludes()
  234. {
  235. includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
  236. }
  237. @PostConstruct
  238. public void initialize()
  239. {
  240. configuration.addChangeListener( this );
  241. initIncludes();
  242. }
  243. }