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 | |
|
24 | |
import org.apache.maven.index.artifact.ArtifactPackagingMapper; |
25 | |
import org.apache.maven.index.artifact.Gav; |
26 | |
import org.apache.maven.index.context.IndexingContext; |
27 | |
import org.apache.maven.index.locator.ArtifactLocator; |
28 | |
import org.apache.maven.index.locator.GavHelpedLocator; |
29 | |
import org.apache.maven.index.locator.Locator; |
30 | |
import org.apache.maven.index.locator.MetadataLocator; |
31 | |
import org.apache.maven.index.locator.PomLocator; |
32 | |
import org.codehaus.plexus.component.annotations.Component; |
33 | |
import org.codehaus.plexus.component.annotations.Requirement; |
34 | |
import org.codehaus.plexus.util.StringUtils; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
@Component( role = ArtifactContextProducer.class ) |
43 | 208 | public class DefaultArtifactContextProducer |
44 | |
implements ArtifactContextProducer |
45 | |
{ |
46 | |
@Requirement |
47 | |
private ArtifactPackagingMapper mapper; |
48 | |
|
49 | 208 | private GavHelpedLocator pl = new PomLocator(); |
50 | |
|
51 | 208 | private Locator ml = new MetadataLocator(); |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public ArtifactContext getArtifactContext( IndexingContext context, File file ) |
57 | |
{ |
58 | |
|
59 | |
|
60 | 27789 | String repositoryPath = context.getRepository().getAbsolutePath(); |
61 | 27789 | String artifactPath = file.getAbsolutePath(); |
62 | |
|
63 | |
|
64 | 27789 | if ( artifactPath.length() <= repositoryPath.length() ) |
65 | |
{ |
66 | 0 | return null; |
67 | |
} |
68 | |
|
69 | 27789 | if ( !isIndexable( file ) ) |
70 | |
{ |
71 | 18116 | return null; |
72 | |
} |
73 | |
|
74 | 9673 | Gav gav = getGavFromPath( context, repositoryPath, artifactPath ); |
75 | |
|
76 | 9673 | if ( gav == null ) |
77 | |
{ |
78 | 572 | return null; |
79 | |
} |
80 | |
|
81 | |
File pom; |
82 | |
File artifact; |
83 | |
|
84 | 9101 | if ( file.getName().endsWith( ".pom" ) ) |
85 | |
{ |
86 | 3311 | ArtifactLocator al = new ArtifactLocator( mapper ); |
87 | 3311 | artifact = al.locate( file, context.getGavCalculator(), gav ); |
88 | |
|
89 | |
|
90 | 3311 | if ( artifact != null ) |
91 | |
{ |
92 | 3165 | gav = getGavFromPath( context, repositoryPath, artifact.getAbsolutePath() ); |
93 | |
} |
94 | |
|
95 | 3311 | pom = file; |
96 | 3311 | } |
97 | |
else |
98 | |
{ |
99 | 5790 | artifact = file; |
100 | 5790 | pom = pl.locate( file, context.getGavCalculator(), gav ); |
101 | |
} |
102 | |
|
103 | 9101 | String groupId = gav.getGroupId(); |
104 | |
|
105 | 9101 | String artifactId = gav.getArtifactId(); |
106 | |
|
107 | 9101 | String version = gav.getBaseVersion(); |
108 | |
|
109 | 9101 | String classifier = gav.getClassifier(); |
110 | |
|
111 | 9101 | ArtifactInfo ai = new ArtifactInfo( context.getRepositoryId(), groupId, artifactId, version, classifier ); |
112 | |
|
113 | |
|
114 | 9101 | if ( !StringUtils.isEmpty( ai.classifier ) ) |
115 | |
{ |
116 | 2542 | ai.packaging = gav.getExtension(); |
117 | |
} |
118 | |
|
119 | 9101 | ai.fname = file.getName(); |
120 | 9101 | ai.fextension = gav.getExtension(); |
121 | |
|
122 | 9101 | File metadata = ml.locate( pom ); |
123 | |
|
124 | 9101 | return new ArtifactContext( pom, artifact, metadata, ai, gav ); |
125 | |
} |
126 | |
|
127 | |
private boolean isIndexable( File file ) |
128 | |
{ |
129 | 27789 | if ( file == null ) |
130 | |
{ |
131 | 0 | return false; |
132 | |
} |
133 | |
|
134 | 27789 | String filename = file.getName(); |
135 | |
|
136 | 27789 | if ( filename.equals( "maven-metadata.xml" ) |
137 | |
|
138 | |
|
139 | |
|
140 | |
|| filename.endsWith( ".properties" ) |
141 | |
|
142 | |
|| filename.endsWith( ".asc" ) || filename.endsWith( ".md5" ) || filename.endsWith( ".sha1" ) ) |
143 | |
{ |
144 | 18116 | return false; |
145 | |
} |
146 | |
|
147 | 9673 | return true; |
148 | |
} |
149 | |
|
150 | |
private Gav getGavFromPath( IndexingContext context, String repositoryPath, String artifactPath ) |
151 | |
{ |
152 | 12838 | String path = artifactPath.substring( repositoryPath.length() + 1 ).replace( '\\', '/' ); |
153 | |
|
154 | 12838 | return context.getGavCalculator().pathToGav( path ); |
155 | |
} |
156 | |
|
157 | |
} |