001package org.apache.archiva.repository.scanner; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.admin.model.RepositoryAdminException; 023import org.apache.archiva.configuration.FileTypes; 024import org.apache.archiva.consumers.InvalidRepositoryContentConsumer; 025import org.apache.archiva.consumers.KnownRepositoryContentConsumer; 026import org.apache.archiva.consumers.RepositoryContentConsumer; 027import org.apache.archiva.repository.ManagedRepository; 028import org.apache.archiva.repository.storage.StorageAsset; 029import org.apache.commons.collections4.CollectionUtils; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032import org.springframework.stereotype.Service; 033 034import javax.inject.Inject; 035import java.io.IOException; 036import java.nio.file.FileVisitOption; 037import java.nio.file.Files; 038import java.util.*; 039 040/** 041 * DefaultRepositoryScanner 042 * 043 * 044 */ 045@Service( "repositoryScanner#default" ) 046public class DefaultRepositoryScanner 047 implements RepositoryScanner 048{ 049 050 private static final Logger log = LoggerFactory.getLogger(DefaultRepositoryScanner.class); 051 052 @Inject 053 private FileTypes filetypes; 054 055 @Inject 056 private RepositoryContentConsumers repositoryContentConsumers; 057 058 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<>(); 059 060 @Override 061 public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince ) 062 throws RepositoryScannerException 063 { 064 List<KnownRepositoryContentConsumer> knownContentConsumers = null; 065 try 066 { 067 knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers(); 068 List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers(); 069 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED ); 070 071 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince ); 072 } 073 catch ( RepositoryAdminException e ) 074 { 075 throw new RepositoryScannerException( e.getMessage(), e ); 076 } finally 077 { 078 repositoryContentConsumers.releaseSelectedKnownConsumers( knownContentConsumers ); 079 } 080 } 081 082 @Override 083 public RepositoryScanStatistics scan( ManagedRepository repository, 084 List<KnownRepositoryContentConsumer> knownContentConsumers, 085 List<InvalidRepositoryContentConsumer> invalidContentConsumers, 086 List<String> ignoredContentPatterns, long changesSince ) 087 throws RepositoryScannerException 088 { 089 if ( repository == null ) 090 { 091 throw new IllegalArgumentException( "Unable to operate on a null repository." ); 092 } 093 094 StorageAsset repositoryBase = repository.getAsset(""); 095 096 //MRM-1342 Repository statistics report doesn't appear to be working correctly 097 //create the repo if not existing to have an empty stats 098 if ( !repositoryBase.exists()) 099 { 100 try { 101 repositoryBase.create(); 102 } catch (IOException e) { 103 throw new UnsupportedOperationException("Unable to scan a repository, directory " + repositoryBase + " does not exist." ); 104 } 105 } 106 107 if ( !repositoryBase.isContainer()) 108 { 109 throw new UnsupportedOperationException( 110 "Unable to scan a repository, path " + repositoryBase+ " is not a directory." ); 111 } 112 113 // Setup Includes / Excludes. 114 115 List<String> allExcludes = new ArrayList<>(); 116 List<String> allIncludes = new ArrayList<>(); 117 118 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) ) 119 { 120 allExcludes.addAll( ignoredContentPatterns ); 121 } 122 123 // Scan All Content. (intentional) 124 allIncludes.add( "**/*" ); 125 126 // Setup the Scan Instance 127 RepositoryScannerInstance scannerInstance = 128 new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince ); 129 130 scannerInstance.setFileNameIncludePattern(allIncludes); 131 scannerInstance.setFileNameExcludePattern(allExcludes); 132 inProgressScans.add( scannerInstance ); 133 134 RepositoryScanStatistics stats = null; 135 try 136 { 137 Files.walkFileTree(repositoryBase.getFilePath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, scannerInstance); 138 139 stats = scannerInstance.getStatistics(); 140 141 stats.setKnownConsumers( gatherIds( knownContentConsumers ) ); 142 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) ); 143 } catch (IOException e) { 144 log.error("Could not scan directory {}: {}", repositoryBase, e.getMessage(), e); 145 } finally 146 { 147 inProgressScans.remove( scannerInstance ); 148 } 149 150 return stats; 151 } 152 153 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers ) 154 { 155 List<String> ids = new ArrayList<>(); 156 for ( RepositoryContentConsumer consumer : consumers ) 157 { 158 ids.add( consumer.getId() ); 159 } 160 return ids; 161 } 162 163 @Override 164 public Set<RepositoryScannerInstance> getInProgressScans() 165 { 166 return inProgressScans; 167 } 168}