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 java.util.List;
23  
24  import org.apache.maven.RepositoryUtils;
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.project.ProjectBuildingRequest;
27  import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
28  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
29  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
30  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
31  import org.codehaus.plexus.PlexusConstants;
32  import org.codehaus.plexus.PlexusContainer;
33  import org.codehaus.plexus.component.annotations.Component;
34  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
35  import org.codehaus.plexus.context.Context;
36  import org.codehaus.plexus.context.ContextException;
37  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
38  
39  
40  /**
41   * 
42   */
43  @Component( role = ArtifactResolver.class, hint = "default" )
44  class DefaultArtifactResolver
45      implements ArtifactResolver, Contextualizable
46  {
47      private PlexusContainer container;
48  
49      @Override
50      public ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
51          throws ArtifactResolverException, IllegalArgumentException
52      {
53          validateParameters( buildingRequest, mavenArtifact );
54          try
55          {
56              return getMavenArtifactResolver( buildingRequest ).resolveArtifact( 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              return getMavenArtifactResolver( buildingRequest ).resolveArtifact( coordinate );
72          }
73          catch ( ComponentLookupException e )
74          {
75              throw new ArtifactResolverException( e.getMessage(), e );
76          }
77      }
78  
79      private void validateParameters( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
80      {
81          if ( buildingRequest == null )
82          {
83              throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
84          }
85          if ( mavenArtifact == null )
86          {
87              throw new IllegalArgumentException( "The parameter mavenArtifact is not allowed to be null." );
88          }
89      }
90  
91      private void validateParameters( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
92      {
93          if ( buildingRequest == null )
94          {
95              throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
96          }
97          if ( coordinate == null )
98          {
99              throw new IllegalArgumentException( "The parameter coordinate is not allowed to be null." );
100         }
101     }
102 
103     /**
104      * @return true if the current Maven version is Maven 3.1.
105      */
106     private boolean isMaven31()
107     {
108         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
109     }
110 
111     private boolean canFindCoreClass( String className )
112     {
113         try
114         {
115             Thread.currentThread().getContextClassLoader().loadClass( className );
116 
117             return true;
118         }
119         catch ( ClassNotFoundException e )
120         {
121             return false;
122         }
123     }
124 
125     /**
126      * Injects the Plexus content.
127      *
128      * @param context Plexus context to inject.
129      * @throws ContextException if the PlexusContainer could not be located.
130      */
131     public void contextualize( Context context )
132         throws ContextException
133     {
134         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
135     }
136     
137     private MavenArtifactResolver getMavenArtifactResolver( ProjectBuildingRequest buildingRequest )
138         throws ComponentLookupException, ArtifactResolverException
139     {
140         if ( isMaven31() )
141         {
142             org.eclipse.aether.RepositorySystem repositorySystem =
143                             container.lookup( org.eclipse.aether.RepositorySystem.class );
144             
145             @SuppressWarnings( "unchecked" )
146             List<org.eclipse.aether.repository.RemoteRepository> aetherRepositories =
147                 (List<org.eclipse.aether.repository.RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos",
148                                                                            List.class,
149                                                                            buildingRequest.getRemoteRepositories() );
150 
151             org.eclipse.aether.RepositorySystemSession session =
152                 (org.eclipse.aether.RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
153             
154             return new Maven31ArtifactResolver( repositorySystem, aetherRepositories, session );
155             
156         }
157         else
158         {
159             org.sonatype.aether.RepositorySystem repositorySystem =
160                             container.lookup( org.sonatype.aether.RepositorySystem.class );
161             
162             @SuppressWarnings( "unchecked" )
163             List<org.sonatype.aether.repository.RemoteRepository> aetherRepositories =
164                 (List<org.sonatype.aether.repository.RemoteRepository>) Invoker.invoke( RepositoryUtils.class,
165                                                                             "toRepos", List.class,
166                                                                             buildingRequest.getRemoteRepositories() );
167 
168             org.sonatype.aether.RepositorySystemSession session =
169                 (org.sonatype.aether.RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
170             
171             return new Maven30ArtifactResolver( repositorySystem, aetherRepositories, session );
172         }
173 
174 
175     }
176 }