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

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