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.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.archiva.model.ArchivaRepository;
26 import org.apache.maven.archiva.model.RepositoryContentStatistics;
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.codehaus.plexus.util.DirectoryWalkListener;
29 import org.codehaus.plexus.util.SelectorUtils;
30 import org.codehaus.plexus.util.StringUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 import java.util.Iterator;
36 import java.util.List;
39 * RepositoryScannerInstance
41 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
44 public class RepositoryScannerInstance implements DirectoryWalkListener
46 private static Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
48 private List consumers;
50 private ArchivaRepository repository;
52 private boolean isCaseSensitive = true;
54 private RepositoryContentStatistics stats;
56 private long onlyModifiedAfterTimestamp = 0;
58 public RepositoryScannerInstance( ArchivaRepository repository, List consumerList )
60 this.repository = repository;
61 this.consumers = consumerList;
62 stats = new RepositoryContentStatistics();
63 stats.setRepositoryId( repository.getId() );
66 Iterator it = this.consumers.iterator();
67 while ( it.hasNext() )
69 Consumer consumer = (Consumer) it.next();
71 if ( !consumer.init( this.repository ) )
73 throw new IllegalStateException( "Consumer [" + consumer.getName() +
74 "] is reporting that it is incompatible with the [" + repository.getId() + "] repository." );
78 if ( SystemUtils.IS_OS_WINDOWS )
80 isCaseSensitive = false;
84 public RepositoryContentStatistics getStatistics()
89 public void directoryWalkStarting( File basedir )
91 log.info( "Walk Started: [" + this.repository.getId() + "] " + this.repository.getRepositoryURL() );
95 public void directoryWalkStep( int percentage, File file )
97 log.debug( "Walk Step: " + percentage + ", " + file );
99 stats.increaseFileCount();
101 // Timestamp finished points to the last successful scan, not this current one.
102 if ( file.lastModified() < onlyModifiedAfterTimestamp )
104 // Skip file as no change has occured.
105 log.debug( "Skipping, No Change: " + file.getAbsolutePath() );
109 synchronized ( consumers )
111 stats.increaseNewFileCount();
113 BaseFile basefile = new BaseFile( repository.getRepositoryURL().getPath(), file );
115 Iterator itConsumers = this.consumers.iterator();
116 while ( itConsumers.hasNext() )
118 Consumer consumer = (Consumer) itConsumers.next();
120 if ( wantsFile( consumer, StringUtils.replace( basefile.getRelativePath(), "\\", "/" ) ) )
124 log.debug( "Sending to consumer: " + consumer.getName() );
125 consumer.processFile( basefile );
127 catch ( Exception e )
129 /* Intentionally Catch all exceptions.
130 * So that the discoverer processing can continue.
132 log.error( "Consumer [" + consumer.getName() + "] had an error when processing file [" +
133 basefile.getAbsolutePath() + "]: " + e.getMessage(), e );
139 "Skipping consumer " + consumer.getName() + " for file " + basefile.getRelativePath() );
145 public void directoryWalkFinished()
147 log.info( "Walk Finished: [" + this.repository.getId() + "] " + this.repository.getRepositoryURL() );
148 stats.triggerFinished();
151 private boolean wantsFile( Consumer consumer, String relativePath )
155 // Test excludes first.
156 it = consumer.getExcludePatterns().iterator();
157 while ( it.hasNext() )
159 String pattern = (String) it.next();
160 if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
162 // Definately does NOT WANT FILE.
167 // Now test includes.
168 it = consumer.getIncludePatterns().iterator();
169 while ( it.hasNext() )
171 String pattern = (String) it.next();
172 if ( SelectorUtils.matchPath( pattern, relativePath, isCaseSensitive ) )
174 // Specifically WANTS FILE.
179 // Not included, and Not excluded? Default to EXCLUDE.
183 public long getOnlyModifiedAfterTimestamp()
185 return onlyModifiedAfterTimestamp;
188 public void setOnlyModifiedAfterTimestamp( long onlyModifiedAfterTimestamp )
190 this.onlyModifiedAfterTimestamp = onlyModifiedAfterTimestamp;
194 * Debug method from DirectoryWalker.
196 public void debug( String message )
198 log.debug( "Repository Scanner: " + message );