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