View Javadoc
1   package org.apache.maven.shared.transfer.repository.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.io.File;
23  
24  import org.apache.maven.RepositoryUtils;
25  import org.apache.maven.artifact.metadata.ArtifactMetadata;
26  import org.apache.maven.project.DefaultProjectBuildingRequest;
27  import org.apache.maven.project.ProjectBuildingRequest;
28  import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
29  import org.apache.maven.shared.transfer.repository.RepositoryManagerException;
30  import org.sonatype.aether.RepositorySystem;
31  import org.sonatype.aether.RepositorySystemSession;
32  import org.sonatype.aether.artifact.Artifact;
33  import org.sonatype.aether.metadata.Metadata;
34  import org.sonatype.aether.metadata.Metadata.Nature;
35  import org.sonatype.aether.repository.LocalRepository;
36  import org.sonatype.aether.repository.LocalRepositoryManager;
37  import org.sonatype.aether.util.DefaultRepositoryCache;
38  import org.sonatype.aether.util.DefaultRepositorySystemSession;
39  import org.sonatype.aether.util.artifact.DefaultArtifact;
40  import org.sonatype.aether.util.metadata.DefaultMetadata;
41  
42  /**
43   * 
44   */
45  class Maven30RepositoryManager
46      implements MavenRepositoryManager
47  {
48      private final RepositorySystem repositorySystem;
49  
50      private final RepositorySystemSession session;
51      
52      Maven30RepositoryManager( RepositorySystem repositorySystem, RepositorySystemSession session )
53      {
54          this.repositorySystem = repositorySystem;
55          this.session = session;
56      }
57  
58      @Override
59      public String getPathForLocalArtifact( org.apache.maven.artifact.Artifact mavenArtifact )
60      {
61          Artifact aetherArtifact;
62  
63          // LRM.getPathForLocalArtifact() won't throw an Exception, so translate reflection error to RuntimeException
64          try
65          {
66              aetherArtifact = (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
67                                                          org.apache.maven.artifact.Artifact.class, mavenArtifact );
68          }
69          catch ( RepositoryManagerException e )
70          {
71              throw new RuntimeException( e.getMessage(), e );
72          }
73  
74          return session.getLocalRepositoryManager().getPathForLocalArtifact( aetherArtifact );
75      }
76  
77      @Override
78      public String getPathForLocalArtifact( ArtifactCoordinate coordinate )
79      {
80          Artifact aetherArtifact = toArtifact( coordinate );
81  
82          // LRM.getPathForLocalArtifact() won't throw an Exception, so translate reflection error to RuntimeException
83  
84          return session.getLocalRepositoryManager().getPathForLocalArtifact( aetherArtifact );
85      }
86  
87      @Override
88      public String getPathForLocalMetadata( ArtifactMetadata metadata )
89      {
90          Metadata aetherMetadata =
91              new DefaultMetadata( metadata.getGroupId(),
92                                   metadata.storedInGroupDirectory() ? null : metadata.getArtifactId(),
93                                   metadata.storedInArtifactVersionDirectory() ? metadata.getBaseVersion() : null,
94                                   "maven-metadata.xml", Nature.RELEASE_OR_SNAPSHOT );
95  
96          return session.getLocalRepositoryManager().getPathForLocalMetadata( aetherMetadata );
97      }
98      
99      @Override
100     public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest, File basedir )
101     {
102         ProjectBuildingRequest newRequest = new DefaultProjectBuildingRequest( buildingRequest );
103 
104         RepositorySystemSession session;
105         try
106         {
107             session = Invoker.invoke( buildingRequest, "getRepositorySession" );
108         }
109         catch ( RepositoryManagerException e )
110         {
111             throw new RuntimeException( e.getMessage(), e );
112         }
113 
114         // "clone" session and replace localRepository
115         DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session );
116 
117         // Clear cache, since we're using a new local repository
118         newSession.setCache( new DefaultRepositoryCache() );
119 
120         // keep same repositoryType
121         String repositoryType = resolveRepositoryType( session.getLocalRepository() );
122 
123         LocalRepositoryManager localRepositoryManager =
124             repositorySystem.newLocalRepositoryManager( new LocalRepository( basedir, repositoryType ) );
125 
126         newSession.setLocalRepositoryManager( localRepositoryManager );
127 
128         try
129         {
130             Invoker.invoke( newRequest, "setRepositorySession", RepositorySystemSession.class, newSession );
131         }
132         catch ( RepositoryManagerException e )
133         {
134             throw new RuntimeException( e.getMessage(), e );
135         }
136 
137         return newRequest;
138     }
139 
140     @Override
141     public File getLocalRepositoryBasedir()
142     {
143         return session.getLocalRepository().getBasedir();
144     }
145 
146     /**
147      * @param localRepository {@link LocalRepository}
148      * @return the resolved type.
149      */
150     protected String resolveRepositoryType( LocalRepository localRepository )
151     {
152         return localRepository.getContentType();
153     }
154 
155     /**
156      * @param coordinate {@link ArtifactCoordinate}
157      * @return {@link Artifact}
158      */
159     protected Artifact toArtifact( ArtifactCoordinate coordinate )
160     {
161         if ( coordinate == null )
162         {
163             return null;
164         }
165 
166         Artifact result =
167             new DefaultArtifact( coordinate.getGroupId(), coordinate.getArtifactId(), coordinate.getClassifier(),
168                                  coordinate.getExtension(), coordinate.getVersion() );
169 
170         return result;
171     }
172 }