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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.admin.model.beans.ManagedRepository;
  21. import org.apache.archiva.common.utils.VersionUtil;
  22. import org.apache.archiva.configuration.ArchivaConfiguration;
  23. import org.apache.archiva.configuration.ConfigurationNames;
  24. import org.apache.archiva.configuration.FileTypes;
  25. import org.apache.archiva.consumers.AbstractMonitoredConsumer;
  26. import org.apache.archiva.consumers.ConsumerException;
  27. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  28. import org.apache.archiva.metadata.model.ArtifactMetadata;
  29. import org.apache.archiva.metadata.model.ProjectMetadata;
  30. import org.apache.archiva.metadata.model.ProjectVersionMetadata;
  31. import org.apache.archiva.metadata.repository.MetadataRepository;
  32. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  33. import org.apache.archiva.metadata.repository.RepositorySession;
  34. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  35. import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
  36. import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
  37. import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
  38. import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
  39. import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
  40. import org.apache.archiva.redback.components.registry.Registry;
  41. import org.apache.archiva.redback.components.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;
  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. public String getId()
  84. {
  85. return this.id;
  86. }
  87. public String getDescription()
  88. {
  89. return this.description;
  90. }
  91. public boolean isPermanent()
  92. {
  93. return true;
  94. }
  95. public List<String> getExcludes()
  96. {
  97. return getDefaultArtifactExclusions();
  98. }
  99. public List<String> getIncludes()
  100. {
  101. return this.includes;
  102. }
  103. public void beginScan( ManagedRepository repo, Date whenGathered )
  104. throws ConsumerException
  105. {
  106. repoId = repo.getId();
  107. this.whenGathered = whenGathered;
  108. }
  109. public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
  110. throws ConsumerException
  111. {
  112. beginScan( repository, whenGathered );
  113. }
  114. public void processFile( String path )
  115. throws ConsumerException
  116. {
  117. RepositorySession repositorySession = repositorySessionFactory.createSession();
  118. try
  119. {
  120. // note that we do minimal processing including checksums and POM information for performance of
  121. // the initial scan. Any request for this information will be intercepted and populated on-demand
  122. // or picked up by subsequent scans
  123. ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path );
  124. ProjectMetadata project = new ProjectMetadata();
  125. project.setNamespace( artifact.getNamespace() );
  126. project.setId( artifact.getProject() );
  127. String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
  128. MetadataRepository metadataRepository = repositorySession.getRepository();
  129. boolean createVersionMetadata = false;
  130. // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
  131. ProjectVersionMetadata versionMetadata = null;
  132. try
  133. {
  134. ReadMetadataRequest readMetadataRequest =
  135. new ReadMetadataRequest().repositoryId( repoId ).namespace( artifact.getNamespace() ).projectId(
  136. artifact.getProject() ).projectVersion( projectVersion );
  137. versionMetadata = repositoryStorage.readProjectVersionMetadata( readMetadataRequest );
  138. createVersionMetadata = true;
  139. }
  140. catch ( RepositoryStorageMetadataNotFoundException e )
  141. {
  142. log.warn( "Missing or invalid POM for artifact:{} (repository:{}); creating empty metadata", path,
  143. repoId );
  144. versionMetadata = new ProjectVersionMetadata();
  145. versionMetadata.setId( projectVersion );
  146. versionMetadata.setIncomplete( true );
  147. createVersionMetadata = true;
  148. }
  149. catch ( RepositoryStorageMetadataInvalidException e )
  150. {
  151. log.warn( "Error occurred resolving POM for artifact:{} (repository:{}); message: {}",
  152. new Object[]{ path, repoId, e.getMessage() } );
  153. }
  154. // read the metadata and update it if it is newer or doesn't exist
  155. artifact.setWhenGathered( whenGathered );
  156. metadataRepository.updateArtifact( repoId, project.getNamespace(), project.getId(), projectVersion,
  157. artifact );
  158. if ( createVersionMetadata )
  159. {
  160. metadataRepository.updateProjectVersion( repoId, project.getNamespace(), project.getId(),
  161. versionMetadata );
  162. }
  163. metadataRepository.updateProject( repoId, project );
  164. repositorySession.save();
  165. }
  166. catch ( MetadataRepositoryException e )
  167. {
  168. log.warn(
  169. "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}" ,
  170. path, repoId, e.getMessage(), e );
  171. repositorySession.revert();
  172. }
  173. catch ( RepositoryStorageRuntimeException e )
  174. {
  175. log.warn(
  176. "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}",
  177. path, repoId, e.getMessage(), e );
  178. repositorySession.revert();
  179. }
  180. finally
  181. {
  182. repositorySession.close();
  183. }
  184. }
  185. public void processFile( String path, boolean executeOnEntireRepo )
  186. throws ConsumerException
  187. {
  188. processFile( path );
  189. }
  190. public void completeScan()
  191. {
  192. /* do nothing */
  193. }
  194. public void completeScan( boolean executeOnEntireRepo )
  195. {
  196. completeScan();
  197. }
  198. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  199. {
  200. if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
  201. {
  202. initIncludes();
  203. }
  204. }
  205. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  206. {
  207. /* do nothing */
  208. }
  209. private void initIncludes()
  210. {
  211. includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
  212. }
  213. @PostConstruct
  214. public void initialize()
  215. {
  216. configuration.addChangeListener( this );
  217. initIncludes();
  218. }
  219. }