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