1 package org.apache.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.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.configuration.FileTypes;
25 import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
26 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.archiva.consumers.RepositoryContentConsumer;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.codehaus.plexus.util.DirectoryWalker;
30 import org.springframework.stereotype.Service;
32 import javax.inject.Inject;
34 import java.util.ArrayList;
35 import java.util.LinkedHashSet;
36 import java.util.List;
40 * DefaultRepositoryScanner
44 @Service( "repositoryScanner#default" )
45 public class DefaultRepositoryScanner
46 implements RepositoryScanner
52 private FileTypes filetypes;
58 private RepositoryContentConsumers consumerUtil;
60 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<RepositoryScannerInstance>();
62 public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
63 throws RepositoryScannerException
67 List<KnownRepositoryContentConsumer> knownContentConsumers = consumerUtil.getSelectedKnownConsumers();
68 List<InvalidRepositoryContentConsumer> invalidContentConsumers = consumerUtil.getSelectedInvalidConsumers();
69 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
71 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
73 catch ( RepositoryAdminException e )
75 throw new RepositoryScannerException( e.getMessage(), e );
79 public RepositoryScanStatistics scan( ManagedRepository repository,
80 List<KnownRepositoryContentConsumer> knownContentConsumers,
81 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
82 List<String> ignoredContentPatterns, long changesSince )
83 throws RepositoryScannerException
85 if ( repository == null )
87 throw new IllegalArgumentException( "Unable to operate on a null repository." );
90 File repositoryBase = new File( repository.getLocation() );
92 //MRM-1342 Repository statistics report doesn't appear to be working correctly
93 //create the repo if not existing to have an empty stats
94 if ( !repositoryBase.exists() && !repositoryBase.mkdirs() )
96 throw new UnsupportedOperationException(
97 "Unable to scan a repository, directory " + repositoryBase.getPath() + " does not exist." );
100 if ( !repositoryBase.isDirectory() )
102 throw new UnsupportedOperationException(
103 "Unable to scan a repository, path " + repositoryBase.getPath() + " is not a directory." );
106 // Setup Includes / Excludes.
108 List<String> allExcludes = new ArrayList<String>();
109 List<String> allIncludes = new ArrayList<String>();
111 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
113 allExcludes.addAll( ignoredContentPatterns );
116 // Scan All Content. (intentional)
117 allIncludes.add( "**/*" );
119 // Setup Directory Walker
120 DirectoryWalker dirWalker = new DirectoryWalker();
122 dirWalker.setBaseDir( repositoryBase );
124 dirWalker.setIncludes( allIncludes );
125 dirWalker.setExcludes( allExcludes );
127 // Setup the Scan Instance
128 RepositoryScannerInstance scannerInstance =
129 new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
131 inProgressScans.add( scannerInstance );
133 RepositoryScanStatistics stats;
136 dirWalker.addDirectoryWalkListener( scannerInstance );
141 stats = scannerInstance.getStatistics();
143 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
144 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
148 inProgressScans.remove( scannerInstance );
154 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
156 List<String> ids = new ArrayList<String>();
157 for ( RepositoryContentConsumer consumer : consumers )
159 ids.add( consumer.getId() );
164 public Set<RepositoryScannerInstance> getInProgressScans()
166 return inProgressScans;