1 package org.apache.archiva.repository.scanner;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.configuration.FileTypes;
23 import org.apache.archiva.consumers.ConsumerException;
24 import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
25 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
26 import org.apache.archiva.consumers.RepositoryContentConsumer;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.apache.archiva.repository.storage.StorageAsset;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Service;
34 import javax.inject.Inject;
35 import java.io.IOException;
36 import java.nio.file.FileVisitOption;
37 import java.nio.file.Files;
41 * DefaultRepositoryScanner
45 @Service( "repositoryScanner#default" )
46 public class DefaultRepositoryScanner
47 implements RepositoryScanner
50 private static final Logger log = LoggerFactory.getLogger(DefaultRepositoryScanner.class);
53 private FileTypes filetypes;
56 private RepositoryContentConsumers repositoryContentConsumers;
58 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<>();
61 public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
62 throws RepositoryScannerException
64 List<KnownRepositoryContentConsumer> knownContentConsumers = null;
67 knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
68 List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
69 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
71 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
73 catch ( ConsumerException e )
75 throw new RepositoryScannerException( e.getMessage( ), e );
79 repositoryContentConsumers.releaseSelectedKnownConsumers( knownContentConsumers );
84 public RepositoryScanStatistics scan( ManagedRepository repository,
85 List<KnownRepositoryContentConsumer> knownContentConsumers,
86 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
87 List<String> ignoredContentPatterns, long changesSince )
88 throws RepositoryScannerException
90 if ( repository == null )
92 throw new IllegalArgumentException( "Unable to operate on a null repository." );
95 StorageAsset repositoryBase = repository.getRoot();
97 //MRM-1342 Repository statistics report doesn't appear to be working correctly
98 //create the repo if not existing to have an empty stats
99 if ( !repositoryBase.exists())
102 repositoryBase.create();
103 } catch (IOException e) {
104 throw new UnsupportedOperationException("Unable to scan a repository, directory " + repositoryBase + " does not exist." );
108 if ( !repositoryBase.isContainer())
110 throw new UnsupportedOperationException(
111 "Unable to scan a repository, path " + repositoryBase+ " is not a directory." );
114 // Setup Includes / Excludes.
116 List<String> allExcludes = new ArrayList<>();
117 List<String> allIncludes = new ArrayList<>();
119 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
121 allExcludes.addAll( ignoredContentPatterns );
124 // Scan All Content. (intentional)
125 allIncludes.add( "**/*" );
127 // Setup the Scan Instance
128 RepositoryScannerInstance scannerInstance =
129 new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
131 scannerInstance.setFileNameIncludePattern(allIncludes);
132 scannerInstance.setFileNameExcludePattern(allExcludes);
133 inProgressScans.add( scannerInstance );
135 RepositoryScanStatistics stats = null;
138 Files.walkFileTree(repositoryBase.getFilePath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, scannerInstance);
140 stats = scannerInstance.getStatistics();
142 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
143 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
144 } catch (IOException e) {
145 log.error("Could not scan directory {}: {}", repositoryBase, e.getMessage(), e);
148 inProgressScans.remove( scannerInstance );
154 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
156 List<String> ids = new ArrayList<>();
157 for ( RepositoryContentConsumer consumer : consumers )
159 ids.add( consumer.getId() );
165 public Set<RepositoryScannerInstance> getInProgressScans()
167 return inProgressScans;