1 package org.apache.maven.archiva.discoverer;
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.artifact.repository.ArtifactRepository;
24 import org.codehaus.plexus.logging.AbstractLogEnabled;
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;
35 * Discoverer Implementation.
37 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
38 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
39 * @plexus.component role="org.apache.maven.archiva.discoverer.Discoverer"
41 public class DefaultDiscoverer
42 extends AbstractLogEnabled
46 * Standard patterns to exclude from discovery as they are usually noise.
48 private static final String[] STANDARD_DISCOVERY_EXCLUDES = {
54 "**/*snapshot-version",
64 public DefaultDiscoverer()
68 public DiscovererStatistics walkRepository( ArtifactRepository repository, List consumers, boolean includeSnapshots )
69 throws DiscovererException
71 return walkRepository( repository, consumers, includeSnapshots, 0, null, null );
74 public DiscovererStatistics walkRepository( ArtifactRepository repository, List consumers,
75 boolean includeSnapshots, long onlyModifiedAfterTimestamp,
76 List extraFileExclusions, List extraFileInclusions )
77 throws DiscovererException
81 if ( repository == null )
83 throw new IllegalArgumentException( "Unable to operate on a null repository." );
86 if ( !"file".equals( repository.getProtocol() ) )
88 throw new UnsupportedOperationException( "Only filesystem repositories are supported." );
91 File repositoryBase = new File( repository.getBasedir() );
93 if ( !repositoryBase.exists() )
95 throw new UnsupportedOperationException( "Unable to scan a repository, directory "
96 + repositoryBase.getAbsolutePath() + " does not exist." );
99 if ( !repositoryBase.isDirectory() )
101 throw new UnsupportedOperationException( "Unable to scan a repository, path "
102 + repositoryBase.getAbsolutePath() + " is not a directory." );
105 // Setup Includes / Excludes.
107 List allExcludes = new ArrayList();
108 List allIncludes = new ArrayList();
110 // Exclude all of the SCM patterns.
111 allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
113 // Exclude all of the archiva noise patterns.
114 allExcludes.addAll( Arrays.asList( STANDARD_DISCOVERY_EXCLUDES ) );
116 if ( !includeSnapshots )
118 allExcludes.add( "**/*-SNAPSHOT*" );
121 if ( extraFileExclusions != null )
123 allExcludes.addAll( extraFileExclusions );
126 Iterator it = consumers.iterator();
127 while ( it.hasNext() )
129 Consumer consumer = (Consumer) it.next();
131 /* NOTE: Do not insert the consumer exclusion patterns here.
132 * Exclusion patterns are handled by RepositoryScanner.wantsFile(Consumer, String)
134 * addUniqueElements( consumer.getExcludePatterns(), allExcludes );
136 addUniqueElements( consumer.getIncludePatterns(), allIncludes );
139 if ( extraFileInclusions != null )
141 allIncludes.addAll( extraFileInclusions );
144 // Setup Directory Walker
146 DirectoryWalker dirWalker = new DirectoryWalker();
148 dirWalker.setBaseDir( repositoryBase );
150 dirWalker.setIncludes( allIncludes );
151 dirWalker.setExcludes( allExcludes );
153 // Setup the Scan Instance
154 RepositoryScanner repoScanner = new RepositoryScanner( repository, consumers );
155 repoScanner.setOnlyModifiedAfterTimestamp( onlyModifiedAfterTimestamp );
157 repoScanner.setLogger( getLogger() );
158 dirWalker.addDirectoryWalkListener( repoScanner );
163 return repoScanner.getStatistics();
166 private void addUniqueElements( List fromList, List toList )
168 Iterator itFrom = fromList.iterator();
169 while ( itFrom.hasNext() )
171 Object o = itFrom.next();
172 if ( !toList.contains( o ) )