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
22 import org.apache.maven.archiva.common.consumers.Consumer;
23 import org.apache.maven.archiva.repository.RepositoryException;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.codehaus.plexus.util.DirectoryWalker;
26 import org.codehaus.plexus.util.FileUtils;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Iterator;
32 import java.util.List;
37 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
40 public class RepositoryScanner
43 * Standard patterns to exclude from discovery as they are usually noise.
45 private static final String[] STANDARD_SCANNER_EXCLUDES = {
51 "**/*snapshot-version",
62 * Walk the repository, and report to the consumers the files found.
64 * Report changes to the appropriate Consumer.
66 * This is just a convenience method to {@link #scan(ArtifactRepository, List, boolean, long, List, List)}
67 * equivalent to calling <code>scan( repository, consumers, includeSnapshots, 0, null, null );</code>
69 * @param repository the repository to change.
70 * @param consumers use the provided list of consumers.
71 * @param includeSnapshots true to include snapshots in the walking of this repository.
72 * @return the statistics for this scan.
73 * @throws RepositoryException if there was a fundamental problem with getting the discoverer started.
75 public ScanStatistics scan( ArtifactRepository repository, List consumers, boolean includeSnapshots )
76 throws RepositoryException
78 return scan( repository, consumers, includeSnapshots, 0, null, null );
82 * Walk the repository, and report to the consumers the files found.
84 * Report changes to the appropriate Consumer.
86 * @param repository the repository to change.
87 * @param consumers use the provided list of consumers.
88 * @param includeSnapshots true to include snapshots in the scanning of this repository.
89 * @param onlyModifiedAfterTimestamp Only report to the consumers, files that have a {@link File#lastModified()})
90 * after the provided timestamp.
91 * @param extraFileExclusions an optional list of file exclusions on the walk.
92 * @param extraFileInclusions an optional list of file inclusions on the walk.
93 * @return the statistics for this scan.
94 * @throws RepositoryException if there was a fundamental problem with getting the discoverer started.
96 public ScanStatistics scan( ArtifactRepository repository, List consumers, boolean includeSnapshots,
97 long onlyModifiedAfterTimestamp, List extraFileExclusions, List extraFileInclusions )
98 throws RepositoryException
100 if ( repository == null )
102 throw new IllegalArgumentException( "Unable to operate on a null repository." );
105 if ( !"file".equals( repository.getProtocol() ) )
107 throw new UnsupportedOperationException( "Only filesystem repositories are supported." );
110 File repositoryBase = new File( repository.getBasedir() );
112 if ( !repositoryBase.exists() )
114 throw new UnsupportedOperationException( "Unable to scan a repository, directory "
115 + repositoryBase.getAbsolutePath() + " does not exist." );
118 if ( !repositoryBase.isDirectory() )
120 throw new UnsupportedOperationException( "Unable to scan a repository, path "
121 + repositoryBase.getAbsolutePath() + " is not a directory." );
124 // Setup Includes / Excludes.
126 List allExcludes = new ArrayList();
127 List allIncludes = new ArrayList();
129 // Exclude all of the SCM patterns.
130 allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
132 // Exclude all of the archiva noise patterns.
133 allExcludes.addAll( Arrays.asList( STANDARD_SCANNER_EXCLUDES ) );
135 if ( !includeSnapshots )
137 allExcludes.add( "**/*-SNAPSHOT*" );
140 if ( extraFileExclusions != null )
142 allExcludes.addAll( extraFileExclusions );
145 Iterator it = consumers.iterator();
146 while ( it.hasNext() )
148 Consumer consumer = (Consumer) it.next();
150 /* NOTE: Do not insert the consumer exclusion patterns here.
151 * Exclusion patterns are handled by RepositoryScanner.wantsFile(Consumer, String)
153 * addUniqueElements( consumer.getExcludePatterns(), allExcludes );
155 addUniqueElements( consumer.getIncludePatterns(), allIncludes );
158 if ( extraFileInclusions != null )
160 allIncludes.addAll( extraFileInclusions );
163 // Setup Directory Walker
165 DirectoryWalker dirWalker = new DirectoryWalker();
167 dirWalker.setBaseDir( repositoryBase );
169 dirWalker.setIncludes( allIncludes );
170 dirWalker.setExcludes( allExcludes );
172 // Setup the Scan Instance
173 RepositoryScannerInstance scannerInstance = new RepositoryScannerInstance( repository, consumers );
174 scannerInstance.setOnlyModifiedAfterTimestamp( onlyModifiedAfterTimestamp );
176 dirWalker.addDirectoryWalkListener( scannerInstance );
181 return scannerInstance.getStatistics();
184 private void addUniqueElements( List fromList, List toList )
186 Iterator itFrom = fromList.iterator();
187 while ( itFrom.hasNext() )
189 Object o = itFrom.next();
190 if ( !toList.contains( o ) )