1 package org.apache.maven.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
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.maven.archiva.configuration.FileTypes;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
30 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.maven.archiva.consumers.RepositoryContentConsumer;
32 import org.apache.maven.archiva.repository.RepositoryException;
33 import org.codehaus.plexus.util.DirectoryWalker;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * DefaultRepositoryScanner
41 * @plexus.component role="org.apache.maven.archiva.repository.scanner.RepositoryScanner"
43 public class DefaultRepositoryScanner
44 implements RepositoryScanner
46 private Logger log = LoggerFactory.getLogger( DefaultRepositoryScanner.class );
51 private FileTypes filetypes;
56 private RepositoryContentConsumers consumerUtil;
58 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository, long changesSince )
59 throws RepositoryException
61 List<KnownRepositoryContentConsumer> knownContentConsumers = consumerUtil.getSelectedKnownConsumers();
62 List<InvalidRepositoryContentConsumer> invalidContentConsumers = consumerUtil.getSelectedInvalidConsumers();
63 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
65 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
68 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository,
69 List<KnownRepositoryContentConsumer> knownContentConsumers,
70 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
71 List<String> ignoredContentPatterns, long changesSince )
72 throws RepositoryException
74 if ( repository == null )
76 throw new IllegalArgumentException( "Unable to operate on a null repository." );
79 File repositoryBase = new File( repository.getLocation() );
81 if ( !repositoryBase.exists() )
83 throw new UnsupportedOperationException( "Unable to scan a repository, directory "
84 + repositoryBase.getAbsolutePath() + " does not exist." );
87 if ( !repositoryBase.isDirectory() )
89 throw new UnsupportedOperationException( "Unable to scan a repository, path "
90 + repositoryBase.getAbsolutePath() + " is not a directory." );
93 // Setup Includes / Excludes.
95 List<String> allExcludes = new ArrayList<String>();
96 List<String> allIncludes = new ArrayList<String>();
98 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
100 allExcludes.addAll( ignoredContentPatterns );
103 // Scan All Content. (intentional)
104 allIncludes.add( "**/*" );
106 // Setup Directory Walker
107 DirectoryWalker dirWalker = new DirectoryWalker();
109 dirWalker.setBaseDir( repositoryBase );
111 dirWalker.setIncludes( allIncludes );
112 dirWalker.setExcludes( allExcludes );
114 // Setup the Scan Instance
115 RepositoryScannerInstance scannerInstance = new RepositoryScannerInstance( repository, knownContentConsumers,
116 invalidContentConsumers, changesSince );
118 dirWalker.addDirectoryWalkListener( scannerInstance );
123 RepositoryScanStatistics stats = scannerInstance.getStatistics();
125 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
126 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
131 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
133 List<String> ids = new ArrayList<String>();
134 for ( RepositoryContentConsumer consumer : consumers )
136 ids.add( consumer.getId() );