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.

SimpleArtifactConsumer.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package $package;
  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.components.registry.Registry;
  21. import org.apache.archiva.components.registry.RegistryListener;
  22. import org.apache.archiva.configuration.provider.ArchivaConfiguration;
  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.repository.MetadataRepositoryException;
  28. import org.apache.archiva.metadata.repository.MetadataResolutionException;
  29. import org.apache.archiva.metadata.repository.RepositorySession;
  30. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  31. import org.apache.archiva.repository.content.BaseRepositoryContentLayout;
  32. import org.apache.archiva.repository.content.LayoutException;
  33. import org.apache.archiva.repository.ManagedRepository;
  34. import org.apache.archiva.repository.ManagedRepositoryContent;
  35. import org.apache.archiva.repository.RepositoryContentFactory;
  36. import org.apache.archiva.repository.content.Artifact;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import org.springframework.context.annotation.Scope;
  40. import org.springframework.stereotype.Service;
  41. import javax.annotation.PostConstruct;
  42. import javax.inject.Inject;
  43. import javax.inject.Named;
  44. import java.util.ArrayList;
  45. import java.util.Date;
  46. import java.util.List;
  47. /**
  48. * <code>SimpleArtifactConsumer</code>
  49. *
  50. */
  51. @Service("knownRepositoryContentConsumer#simple")
  52. @Scope("prototype")
  53. public class SimpleArtifactConsumer
  54. extends AbstractMonitoredConsumer
  55. implements KnownRepositoryContentConsumer, RegistryListener
  56. {
  57. private Logger log = LoggerFactory.getLogger( SimpleArtifactConsumer.class );
  58. /**
  59. * default-value="simple-artifact-consumer"
  60. */
  61. private String id = "simple-artifact-consumer";
  62. private String description = "Simple consumer to illustrate how to consume the contents of a repository.";
  63. @Inject
  64. private FileTypes filetypes;
  65. @Inject
  66. private ArchivaConfiguration configuration;
  67. private List<String> propertyNameTriggers = new ArrayList<>();
  68. private List<String> includes = new ArrayList<>();
  69. /** current repository being scanned */
  70. private ManagedRepository repository;
  71. @Inject
  72. @Named( value = "repositoryContentFactory#default" )
  73. private RepositoryContentFactory repositoryContentFactory;
  74. @Inject
  75. private RepositorySessionFactory repositorySessionFactory;
  76. private RepositorySession repositorySession;
  77. public void beginScan( ManagedRepository repository, Date whenGathered )
  78. throws ConsumerException
  79. {
  80. beginScan( repository, whenGathered, true );
  81. }
  82. public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
  83. throws ConsumerException
  84. {
  85. this.repository = repository;
  86. log.info( "Beginning scan of repository [{}]", this.repository.getId() );
  87. try
  88. {
  89. repositorySession = repositorySessionFactory.createSession( );
  90. } catch (MetadataRepositoryException e) {
  91. log.error("Could not create repository session {}", e.getMessage());
  92. throw new ConsumerException( "Could not create repository session: " + e.getMessage( ), e );
  93. }
  94. }
  95. public void processFile( String path )
  96. throws ConsumerException
  97. {
  98. processFile( path, true );
  99. }
  100. public void processFile( String path, boolean executeOnEntireRepo )
  101. throws ConsumerException
  102. {
  103. log.info( "Processing entry [{}] from repository [{}]", path, this.repository.getId() );
  104. try
  105. {
  106. ManagedRepositoryContent repositoryContent = repository.getContent();
  107. BaseRepositoryContentLayout layout = repositoryContent.getLayout( BaseRepositoryContentLayout.class );
  108. Artifact artifact = layout.getArtifact( path );
  109. repositorySession.getRepository().getArtifacts( repositorySession, repository.getId(), artifact.getNamespace().getId(),
  110. artifact.getId(), artifact.getVersion().getId() );
  111. }
  112. catch ( LayoutException | MetadataResolutionException e )
  113. {
  114. throw new ConsumerException( e.getLocalizedMessage(), e );
  115. }
  116. }
  117. public void completeScan()
  118. {
  119. completeScan( true );
  120. }
  121. public void completeScan( boolean executeOnEntireRepo )
  122. {
  123. log.info( "Finished scan of repository [" + this.repository.getId() + "]" );
  124. repositorySession.close();
  125. }
  126. /**
  127. * Used by archiva to determine if the consumer wishes to process all of a repository's entries or just those that
  128. * have been modified since the last scan.
  129. *
  130. * @return boolean true if the consumer wishes to process all entries on each scan, false for only those modified
  131. * since the last scan
  132. */
  133. public boolean isProcessUnmodified()
  134. {
  135. return super.isProcessUnmodified();
  136. }
  137. public void afterConfigurationChange( org.apache.archiva.components.registry.Registry registry, String propertyName, Object propertyValue )
  138. {
  139. if ( propertyNameTriggers.contains( propertyName ) )
  140. {
  141. initIncludes();
  142. }
  143. }
  144. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  145. {
  146. /* do nothing */
  147. }
  148. private void initIncludes()
  149. {
  150. includes.clear();
  151. includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
  152. }
  153. @PostConstruct
  154. public void initialize()
  155. {
  156. propertyNameTriggers = new ArrayList<>();
  157. propertyNameTriggers.add( "repositoryScanning" );
  158. propertyNameTriggers.add( "fileTypes" );
  159. propertyNameTriggers.add( "fileType" );
  160. propertyNameTriggers.add( "patterns" );
  161. propertyNameTriggers.add( "pattern" );
  162. configuration.addChangeListener( this );
  163. initIncludes();
  164. }
  165. public String getId()
  166. {
  167. return this.id;
  168. }
  169. public String getDescription()
  170. {
  171. return this.description;
  172. }
  173. public List<String> getExcludes()
  174. {
  175. return null;
  176. }
  177. public List<String> getIncludes()
  178. {
  179. return this.includes;
  180. }
  181. public boolean isPermanent()
  182. {
  183. return false;
  184. }
  185. }