1   package org.apache.maven.project;
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.artifact.InvalidRepositoryException;
24  import org.apache.maven.artifact.factory.ArtifactFactory;
25  import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
26  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
27  import org.apache.maven.artifact.metadata.ResolutionGroup;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
30  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
31  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
32  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
33  import org.apache.maven.artifact.resolver.DefaultArtifactResolver;
34  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
35  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
36  import org.apache.maven.artifact.versioning.VersionRange;
37  import org.apache.maven.model.Dependency;
38  import org.apache.maven.model.Model;
39  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
40  import org.codehaus.plexus.PlexusConstants;
41  import org.codehaus.plexus.PlexusContainer;
42  import org.codehaus.plexus.context.Context;
43  import org.codehaus.plexus.context.ContextException;
44  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
45  import org.codehaus.plexus.util.IOUtil;
46  import org.codehaus.plexus.util.StringUtils;
47  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
48  
49  import java.io.File;
50  import java.io.IOException;
51  import java.io.InputStreamReader;
52  import java.util.HashSet;
53  import java.util.Iterator;
54  import java.util.List;
55  import java.util.Set;
56  
57  public class TestArtifactResolver
58      extends DefaultArtifactResolver
59      implements Contextualizable
60  {
61      public static final String ROLE = TestArtifactResolver.class.getName();
62  
63      private ArtifactRepositoryFactory repositoryFactory;
64  
65      private PlexusContainer container;
66  
67      static class Source
68          implements ArtifactMetadataSource
69      {
70          private ArtifactFactory artifactFactory;
71  
72          private final ArtifactRepositoryFactory repositoryFactory;
73  
74          private final PlexusContainer container;
75  
76          public Source( ArtifactFactory artifactFactory, ArtifactRepositoryFactory repositoryFactory,
77                         PlexusContainer container )
78          {
79              this.artifactFactory = artifactFactory;
80              this.repositoryFactory = repositoryFactory;
81              this.container = container;
82          }
83  
84          public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository,
85                                           List remoteRepositories )
86              throws ArtifactMetadataRetrievalException
87          {
88              Model model = null;
89              InputStreamReader r = null;
90              try
91              {
92                  String scope = artifact.getArtifactId().substring( "scope-".length() );
93                  if ( "maven-test".equals( artifact.getGroupId() ) )
94                  {
95                      String name = "/projects/scope/transitive-" + scope + "-dep.xml";
96                      r = new InputStreamReader( getClass().getResourceAsStream( name ) );
97                      MavenXpp3Reader reader = new MavenXpp3Reader();
98                      model = reader.read( r );
99                  }
100                 else
101                 {
102                     model = new Model();
103                 }
104                 model.setGroupId( artifact.getGroupId() );
105                 model.setArtifactId( artifact.getArtifactId() );
106             }
107             catch ( IOException e )
108             {
109                 throw new ArtifactMetadataRetrievalException( e );
110             }
111             catch ( XmlPullParserException e )
112             {
113                 throw new ArtifactMetadataRetrievalException( e );
114             }
115             finally
116             {
117                 IOUtil.close( r );
118             }
119 
120             Set artifacts;
121             try
122             {
123                 artifacts = createArtifacts( model.getDependencies(), artifact.getScope() );
124             }
125             catch ( InvalidVersionSpecificationException e )
126             {
127                 throw new ArtifactMetadataRetrievalException( e );
128             }
129 
130             List artifactRepositories;
131             try
132             {
133                 artifactRepositories =
134                     ProjectUtils.buildArtifactRepositories( model.getRepositories(), repositoryFactory, container );
135             }
136             catch ( InvalidRepositoryException e )
137             {
138                 throw new ArtifactMetadataRetrievalException( e );
139             }
140 
141             return new ResolutionGroup( artifact, artifacts, artifactRepositories );
142         }
143 
144         public List retrieveAvailableVersions( Artifact artifact, ArtifactRepository localRepository,
145                                                List remoteRepositories )
146         {
147             throw new UnsupportedOperationException( "Cannot get available versions in this test case" );
148         }
149 
150         protected Set createArtifacts( List dependencies, String inheritedScope )
151             throws InvalidVersionSpecificationException
152         {
153             Set projectArtifacts = new HashSet();
154 
155             for ( Iterator i = dependencies.iterator(); i.hasNext(); )
156             {
157                 Dependency d = (Dependency) i.next();
158 
159                 String scope = d.getScope();
160 
161                 if ( StringUtils.isEmpty( scope ) )
162                 {
163                     scope = Artifact.SCOPE_COMPILE;
164 
165                     d.setScope( scope );
166                 }
167 
168                 VersionRange versionRange = VersionRange.createFromVersionSpec( d.getVersion() );
169                 Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(),
170                                                                               versionRange, d.getType(),
171                                                                               d.getClassifier(), scope,
172                                                                               inheritedScope );
173                 if ( artifact != null )
174                 {
175                     projectArtifacts.add( artifact );
176                 }
177             }
178 
179             return projectArtifacts;
180         }
181 
182         public Artifact retrieveRelocatedArtifact( Artifact artifact,
183                                                    ArtifactRepository localRepository,
184                                                    List remoteRepositories )
185             throws ArtifactMetadataRetrievalException
186         {
187             return artifact;
188         }
189     }
190 
191     public Source source()
192     {
193         return new Source( artifactFactory, repositoryFactory, container );
194     }
195 
196     /**
197      * @noinspection RefusedBequest
198      */
199     public void resolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository )
200         throws ArtifactResolutionException
201     {
202         artifact.setFile( new File( "dummy" ) );
203     }
204 
205     public ArtifactResolutionResult resolveTransitively( Set artifacts, Artifact originatingArtifact,
206                                                          ArtifactRepository localRepository, List remoteRepositories,
207                                                          ArtifactMetadataSource source, ArtifactFilter filter )
208         throws ArtifactResolutionException, ArtifactNotFoundException
209     {
210         return super.resolveTransitively( artifacts, originatingArtifact, localRepository, remoteRepositories,
211                                           new Source( artifactFactory, repositoryFactory, container ), filter );
212     }
213 
214     public ArtifactResolutionResult resolveTransitively( Set artifacts, Artifact originatingArtifact,
215                                                          List remoteRepositories, ArtifactRepository localRepository,
216                                                          ArtifactMetadataSource source )
217         throws ArtifactResolutionException, ArtifactNotFoundException
218     {
219         return super.resolveTransitively( artifacts, originatingArtifact, remoteRepositories, localRepository,
220                                           new Source( artifactFactory, repositoryFactory, container ) );
221     }
222 
223     public void contextualize( Context context )
224         throws ContextException
225     {
226         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
227     }
228 
229 }