1 | |
package org.apache.maven.extension; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.MavenArtifactFilterManager; |
23 | |
import org.apache.maven.artifact.Artifact; |
24 | |
import org.apache.maven.artifact.ArtifactUtils; |
25 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
26 | |
import org.apache.maven.artifact.manager.WagonManager; |
27 | |
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; |
28 | |
import org.apache.maven.artifact.metadata.ArtifactMetadataSource; |
29 | |
import org.apache.maven.artifact.metadata.ResolutionGroup; |
30 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
31 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
32 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
33 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionResult; |
34 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
35 | |
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; |
36 | |
import org.apache.maven.model.Extension; |
37 | |
import org.apache.maven.plugin.DefaultPluginManager; |
38 | |
import org.apache.maven.project.MavenProject; |
39 | |
import org.apache.maven.wagon.Wagon; |
40 | |
import org.codehaus.classworlds.ClassRealm; |
41 | |
import org.codehaus.classworlds.ClassWorld; |
42 | |
import org.codehaus.classworlds.DuplicateRealmException; |
43 | |
import org.codehaus.classworlds.NoSuchRealmException; |
44 | |
import org.codehaus.plexus.DefaultPlexusContainer; |
45 | |
import org.codehaus.plexus.PlexusConstants; |
46 | |
import org.codehaus.plexus.PlexusContainer; |
47 | |
import org.codehaus.plexus.PlexusContainerException; |
48 | |
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
49 | |
import org.codehaus.plexus.context.Context; |
50 | |
import org.codehaus.plexus.context.ContextException; |
51 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
52 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
53 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
54 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
55 | |
|
56 | |
import java.io.File; |
57 | |
import java.io.InputStream; |
58 | |
import java.io.InputStreamReader; |
59 | |
import java.util.Collections; |
60 | |
import java.util.Iterator; |
61 | |
import java.util.LinkedHashSet; |
62 | |
import java.util.Map; |
63 | |
import java.util.Set; |
64 | |
import java.util.jar.JarFile; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | 0 | public class DefaultExtensionManager |
74 | |
extends AbstractLogEnabled |
75 | |
implements ExtensionManager, Contextualizable |
76 | |
{ |
77 | |
private ArtifactResolver artifactResolver; |
78 | |
|
79 | |
private ArtifactFactory artifactFactory; |
80 | |
|
81 | |
private ArtifactMetadataSource artifactMetadataSource; |
82 | |
|
83 | |
private DefaultPlexusContainer container; |
84 | |
|
85 | 0 | private ArtifactFilter artifactFilter = MavenArtifactFilterManager.createExtensionFilter(); |
86 | |
|
87 | |
private WagonManager wagonManager; |
88 | |
|
89 | |
private PlexusContainer extensionContainer; |
90 | |
|
91 | |
private static final String CONTAINER_NAME = "extensions"; |
92 | |
|
93 | |
public void addExtension( Extension extension, |
94 | |
MavenProject project, |
95 | |
ArtifactRepository localRepository ) |
96 | |
throws ArtifactResolutionException, PlexusContainerException, ArtifactNotFoundException |
97 | |
{ |
98 | 0 | String extensionId = ArtifactUtils.versionlessKey( extension.getGroupId(), extension.getArtifactId() ); |
99 | |
|
100 | 0 | getLogger().debug( "Initialising extension: " + extensionId ); |
101 | |
|
102 | 0 | Artifact artifact = (Artifact) project.getExtensionArtifactMap().get( extensionId ); |
103 | |
|
104 | 0 | if ( artifact != null ) |
105 | |
{ |
106 | 0 | ArtifactFilter filter = new ProjectArtifactExceptionFilter( artifactFilter, project.getArtifact() ); |
107 | |
|
108 | |
ResolutionGroup resolutionGroup; |
109 | |
try |
110 | |
{ |
111 | 0 | resolutionGroup = artifactMetadataSource.retrieve( artifact, localRepository, |
112 | |
project.getRemoteArtifactRepositories() ); |
113 | |
} |
114 | 0 | catch ( ArtifactMetadataRetrievalException e ) |
115 | |
{ |
116 | 0 | throw new ArtifactResolutionException( "Unable to download metadata from repository for plugin '" + |
117 | |
artifact.getId() + "': " + e.getMessage(), artifact, e ); |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | 0 | Set rgArtifacts = resolutionGroup.getArtifacts(); |
123 | 0 | rgArtifacts = DefaultPluginManager.checkPlexusUtils( rgArtifacts, artifactFactory ); |
124 | |
|
125 | 0 | Set dependencies = new LinkedHashSet(); |
126 | 0 | dependencies.add( artifact ); |
127 | 0 | dependencies.addAll( rgArtifacts ); |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | 0 | ArtifactResolutionResult result = artifactResolver.resolveTransitively( dependencies, project.getArtifact(), |
133 | |
Collections.EMPTY_MAP, |
134 | |
|
135 | |
localRepository, |
136 | |
project.getRemoteArtifactRepositories(), |
137 | |
artifactMetadataSource, filter ); |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | 0 | Set artifacts = result.getArtifacts(); |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | 0 | if ( extensionContainsLifeycle( artifact.getFile() ) ) |
151 | |
{ |
152 | 0 | for ( Iterator i = artifacts.iterator(); i.hasNext(); ) |
153 | |
{ |
154 | 0 | Artifact a = (Artifact) i.next(); |
155 | |
|
156 | 0 | if ( artifactFilter.include( a ) ) |
157 | |
{ |
158 | 0 | getLogger().debug( "Adding extension to core container: " + a.getFile() ); |
159 | |
|
160 | 0 | container.addJarResource( a.getFile() ); |
161 | |
} |
162 | |
} |
163 | |
} |
164 | 0 | else if ( artifacts.size() == 2 ) |
165 | |
{ |
166 | 0 | for ( Iterator i = artifacts.iterator(); i.hasNext(); ) |
167 | |
{ |
168 | 0 | Artifact a = (Artifact) i.next(); |
169 | |
|
170 | 0 | if ( !a.getArtifactId().equals( "plexus-utils" ) ) |
171 | |
{ |
172 | 0 | a = project.replaceWithActiveArtifact( a ); |
173 | |
|
174 | 0 | getLogger().debug( "Adding extension to core container: " + a.getFile() ); |
175 | |
|
176 | 0 | container.addJarResource( a.getFile() ); |
177 | |
} |
178 | |
} |
179 | |
} |
180 | |
else |
181 | |
{ |
182 | |
|
183 | |
|
184 | |
|
185 | 0 | if ( extensionContainer == null ) |
186 | |
{ |
187 | 0 | extensionContainer = createContainer(); |
188 | |
} |
189 | |
|
190 | 0 | for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); ) |
191 | |
{ |
192 | 0 | Artifact a = (Artifact) i.next(); |
193 | |
|
194 | 0 | a = project.replaceWithActiveArtifact( a ); |
195 | |
|
196 | 0 | getLogger().debug( "Adding to extension classpath: " + a.getFile() ); |
197 | |
|
198 | 0 | extensionContainer.addJarResource( a.getFile() ); |
199 | |
} |
200 | |
|
201 | 0 | if ( getLogger().isDebugEnabled() ) |
202 | |
{ |
203 | 0 | getLogger().debug( "Extension container contents:" ); |
204 | 0 | extensionContainer.getContainerRealm().display(); |
205 | |
} |
206 | |
} |
207 | |
} |
208 | 0 | } |
209 | |
|
210 | |
private PlexusContainer createContainer() |
211 | |
throws PlexusContainerException |
212 | |
{ |
213 | 0 | DefaultPlexusContainer child = new DefaultPlexusContainer(); |
214 | |
|
215 | 0 | ClassWorld classWorld = container.getClassWorld(); |
216 | 0 | child.setClassWorld( classWorld ); |
217 | |
|
218 | 0 | ClassRealm childRealm = null; |
219 | |
|
220 | |
|
221 | |
|
222 | 0 | String childRealmId = "plexus.core.child-container[" + CONTAINER_NAME + "]"; |
223 | |
try |
224 | |
{ |
225 | 0 | childRealm = classWorld.getRealm( childRealmId ); |
226 | |
} |
227 | 0 | catch ( NoSuchRealmException e ) |
228 | |
{ |
229 | |
try |
230 | |
{ |
231 | 0 | childRealm = classWorld.newRealm( childRealmId ); |
232 | |
} |
233 | 0 | catch ( DuplicateRealmException impossibleError ) |
234 | |
{ |
235 | 0 | getLogger().error( "An impossible error has occurred. After getRealm() failed, newRealm() " + |
236 | |
"produced duplication error on same id!", impossibleError ); |
237 | 0 | } |
238 | 0 | } |
239 | |
|
240 | 0 | childRealm.setParent( container.getContainerRealm() ); |
241 | |
|
242 | 0 | child.setCoreRealm( childRealm ); |
243 | |
|
244 | 0 | child.setName( CONTAINER_NAME ); |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | 0 | child.setLoggerManager( container.getLoggerManager() ); |
256 | |
|
257 | 0 | child.initialize(); |
258 | |
|
259 | 0 | child.start(); |
260 | |
|
261 | 0 | return child; |
262 | |
} |
263 | |
|
264 | |
public void registerWagons() |
265 | |
{ |
266 | 0 | if ( extensionContainer != null ) |
267 | |
{ |
268 | |
try |
269 | |
{ |
270 | 0 | Map wagons = extensionContainer.lookupMap( Wagon.ROLE ); |
271 | 0 | getLogger().debug( "Wagons to register: " + wagons.keySet() ); |
272 | 0 | wagonManager.registerWagons( wagons.keySet(), extensionContainer ); |
273 | |
} |
274 | 0 | catch ( ComponentLookupException e ) |
275 | |
{ |
276 | |
|
277 | 0 | getLogger().debug( "No wagons found in the extensions or other internal error: " + e.getMessage(), e ); |
278 | 0 | } |
279 | |
} |
280 | |
else |
281 | |
{ |
282 | 0 | getLogger().debug( "Wagons could not be registered as the extension container was never created" ); |
283 | |
} |
284 | 0 | } |
285 | |
|
286 | |
public void contextualize( Context context ) |
287 | |
throws ContextException |
288 | |
{ |
289 | 0 | container = (DefaultPlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
290 | 0 | } |
291 | |
|
292 | 0 | private static final class ProjectArtifactExceptionFilter |
293 | |
implements ArtifactFilter |
294 | |
{ |
295 | |
private ArtifactFilter passThroughFilter; |
296 | |
|
297 | |
private String projectDependencyConflictId; |
298 | |
|
299 | |
ProjectArtifactExceptionFilter( ArtifactFilter passThroughFilter, |
300 | |
Artifact projectArtifact ) |
301 | 0 | { |
302 | 0 | this.passThroughFilter = passThroughFilter; |
303 | 0 | projectDependencyConflictId = projectArtifact.getDependencyConflictId(); |
304 | 0 | } |
305 | |
|
306 | |
public boolean include( Artifact artifact ) |
307 | |
{ |
308 | 0 | String depConflictId = artifact.getDependencyConflictId(); |
309 | |
|
310 | 0 | return projectDependencyConflictId.equals( depConflictId ) || passThroughFilter.include( artifact ); |
311 | |
} |
312 | |
} |
313 | |
|
314 | |
private boolean extensionContainsLifeycle( File extension ) |
315 | |
{ |
316 | |
JarFile f; |
317 | |
|
318 | |
try |
319 | |
{ |
320 | 0 | f = new JarFile( extension ); |
321 | |
|
322 | 0 | InputStream is = f.getInputStream( f.getEntry( "META-INF/plexus/components.xml" ) ); |
323 | |
|
324 | 0 | if ( is == null ) |
325 | |
{ |
326 | 0 | return false; |
327 | |
} |
328 | |
|
329 | 0 | Xpp3Dom dom = Xpp3DomBuilder.build( new InputStreamReader( is ) ); |
330 | |
|
331 | 0 | Xpp3Dom[] components = dom.getChild( "components" ).getChildren( "component" ); |
332 | |
|
333 | 0 | for ( int i = 0; i < components.length; i++ ) |
334 | |
{ |
335 | 0 | if ( components[i].getChild( "role" ).getValue().equals( "org.apache.maven.lifecycle.mapping.LifecycleMapping" ) ) |
336 | |
{ |
337 | 0 | return true; |
338 | |
} |
339 | |
} |
340 | |
} |
341 | 0 | catch( Exception e ) |
342 | |
{ |
343 | |
|
344 | 0 | } |
345 | |
|
346 | 0 | return false; |
347 | |
} |
348 | |
} |