1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.index;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.File;
26 import java.util.Arrays;
27 import java.util.Comparator;
28 import java.util.Set;
29 import java.util.TreeSet;
30
31 import org.apache.maven.index.context.IndexingContext;
32
33
34
35
36
37
38
39 @Singleton
40 @Named
41 public class DefaultScanner implements Scanner {
42
43 private final ArtifactContextProducer artifactContextProducer;
44
45 @Inject
46 public DefaultScanner(ArtifactContextProducer artifactContextProducer) {
47 this.artifactContextProducer = artifactContextProducer;
48 }
49
50 public ScanningResult scan(ScanningRequest request) {
51 request.getArtifactScanningListener().scanningStarted(request.getIndexingContext());
52
53 ScanningResult result = new ScanningResult(request);
54
55 scanDirectory(request.getStartingDirectory(), request);
56
57 request.getArtifactScanningListener().scanningFinished(request.getIndexingContext(), result);
58
59 return result;
60 }
61
62 private void scanDirectory(File dir, ScanningRequest request) {
63 if (dir == null) {
64 return;
65 }
66
67 File[] fileArray = dir.listFiles();
68
69 if (fileArray != null) {
70 Set<File> files = new TreeSet<>(new ScannerFileComparator());
71
72 files.addAll(Arrays.asList(fileArray));
73
74 for (File f : files) {
75 if (f.getName().startsWith(".")) {
76 continue;
77 }
78
79 if (f.isDirectory()) {
80 scanDirectory(f, request);
81 }
82
83
84
85
86 else {
87 processFile(f, request);
88 }
89 }
90 }
91 }
92
93 private void processFile(File file, ScanningRequest request) {
94 IndexingContext context = request.getIndexingContext();
95
96 ArtifactContext ac = artifactContextProducer.getArtifactContext(context, file);
97
98 if (ac != null) {
99 request.getArtifactScanningListener().artifactDiscovered(ac);
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 private static class ScannerFileComparator implements Comparator<File> {
113 public int compare(File o1, File o2) {
114 if (o1.getName().endsWith(".pom") && !o2.getName().endsWith(".pom")) {
115
116 return 1;
117 } else if (!o1.getName().endsWith(".pom") && o2.getName().endsWith(".pom")) {
118
119 return -1;
120 } else {
121
122
123
124 return o2.getName().compareTo(o1.getName());
125 }
126 }
127 }
128 }