Skip to content

Commit

Permalink
[MNG-4787] Allow class realm manager delegates to alter public part o…
Browse files Browse the repository at this point in the history
…f Maven core realm

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@991347 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
bentmann committed Aug 31, 2010
1 parent 8c54143 commit fc2a9f9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,30 @@
public interface ClassRealmRequest
{

/**
* The type of a class realm.
*/
enum RealmType
{
Project, Extension, Plugin,
/**
* The class realm for the public API of the Maven core.
*/
Core,

/**
* A class realm for a project to aggregates its build extensions.
*/
Project,

/**
* A class realm for a build extension.
*/
Extension,

/**
* A class realm for a plugin.
*/
Plugin,
}

/**
Expand All @@ -49,7 +70,7 @@ enum RealmType
ClassLoader getParent();

/**
* Gets the packages/types to import from the parent realm
* Gets the packages/types to import from the parent realm.
*
* @return The modifiable list of packages/types to import from the parent realm, never {@code null}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,26 @@ private synchronized ClassRealm getMavenRealm()
if ( mavenRealm == null )
{
mavenRealm = newRealm( "maven.api" );

importMavenApi( mavenRealm );

mavenRealm.setParentClassLoader( ClassLoader.getSystemClassLoader() );

List<ClassRealmManagerDelegate> delegates = getDelegates();
if ( !delegates.isEmpty() )
{
List<ClassRealmConstituent> constituents = new ArrayList<ClassRealmConstituent>();

ClassRealmRequest request =
new DefaultClassRealmRequest( RealmType.Core, null, new ArrayList<String>(), constituents );

for ( ClassRealmManagerDelegate delegate : delegates )
{
delegate.setupRealm( mavenRealm, request );
}

populateRealm( mavenRealm, constituents );
}
}
return mavenRealm;
}
Expand Down Expand Up @@ -150,8 +168,6 @@ private ClassRealm createRealm( String baseRealmId, RealmType type, ClassLoader
imports = new ArrayList<String>();
}

ClassRealmRequest request = new DefaultClassRealmRequest( type, parent, imports, constituents );

ClassRealm classRealm = newRealm( baseRealmId );

if ( parent != null )
Expand All @@ -163,9 +179,15 @@ private ClassRealm createRealm( String baseRealmId, RealmType type, ClassLoader
classRealm.setParentRealm( getMavenRealm() );
}

for ( ClassRealmManagerDelegate delegate : getDelegates() )
List<ClassRealmManagerDelegate> delegates = getDelegates();
if ( !delegates.isEmpty() )
{
delegate.setupRealm( classRealm, request );
ClassRealmRequest request = new DefaultClassRealmRequest( type, parent, imports, constituents );

for ( ClassRealmManagerDelegate delegate : delegates )
{
delegate.setupRealm( classRealm, request );
}
}

if ( importXpp3Dom )
Expand Down Expand Up @@ -193,35 +215,12 @@ private ClassRealm createRealm( String baseRealmId, RealmType type, ClassLoader
}
}

if ( logger.isDebugEnabled() )
{
logger.debug( "Populating class realm " + classRealm.getId() );
}

for ( ClassRealmConstituent constituent : constituents )
{
File file = constituent.getFile();

String id = getId( constituent );
artifactIds.remove( id );

if ( logger.isDebugEnabled() )
{
logger.debug( " Included: " + id );
}

try
{
classRealm.addURL( file.toURI().toURL() );
}
catch ( MalformedURLException e )
{
// Not going to happen
}
}
Set<String> includedIds = populateRealm( classRealm, constituents );

if ( logger.isDebugEnabled() )
{
artifactIds.removeAll( includedIds );

for ( String id : artifactIds )
{
logger.debug( " Excluded: " + id );
Expand Down Expand Up @@ -362,8 +361,45 @@ private List<ClassRealmManagerDelegate> getDelegates()
}
catch ( ComponentLookupException e )
{
logger.error( "Failed to lookup class realm delegates: " + e.getMessage(), e );

return Collections.emptyList();
}
}

private Set<String> populateRealm( ClassRealm classRealm, List<ClassRealmConstituent> constituents )
{
Set<String> includedIds = new LinkedHashSet<String>();

if ( logger.isDebugEnabled() )
{
logger.debug( "Populating class realm " + classRealm.getId() );
}

for ( ClassRealmConstituent constituent : constituents )
{
File file = constituent.getFile();

String id = getId( constituent );
includedIds.add( id );

if ( logger.isDebugEnabled() )
{
logger.debug( " Included: " + id );
}

try
{
classRealm.addURL( file.toURI().toURL() );
}
catch ( MalformedURLException e )
{
// Not going to happen
logger.error( e.getMessage(), e );
}
}

return includedIds;
}

}

0 comments on commit fc2a9f9

Please sign in to comment.