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.consumers.Consumer;
23 import org.apache.maven.archiva.model.ArchivaRepository;
24 import org.apache.maven.archiva.model.RepositoryContentStatistics;
25 import org.apache.maven.archiva.repository.RepositoryException;
26 import org.codehaus.plexus.util.DirectoryWalker;
27 import org.codehaus.plexus.util.FileUtils;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Iterator;
33 import java.util.List;
38 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
41 public class RepositoryScanner
44 * Standard patterns to exclude from discovery as they are usually noise.
46 private static final String[] STANDARD_SCANNER_EXCLUDES = {
52 "**/*snapshot-version",
63 * Walk the repository, and report to the consumers the files found.
65 * Report changes to the appropriate Consumer.
67 * This is just a convenience method to {@link #scan(ArtifactRepository, List, boolean, long, List, List)}
68 * equivalent to calling <code>scan( repository, consumers, includeSnapshots, 0, null, null );</code>
70 * @param repository the repository to change.
71 * @param consumers use the provided list of consumers.
72 * @param includeSnapshots true to include snapshots in the walking of this repository.
73 * @return the statistics for this scan.
74 * @throws RepositoryException if there was a fundamental problem with getting the discoverer started.
76 public RepositoryContentStatistics scan( ArchivaRepository repository, List consumers, boolean includeSnapshots )
77 throws RepositoryException
79 return scan( repository, consumers, includeSnapshots, 0, null, null );
83 * Walk the repository, and report to the consumers the files found.
85 * Report changes to the appropriate Consumer.
87 * @param repository the repository to change.
88 * @param consumers use the provided list of consumers.
89 * @param includeSnapshots true to include snapshots in the scanning of this repository.
90 * @param onlyModifiedAfterTimestamp Only report to the consumers, files that have a {@link File#lastModified()})
91 * after the provided timestamp.
92 * @param extraFileExclusions an optional list of file exclusions on the walk.
93 * @param extraFileInclusions an optional list of file inclusions on the walk.
94 * @return the statistics for this scan.
95 * @throws RepositoryException if there was a fundamental problem with getting the discoverer started.
97 public RepositoryContentStatistics scan( ArchivaRepository repository, List consumers, boolean includeSnapshots,
98 long onlyModifiedAfterTimestamp, List extraFileExclusions, List extraFileInclusions )
99 throws RepositoryException
101 if ( repository == null )
103 throw new IllegalArgumentException( "Unable to operate on a null repository." );
106 if ( !"file".equals( repository.getRepositoryURL().getProtocol() ) )
108 throw new UnsupportedOperationException( "Only filesystem repositories are supported." );
111 File repositoryBase = new File( repository.getRepositoryURL().getPath() );
113 if ( !repositoryBase.exists() )
115 throw new UnsupportedOperationException( "Unable to scan a repository, directory "
116 + repositoryBase.getAbsolutePath() + " does not exist." );
119 if ( !repositoryBase.isDirectory() )
121 throw new UnsupportedOperationException( "Unable to scan a repository, path "
122 + repositoryBase.getAbsolutePath() + " is not a directory." );
125 // Setup Includes / Excludes.
127 List allExcludes = new ArrayList();
128 List allIncludes = new ArrayList();
130 // Exclude all of the SCM patterns.
131 allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
133 // Exclude all of the archiva noise patterns.
134 allExcludes.addAll( Arrays.asList( STANDARD_SCANNER_EXCLUDES ) );
136 if ( !includeSnapshots )
138 allExcludes.add( "**/*-SNAPSHOT*" );
141 if ( extraFileExclusions != null )
143 allExcludes.addAll( extraFileExclusions );
146 Iterator it = consumers.iterator();
147 while ( it.hasNext() )
149 Consumer consumer = (Consumer) it.next();
151 /* NOTE: Do not insert the consumer exclusion patterns here.
152 * Exclusion patterns are handled by RepositoryScanner.wantsFile(Consumer, String)
154 * addUniqueElements( consumer.getExcludePatterns(), allExcludes );
156 addUniqueElements( consumer.getIncludePatterns(), allIncludes );
159 if ( extraFileInclusions != null )
161 allIncludes.addAll( extraFileInclusions );
164 // Setup Directory Walker
166 DirectoryWalker dirWalker = new DirectoryWalker();
168 dirWalker.setBaseDir( repositoryBase );
170 dirWalker.setIncludes( allIncludes );
171 dirWalker.setExcludes( allExcludes );
173 // Setup the Scan Instance
174 RepositoryScannerInstance scannerInstance = new RepositoryScannerInstance( repository, consumers );
175 scannerInstance.setOnlyModifiedAfterTimestamp( onlyModifiedAfterTimestamp );
177 dirWalker.addDirectoryWalkListener( scannerInstance );
182 return scannerInstance.getStatistics();
185 private void addUniqueElements( List fromList, List toList )
187 Iterator itFrom = fromList.iterator();
188 while ( itFrom.hasNext() )
190 Object o = itFrom.next();
191 if ( !toList.contains( o ) )