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.commons.collections.CollectionUtils;
23 import org.apache.maven.archiva.configuration.FileTypes;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
26 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.maven.archiva.consumers.RepositoryContentConsumer;
28 import org.codehaus.plexus.util.DirectoryWalker;
29 import org.springframework.stereotype.Service;
32 import java.util.ArrayList;
33 import java.util.LinkedHashSet;
34 import java.util.List;
36 import javax.inject.Inject;
39 * DefaultRepositoryScanner
43 @Service("repositoryScanner#default")
44 public class DefaultRepositoryScanner
45 implements RepositoryScanner
51 private FileTypes filetypes;
57 private RepositoryContentConsumers consumerUtil;
59 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<RepositoryScannerInstance>();
61 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository, long changesSince )
62 throws RepositoryScannerException
64 List<KnownRepositoryContentConsumer> knownContentConsumers = consumerUtil.getSelectedKnownConsumers();
65 List<InvalidRepositoryContentConsumer> invalidContentConsumers = consumerUtil.getSelectedInvalidConsumers();
66 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
68 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
71 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository,
72 List<KnownRepositoryContentConsumer> knownContentConsumers,
73 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
74 List<String> ignoredContentPatterns, long changesSince )
75 throws RepositoryScannerException
77 if ( repository == null )
79 throw new IllegalArgumentException( "Unable to operate on a null repository." );
82 File repositoryBase = new File( repository.getLocation() );
84 //MRM-1342 Repository statistics report doesn't appear to be working correctly
85 //create the repo if not existing to have an empty stats
86 if ( !repositoryBase.exists() && !repositoryBase.mkdirs() )
88 throw new UnsupportedOperationException( "Unable to scan a repository, directory "
89 + repositoryBase.getPath() + " does not exist." );
92 if ( !repositoryBase.isDirectory() )
94 throw new UnsupportedOperationException( "Unable to scan a repository, path "
95 + repositoryBase.getPath() + " is not a directory." );
98 // Setup Includes / Excludes.
100 List<String> allExcludes = new ArrayList<String>();
101 List<String> allIncludes = new ArrayList<String>();
103 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
105 allExcludes.addAll( ignoredContentPatterns );
108 // Scan All Content. (intentional)
109 allIncludes.add( "**/*" );
111 // Setup Directory Walker
112 DirectoryWalker dirWalker = new DirectoryWalker();
114 dirWalker.setBaseDir( repositoryBase );
116 dirWalker.setIncludes( allIncludes );
117 dirWalker.setExcludes( allExcludes );
119 // Setup the Scan Instance
120 RepositoryScannerInstance scannerInstance = new RepositoryScannerInstance( repository, knownContentConsumers,
121 invalidContentConsumers, changesSince );
123 inProgressScans.add( scannerInstance );
125 dirWalker.addDirectoryWalkListener( scannerInstance );
130 RepositoryScanStatistics stats = scannerInstance.getStatistics();
132 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
133 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
135 inProgressScans.remove( scannerInstance );
140 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
142 List<String> ids = new ArrayList<String>();
143 for ( RepositoryContentConsumer consumer : consumers )
145 ids.add( consumer.getId() );
150 public Set<RepositoryScannerInstance> getInProgressScans()
152 return inProgressScans;