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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.admin.model.beans.ManagedRepository;
  22. import org.apache.archiva.configuration.FileTypes;
  23. import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
  24. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  25. import org.apache.archiva.consumers.RepositoryContentConsumer;
  26. import org.apache.commons.collections.CollectionUtils;
  27. import org.codehaus.plexus.util.DirectoryWalker;
  28. import org.springframework.stereotype.Service;
  29. import javax.inject.Inject;
  30. import java.io.File;
  31. import java.util.ArrayList;
  32. import java.util.LinkedHashSet;
  33. import java.util.List;
  34. import java.util.Set;
  35. /**
  36. * DefaultRepositoryScanner
  37. *
  38. *
  39. */
  40. @Service( "repositoryScanner#default" )
  41. public class DefaultRepositoryScanner
  42. implements RepositoryScanner
  43. {
  44. @Inject
  45. private FileTypes filetypes;
  46. @Inject
  47. private RepositoryContentConsumers consumerUtil;
  48. private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<RepositoryScannerInstance>();
  49. public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
  50. throws RepositoryScannerException
  51. {
  52. try
  53. {
  54. List<KnownRepositoryContentConsumer> knownContentConsumers = consumerUtil.getSelectedKnownConsumers();
  55. List<InvalidRepositoryContentConsumer> invalidContentConsumers = consumerUtil.getSelectedInvalidConsumers();
  56. List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
  57. return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
  58. }
  59. catch ( RepositoryAdminException e )
  60. {
  61. throw new RepositoryScannerException( e.getMessage(), e );
  62. }
  63. }
  64. public RepositoryScanStatistics scan( ManagedRepository repository,
  65. List<KnownRepositoryContentConsumer> knownContentConsumers,
  66. List<InvalidRepositoryContentConsumer> invalidContentConsumers,
  67. List<String> ignoredContentPatterns, long changesSince )
  68. throws RepositoryScannerException
  69. {
  70. if ( repository == null )
  71. {
  72. throw new IllegalArgumentException( "Unable to operate on a null repository." );
  73. }
  74. File repositoryBase = new File( repository.getLocation() );
  75. //MRM-1342 Repository statistics report doesn't appear to be working correctly
  76. //create the repo if not existing to have an empty stats
  77. if ( !repositoryBase.exists() && !repositoryBase.mkdirs() )
  78. {
  79. throw new UnsupportedOperationException(
  80. "Unable to scan a repository, directory " + repositoryBase.getPath() + " does not exist." );
  81. }
  82. if ( !repositoryBase.isDirectory() )
  83. {
  84. throw new UnsupportedOperationException(
  85. "Unable to scan a repository, path " + repositoryBase.getPath() + " is not a directory." );
  86. }
  87. // Setup Includes / Excludes.
  88. List<String> allExcludes = new ArrayList<>();
  89. List<String> allIncludes = new ArrayList<>();
  90. if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
  91. {
  92. allExcludes.addAll( ignoredContentPatterns );
  93. }
  94. // Scan All Content. (intentional)
  95. allIncludes.add( "**/*" );
  96. // Setup Directory Walker
  97. DirectoryWalker dirWalker = new DirectoryWalker();
  98. dirWalker.setBaseDir( repositoryBase );
  99. dirWalker.setIncludes( allIncludes );
  100. dirWalker.setExcludes( allExcludes );
  101. // Setup the Scan Instance
  102. RepositoryScannerInstance scannerInstance =
  103. new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
  104. inProgressScans.add( scannerInstance );
  105. RepositoryScanStatistics stats;
  106. try
  107. {
  108. dirWalker.addDirectoryWalkListener( scannerInstance );
  109. // Execute scan.
  110. dirWalker.scan();
  111. stats = scannerInstance.getStatistics();
  112. stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
  113. stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
  114. }
  115. finally
  116. {
  117. inProgressScans.remove( scannerInstance );
  118. }
  119. return stats;
  120. }
  121. private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
  122. {
  123. List<String> ids = new ArrayList<>();
  124. for ( RepositoryContentConsumer consumer : consumers )
  125. {
  126. ids.add( consumer.getId() );
  127. }
  128. return ids;
  129. }
  130. public Set<RepositoryScannerInstance> getInProgressScans()
  131. {
  132. return inProgressScans;
  133. }
  134. }