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.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.configuration.FileTypes;
25 import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
26 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.archiva.consumers.RepositoryContentConsumer;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
33 import javax.inject.Inject;
34 import java.io.IOException;
35 import java.nio.file.FileVisitOption;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
42 * DefaultRepositoryScanner
46 @Service( "repositoryScanner#default" )
47 public class DefaultRepositoryScanner
48 implements RepositoryScanner
51 private static final Logger log = LoggerFactory.getLogger(DefaultRepositoryScanner.class);
54 private FileTypes filetypes;
57 private RepositoryContentConsumers repositoryContentConsumers;
59 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<>();
62 public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
63 throws RepositoryScannerException
65 List<KnownRepositoryContentConsumer> knownContentConsumers = null;
68 knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
69 List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
70 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
72 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
74 catch ( RepositoryAdminException e )
76 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 Path repositoryBase = Paths.get( repository.getLocation() );
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 ( !Files.exists(repositoryBase))
102 Files.createDirectories(repositoryBase);
103 } catch (IOException e) {
104 throw new UnsupportedOperationException("Unable to scan a repository, directory " + repositoryBase + " does not exist." );
108 if ( !Files.isDirectory(repositoryBase) )
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, 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);
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;