This project has retired. For details please refer to its
Attic page.
DefaultRepositoryScanner xref
1 package org.apache.archiva.repository.scanner;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.archiva.repository.storage.StorageAsset;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Service;
33
34 import javax.inject.Inject;
35 import java.io.IOException;
36 import java.nio.file.FileVisitOption;
37 import java.nio.file.Files;
38 import java.util.*;
39
40
41
42
43
44
45 @Service( "repositoryScanner#default" )
46 public class DefaultRepositoryScanner
47 implements RepositoryScanner
48 {
49
50 private static final Logger log = LoggerFactory.getLogger(DefaultRepositoryScanner.class);
51
52 @Inject
53 private FileTypes filetypes;
54
55 @Inject
56 private RepositoryContentConsumers repositoryContentConsumers;
57
58 private Set<RepositoryScannerInstance> inProgressScans = new LinkedHashSet<>();
59
60 @Override
61 public RepositoryScanStatistics scan( ManagedRepository repository, long changesSince )
62 throws RepositoryScannerException
63 {
64 List<KnownRepositoryContentConsumer> knownContentConsumers = null;
65 try
66 {
67 knownContentConsumers = repositoryContentConsumers.getSelectedKnownConsumers();
68 List<InvalidRepositoryContentConsumer> invalidContentConsumers = repositoryContentConsumers.getSelectedInvalidConsumers();
69 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
70
71 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
72 }
73 catch ( RepositoryAdminException e )
74 {
75 throw new RepositoryScannerException( e.getMessage(), e );
76 } finally
77 {
78 repositoryContentConsumers.releaseSelectedKnownConsumers( knownContentConsumers );
79 }
80 }
81
82 @Override
83 public RepositoryScanStatistics scan( ManagedRepository repository,
84 List<KnownRepositoryContentConsumer> knownContentConsumers,
85 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
86 List<String> ignoredContentPatterns, long changesSince )
87 throws RepositoryScannerException
88 {
89 if ( repository == null )
90 {
91 throw new IllegalArgumentException( "Unable to operate on a null repository." );
92 }
93
94 StorageAsset repositoryBase = repository.getAsset("");
95
96
97
98 if ( !repositoryBase.exists())
99 {
100 try {
101 repositoryBase.create();
102 } catch (IOException e) {
103 throw new UnsupportedOperationException("Unable to scan a repository, directory " + repositoryBase + " does not exist." );
104 }
105 }
106
107 if ( !repositoryBase.isContainer())
108 {
109 throw new UnsupportedOperationException(
110 "Unable to scan a repository, path " + repositoryBase+ " is not a directory." );
111 }
112
113
114
115 List<String> allExcludes = new ArrayList<>();
116 List<String> allIncludes = new ArrayList<>();
117
118 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
119 {
120 allExcludes.addAll( ignoredContentPatterns );
121 }
122
123
124 allIncludes.add( "**/*" );
125
126
127 RepositoryScannerInstance scannerInstance =
128 new RepositoryScannerInstance( repository, knownContentConsumers, invalidContentConsumers, changesSince );
129
130 scannerInstance.setFileNameIncludePattern(allIncludes);
131 scannerInstance.setFileNameExcludePattern(allExcludes);
132 inProgressScans.add( scannerInstance );
133
134 RepositoryScanStatistics stats = null;
135 try
136 {
137 Files.walkFileTree(repositoryBase.getFilePath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, scannerInstance);
138
139 stats = scannerInstance.getStatistics();
140
141 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
142 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
143 } catch (IOException e) {
144 log.error("Could not scan directory {}: {}", repositoryBase, e.getMessage(), e);
145 } finally
146 {
147 inProgressScans.remove( scannerInstance );
148 }
149
150 return stats;
151 }
152
153 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
154 {
155 List<String> ids = new ArrayList<>();
156 for ( RepositoryContentConsumer consumer : consumers )
157 {
158 ids.add( consumer.getId() );
159 }
160 return ids;
161 }
162
163 @Override
164 public Set<RepositoryScannerInstance> getInProgressScans()
165 {
166 return inProgressScans;
167 }
168 }