1 package org.apache.maven.classrealm;
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.net.MalformedURLException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.LinkedHashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Random;
31 import java.util.Set;
32 import java.util.TreeMap;
33
34 import org.apache.maven.artifact.ArtifactUtils;
35 import org.apache.maven.classrealm.ClassRealmRequest.RealmType;
36 import org.apache.maven.model.Model;
37 import org.apache.maven.model.Plugin;
38 import org.codehaus.plexus.MutablePlexusContainer;
39 import org.codehaus.plexus.PlexusContainer;
40 import org.codehaus.plexus.classworlds.ClassWorld;
41 import org.codehaus.plexus.classworlds.realm.ClassRealm;
42 import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
43 import org.codehaus.plexus.component.annotations.Component;
44 import org.codehaus.plexus.component.annotations.Requirement;
45 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
46 import org.codehaus.plexus.logging.Logger;
47 import org.codehaus.plexus.util.StringUtils;
48 import org.sonatype.aether.artifact.Artifact;
49
50
51
52
53
54
55
56
57 @Component( role = ClassRealmManager.class )
58 public class DefaultClassRealmManager
59 implements ClassRealmManager
60 {
61
62 @Requirement
63 private Logger logger;
64
65 @Requirement
66 protected PlexusContainer container;
67
68 private ClassRealm mavenRealm;
69
70 private ClassWorld getClassWorld()
71 {
72 return ( (MutablePlexusContainer) container ).getClassWorld();
73 }
74
75 private ClassRealm newRealm( String id )
76 {
77 ClassWorld world = getClassWorld();
78
79 synchronized ( world )
80 {
81 String realmId = id;
82
83 Random random = new Random();
84
85 while ( true )
86 {
87 try
88 {
89 ClassRealm classRealm = world.newRealm( realmId, null );
90
91 if ( logger.isDebugEnabled() )
92 {
93 logger.debug( "Created new class realm " + realmId );
94 }
95
96 return classRealm;
97 }
98 catch ( DuplicateRealmException e )
99 {
100 realmId = id + '-' + random.nextInt();
101 }
102 }
103 }
104 }
105
106 public synchronized ClassRealm getMavenApiRealm()
107 {
108 if ( mavenRealm == null )
109 {
110 mavenRealm = newRealm( "maven.api" );
111
112 List<ClassRealmConstituent> constituents = new ArrayList<ClassRealmConstituent>();
113
114 List<String> parentImports = new ArrayList<String>();
115
116 Map<String, ClassLoader> foreignImports = new HashMap<String, ClassLoader>();
117 importMavenApi( foreignImports );
118
119 callDelegates( mavenRealm, RealmType.Core, mavenRealm.getParentClassLoader(), parentImports,
120 foreignImports, constituents );
121
122 wireRealm( mavenRealm, parentImports, foreignImports );
123
124 populateRealm( mavenRealm, constituents );
125 }
126
127 return mavenRealm;
128 }
129
130 private void importMavenApi( Map<String, ClassLoader> imports )
131 {
132 ClassRealm coreRealm = getCoreRealm();
133
134
135 imports.put( "org.apache.maven.*", coreRealm );
136 imports.put( "org.apache.maven.artifact", coreRealm );
137 imports.put( "org.apache.maven.classrealm", coreRealm );
138 imports.put( "org.apache.maven.cli", coreRealm );
139 imports.put( "org.apache.maven.configuration", coreRealm );
140 imports.put( "org.apache.maven.exception", coreRealm );
141 imports.put( "org.apache.maven.execution", coreRealm );
142 imports.put( "org.apache.maven.lifecycle", coreRealm );
143 imports.put( "org.apache.maven.model", coreRealm );
144 imports.put( "org.apache.maven.monitor", coreRealm );
145 imports.put( "org.apache.maven.plugin", coreRealm );
146 imports.put( "org.apache.maven.profiles", coreRealm );
147 imports.put( "org.apache.maven.project", coreRealm );
148 imports.put( "org.apache.maven.reporting", coreRealm );
149 imports.put( "org.apache.maven.repository", coreRealm );
150 imports.put( "org.apache.maven.settings", coreRealm );
151 imports.put( "org.apache.maven.toolchain", coreRealm );
152 imports.put( "org.apache.maven.usability", coreRealm );
153
154
155 imports.put( "org.apache.maven.wagon.*", coreRealm );
156 imports.put( "org.apache.maven.wagon.authentication", coreRealm );
157 imports.put( "org.apache.maven.wagon.authorization", coreRealm );
158 imports.put( "org.apache.maven.wagon.events", coreRealm );
159 imports.put( "org.apache.maven.wagon.observers", coreRealm );
160 imports.put( "org.apache.maven.wagon.proxy", coreRealm );
161 imports.put( "org.apache.maven.wagon.repository", coreRealm );
162 imports.put( "org.apache.maven.wagon.resource", coreRealm );
163
164
165 imports.put( "org.sonatype.aether.*", coreRealm );
166 imports.put( "org.sonatype.aether.artifact", coreRealm );
167 imports.put( "org.sonatype.aether.collection", coreRealm );
168 imports.put( "org.sonatype.aether.deployment", coreRealm );
169 imports.put( "org.sonatype.aether.graph", coreRealm );
170 imports.put( "org.sonatype.aether.impl", coreRealm );
171 imports.put( "org.sonatype.aether.installation", coreRealm );
172 imports.put( "org.sonatype.aether.metadata", coreRealm );
173 imports.put( "org.sonatype.aether.repository", coreRealm );
174 imports.put( "org.sonatype.aether.resolution", coreRealm );
175 imports.put( "org.sonatype.aether.spi", coreRealm );
176 imports.put( "org.sonatype.aether.transfer", coreRealm );
177 imports.put( "org.sonatype.aether.version", coreRealm );
178
179
180 imports.put( "org.codehaus.plexus.classworlds", coreRealm );
181
182
183 imports.put( "org.codehaus.classworlds", coreRealm );
184
185
186 imports.put( "org.codehaus.plexus.*", coreRealm );
187 imports.put( "org.codehaus.plexus.component", coreRealm );
188 imports.put( "org.codehaus.plexus.configuration", coreRealm );
189 imports.put( "org.codehaus.plexus.container", coreRealm );
190 imports.put( "org.codehaus.plexus.context", coreRealm );
191 imports.put( "org.codehaus.plexus.lifecycle", coreRealm );
192 imports.put( "org.codehaus.plexus.logging", coreRealm );
193 imports.put( "org.codehaus.plexus.personality", coreRealm );
194
195
196 imports.put( "org.codehaus.plexus.util.xml.Xpp3Dom", coreRealm );
197 imports.put( "org.codehaus.plexus.util.xml.pull.XmlPullParser", coreRealm );
198 imports.put( "org.codehaus.plexus.util.xml.pull.XmlPullParserException", coreRealm );
199 imports.put( "org.codehaus.plexus.util.xml.pull.XmlSerializer", coreRealm );
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214 private ClassRealm createRealm( String baseRealmId, RealmType type, ClassLoader parent, List<String> parentImports,
215 Map<String, ClassLoader> foreignImports, List<Artifact> artifacts )
216 {
217 Set<String> artifactIds = new LinkedHashSet<String>();
218
219 List<ClassRealmConstituent> constituents = new ArrayList<ClassRealmConstituent>();
220
221 if ( artifacts != null )
222 {
223 for ( Artifact artifact : artifacts )
224 {
225 artifactIds.add( getId( artifact ) );
226 if ( artifact.getFile() != null )
227 {
228 constituents.add( new ArtifactClassRealmConstituent( artifact ) );
229 }
230 }
231 }
232
233 if ( parentImports != null )
234 {
235 parentImports = new ArrayList<String>( parentImports );
236 }
237 else
238 {
239 parentImports = new ArrayList<String>();
240 }
241
242 if ( foreignImports != null )
243 {
244 foreignImports = new TreeMap<String, ClassLoader>( foreignImports );
245 }
246 else
247 {
248 foreignImports = new TreeMap<String, ClassLoader>();
249 }
250
251 ClassRealm classRealm = newRealm( baseRealmId );
252
253 if ( parent != null )
254 {
255 classRealm.setParentClassLoader( parent );
256 }
257
258 callDelegates( classRealm, type, parent, parentImports, foreignImports, constituents );
259
260 wireRealm( classRealm, parentImports, foreignImports );
261
262 Set<String> includedIds = populateRealm( classRealm, constituents );
263
264 if ( logger.isDebugEnabled() )
265 {
266 artifactIds.removeAll( includedIds );
267
268 for ( String id : artifactIds )
269 {
270 logger.debug( " Excluded: " + id );
271 }
272 }
273
274 return classRealm;
275 }
276
277 public ClassRealm getCoreRealm()
278 {
279 return container.getContainerRealm();
280 }
281
282 public ClassRealm createProjectRealm( Model model, List<Artifact> artifacts )
283 {
284 if ( model == null )
285 {
286 throw new IllegalArgumentException( "model missing" );
287 }
288
289 ClassLoader parent = getMavenApiRealm();
290
291 return createRealm( getKey( model ), RealmType.Project, parent, null, null, artifacts );
292 }
293
294 private static String getKey( Model model )
295 {
296 return "project>" + model.getGroupId() + ":" + model.getArtifactId() + ":" + model.getVersion();
297 }
298
299 public ClassRealm createExtensionRealm( Plugin plugin, List<Artifact> artifacts )
300 {
301 if ( plugin == null )
302 {
303 throw new IllegalArgumentException( "extension plugin missing" );
304 }
305
306 ClassLoader parent = ClassLoader.getSystemClassLoader();
307
308 Map<String, ClassLoader> foreignImports =
309 Collections.<String, ClassLoader> singletonMap( "", getMavenApiRealm() );
310
311 return createRealm( getKey( plugin, true ), RealmType.Extension, parent, null, foreignImports, artifacts );
312 }
313
314 public ClassRealm createPluginRealm( Plugin plugin, ClassLoader parent, List<String> parentImports,
315 Map<String, ClassLoader> foreignImports, List<Artifact> artifacts )
316 {
317 if ( plugin == null )
318 {
319 throw new IllegalArgumentException( "plugin missing" );
320 }
321
322 if ( parent == null )
323 {
324 parent = ClassLoader.getSystemClassLoader();
325 }
326
327 return createRealm( getKey( plugin, false ), RealmType.Plugin, parent, parentImports, foreignImports, artifacts );
328 }
329
330 private static String getKey( Plugin plugin, boolean extension )
331 {
332 String version = ArtifactUtils.toSnapshotVersion( plugin.getVersion() );
333 return ( extension ? "extension>" : "plugin>" ) + plugin.getGroupId() + ":" + plugin.getArtifactId() + ":"
334 + version;
335 }
336
337 private static String getId( Artifact artifact )
338 {
339 return getId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(),
340 artifact.getClassifier(), artifact.getBaseVersion() );
341 }
342
343 private static String getId( ClassRealmConstituent constituent )
344 {
345 return getId( constituent.getGroupId(), constituent.getArtifactId(), constituent.getType(),
346 constituent.getClassifier(), constituent.getVersion() );
347 }
348
349 private static String getId( String gid, String aid, String type, String cls, String ver )
350 {
351 return gid + ':' + aid + ':' + type + ( StringUtils.isNotEmpty( cls ) ? ':' + cls : "" ) + ':' + ver;
352 }
353
354 private List<ClassRealmManagerDelegate> getDelegates()
355 {
356 try
357 {
358 return container.lookupList( ClassRealmManagerDelegate.class );
359 }
360 catch ( ComponentLookupException e )
361 {
362 logger.error( "Failed to lookup class realm delegates: " + e.getMessage(), e );
363
364 return Collections.emptyList();
365 }
366 }
367
368 private void callDelegates( ClassRealm classRealm, RealmType type, ClassLoader parent, List<String> parentImports,
369 Map<String, ClassLoader> foreignImports, List<ClassRealmConstituent> constituents )
370 {
371 List<ClassRealmManagerDelegate> delegates = getDelegates();
372
373 if ( !delegates.isEmpty() )
374 {
375 ClassRealmRequest request =
376 new DefaultClassRealmRequest( type, parent, parentImports, foreignImports, constituents );
377
378 for ( ClassRealmManagerDelegate delegate : delegates )
379 {
380 try
381 {
382 delegate.setupRealm( classRealm, request );
383 }
384 catch ( Exception e )
385 {
386 logger.error( delegate.getClass().getName() + " failed to setup class realm " + classRealm + ": "
387 + e.getMessage(), e );
388 }
389 }
390 }
391 }
392
393 private Set<String> populateRealm( ClassRealm classRealm, List<ClassRealmConstituent> constituents )
394 {
395 Set<String> includedIds = new LinkedHashSet<String>();
396
397 if ( logger.isDebugEnabled() )
398 {
399 logger.debug( "Populating class realm " + classRealm.getId() );
400 }
401
402 for ( ClassRealmConstituent constituent : constituents )
403 {
404 File file = constituent.getFile();
405
406 String id = getId( constituent );
407 includedIds.add( id );
408
409 if ( logger.isDebugEnabled() )
410 {
411 logger.debug( " Included: " + id );
412 }
413
414 try
415 {
416 classRealm.addURL( file.toURI().toURL() );
417 }
418 catch ( MalformedURLException e )
419 {
420
421 logger.error( e.getMessage(), e );
422 }
423 }
424
425 return includedIds;
426 }
427
428 private void wireRealm( ClassRealm classRealm, List<String> parentImports, Map<String, ClassLoader> foreignImports )
429 {
430 if ( foreignImports != null && !foreignImports.isEmpty() )
431 {
432 if ( logger.isDebugEnabled() )
433 {
434 logger.debug( "Importing foreign packages into class realm " + classRealm.getId() );
435 }
436
437 for ( Map.Entry<String, ClassLoader> entry : foreignImports.entrySet() )
438 {
439 ClassLoader importedRealm = entry.getValue();
440 String imp = entry.getKey();
441
442 if ( logger.isDebugEnabled() )
443 {
444 logger.debug( " Imported: " + imp + " < " + getId( importedRealm ) );
445 }
446
447 classRealm.importFrom( importedRealm, imp );
448 }
449 }
450
451 if ( parentImports != null && !parentImports.isEmpty() )
452 {
453 if ( logger.isDebugEnabled() )
454 {
455 logger.debug( "Importing parent packages into class realm " + classRealm.getId() );
456 }
457
458 for ( String imp : parentImports )
459 {
460 if ( logger.isDebugEnabled() )
461 {
462 logger.debug( " Imported: " + imp + " < " + getId( classRealm.getParentClassLoader() ) );
463 }
464
465 classRealm.importFromParent( imp );
466 }
467 }
468 }
469
470 private String getId( ClassLoader classLoader )
471 {
472 if ( classLoader instanceof ClassRealm )
473 {
474 return ( (ClassRealm) classLoader ).getId();
475 }
476 return String.valueOf( classLoader );
477 }
478
479 }