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.codehaus.plexus.util.DirectoryWalker;
30 import org.springframework.stereotype.Service;
32 import javax.inject.Inject;
34 import java.util.ArrayList;
35 import java.util.LinkedHashSet;
36 import java.util.List;
40 * DefaultRepositoryScanner
44 @Service( "repositoryScanner#default" )
45 public class DefaultRepositoryScanner
46 implements RepositoryScanner
49 private FileTypes filetypes;
52 private RepositoryContentConsumers repositoryContentConsumers;
54 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<RepositoryScannerInstance>();
57 public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
58 throws RepositoryScannerException
62 List<KnownRepositoryContentConsumer> knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
63 List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
64 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
66 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
68 catch ( RepositoryAdminException e )
70 throw new RepositoryScannerException( e.getMessage(), e );
75 public RepositoryScanStatistics scan( ManagedRepository repository,
76 List<KnownRepositoryContentConsumer> knownContentConsumers,
77 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
78 List<String> ignoredContentPatterns, long changesSince )
79 throws RepositoryScannerException
81 if ( repository == null )
83 throw new IllegalArgumentException( "Unable to operate on a null repository." );
86 File repositoryBase = new File( repository.getLocation() );
88 //MRM-1342 Repository statistics report doesn't appear to be working correctly
89 //create the repo if not existing to have an empty stats
90 if ( !repositoryBase.exists() && !repositoryBase.mkdirs() )
92 throw new UnsupportedOperationException(
93 "Unable to scan a repository, directory " + repositoryBase.getPath() + " does not exist." );
96 if ( !repositoryBase.isDirectory() )
98 throw new UnsupportedOperationException(
99 "Unable to scan a repository, path " + repositoryBase.getPath() + " is not a directory." );
102 // Setup Includes / Excludes.
104 List<String> allExcludes = new ArrayList<>();
105 List<String> allIncludes = new ArrayList<>();
107 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
109 allExcludes.addAll( ignoredContentPatterns );
112 // Scan All Content. (intentional)
113 allIncludes.add( "**/*" );
115 // Setup Directory Walker
116 DirectoryWalker dirWalker = new DirectoryWalker();
118 dirWalker.setBaseDir( repositoryBase );
120 dirWalker.setIncludes( allIncludes );
121 dirWalker.setExcludes( allExcludes );
123 // Setup the Scan Instance
124 RepositoryScannerInstance scannerInstance =
125 new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
127 inProgressScans.add( scannerInstance );
129 RepositoryScanStatistics stats;
132 dirWalker.addDirectoryWalkListener( scannerInstance );
137 stats = scannerInstance.getStatistics();
139 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
140 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
144 inProgressScans.remove( scannerInstance );
150 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
152 List<String> ids = new ArrayList<>();
153 for ( RepositoryContentConsumer consumer : consumers )
155 ids.add( consumer.getId() );
161 public Set<RepositoryScannerInstance> getInProgressScans()
163 return inProgressScans;