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, managedDependencies,
65                                                                    filter );
66          }
67          catch ( ComponentLookupException e )
68          {
69              throw new DependencyResolverException( e.getMessage(), e );
70          }
71      }
72  
73      @Override
74      public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
75                                                           DependableCoordinate coordinate, TransformableFilter filter )
76          throws DependencyResolverException
77      {
78          validateParameters( buildingRequest, coordinate, filter );
79          try
80          {
81              String hint = isMaven31() ? "maven31" : "maven3";
82  
83              DependencyResolver effectiveArtifactResolver = container.lookup( DependencyResolver.class, hint );
84  
85              return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinate, filter );
86          }
87          catch ( ComponentLookupException e )
88          {
89              throw new DependencyResolverException( e.getMessage(), e );
90          }
91      }
92  
93      @Override
94      public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, Model model,
95                                                           TransformableFilter filter )
96          throws DependencyResolverException
97      {
98          validateParameters( buildingRequest, model, filter );
99          try
100         {
101             String hint = isMaven31() ? "maven31" : "maven3";
102 
103             DependencyResolver effectiveArtifactResolver = container.lookup( DependencyResolver.class, hint );
104 
105             return effectiveArtifactResolver.resolveDependencies( buildingRequest, model, filter );
106         }
107         catch ( ComponentLookupException e )
108         {
109             throw new DependencyResolverException( e.getMessage(), e );
110         }
111     }
112 
113     /**
114      * @return true if the current Maven version is Maven 3.1.
115      */
116     private boolean isMaven31()
117     {
118         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
119     }
120 
121     private boolean canFindCoreClass( String className )
122     {
123         try
124         {
125             Thread.currentThread().getContextClassLoader().loadClass( className );
126 
127             return true;
128         }
129         catch ( ClassNotFoundException e )
130         {
131             return false;
132         }
133     }
134 
135     /**
136      * Injects the Plexus content.
137      *
138      * @param context Plexus context to inject.
139      * @throws ContextException if the PlexusContainer could not be located.
140      */
141     public void contextualize( Context context )
142         throws ContextException
143     {
144         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
145     }
146 
147     private void validateParameters( ProjectBuildingRequest buildingRequest, DependableCoordinate coordinate,
148                                      TransformableFilter filter )
149     {
150         validateBuildingRequest( buildingRequest );
151         if ( coordinate == null )
152         {
153             throw new IllegalArgumentException( "The parameter coordinate is not allowed to be null." );
154         }
155     }
156 
157     private void validateParameters( ProjectBuildingRequest buildingRequest, Model model,
158                                      TransformableFilter filter )
159     {
160         validateBuildingRequest( buildingRequest );
161         if ( model == null )
162         {
163             throw new IllegalArgumentException( "The parameter model is not allowed to be null." );
164         }
165 
166     }
167 
168     private void validateBuildingRequest( ProjectBuildingRequest buildingRequest )
169     {
170         if ( buildingRequest == null )
171         {
172             throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
173         }
174     }
175 
176 }