This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.repository.scanner;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.admin.model.RepositoryAdminException;
023import org.apache.archiva.admin.model.beans.ManagedRepository;
024import org.apache.archiva.configuration.ArchivaConfiguration;
025import org.apache.archiva.configuration.FileTypes;
026import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
027import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
028import org.apache.archiva.consumers.RepositoryContentConsumer;
029import org.apache.commons.collections.CollectionUtils;
030import org.codehaus.plexus.util.DirectoryWalker;
031import org.springframework.stereotype.Service;
032
033import javax.inject.Inject;
034import java.io.File;
035import java.util.ArrayList;
036import java.util.Collections;
037import java.util.LinkedHashSet;
038import java.util.List;
039import java.util.Set;
040
041/**
042 * DefaultRepositoryScanner
043 *
044 *
045 */
046@Service( "repositoryScanner#default" )
047public class DefaultRepositoryScanner
048    implements RepositoryScanner
049{
050    @Inject
051    private FileTypes filetypes;
052
053    @Inject
054    private RepositoryContentConsumers repositoryContentConsumers;
055
056    private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<RepositoryScannerInstance>();
057
058    @Override
059    public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
060        throws RepositoryScannerException
061    {
062        List<KnownRepositoryContentConsumer> knownContentConsumers = null;
063        try
064        {
065            knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
066            List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
067            List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
068
069            return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
070        }
071        catch ( RepositoryAdminException e )
072        {
073            throw new RepositoryScannerException( e.getMessage(), e );
074        } finally
075        {
076            repositoryContentConsumers.releaseSelectedKnownConsumers( knownContentConsumers );
077        }
078    }
079
080    @Override
081    public RepositoryScanStatistics scan( ManagedRepository repository,
082                                          List<KnownRepositoryContentConsumer> knownContentConsumers,
083                                          List<InvalidRepositoryContentConsumer> invalidContentConsumers,
084                                          List<String> ignoredContentPatterns, long changesSince )
085        throws RepositoryScannerException
086    {
087        if ( repository == null )
088        {
089            throw new IllegalArgumentException( "Unable to operate on a null repository." );
090        }
091
092        File repositoryBase = new File( repository.getLocation() );
093
094        //MRM-1342 Repository statistics report doesn't appear to be working correctly
095        //create the repo if not existing to have an empty stats
096        if ( !repositoryBase.exists() && !repositoryBase.mkdirs() )
097        {
098            throw new UnsupportedOperationException(
099                "Unable to scan a repository, directory " + repositoryBase.getPath() + " does not exist." );
100        }
101
102        if ( !repositoryBase.isDirectory() )
103        {
104            throw new UnsupportedOperationException(
105                "Unable to scan a repository, path " + repositoryBase.getPath() + " is not a directory." );
106        }
107
108        // Setup Includes / Excludes.
109
110        List<String> allExcludes = new ArrayList<>();
111        List<String> allIncludes = new ArrayList<>();
112
113        if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
114        {
115            allExcludes.addAll( ignoredContentPatterns );
116        }
117
118        // Scan All Content. (intentional)
119        allIncludes.add( "**/*" );
120
121        // Setup Directory Walker
122        DirectoryWalker dirWalker = new DirectoryWalker();
123
124        dirWalker.setBaseDir( repositoryBase );
125
126        dirWalker.setIncludes( allIncludes );
127        dirWalker.setExcludes( allExcludes );
128
129        // Setup the Scan Instance
130        RepositoryScannerInstance scannerInstance =
131            new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
132
133        inProgressScans.add( scannerInstance );
134
135        RepositoryScanStatistics stats;
136        try
137        {
138            dirWalker.addDirectoryWalkListener( scannerInstance );
139
140            // Execute scan.
141            dirWalker.scan();
142
143            stats = scannerInstance.getStatistics();
144
145            stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
146            stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
147        }
148        finally
149        {
150            inProgressScans.remove( scannerInstance );
151        }
152
153        return stats;
154    }
155
156    private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
157    {
158        List<String> ids = new ArrayList<>();
159        for ( RepositoryContentConsumer consumer : consumers )
160        {
161            ids.add( consumer.getId() );
162        }
163        return ids;
164    }
165
166    @Override
167    public Set<RepositoryScannerInstance> getInProgressScans()
168    {
169        return inProgressScans;
170    }
171}