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.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
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<RepositoryScannerInstance>();
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
95
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
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
119 allIncludes.add( "**/*" );
120
121
122 DirectoryWalker dirWalker = new DirectoryWalker();
123
124 dirWalker.setBaseDir( repositoryBase );
125
126 dirWalker.setIncludes( allIncludes );
127 dirWalker.setExcludes( allExcludes );
128
129
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
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 }