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.ArchivaConfiguration;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
27 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.apache.archiva.consumers.RepositoryContentConsumer;
29 import org.apache.commons.collections.CollectionUtils;
30 import org.codehaus.plexus.util.DirectoryWalker;
31 import org.springframework.stereotype.Service;
33 import javax.inject.Inject;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.LinkedHashSet;
38 import java.util.List;
42 * DefaultRepositoryScanner
46 @Service( "repositoryScanner#default" )
47 public class DefaultRepositoryScanner
48 implements RepositoryScanner
51 private FileTypes filetypes;
54 private RepositoryContentConsumers repositoryContentConsumers;
56 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<>();
59 public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
60 throws RepositoryScannerException
62 List<KnownRepositoryContentConsumer> knownContentConsumers = null;
65 knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
66 List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
67 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
69 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
71 catch ( RepositoryAdminException e )
73 throw new RepositoryScannerException( e.getMessage(), e );
76 repositoryContentConsumers.releaseSelectedKnownConsumers( knownContentConsumers );
81 public RepositoryScanStatistics scan( ManagedRepository repository,
82 List<KnownRepositoryContentConsumer> knownContentConsumers,
83 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
84 List<String> ignoredContentPatterns, long changesSince )
85 throws RepositoryScannerException
87 if ( repository == null )
89 throw new IllegalArgumentException( "Unable to operate on a null repository." );
92 File repositoryBase = new File( repository.getLocation() );
94 //MRM-1342 Repository statistics report doesn't appear to be working correctly
95 //create the repo if not existing to have an empty stats
96 if ( !repositoryBase.exists() && !repositoryBase.mkdirs() )
98 throw new UnsupportedOperationException(
99 "Unable to scan a repository, directory " + repositoryBase.getPath() + " does not exist." );
102 if ( !repositoryBase.isDirectory() )
104 throw new UnsupportedOperationException(
105 "Unable to scan a repository, path " + repositoryBase.getPath() + " is not a directory." );
108 // Setup Includes / Excludes.
110 List<String> allExcludes = new ArrayList<>();
111 List<String> allIncludes = new ArrayList<>();
113 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
115 allExcludes.addAll( ignoredContentPatterns );
118 // Scan All Content. (intentional)
119 allIncludes.add( "**/*" );
121 // Setup Directory Walker
122 DirectoryWalker dirWalker = new DirectoryWalker();
124 dirWalker.setBaseDir( repositoryBase );
126 dirWalker.setIncludes( allIncludes );
127 dirWalker.setExcludes( allExcludes );
129 // Setup the Scan Instance
130 RepositoryScannerInstance scannerInstance =
131 new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
133 inProgressScans.add( scannerInstance );
135 RepositoryScanStatistics stats;
138 dirWalker.addDirectoryWalkListener( scannerInstance );
143 stats = scannerInstance.getStatistics();
145 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
146 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
150 inProgressScans.remove( scannerInstance );
156 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
158 List<String> ids = new ArrayList<>();
159 for ( RepositoryContentConsumer consumer : consumers )
161 ids.add( consumer.getId() );
167 public Set<RepositoryScannerInstance> getInProgressScans()
169 return inProgressScans;