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