View Javadoc
1   package org.apache.maven.shared.transfer.artifact.resolve.internal;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.project.ProjectBuildingRequest;
24  import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
25  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
26  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
27  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
28  import org.codehaus.plexus.PlexusConstants;
29  import org.codehaus.plexus.PlexusContainer;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
32  import org.codehaus.plexus.context.Context;
33  import org.codehaus.plexus.context.ContextException;
34  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
35  
36  /**
37   * 
38   */
39  @Component( role = ArtifactResolver.class, hint = "default" )
40  class DefaultArtifactResolver
41      implements ArtifactResolver, Contextualizable
42  {
43      private PlexusContainer container;
44  
45      @Override
46      public ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
47          throws ArtifactResolverException, IllegalArgumentException
48      {
49          validateParameters( buildingRequest, mavenArtifact );
50          try
51          {
52              String hint = isMaven31() ? "maven31" : "maven3";
53  
54              ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
55  
56              return effectiveArtifactResolver.resolveArtifact( buildingRequest, mavenArtifact );
57          }
58          catch ( ComponentLookupException e )
59          {
60              throw new ArtifactResolverException( e.getMessage(), e );
61          }
62      }
63  
64      @Override
65      public ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
66          throws ArtifactResolverException, IllegalArgumentException
67      {
68          validateParameters( buildingRequest, coordinate );
69          try
70          {
71              String hint = isMaven31() ? "maven31" : "maven3";
72  
73              ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
74  
75              return effectiveArtifactResolver.resolveArtifact( buildingRequest, coordinate );
76          }
77          catch ( ComponentLookupException e )
78          {
79              throw new ArtifactResolverException( e.getMessage(), e );
80          }
81      }
82  
83      private void validateParameters( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
84      {
85          if ( buildingRequest == null )
86          {
87              throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
88          }
89          if ( mavenArtifact == null )
90          {
91              throw new IllegalArgumentException( "The parameter mavenArtifact is not allowed to be null." );
92          }
93      }
94  
95      private void validateParameters( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
96      {
97          if ( buildingRequest == null )
98          {
99              throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
100         }
101         if ( coordinate == null )
102         {
103             throw new IllegalArgumentException( "The parameter coordinate is not allowed to be null." );
104         }
105     }
106 
107     /**
108      * @return true if the current Maven version is Maven 3.1.
109      */
110     private boolean isMaven31()
111     {
112         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
113     }
114 
115     private boolean canFindCoreClass( String className )
116     {
117         try
118         {
119             Thread.currentThread().getContextClassLoader().loadClass( className );
120 
121             return true;
122         }
123         catch ( ClassNotFoundException e )
124         {
125             return false;
126         }
127     }
128 
129     /**
130      * Injects the Plexus content.
131      *
132      * @param context Plexus context to inject.
133      * @throws ContextException if the PlexusContainer could not be located.
134      */
135     public void contextualize( Context context )
136         throws ContextException
137     {
138         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
139     }
140 }