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 6.7KB

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