]> source.dussan.org Git - archiva.git/blob
6257880dd411ac94ce8f78ea0a181fa2fe9c1fe2
[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.ArchivaConfiguration;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
27 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.apache.archiva.consumers.RepositoryContentConsumer;
29 import org.apache.commons.collections.CollectionUtils;
30 import org.codehaus.plexus.util.DirectoryWalker;
31 import org.springframework.stereotype.Service;
32
33 import javax.inject.Inject;
34 import java.io.File;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.LinkedHashSet;
38 import java.util.List;
39 import java.util.Set;
40
41 /**
42  * DefaultRepositoryScanner
43  *
44  *
45  */
46 @Service( "repositoryScanner#default" )
47 public class DefaultRepositoryScanner
48     implements RepositoryScanner
49 {
50     @Inject
51     private FileTypes filetypes;
52
53     @Inject
54     private RepositoryContentConsumers repositoryContentConsumers;
55
56     private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<>();
57
58     @Override
59     public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
60         throws RepositoryScannerException
61     {
62         List<KnownRepositoryContentConsumer> knownContentConsumers = null;
63         try
64         {
65             knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
66             List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
67             List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
68
69             return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
70         }
71         catch ( RepositoryAdminException e )
72         {
73             throw new RepositoryScannerException( e.getMessage(), e );
74         } finally
75         {
76             repositoryContentConsumers.releaseSelectedKnownConsumers( knownContentConsumers );
77         }
78     }
79
80     @Override
81     public RepositoryScanStatistics scan( ManagedRepository repository,
82                                           List<KnownRepositoryContentConsumer> knownContentConsumers,
83                                           List<InvalidRepositoryContentConsumer> invalidContentConsumers,
84                                           List<String> ignoredContentPatterns, long changesSince )
85         throws RepositoryScannerException
86     {
87         if ( repository == null )
88         {
89             throw new IllegalArgumentException( "Unable to operate on a null repository." );
90         }
91
92         File repositoryBase = new File( repository.getLocation() );
93
94         //MRM-1342 Repository statistics report doesn't appear to be working correctly
95         //create the repo if not existing to have an empty stats
96         if ( !repositoryBase.exists() && !repositoryBase.mkdirs() )
97         {
98             throw new UnsupportedOperationException(
99                 "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 }