View Javadoc

1   package org.apache.maven.index.locator;
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  import java.io.FileInputStream;
24  import java.io.IOException;
25  
26  import org.apache.maven.index.ArtifactContext.ModelReader;
27  import org.apache.maven.index.artifact.ArtifactPackagingMapper;
28  import org.apache.maven.index.artifact.Gav;
29  import org.apache.maven.index.artifact.GavCalculator;
30  import org.apache.maven.model.Model;
31  
32  /**
33   * Artifact locator.
34   * 
35   * @author Damian Bradicich
36   */
37  public class ArtifactLocator
38      implements GavHelpedLocator
39  {
40      private final ArtifactPackagingMapper mapper;
41  
42      public ArtifactLocator( ArtifactPackagingMapper mapper )
43      {
44          this.mapper = mapper;
45      }
46  
47      public File locate( File source, GavCalculator gavCalculator, Gav gav )
48      {
49          // if we don't have this data, nothing we can do
50          if ( source == null || !source.exists() || gav == null || gav.getArtifactId() == null
51              || gav.getVersion() == null )
52          {
53              return null;
54          }
55  
56          try
57          {
58              // need to read the pom model to get packaging
59              Model model = new ModelReader().readModel( new FileInputStream( source ) );
60  
61              if ( model == null )
62              {
63                  return null;
64              }
65  
66              // now generate the artifactname
67              String artifactName =
68                  gav.getArtifactId() + "-" + gav.getVersion() + "."
69                      + mapper.getExtensionForPackaging( model.getPackaging() );
70  
71              File artifact = new File( source.getParent(), artifactName );
72  
73              if ( !artifact.exists() )
74              {
75                  return null;
76              }
77  
78              return artifact;
79          }
80          catch ( IOException e )
81          {
82              return null;
83          }
84      }
85  }