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.codehaus.plexus.PlexusConstants;
32  import org.codehaus.plexus.PlexusContainer;
33  import org.codehaus.plexus.component.annotations.Component;
34  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
35  import org.codehaus.plexus.context.Context;
36  import org.codehaus.plexus.context.ContextException;
37  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
38  
39  /**
40   * 
41   */
42  @Component( role = RepositoryManager.class )
43  class DefaultRepositoryManager
44      implements RepositoryManager, Contextualizable
45  {
46      private PlexusContainer container;
47  
48      @Override
49      public String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest, Artifact artifact )
50      {
51          try
52          {
53              String hint = isMaven31() ? "maven31" : "maven3";
54  
55              RepositoryManager effectiveRepositoryManager = container.lookup( RepositoryManager.class, hint );
56  
57              return effectiveRepositoryManager.getPathForLocalArtifact( buildingRequest, artifact );
58          }
59          catch ( ComponentLookupException e )
60          {
61              throw new IllegalStateException( e.getMessage(), e );
62          }
63      }
64  
65      @Override
66      public String getPathForLocalArtifact( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coor )
67      {
68          try
69          {
70              String hint = isMaven31() ? "maven31" : "maven3";
71  
72              RepositoryManager effectiveRepositoryManager = container.lookup( RepositoryManager.class, hint );
73  
74              return effectiveRepositoryManager.getPathForLocalArtifact( buildingRequest, coor );
75          }
76          catch ( ComponentLookupException e )
77          {
78              throw new IllegalStateException( e.getMessage(), e );
79          }
80      }
81  
82      @Override
83      public String getPathForLocalMetadata( ProjectBuildingRequest buildingRequest, ArtifactMetadata metadata )
84      {
85          if ( metadata instanceof ProjectArtifactMetadata )
86          {
87              DefaultArtifactCoordinate pomCoordinate = new DefaultArtifactCoordinate();
88              pomCoordinate.setGroupId( metadata.getGroupId() );
89              pomCoordinate.setArtifactId( metadata.getArtifactId() );
90              pomCoordinate.setVersion( metadata.getBaseVersion() );
91              pomCoordinate.setExtension( "pom" );
92              return getPathForLocalArtifact( buildingRequest, pomCoordinate );
93          }
94          
95          try
96          {
97              
98              String hint = isMaven31() ? "maven31" : "maven3";
99              
100             RepositoryManager effectiveRepositoryManager = container.lookup( RepositoryManager.class, hint );
101             
102             return effectiveRepositoryManager.getPathForLocalMetadata( buildingRequest, metadata );
103         }
104         catch ( ComponentLookupException e )
105         {
106             throw new IllegalStateException( e.getMessage(), e );
107         }
108     }
109 
110     @Override
111     public ProjectBuildingRequest setLocalRepositoryBasedir( ProjectBuildingRequest request, File basedir )
112     {
113         try
114         {
115             String hint = isMaven31() ? "maven31" : isMaven302() ? "maven302" : "maven3";
116 
117             RepositoryManager effectiveRepositoryManager = container.lookup( RepositoryManager.class, hint );
118 
119             return effectiveRepositoryManager.setLocalRepositoryBasedir( request, basedir );
120         }
121         catch ( ComponentLookupException e )
122         {
123             throw new IllegalStateException( e.getMessage(), e );
124         }
125     }
126 
127     @Override
128     public File getLocalRepositoryBasedir( ProjectBuildingRequest request )
129     {
130         try
131         {
132             String hint = isMaven31() ? "maven31" : isMaven302() ? "maven302" : "maven3";
133 
134             RepositoryManager effectiveRepositoryManager = container.lookup( RepositoryManager.class, hint );
135 
136             return effectiveRepositoryManager.getLocalRepositoryBasedir( request );
137         }
138         catch ( ComponentLookupException e )
139         {
140             throw new IllegalStateException( e.getMessage(), e );
141         }
142     }
143 
144     /**
145      * @return true if the current Maven version is Maven 3.1.
146      */
147     protected static boolean isMaven31()
148     {
149         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
150     }
151 
152     /**
153      * @return true if the current Maven version is Maven 3.0.2
154      */
155     protected static boolean isMaven302()
156     {
157         return canFindCoreClass( "org.sonatype.aether.spi.localrepo.LocalRepositoryManagerFactory" );
158     }
159 
160     private static boolean canFindCoreClass( String className )
161     {
162         try
163         {
164             Thread.currentThread().getContextClassLoader().loadClass( className );
165 
166             return true;
167         }
168         catch ( ClassNotFoundException e )
169         {
170             return false;
171         }
172     }
173 
174     /**
175      * Injects the Plexus content.
176      *
177      * @param context Plexus context to inject.
178      * @throws ContextException if the PlexusContainer could not be located.
179      */
180     public void contextualize( Context context )
181         throws ContextException
182     {
183         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
184     }
185 }