Cleaning up extension and plugin realms for a project once the build completes.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@588029 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2007-10-24 21:14:27 +00:00
parent 0d161a73a4
commit 16c064c4bc
8 changed files with 159 additions and 55 deletions

View File

@ -16,7 +16,7 @@
# under the License.
classworlds.version=1.2-alpha-10
plexus.version=1.0-alpha-34
plexus.version=1.0-alpha-35-SNAPSHOT
plexus-utils.version=1.4.5
maven-artifact.version=3.0-SNAPSHOT
commons-cli.version=1.0

View File

@ -88,6 +88,7 @@ under the License.
<artifactId>maven-plugin-api</artifactId>
<version>2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interactivity-api</artifactId>

View File

@ -192,6 +192,8 @@ public class DefaultMaven
dispatcher,
projectSessions );
try
{
for ( Iterator i = request.getGoals().iterator(); i.hasNext(); )
{
String goal = (String) i.next();
@ -246,6 +248,11 @@ public class DefaultMaven
return result;
}
finally
{
session.dispose();
}
}
/**
* Initialize some context objects to be stored in the container's context map for reference by

View File

@ -12,6 +12,7 @@ import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
@ -54,6 +55,40 @@ public class MavenProjectSession
projectRealm = container.createComponentRealm( projectId, Collections.EMPTY_LIST );
}
public void dispose()
{
for ( Iterator it = componentRealms.values().iterator(); it.hasNext(); )
{
ClassRealm realm = (ClassRealm) it.next();
disposeRealm( realm );
}
disposeRealm( projectRealm );
try
{
container.removeComponentRealm( projectRealm );
}
catch( PlexusContainerException e )
{
e.printStackTrace();
}
componentRealms.clear();
projectRealm = null;
}
private void disposeRealm( ClassRealm realm )
{
try
{
realm.getWorld().disposeRealm( realm.getId() );
}
catch ( NoSuchRealmException e )
{
// impossible
}
}
public String getProjectId()
{
return projectId;
@ -66,12 +101,24 @@ public class MavenProjectSession
public boolean containsExtensionRealm( Artifact extensionArtifact )
{
checkDisposed();
String id = createExtensionRealmId( extensionArtifact );
return componentRealms.containsKey( id );
}
private void checkDisposed()
{
if ( projectRealm == null )
{
throw new IllegalStateException( "MavenProjectSession for: " + projectId + " with hashcode: " + hashCode() + " has been disposed, and is no longer valid for use." );
}
}
public boolean containsPluginRealm( Plugin plugin )
{
checkDisposed();
String realmId = createPluginRealmId( ArtifactUtils.versionlessKey( plugin.getGroupId(), plugin.getArtifactId() ) );
return componentRealms.containsKey( realmId );
@ -96,6 +143,8 @@ public class MavenProjectSession
public ClassRealm createExtensionRealm( Artifact extensionArtifact )
throws DuplicateRealmException
{
checkDisposed();
String realmId = createExtensionRealmId( extensionArtifact );
ClassRealm extRealm = container.getContainerRealm().createChildRealm( realmId );
@ -124,6 +173,8 @@ public class MavenProjectSession
public ClassRealm getPluginRealm( Plugin plugin )
throws NoSuchRealmException
{
checkDisposed();
String realmId = createPluginRealmId( ArtifactUtils.versionlessKey( plugin.getGroupId(), plugin.getArtifactId() ) );
return projectRealm.getWorld().getRealm( realmId );
@ -133,6 +184,8 @@ public class MavenProjectSession
public ClassRealm createPluginRealm( Plugin plugin )
throws DuplicateRealmException
{
checkDisposed();
String realmId = createPluginRealmId( ArtifactUtils.versionlessKey( plugin.getGroupId(), plugin.getArtifactId() ) );
ClassRealm extRealm = projectRealm.createChildRealm( realmId );
@ -150,6 +203,8 @@ public class MavenProjectSession
public ClassRealm getPluginRealm( PluginDescriptor pd )
throws NoSuchRealmException
{
checkDisposed();
String realmId = createPluginRealmId( ArtifactUtils.versionlessKey( pd.getGroupId(), pd.getArtifactId() ) );
ClassRealm extRealm = projectRealm.getWorld().getRealm( realmId );

View File

@ -29,6 +29,8 @@ import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -68,7 +70,16 @@ public class MavenSession
this.reactorManager = reactorManager;
this.projectSessions = projectSessions;
this.projectSessions = projectSessions == null ? new HashMap() : projectSessions;
}
public void dispose()
{
for ( Iterator it = projectSessions.values().iterator(); it.hasNext(); )
{
MavenProjectSession session = (MavenProjectSession) it.next();
session.dispose();
}
}
// TODO: Figure out how/when we can shut down all the realms for extensions/plugins connected to each project session...

View File

@ -664,7 +664,28 @@ public class DefaultPluginManager
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
MavenProjectSession projectSession;
try
{
projectSession = session.getProjectSession( project );
}
catch ( PlexusContainerException e )
{
throw new PluginManagerException( mojoDescriptor, "Failed to create project-specific session for project: " + project.getId()
+ ".", project, e );
}
ClassRealm pluginRealm;
try
{
pluginRealm = projectSession.getPluginRealm( pluginDescriptor );
}
catch ( NoSuchRealmException e )
{
getLogger().debug( "Plugin realm: " + pluginDescriptor.getId() + " not found in project session for: " + project.getId() + ". Using project realm instead." );
pluginRealm = projectSession.getProjectRealm();
}
ClassRealm oldRealm = null;
try
@ -707,6 +728,7 @@ public class DefaultPluginManager
}
finally
{
pluginDescriptor.setClassRealm( null );
if ( oldRealm != null )
{
container.setLookupRealm( oldRealm );
@ -810,6 +832,7 @@ public class DefaultPluginManager
// lookups that occur in contextualize calls in line with the right realm.
ClassRealm oldRealm = container.setLookupRealm( realm );
pluginDescriptor.setClassRealm( realm );
getLogger().debug(
"Looking up mojo " + mojoDescriptor.getRoleHint() + " in realm "

View File

@ -321,8 +321,15 @@ public class MavenEmbedder
MavenSession session = new MavenSession( container, request, null, null, new HashMap() );
try
{
pluginManager.verifyPlugin( plugin, project, session );
}
finally
{
session.dispose();
}
}
/** protected for tests only.. */
protected Map getPluginExtensionComponents( Plugin plugin )

View File

@ -138,7 +138,7 @@ under the License.
<module>maven-embedder</module>
</modules>
<properties>
<plexusVersion>1.0-alpha-34</plexusVersion>
<plexusVersion>1.0-alpha-35-SNAPSHOT</plexusVersion>
<wagonVersion>1.0-beta-2</wagonVersion>
</properties>
<dependencies>