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 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   * A default implementation of the {@link ArtifactContextProducer}.
38   * 
39   * @author Tamas Cservenak
40   * @author Eugene Kuleshov
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       * Get ArtifactContext for given pom or artifact (jar, war, etc). A file can be
55       */
56      public ArtifactContext getArtifactContext( IndexingContext context, File file )
57      {
58          // TODO shouldn't this use repository layout instead?
59  
60          String repositoryPath = context.getRepository().getAbsolutePath();
61          String artifactPath = file.getAbsolutePath();
62  
63          // protection from IndexOutOfBounds
64          if ( artifactPath.length() <= repositoryPath.length() )
65          {
66              return null; // not an artifact
67          }
68  
69          if ( !isIndexable( file ) )
70          {
71              return null; // skipped
72          }
73  
74          Gav gav = getGavFromPath( context, repositoryPath, artifactPath );
75  
76          if ( gav == null )
77          {
78              return null; // not an artifact, but rather metadata
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              // If we found the matching artifact, switch over to indexing that, instead of the pom
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         // store extension if classifier is not empty
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         // || filename.endsWith( "-javadoc.jar" )
138         // || filename.endsWith( "-javadocs.jar" )
139         // || filename.endsWith( "-sources.jar" )
140             || filename.endsWith( ".properties" )
141             // || filename.endsWith( ".xml" ) // NEXUS-3029
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 }