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 public class DefaultArtifactContextProducer
44 implements ArtifactContextProducer
45 {
46 @Requirement
47 private ArtifactPackagingMapper mapper;
48
49 private GavHelpedLocator pl = new PomLocator();
50
51 private Locator ml = new MetadataLocator();
52
53
54
55
56 public ArtifactContext getArtifactContext( IndexingContext context, File file )
57 {
58
59
60 String repositoryPath = context.getRepository().getAbsolutePath();
61 String artifactPath = file.getAbsolutePath();
62
63
64 if ( artifactPath.length() <= repositoryPath.length() )
65 {
66 return null;
67 }
68
69 if ( !isIndexable( file ) )
70 {
71 return null;
72 }
73
74 Gav gav = getGavFromPath( context, repositoryPath, artifactPath );
75
76 if ( gav == null )
77 {
78 return null;
79 }
80
81 File pom;
82 File artifact;
83
84 if ( file.getName().endsWith( ".pom" ) )
85 {
86 ArtifactLocator al = new ArtifactLocator( mapper );
87 artifact = al.locate( file, context.getGavCalculator(), gav );
88
89
90 if ( artifact != null )
91 {
92 gav = getGavFromPath( context, repositoryPath, artifact.getAbsolutePath() );
93 }
94
95 pom = file;
96 }
97 else
98 {
99 artifact = file;
100 pom = pl.locate( file, context.getGavCalculator(), gav );
101 }
102
103 String groupId = gav.getGroupId();
104
105 String artifactId = gav.getArtifactId();
106
107 String version = gav.getBaseVersion();
108
109 String classifier = gav.getClassifier();
110
111 ArtifactInfo ai = new ArtifactInfo( context.getRepositoryId(), groupId, artifactId, version, classifier );
112
113
114 if ( !StringUtils.isEmpty( ai.classifier ) )
115 {
116 ai.packaging = gav.getExtension();
117 }
118
119 ai.fname = file.getName();
120 ai.fextension = gav.getExtension();
121
122 File metadata = ml.locate( pom );
123
124 return new ArtifactContext( pom, artifact, metadata, ai, gav );
125 }
126
127 private boolean isIndexable( File file )
128 {
129 if ( file == null )
130 {
131 return false;
132 }
133
134 String filename = file.getName();
135
136 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 return false;
145 }
146
147 return true;
148 }
149
150 private Gav getGavFromPath( IndexingContext context, String repositoryPath, String artifactPath )
151 {
152 String path = artifactPath.substring( repositoryPath.length() + 1 ).replace( '\\', '/' );
153
154 return context.getGavCalculator().pathToGav( path );
155 }
156
157 }