View Javadoc
1   package org.apache.maven.shared.transfer.artifact.install.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  import java.util.Collection;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.project.ProjectBuildingRequest;
27  import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
28  import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
29  import org.codehaus.plexus.PlexusConstants;
30  import org.codehaus.plexus.PlexusContainer;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  import org.codehaus.plexus.context.Context;
34  import org.codehaus.plexus.context.ContextException;
35  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
36  
37  /**
38   * 
39   */
40  @Component( role = ArtifactInstaller.class )
41  class DefaultArtifactInstaller
42      implements ArtifactInstaller, Contextualizable
43  {
44  
45      private PlexusContainer container;
46  
47      @Override
48      public void install( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
49          throws ArtifactInstallerException, IllegalArgumentException
50      {
51          validateParameters( request, mavenArtifacts );
52          try
53          {
54              String hint = isMaven31() ? "maven31" : "maven3";
55  
56              ArtifactInstaller effectiveArtifactInstaller = container.lookup( ArtifactInstaller.class, hint );
57  
58              effectiveArtifactInstaller.install( request, mavenArtifacts );
59          }
60          catch ( ComponentLookupException e )
61          {
62              throw new ArtifactInstallerException( e.getMessage(), e );
63          }
64      }
65  
66      @Override
67      public void install( ProjectBuildingRequest request, File localRepositry, Collection<Artifact> mavenArtifacts )
68          throws ArtifactInstallerException
69      {
70          validateParameters( request, mavenArtifacts );
71          if ( localRepositry == null )
72          {
73              throw new IllegalArgumentException( "The parameter localRepository is not allowed to be null." );
74          }
75          if ( !localRepositry.isDirectory() )
76          {
77              throw new IllegalArgumentException( "The parameter localRepository must be a directory." );
78          }
79  
80          // TODO: Should we check for exists() ?
81  
82          try
83          {
84              String hint = isMaven31() ? "maven31" : "maven3";
85  
86              ArtifactInstaller effectiveArtifactInstaller = container.lookup( ArtifactInstaller.class, hint );
87  
88              effectiveArtifactInstaller.install( request, localRepositry, mavenArtifacts );
89          }
90          catch ( ComponentLookupException e )
91          {
92              throw new ArtifactInstallerException( e.getMessage(), e );
93          }
94      }
95  
96      private void validateParameters( ProjectBuildingRequest request, Collection<Artifact> mavenArtifacts )
97      {
98          if ( request == null )
99          {
100             throw new IllegalArgumentException( "The parameter request is not allowed to be null." );
101         }
102         if ( mavenArtifacts == null )
103         {
104             throw new IllegalArgumentException( "The parameter mavenArtifacts is not allowed to be null." );
105         }
106         if ( mavenArtifacts.isEmpty() )
107         {
108             throw new IllegalArgumentException( "The collection mavenArtifacts is not allowed to be empty." );
109         }
110     }
111 
112     /**
113      * @return true if the current Maven version is Maven 3.1.
114      */
115     protected static boolean isMaven31()
116     {
117         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
118     }
119 
120     private static boolean canFindCoreClass( String className )
121     {
122         try
123         {
124             Thread.currentThread().getContextClassLoader().loadClass( className );
125 
126             return true;
127         }
128         catch ( ClassNotFoundException e )
129         {
130             return false;
131         }
132     }
133 
134     /**
135      * Injects the Plexus content.
136      *
137      * @param context Plexus context to inject.
138      * @throws ContextException if the PlexusContainer could not be located.
139      */
140     public void contextualize( Context context )
141         throws ContextException
142     {
143         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
144     }
145 }