View Javadoc
1   package org.apache.maven.index;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * 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
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       * Get ArtifactContext for given pom or artifact (jar, war, etc). A file can be
64       */
65      public ArtifactContext getArtifactContext( IndexingContext context, File file )
66      {
67          // TODO shouldn't this use repository layout instead?
68  
69          String repositoryPath = context.getRepository().getAbsolutePath();
70          String artifactPath = file.getAbsolutePath();
71  
72          // protection from IndexOutOfBounds
73          if ( artifactPath.length() <= repositoryPath.length() )
74          {
75              return null; // not an artifact
76          }
77  
78          if ( !isIndexable( file ) )
79          {
80              return null; // skipped
81          }
82  
83          Gav gav = getGavFromPath( context, repositoryPath, artifactPath );
84  
85          if ( gav == null )
86          {
87              return null; // not an artifact, but rather metadata
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              // If we found the matching artifact, switch over to indexing that, instead of the pom
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         // store extension if classifier is not empty
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                 && !( filename.startsWith( "maven-metadata-" ) && filename.endsWith( ".xml" ) )
148                 // || filename.endsWith( "-javadoc.jar" )
149                 // || filename.endsWith( "-javadocs.jar" )
150                 // || filename.endsWith( "-sources.jar" )
151                 && !filename.endsWith( ".properties" )
152                 // || filename.endsWith( ".xml" ) // NEXUS-3029
153                 && !filename.endsWith( ".asc" ) && !filename.endsWith( ".md5" ) && !filename.endsWith( ".sha1" );
154     }
155 
156     protected Gav getGavFromPath( IndexingContext context, String repositoryPath, String artifactPath )
157     {
158         String path = artifactPath.substring( repositoryPath.length() + 1 ).replace( '\\', '/' );
159 
160         return context.getGavCalculator().pathToGav( path );
161     }
162 
163 }