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.commons.lang.SystemUtils;
23 import org.apache.maven.archiva.common.utils.BaseFile;
24 import org.apache.maven.archiva.consumers.Consumer;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.codehaus.plexus.logging.Logger;
27 import org.codehaus.plexus.util.DirectoryWalkListener;
28 import org.codehaus.plexus.util.SelectorUtils;
29 import org.codehaus.plexus.util.StringUtils;
32 import java.util.Iterator;
33 import java.util.List;
36 * RepositoryScanner - this is an instance of a scan against a repository.
38 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
41 public class RepositoryScanner
42 implements DirectoryWalkListener
44 public static final String ROLE = RepositoryScanner.class.getName();
46 private List consumers;
48 private ArtifactRepository repository;
50 private Logger logger;
52 private boolean isCaseSensitive = true;
54 private DiscovererStatistics stats;
56 private long onlyModifiedAfterTimestamp = 0;
58 public RepositoryScanner( ArtifactRepository repository, List consumerList )
60 this.repository = repository;
61 this.consumers = consumerList;
62 stats = new DiscovererStatistics( repository );
64 Iterator it = this.consumers.iterator();
65 while ( it.hasNext() )
67 Consumer consumer = (Consumer) it.next();
69 if ( !consumer.init( this.repository ) )
71 throw new IllegalStateException( "Consumer [" + consumer.getName() +
72 "] is reporting that it is incompatible with the [" + repository.getId() + "] repository." );
76 if ( SystemUtils.IS_OS_WINDOWS )
78 isCaseSensitive = false;
82 public DiscovererStatistics getStatistics()
87 public void directoryWalkStarting( File basedir )
89 getLogger().info( "Walk Started: [" + this.repository.getId() + "] " + this.repository.getBasedir() );
91 stats.timestampStarted = System.currentTimeMillis();
94 public void directoryWalkStep( int percentage, File file )
96 getLogger().debug( "Walk Step: " + percentage + ", " + file );
98 // Timestamp finished points to the last successful scan, not this current one.
99 if ( file.lastModified() < onlyModifiedAfterTimestamp )
101 // Skip file as no change has occured.
102 getLogger().debug( "Skipping, No Change: " + file.getAbsolutePath() );
103 stats.filesSkipped++;
107 synchronized ( consumers )
109 stats.filesIncluded++;
111 BaseFile basefile = new BaseFile( repository.getBasedir(), file );
113 Iterator itConsumers = this.consumers.iterator();
114 while ( itConsumers.hasNext() )
116 Consumer consumer = (Consumer) itConsumers.next();
118 if ( wantsFile( consumer, StringUtils.replace( basefile.getRelativePath(), "\\", "/" ) ) )
122 getLogger().debug( "Sending to consumer: " + consumer.getName() );
123 stats.filesConsumed++;
124 consumer.processFile( basefile );
126 catch ( Exception e )
128 /* Intentionally Catch all exceptions.
129 * So that the discoverer processing can continue.
131 getLogger().error( "Consumer [" + consumer.getName() + "] had an error when processing file [" +
132 basefile.getAbsolutePath() + "]: " + e.getMessage(), e );
138 "Skipping consumer " + consumer.getName() + " for file " + basefile.getRelativePath() );
144 public void directoryWalkFinished()
146 getLogger().info( "Walk Finished: [" + this.repository.getId() + "] " + this.repository.getBasedir() );
147 stats.timestampFinished = System.currentTimeMillis();
150 private boolean wantsFile( Consumer consumer, String relativePath )
154 // Test excludes first.
155 it = consumer.getExcludePatterns().iterator();
156 while ( it.hasNext() )
158 String pattern = (String) it.next();
159 if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
161 // Definately does NOT WANT FILE.
166 // Now test includes.
167 it = consumer.getIncludePatterns().iterator();
168 while ( it.hasNext() )
170 String pattern = (String) it.next();
171 if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
173 // Specifically WANTS FILE.
178 // Not included, and Not excluded? Default to EXCLUDE.
182 public long getOnlyModifiedAfterTimestamp()
184 return onlyModifiedAfterTimestamp;
187 public void setOnlyModifiedAfterTimestamp( long onlyModifiedAfterTimestamp )
189 this.onlyModifiedAfterTimestamp = onlyModifiedAfterTimestamp;
193 * Debug method from DirectoryWalker.
195 public void debug( String message )
197 getLogger().debug( "Repository Scanner: " + message );
200 public Logger getLogger()
205 public void setLogger( Logger logger )
207 this.logger = logger;