View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * A default implementation of the {@link ArtifactContextProducer}.
39   *
40   * @author Tamas Cservenak
41   * @author Eugene Kuleshov
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       * Get ArtifactContext for given pom or artifact (jar, war, etc). A file can be
60       */
61      public ArtifactContext getArtifactContext(IndexingContext context, File file) {
62          // TODO shouldn't this use repository layout instead?
63  
64          String repositoryPath = context.getRepository().getAbsolutePath();
65          String artifactPath = file.getAbsolutePath();
66  
67          // protection from IndexOutOfBounds
68          if (artifactPath.length() <= repositoryPath.length()) {
69              return null; // not an artifact
70          }
71  
72          if (!isIndexable(file)) {
73              return null; // skipped
74          }
75  
76          Gav gav = getGavFromPath(context, repositoryPath, artifactPath);
77  
78          if (gav == null) {
79              return null; // not an artifact, but rather metadata
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              // If we found the matching artifact, switch over to indexing that, instead of the pom
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         // store extension if classifier is not empty
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                 // || filename.endsWith( "-javadoc.jar" )
135                 // || filename.endsWith( "-javadocs.jar" )
136                 // || filename.endsWith( "-sources.jar" )
137                 && !filename.endsWith(".properties")
138                 // || filename.endsWith( ".xml" ) // NEXUS-3029
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 }