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.

DefaultRepositoryScanner.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package org.apache.archiva.repository.scanner;
  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.RepositoryAdminException;
  21. import org.apache.archiva.configuration.FileTypes;
  22. import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
  23. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  24. import org.apache.archiva.consumers.RepositoryContentConsumer;
  25. import org.apache.archiva.repository.ManagedRepository;
  26. import org.apache.archiva.repository.storage.StorageAsset;
  27. import org.apache.commons.collections4.CollectionUtils;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.springframework.stereotype.Service;
  31. import javax.inject.Inject;
  32. import java.io.IOException;
  33. import java.nio.file.FileVisitOption;
  34. import java.nio.file.Files;
  35. import java.util.*;
  36. /**
  37. * DefaultRepositoryScanner
  38. *
  39. *
  40. */
  41. @Service( "repositoryScanner#default" )
  42. public class DefaultRepositoryScanner
  43. implements RepositoryScanner
  44. {
  45. private static final Logger log = LoggerFactory.getLogger(DefaultRepositoryScanner.class);
  46. @Inject
  47. private FileTypes filetypes;
  48. @Inject
  49. private RepositoryContentConsumers repositoryContentConsumers;
  50. private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<>();
  51. @Override
  52. public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
  53. throws RepositoryScannerException
  54. {
  55. List<KnownRepositoryContentConsumer> knownContentConsumers = null;
  56. try
  57. {
  58. knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
  59. List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
  60. List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
  61. return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
  62. }
  63. catch ( RepositoryAdminException e )
  64. {
  65. throw new RepositoryScannerException( e.getMessage(), e );
  66. } finally
  67. {
  68. repositoryContentConsumers.releaseSelectedKnownConsumers( knownContentConsumers );
  69. }
  70. }
  71. @Override
  72. public RepositoryScanStatistics scan( ManagedRepository repository,
  73. List<KnownRepositoryContentConsumer> knownContentConsumers,
  74. List<InvalidRepositoryContentConsumer> invalidContentConsumers,
  75. List<String> ignoredContentPatterns, long changesSince )
  76. throws RepositoryScannerException
  77. {
  78. if ( repository == null )
  79. {
  80. throw new IllegalArgumentException( "Unable to operate on a null repository." );
  81. }
  82. StorageAsset repositoryBase = repository.getAsset("");
  83. //MRM-1342 Repository statistics report doesn't appear to be working correctly
  84. //create the repo if not existing to have an empty stats
  85. if ( !repositoryBase.exists())
  86. {
  87. try {
  88. repositoryBase.create();
  89. } catch (IOException e) {
  90. throw new UnsupportedOperationException("Unable to scan a repository, directory " + repositoryBase + " does not exist." );
  91. }
  92. }
  93. if ( !repositoryBase.isContainer())
  94. {
  95. throw new UnsupportedOperationException(
  96. "Unable to scan a repository, path " + repositoryBase+ " is not a directory." );
  97. }
  98. // Setup Includes / Excludes.
  99. List<String> allExcludes = new ArrayList<>();
  100. List<String> allIncludes = new ArrayList<>();
  101. if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
  102. {
  103. allExcludes.addAll( ignoredContentPatterns );
  104. }
  105. // Scan All Content. (intentional)
  106. allIncludes.add( "**/*" );
  107. // Setup the Scan Instance
  108. RepositoryScannerInstance scannerInstance =
  109. new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
  110. scannerInstance.setFileNameIncludePattern(allIncludes);
  111. scannerInstance.setFileNameExcludePattern(allExcludes);
  112. inProgressScans.add( scannerInstance );
  113. RepositoryScanStatistics stats = null;
  114. try
  115. {
  116. Files.walkFileTree(repositoryBase.getFilePath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, scannerInstance);
  117. stats = scannerInstance.getStatistics();
  118. stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
  119. stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
  120. } catch (IOException e) {
  121. log.error("Could not scan directory {}", repositoryBase);
  122. } finally
  123. {
  124. inProgressScans.remove( scannerInstance );
  125. }
  126. return stats;
  127. }
  128. private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
  129. {
  130. List<String> ids = new ArrayList<>();
  131. for ( RepositoryContentConsumer consumer : consumers )
  132. {
  133. ids.add( consumer.getId() );
  134. }
  135. return ids;
  136. }
  137. @Override
  138. public Set<RepositoryScannerInstance> getInProgressScans()
  139. {
  140. return inProgressScans;
  141. }
  142. }