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;
36 * DefaultRepositoryScanner
39 * @plexus.component role="org.apache.maven.archiva.repository.scanner.RepositoryScanner"
41 public class DefaultRepositoryScanner
42 implements RepositoryScanner
47 private FileTypes filetypes;
52 private RepositoryContentConsumers consumerUtil;
54 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository, long changesSince )
55 throws RepositoryException
57 List<KnownRepositoryContentConsumer> knownContentConsumers = consumerUtil.getSelectedKnownConsumers();
58 List<InvalidRepositoryContentConsumer> invalidContentConsumers = consumerUtil.getSelectedInvalidConsumers();
59 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
61 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
64 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository,
65 List<KnownRepositoryContentConsumer> knownContentConsumers,
66 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
67 List<String> ignoredContentPatterns, long changesSince )
68 throws RepositoryException
70 if ( repository == null )
72 throw new IllegalArgumentException( "Unable to operate on a null repository." );
75 File repositoryBase = new File( repository.getLocation() );
77 if ( !repositoryBase.exists() )
79 throw new UnsupportedOperationException( "Unable to scan a repository, directory "
80 + repositoryBase.getPath() + " does not exist." );
83 if ( !repositoryBase.isDirectory() )
85 throw new UnsupportedOperationException( "Unable to scan a repository, path "
86 + repositoryBase.getPath() + " is not a directory." );
89 // Setup Includes / Excludes.
91 List<String> allExcludes = new ArrayList<String>();
92 List<String> allIncludes = new ArrayList<String>();
94 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
96 allExcludes.addAll( ignoredContentPatterns );
99 // Scan All Content. (intentional)
100 allIncludes.add( "**/*" );
102 // Setup Directory Walker
103 DirectoryWalker dirWalker = new DirectoryWalker();
105 dirWalker.setBaseDir( repositoryBase );
107 dirWalker.setIncludes( allIncludes );
108 dirWalker.setExcludes( allExcludes );
110 // Setup the Scan Instance
111 RepositoryScannerInstance scannerInstance = new RepositoryScannerInstance( repository, knownContentConsumers,
112 invalidContentConsumers, changesSince );
114 dirWalker.addDirectoryWalkListener( scannerInstance );
119 RepositoryScanStatistics stats = scannerInstance.getStatistics();
121 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
122 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
127 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
129 List<String> ids = new ArrayList<String>();
130 for ( RepositoryContentConsumer consumer : consumers )
132 ids.add( consumer.getId() );