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