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
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.maven.archiva.configuration.FileTypes;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
30 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.maven.archiva.consumers.RepositoryContentConsumer;
32 import org.apache.maven.archiva.repository.RepositoryException;
33 import org.codehaus.plexus.util.DirectoryWalker;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * DefaultRepositoryScanner
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
42 * @plexus.component role="org.apache.maven.archiva.repository.scanner.RepositoryScanner"
44 public class DefaultRepositoryScanner
45 implements RepositoryScanner
47 private Logger log = LoggerFactory.getLogger( DefaultRepositoryScanner.class );
52 private FileTypes filetypes;
57 private RepositoryContentConsumers consumerUtil;
59 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository, long changesSince )
60 throws RepositoryException
62 List<KnownRepositoryContentConsumer> knownContentConsumers = consumerUtil.getSelectedKnownConsumers();
63 List<InvalidRepositoryContentConsumer> invalidContentConsumers = consumerUtil.getSelectedInvalidConsumers();
64 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
66 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
69 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository,
70 List<KnownRepositoryContentConsumer> knownContentConsumers,
71 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
72 List<String> ignoredContentPatterns, long changesSince )
73 throws RepositoryException
75 if ( repository == null )
77 throw new IllegalArgumentException( "Unable to operate on a null repository." );
80 File repositoryBase = new File( repository.getLocation() );
82 if ( !repositoryBase.exists() )
84 throw new UnsupportedOperationException( "Unable to scan a repository, directory "
85 + repositoryBase.getAbsolutePath() + " does not exist." );
88 if ( !repositoryBase.isDirectory() )
90 throw new UnsupportedOperationException( "Unable to scan a repository, path "
91 + repositoryBase.getAbsolutePath() + " is not a directory." );
94 // Setup Includes / Excludes.
96 List<String> allExcludes = new ArrayList<String>();
97 List<String> allIncludes = new ArrayList<String>();
99 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
101 allExcludes.addAll( ignoredContentPatterns );
104 // Scan All Content. (intentional)
105 allIncludes.add( "**/*" );
107 // Setup Directory Walker
108 DirectoryWalker dirWalker = new DirectoryWalker();
110 dirWalker.setBaseDir( repositoryBase );
112 dirWalker.setIncludes( allIncludes );
113 dirWalker.setExcludes( allExcludes );
115 // Setup the Scan Instance
116 RepositoryScannerInstance scannerInstance = new RepositoryScannerInstance( repository, knownContentConsumers,
117 invalidContentConsumers, changesSince );
119 dirWalker.addDirectoryWalkListener( scannerInstance );
124 RepositoryScanStatistics stats = scannerInstance.getStatistics();
126 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
127 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
132 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
134 List<String> ids = new ArrayList<String>();
135 for ( RepositoryContentConsumer consumer : consumers )
137 ids.add( consumer.getId() );