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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.ArchivaConfiguration;
  22. import org.apache.archiva.configuration.ConfigurationNames;
  23. import org.apache.archiva.configuration.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.MetadataRepository;
  31. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  32. import org.apache.archiva.metadata.repository.RepositorySession;
  33. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  34. import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
  35. import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
  36. import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
  37. import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
  38. import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
  39. import org.apache.archiva.redback.components.registry.Registry;
  40. import org.apache.archiva.redback.components.registry.RegistryListener;
  41. import org.apache.archiva.repository.ManagedRepository;
  42. import org.slf4j.Logger;
  43. import org.slf4j.LoggerFactory;
  44. import org.springframework.context.annotation.Scope;
  45. import org.springframework.stereotype.Service;
  46. import javax.annotation.PostConstruct;
  47. import javax.inject.Inject;
  48. import javax.inject.Named;
  49. import java.util.ArrayList;
  50. import java.util.Date;
  51. import java.util.List;
  52. /**
  53. * Take an artifact off of disk and put it into the metadata repository.
  54. */
  55. @Service ("knownRepositoryContentConsumer#create-archiva-metadata")
  56. @Scope ("prototype")
  57. public class ArchivaMetadataCreationConsumer
  58. extends AbstractMonitoredConsumer
  59. implements KnownRepositoryContentConsumer, RegistryListener
  60. {
  61. private String id = "create-archiva-metadata";
  62. private String description = "Create basic metadata for Archiva to be able to reference the artifact";
  63. @Inject
  64. private ArchivaConfiguration configuration;
  65. @Inject
  66. private FileTypes filetypes;
  67. private Date whenGathered;
  68. private List<String> includes = new ArrayList<>( 0 );
  69. /**
  70. * FIXME: this could be multiple implementations and needs to be configured.
  71. */
  72. @Inject
  73. private RepositorySessionFactory repositorySessionFactory;
  74. /**
  75. * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
  76. * factory.
  77. */
  78. @Inject
  79. @Named (value = "repositoryStorage#maven2")
  80. private RepositoryStorage repositoryStorage;
  81. private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
  82. private String repoId;
  83. @Override
  84. public String getId()
  85. {
  86. return this.id;
  87. }
  88. @Override
  89. public String getDescription()
  90. {
  91. return this.description;
  92. }
  93. @Override
  94. public List<String> getExcludes()
  95. {
  96. return getDefaultArtifactExclusions();
  97. }
  98. @Override
  99. public List<String> getIncludes()
  100. {
  101. return this.includes;
  102. }
  103. @Override
  104. public void beginScan( ManagedRepository repo, Date whenGathered )
  105. throws ConsumerException
  106. {
  107. repoId = repo.getId();
  108. this.whenGathered = whenGathered;
  109. }
  110. @Override
  111. public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
  112. throws ConsumerException
  113. {
  114. beginScan( repository, whenGathered );
  115. }
  116. @Override
  117. public void processFile( String path )
  118. throws ConsumerException
  119. {
  120. RepositorySession repositorySession = repositorySessionFactory.createSession();
  121. try
  122. {
  123. // note that we do minimal processing including checksums and POM information for performance of
  124. // the initial scan. Any request for this information will be intercepted and populated on-demand
  125. // or picked up by subsequent scans
  126. ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path );
  127. ProjectMetadata project = new ProjectMetadata();
  128. project.setNamespace( artifact.getNamespace() );
  129. project.setId( artifact.getProject() );
  130. String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
  131. MetadataRepository metadataRepository = repositorySession.getRepository();
  132. boolean createVersionMetadata = false;
  133. // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
  134. ProjectVersionMetadata versionMetadata = null;
  135. try
  136. {
  137. ReadMetadataRequest readMetadataRequest =
  138. new ReadMetadataRequest().repositoryId( repoId ).namespace( artifact.getNamespace() ).projectId(
  139. artifact.getProject() ).projectVersion( projectVersion );
  140. versionMetadata = repositoryStorage.readProjectVersionMetadata( readMetadataRequest );
  141. createVersionMetadata = true;
  142. }
  143. catch ( RepositoryStorageMetadataNotFoundException e )
  144. {
  145. log.warn( "Missing or invalid POM for artifact:{} (repository:{}); creating empty metadata", path,
  146. repoId );
  147. versionMetadata = new ProjectVersionMetadata();
  148. versionMetadata.setId( projectVersion );
  149. versionMetadata.setIncomplete( true );
  150. createVersionMetadata = true;
  151. }
  152. catch ( RepositoryStorageMetadataInvalidException e )
  153. {
  154. log.warn( "Error occurred resolving POM for artifact:{} (repository:{}); message: {}",
  155. new Object[]{ path, repoId, e.getMessage() } );
  156. }
  157. // read the metadata and update it if it is newer or doesn't exist
  158. artifact.setWhenGathered( whenGathered );
  159. metadataRepository.updateArtifact( repoId, project.getNamespace(), project.getId(), projectVersion,
  160. artifact );
  161. if ( createVersionMetadata )
  162. {
  163. metadataRepository.updateProjectVersion( repoId, project.getNamespace(), project.getId(),
  164. versionMetadata );
  165. }
  166. metadataRepository.updateProject( repoId, project );
  167. repositorySession.save();
  168. }
  169. catch ( MetadataRepositoryException e )
  170. {
  171. log.warn(
  172. "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}" ,
  173. path, repoId, e.getMessage(), e );
  174. repositorySession.revert();
  175. }
  176. catch ( RepositoryStorageRuntimeException e )
  177. {
  178. log.warn(
  179. "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}",
  180. path, repoId, e.getMessage(), e );
  181. repositorySession.revert();
  182. }
  183. finally
  184. {
  185. repositorySession.close();
  186. }
  187. }
  188. @Override
  189. public void processFile( String path, boolean executeOnEntireRepo )
  190. throws ConsumerException
  191. {
  192. processFile( path );
  193. }
  194. @Override
  195. public void completeScan()
  196. {
  197. /* do nothing */
  198. }
  199. @Override
  200. public void completeScan( boolean executeOnEntireRepo )
  201. {
  202. completeScan();
  203. }
  204. @Override
  205. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  206. {
  207. if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
  208. {
  209. initIncludes();
  210. }
  211. }
  212. @Override
  213. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  214. {
  215. /* do nothing */
  216. }
  217. private void initIncludes()
  218. {
  219. includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
  220. }
  221. @PostConstruct
  222. public void initialize()
  223. {
  224. configuration.addChangeListener( this );
  225. initIncludes();
  226. }
  227. }