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.artifact.Artifact;
25  import org.apache.maven.artifact.metadata.ArtifactMetadata;
26  import org.apache.maven.project.ProjectBuildingRequest;
27  import org.apache.maven.project.artifact.ProjectArtifactMetadata;
28  import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
29  import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
30  import org.apache.maven.shared.transfer.repository.RepositoryManager;
31  import org.apache.maven.shared.transfer.repository.RepositoryManagerException;
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 = RepositoryManager.class )
44  class DefaultRepositoryManager
45      implements RepositoryManager, Contextualizable 
46  {
47      private PlexusContainer container;
48      
49      @Override
50      public String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest, Artifact artifact )
51      {
52          try
53          {
54              return getMavenRepositoryManager( buildingRequest ).getPathForLocalArtifact( artifact );
55          }
56          catch ( ComponentLookupException | RepositoryManagerException e )
57          {
58              throw new IllegalStateException( e.getMessage(), e );
59          }
60      }
61  
62      @Override
63      public String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coor )
64      {
65          try
66          {
67              return getMavenRepositoryManager( buildingRequest ).getPathForLocalArtifact( coor );
68          }
69          catch ( ComponentLookupException | RepositoryManagerException e )
70          {
71              throw new IllegalStateException( e.getMessage(), e );
72          }
73      }
74  
75      @Override
76      public String getPathForLocalMetadata( ProjectBuildingRequest buildingRequest, ArtifactMetadata metadata )
77      {
78          if ( metadata instanceof ProjectArtifactMetadata )
79          {
80              DefaultArtifactCoordinate pomCoordinate = new DefaultArtifactCoordinate();
81              pomCoordinate.setGroupId( metadata.getGroupId() );
82              pomCoordinate.setArtifactId( metadata.getArtifactId() );
83              pomCoordinate.setVersion( metadata.getBaseVersion() );
84              pomCoordinate.setExtension( "pom" );
85              return getPathForLocalArtifact( buildingRequest, pomCoordinate );
86          }
87  
88          try
89          {
90              return getMavenRepositoryManager( buildingRequest ).getPathForLocalMetadata( metadata );
91          }
92          catch ( ComponentLookupException | RepositoryManagerException e )
93          {
94              throw new IllegalStateException( e.getMessage(), e );
95          }
96      }
97  
98      @Override
99      public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest, File basedir )
100     {
101         try
102         {
103             return getMavenRepositoryManager( buildingRequest ).setLocalRepositoryBasedir( buildingRequest, basedir );
104         }
105         catch ( ComponentLookupException | RepositoryManagerException e )
106         {
107             throw new IllegalStateException( e.getMessage(), e );
108         }
109     }
110 
111     @Override
112     public File getLocalRepositoryBasedir( ProjectBuildingRequest buildingRequest )
113     {
114         try
115         {
116             return getMavenRepositoryManager( buildingRequest ).getLocalRepositoryBasedir();
117         }
118         catch ( ComponentLookupException | RepositoryManagerException e )
119         {
120             throw new IllegalStateException( e.getMessage(), e );
121         }
122     }
123 
124     /**
125      * @return true if the current Maven version is Maven 3.1.
126      */
127     private boolean isMaven31()
128     {
129         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
130     }
131 
132     /**
133      * @return true if the current Maven version is Maven 3.0.2
134      */
135     private boolean isMaven302()
136     {
137         return canFindCoreClass( "org.sonatype.aether.spi.localrepo.LocalRepositoryManagerFactory" );
138     }
139 
140     private boolean canFindCoreClass( String className )
141     {
142         try
143         {
144             Thread.currentThread().getContextClassLoader().loadClass( className );
145 
146             return true;
147         }
148         catch ( ClassNotFoundException e )
149         {
150             return false;
151         }
152     }
153     
154     private MavenRepositoryManager getMavenRepositoryManager( ProjectBuildingRequest buildingRequest )
155         throws ComponentLookupException, RepositoryManagerException
156     {
157         if ( isMaven31() )
158         {
159             org.eclipse.aether.RepositorySystem m31RepositorySystem =
160                             container.lookup( org.eclipse.aether.RepositorySystem.class );
161 
162             org.eclipse.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
163                     "getRepositorySession" );
164 
165             return new Maven31RepositoryManager( m31RepositorySystem, session );
166         }
167         else
168         {
169             org.sonatype.aether.RepositorySystem m30RepositorySystem =
170                 container.lookup( org.sonatype.aether.RepositorySystem.class );
171 
172             org.sonatype.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
173                     "getRepositorySession" );
174             
175             if ( isMaven302() )
176             {
177                 return new Maven302RepositoryManager( m30RepositorySystem, session );
178                 
179             }
180             else
181             {
182                 return new Maven30RepositoryManager( m30RepositorySystem, session );
183             }
184         }
185     }
186     
187     public void contextualize( Context context ) throws ContextException
188     {
189         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
190     }
191 }