View Javadoc
1   package org.apache.maven.shared.transfer.dependencies.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.Collection;
23  
24  import org.apache.maven.model.Dependency;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.project.ProjectBuildingRequest;
27  import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter;
28  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
29  import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
30  import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
31  import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
32  import org.codehaus.plexus.PlexusConstants;
33  import org.codehaus.plexus.PlexusContainer;
34  import org.codehaus.plexus.component.annotations.Component;
35  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
36  import org.codehaus.plexus.context.Context;
37  import org.codehaus.plexus.context.ContextException;
38  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
39  
40  /**
41   * 
42   */
43  @Component( role = DependencyResolver.class, hint = "default" )
44  class DefaultDependencyResolver
45      implements DependencyResolver, Contextualizable
46  {
47      private PlexusContainer container;
48  
49      @Override
50      public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
51                                                           Collection<Dependency> coordinates,
52                                                           Collection<Dependency> managedDependencies,
53                                                           TransformableFilter filter )
54          throws DependencyResolverException
55      {
56          validateBuildingRequest( buildingRequest );
57          
58          try
59          {
60              String hint = isMaven31() ? "maven31" : "maven3";
61  
62              DependencyResolver effectiveArtifactResolver = container.lookup( DependencyResolver.class, hint );
63  
64              return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinates, null, filter );
65          }
66          catch ( ComponentLookupException e )
67          {
68              throw new DependencyResolverException( e.getMessage(), e );
69          }
70      }
71  
72      @Override
73      public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
74                                                           DependableCoordinate coordinate, TransformableFilter filter )
75          throws DependencyResolverException
76      {
77          validateParameters( buildingRequest, coordinate, filter );
78          try
79          {
80              String hint = isMaven31() ? "maven31" : "maven3";
81  
82              DependencyResolver effectiveArtifactResolver = container.lookup( DependencyResolver.class, hint );
83  
84              return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinate, filter );
85          }
86          catch ( ComponentLookupException e )
87          {
88              throw new DependencyResolverException( e.getMessage(), e );
89          }
90      }
91  
92      @Override
93      public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, Model model,
94                                                           TransformableFilter filter )
95          throws DependencyResolverException
96      {
97          validateParameters( buildingRequest, model, filter );
98          try
99          {
100             String hint = isMaven31() ? "maven31" : "maven3";
101 
102             DependencyResolver effectiveArtifactResolver = container.lookup( DependencyResolver.class, hint );
103 
104             return effectiveArtifactResolver.resolveDependencies( buildingRequest, model, filter );
105         }
106         catch ( ComponentLookupException e )
107         {
108             throw new DependencyResolverException( e.getMessage(), e );
109         }
110     }
111 
112     /**
113      * @return true if the current Maven version is Maven 3.1.
114      */
115     protected static boolean isMaven31()
116     {
117         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
118     }
119 
120     private static boolean canFindCoreClass( String className )
121     {
122         try
123         {
124             Thread.currentThread().getContextClassLoader().loadClass( className );
125 
126             return true;
127         }
128         catch ( ClassNotFoundException e )
129         {
130             return false;
131         }
132     }
133 
134     /**
135      * Injects the Plexus content.
136      *
137      * @param context Plexus context to inject.
138      * @throws ContextException if the PlexusContainer could not be located.
139      */
140     public void contextualize( Context context )
141         throws ContextException
142     {
143         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
144     }
145 
146     private void validateParameters( ProjectBuildingRequest buildingRequest, DependableCoordinate coordinate,
147                                      TransformableFilter filter )
148     {
149         validateBuildingRequest( buildingRequest );
150         if ( coordinate == null )
151         {
152             throw new IllegalArgumentException( "The parameter coordinate is not allowed to be null." );
153         }
154         if ( filter == null )
155         {
156             throw new IllegalArgumentException( "The parameter filter is not allowed to be null." );
157         }
158 
159     }
160 
161     private void validateParameters( ProjectBuildingRequest buildingRequest, Model model,
162                                      TransformableFilter filter )
163     {
164         validateBuildingRequest( buildingRequest );
165         if ( model == null )
166         {
167             throw new IllegalArgumentException( "The parameter model is not allowed to be null." );
168         }
169         if ( filter == null )
170         {
171             throw new IllegalArgumentException( "The parameter filter is not allowed to be null." );
172         }
173 
174     }
175 
176     private void validateBuildingRequest( ProjectBuildingRequest buildingRequest )
177     {
178         if ( buildingRequest == null )
179         {
180             throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
181         }
182     }
183 
184 }