View Javadoc
1   package org.apache.maven.shared.transfer.dependencies.collect.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.handler.manager.ArtifactHandlerManager;
26  import org.apache.maven.model.Dependency;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.project.ProjectBuildingRequest;
29  import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
30  import org.apache.maven.shared.transfer.dependencies.collect.CollectorResult;
31  import org.apache.maven.shared.transfer.dependencies.collect.DependencyCollector;
32  import org.apache.maven.shared.transfer.dependencies.collect.DependencyCollectorException;
33  import org.codehaus.plexus.PlexusConstants;
34  import org.codehaus.plexus.PlexusContainer;
35  import org.codehaus.plexus.component.annotations.Component;
36  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
37  import org.codehaus.plexus.context.Context;
38  import org.codehaus.plexus.context.ContextException;
39  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
40  
41  /**
42   * This DependencyCollector passes the request to the proper Maven 3.x implementation
43   * 
44   * @author Robert Scholte
45   */
46  @Component( role = DependencyCollector.class, hint = "default" )
47  class DefaultDependencyCollector implements DependencyCollector, Contextualizable 
48  {
49      private PlexusContainer container;
50  
51      @Override
52      public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, Dependency root )
53          throws DependencyCollectorException
54      {
55          validateParameters( buildingRequest, root );
56  
57          try
58          {
59              String hint = isMaven31() ? "maven31" : "maven3";
60  
61              DependencyCollector effectiveDependencyCollector = container.lookup( DependencyCollector.class, hint );
62  
63              return effectiveDependencyCollector.collectDependencies( buildingRequest, root );
64          }
65          catch ( ComponentLookupException e )
66          {
67              throw new DependencyCollectorException( e.getMessage(), e );
68          }
69      }
70  
71      @Override
72      public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, DependableCoordinate root )
73          throws DependencyCollectorException
74      {
75          validateParameters( buildingRequest, root );
76  
77          try
78          {
79              return getMavenDependencyCollector( buildingRequest ).collectDependencies( root );
80          }
81          catch ( ComponentLookupException e )
82          {
83              throw new DependencyCollectorException( e.getMessage(), e );
84          }
85      }
86  
87      @Override
88      public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, Model root )
89          throws DependencyCollectorException
90      {
91          validateParameters( buildingRequest, root );
92  
93          try
94          {
95              String hint = isMaven31() ? "maven31" : "maven3";
96  
97              DependencyCollector effectiveDependencyCollector = container.lookup( DependencyCollector.class, hint );
98  
99              return effectiveDependencyCollector.collectDependencies( buildingRequest, root );
100         }
101         catch ( ComponentLookupException e )
102         {
103             throw new DependencyCollectorException( e.getMessage(), e );
104         }
105     }
106 
107     private void validateParameters( ProjectBuildingRequest buildingRequest, DependableCoordinate root )
108     {
109         validateBuildingRequest( buildingRequest );
110         if ( root == null )
111         {
112             throw new IllegalArgumentException( "The parameter root is not allowed to be null." );
113         }
114     }
115 
116     private void validateParameters( ProjectBuildingRequest buildingRequest, Dependency root )
117     {
118         validateBuildingRequest( buildingRequest );
119         if ( root == null )
120         {
121             throw new IllegalArgumentException( "The parameter root is not allowed to be null." );
122         }
123     }
124 
125     private void validateParameters( ProjectBuildingRequest buildingRequest, Model root )
126     {
127         validateBuildingRequest( buildingRequest );
128         if ( root == null )
129         {
130             throw new IllegalArgumentException( "The parameter root is not allowed to be null." );
131         }
132     }
133 
134     private void validateBuildingRequest( ProjectBuildingRequest buildingRequest )
135     {
136         if ( buildingRequest == null )
137         {
138             throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
139         }
140     }
141 
142     /**
143      * @return true if the current Maven version is Maven 3.1.
144      */
145     private boolean isMaven31()
146     {
147         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
148     }
149 
150     private boolean canFindCoreClass( String className )
151     {
152         try
153         {
154             Thread.currentThread().getContextClassLoader().loadClass( className );
155 
156             return true;
157         }
158         catch ( ClassNotFoundException e )
159         {
160             return false;
161         }
162     }
163 
164     /**
165      * Injects the Plexus content.
166      *
167      * @param context Plexus context to inject.
168      * @throws ContextException if the PlexusContainer could not be located.
169      */
170     public void contextualize( Context context )
171         throws ContextException
172     {
173         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
174     }
175     
176     private MavenDependencyCollector getMavenDependencyCollector( ProjectBuildingRequest buildingRequest )
177         throws ComponentLookupException, DependencyCollectorException
178     {
179         ArtifactHandlerManager artifactHandlerManager = container.lookup( ArtifactHandlerManager.class );
180         
181         if ( isMaven31() )
182         {
183             org.eclipse.aether.RepositorySystem m31RepositorySystem =
184                 container.lookup( org.eclipse.aether.RepositorySystem.class );
185 
186             org.eclipse.aether.RepositorySystemSession session =
187                 (org.eclipse.aether.RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
188 
189             @SuppressWarnings( "unchecked" )
190             List<org.eclipse.aether.repository.RemoteRepository> aetherRepositories =
191                 (List<org.eclipse.aether.repository.RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos",
192                                                                            List.class,
193                                                                            buildingRequest.getRemoteRepositories() );
194 
195             return new Maven31DependencyCollector( m31RepositorySystem, artifactHandlerManager, session,
196                                                    aetherRepositories );
197             
198         }
199         else
200         {
201 
202             org.sonatype.aether.RepositorySystem m30RepositorySystem =
203                             container.lookup( org.sonatype.aether.RepositorySystem.class );
204 
205             org.sonatype.aether.RepositorySystemSession session =
206                 (org.sonatype.aether.RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
207 
208             @SuppressWarnings( "unchecked" )
209             List<org.sonatype.aether.repository.RemoteRepository> aetherRepositories =
210                 (List<org.sonatype.aether.repository.RemoteRepository>) Invoker.invoke( RepositoryUtils.class,
211                                                                             "toRepos", List.class,
212                                                                             buildingRequest.getRemoteRepositories() );
213 
214             return new Maven30DependencyCollector( m30RepositorySystem, artifactHandlerManager, session,
215                                                    aetherRepositories );
216         }
217 
218     }
219 
220 }