]> source.dussan.org Git - archiva.git/blob
7e080158d17f8656ebc7a2ef5572bb4ab5de6644
[archiva.git] /
1 package org.apache.archiva.repository.scanner;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.configuration.FileTypes;
24 import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
25 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
26 import org.apache.archiva.consumers.RepositoryContentConsumer;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
32
33 import javax.inject.Inject;
34 import java.io.IOException;
35 import java.nio.file.FileVisitOption;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.ArrayList;
40 import java.util.EnumSet;
41 import java.util.LinkedHashSet;
42 import java.util.List;
43 import java.util.Set;
44
45 /**
46  * DefaultRepositoryScanner
47  *
48  *
49  */
50 @Service( "repositoryScanner#default" )
51 public class DefaultRepositoryScanner
52     implements RepositoryScanner
53 {
54
55     private static final Logger log  = LoggerFactory.getLogger(DefaultRepositoryScanner.class);
56
57     @Inject
58     private FileTypes filetypes;
59
60     @Inject
61     private RepositoryContentConsumers repositoryContentConsumers;
62
63     private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<>();
64
65     @Override
66     public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
67         throws RepositoryScannerException
68     {
69         List<KnownRepositoryContentConsumer> knownContentConsumers = null;
70         try
71         {
72             knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
73             List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
74             List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
75
76             return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
77         }
78         catch ( RepositoryAdminException e )
79         {
80             throw new RepositoryScannerException( e.getMessage(), e );
81         } finally
82         {
83             repositoryContentConsumers.releaseSelectedKnownConsumers( knownContentConsumers );
84         }
85     }
86
87     @Override
88     public RepositoryScanStatistics scan( ManagedRepository repository,
89                                           List<KnownRepositoryContentConsumer> knownContentConsumers,
90                                           List<InvalidRepositoryContentConsumer> invalidContentConsumers,
91                                           List<String> ignoredContentPatterns, long changesSince )
92         throws RepositoryScannerException
93     {
94         if ( repository == null )
95         {
96             throw new IllegalArgumentException( "Unable to operate on a null repository." );
97         }
98
99         Path repositoryBase = Paths.get( repository.getLocation() );
100
101         //MRM-1342 Repository statistics report doesn't appear to be working correctly
102         //create the repo if not existing to have an empty stats
103         if ( !Files.exists(repositoryBase))
104         {
105             try {
106                 Files.createDirectories(repositoryBase);
107             } catch (IOException e) {
108                 throw new UnsupportedOperationException("Unable to scan a repository, directory " + repositoryBase + " does not exist." );
109             }
110         }
111
112         if ( !Files.isDirectory(repositoryBase) )
113         {
114             throw new UnsupportedOperationException(
115                 "Unable to scan a repository, path " + repositoryBase+ " is not a directory." );
116         }
117
118         // Setup Includes / Excludes.
119
120         List<String> allExcludes = new ArrayList<>();
121         List<String> allIncludes = new ArrayList<>();
122
123         if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
124         {
125             allExcludes.addAll( ignoredContentPatterns );
126         }
127
128         // Scan All Content. (intentional)
129         allIncludes.add( "**/*" );
130
131         // Setup the Scan Instance
132         RepositoryScannerInstance scannerInstance =
133             new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
134
135         scannerInstance.setFileNameIncludePattern(allIncludes);
136         scannerInstance.setFileNameExcludePattern(allExcludes);
137         inProgressScans.add( scannerInstance );
138
139         RepositoryScanStatistics stats = null;
140         try
141         {
142             Files.walkFileTree(repositoryBase, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, scannerInstance);
143
144             stats = scannerInstance.getStatistics();
145
146             stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
147             stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
148         } catch (IOException e) {
149             log.error("Could not scan directory {}", repositoryBase);
150         } finally
151         {
152             inProgressScans.remove( scannerInstance );
153         }
154
155         return stats;
156     }
157
158     private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
159     {
160         List<String> ids = new ArrayList<>();
161         for ( RepositoryContentConsumer consumer : consumers )
162         {
163             ids.add( consumer.getId() );
164         }
165         return ids;
166     }
167
168     @Override
169     public Set<RepositoryScannerInstance> getInProgressScans()
170     {
171         return inProgressScans;
172     }
173 }