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.configuration.FileTypes;
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.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;
39 import java.util.ArrayList;
40 import java.util.EnumSet;
41 import java.util.LinkedHashSet;
42 import java.util.List;
46 * DefaultRepositoryScanner
50 @Service( "repositoryScanner#default" )
51 public class DefaultRepositoryScanner
52 implements RepositoryScanner
55 private static final Logger log = LoggerFactory.getLogger(DefaultRepositoryScanner.class);
58 private FileTypes filetypes;
61 private RepositoryContentConsumers repositoryContentConsumers;
63 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<>();
66 public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
67 throws RepositoryScannerException
69 List<KnownRepositoryContentConsumer> knownContentConsumers = null;
72 knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
73 List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
74 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
76 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
78 catch ( RepositoryAdminException e )
80 throw new RepositoryScannerException( e.getMessage(), e );
83 repositoryContentConsumers.releaseSelectedKnownConsumers( knownContentConsumers );
88 public RepositoryScanStatistics scan( ManagedRepository repository,
89 List<KnownRepositoryContentConsumer> knownContentConsumers,
90 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
91 List<String> ignoredContentPatterns, long changesSince )
92 throws RepositoryScannerException
94 if ( repository == null )
96 throw new IllegalArgumentException( "Unable to operate on a null repository." );
99 Path repositoryBase = Paths.get( repository.getLocation() );
101 //MRM-1342 Repository statistics report doesn't appear to be working correctly
102 //create the repo if not existing to have an empty stats
103 if ( !Files.exists(repositoryBase))
106 Files.createDirectories(repositoryBase);
107 } catch (IOException e) {
108 throw new UnsupportedOperationException("Unable to scan a repository, directory " + repositoryBase + " does not exist." );
112 if ( !Files.isDirectory(repositoryBase) )
114 throw new UnsupportedOperationException(
115 "Unable to scan a repository, path " + repositoryBase+ " is not a directory." );
118 // Setup Includes / Excludes.
120 List<String> allExcludes = new ArrayList<>();
121 List<String> allIncludes = new ArrayList<>();
123 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
125 allExcludes.addAll( ignoredContentPatterns );
128 // Scan All Content. (intentional)
129 allIncludes.add( "**/*" );
131 // Setup the Scan Instance
132 RepositoryScannerInstance scannerInstance =
133 new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
135 scannerInstance.setFileNameIncludePattern(allIncludes);
136 scannerInstance.setFileNameExcludePattern(allExcludes);
137 inProgressScans.add( scannerInstance );
139 RepositoryScanStatistics stats = null;
142 Files.walkFileTree(repositoryBase, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, scannerInstance);
144 stats = scannerInstance.getStatistics();
146 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
147 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
148 } catch (IOException e) {
149 log.error("Could not scan directory {}", repositoryBase);
152 inProgressScans.remove( scannerInstance );
158 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
160 List<String> ids = new ArrayList<>();
161 for ( RepositoryContentConsumer consumer : consumers )
163 ids.add( consumer.getId() );
169 public Set<RepositoryScannerInstance> getInProgressScans()
171 return inProgressScans;