1 package org.apache.maven.shared.transfer.artifact.install.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
114
115 protected static boolean isMaven31()
116 {
117 return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" );
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
136
137
138
139
140 public void contextualize( Context context )
141 throws ContextException
142 {
143 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
144 }
145 }