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
42 * plexus.component role="org.apache.archiva.repository.scanner.RepositoryScanner"
44 @Service("repositoryScanner#default")
45 public class DefaultRepositoryScanner
46 implements RepositoryScanner
52 private FileTypes filetypes;
58 private RepositoryContentConsumers consumerUtil;
60 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<RepositoryScannerInstance>();
62 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository, long changesSince )
63 throws RepositoryScannerException
65 List<KnownRepositoryContentConsumer> knownContentConsumers = consumerUtil.getSelectedKnownConsumers();
66 List<InvalidRepositoryContentConsumer> invalidContentConsumers = consumerUtil.getSelectedInvalidConsumers();
67 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
69 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
72 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository,
73 List<KnownRepositoryContentConsumer> knownContentConsumers,
74 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
75 List<String> ignoredContentPatterns, long changesSince )
76 throws RepositoryScannerException
78 if ( repository == null )
80 throw new IllegalArgumentException( "Unable to operate on a null repository." );
83 File repositoryBase = new File( repository.getLocation() );
85 //MRM-1342 Repository statistics report doesn't appear to be working correctly
86 //create the repo if not existing to have an empty stats
87 if ( !repositoryBase.exists() && !repositoryBase.mkdirs() )
89 throw new UnsupportedOperationException( "Unable to scan a repository, directory "
90 + repositoryBase.getPath() + " does not exist." );
93 if ( !repositoryBase.isDirectory() )
95 throw new UnsupportedOperationException( "Unable to scan a repository, path "
96 + repositoryBase.getPath() + " is not a directory." );
99 // Setup Includes / Excludes.
101 List<String> allExcludes = new ArrayList<String>();
102 List<String> allIncludes = new ArrayList<String>();
104 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
106 allExcludes.addAll( ignoredContentPatterns );
109 // Scan All Content. (intentional)
110 allIncludes.add( "**/*" );
112 // Setup Directory Walker
113 DirectoryWalker dirWalker = new DirectoryWalker();
115 dirWalker.setBaseDir( repositoryBase );
117 dirWalker.setIncludes( allIncludes );
118 dirWalker.setExcludes( allExcludes );
120 // Setup the Scan Instance
121 RepositoryScannerInstance scannerInstance = new RepositoryScannerInstance( repository, knownContentConsumers,
122 invalidContentConsumers, changesSince );
124 inProgressScans.add( scannerInstance );
126 dirWalker.addDirectoryWalkListener( scannerInstance );
131 RepositoryScanStatistics stats = scannerInstance.getStatistics();
133 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
134 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
136 inProgressScans.remove( scannerInstance );
141 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
143 List<String> ids = new ArrayList<String>();
144 for ( RepositoryContentConsumer consumer : consumers )
146 ids.add( consumer.getId() );
151 public Set<RepositoryScannerInstance> getInProgressScans()
153 return inProgressScans;