Author: jdcasey
Date: Tue Oct 23 16:14:28 2007
New Revision: 587690

URL: http://svn.apache.org/viewvc?rev=587690&view=rev
Log:
Cleanup the discovery realm/world after new extension components are found.

Modified:
    
maven/components/trunk/maven-core/src/main/java/org/apache/maven/extension/DefaultExtensionManager.java

Modified: 
maven/components/trunk/maven-core/src/main/java/org/apache/maven/extension/DefaultExtensionManager.java
URL: 
http://svn.apache.org/viewvc/maven/components/trunk/maven-core/src/main/java/org/apache/maven/extension/DefaultExtensionManager.java?rev=587690&r1=587689&r2=587690&view=diff
==============================================================================
--- 
maven/components/trunk/maven-core/src/main/java/org/apache/maven/extension/DefaultExtensionManager.java
 (original)
+++ 
maven/components/trunk/maven-core/src/main/java/org/apache/maven/extension/DefaultExtensionManager.java
 Tue Oct 23 16:14:28 2007
@@ -64,6 +64,7 @@
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
 
 import java.net.MalformedURLException;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -291,71 +292,90 @@
     {
         String projectId = projectSession.getProjectId();
 
-        // Create an entire new ClassWorld, ClassRealm for discovering
-        // the immediate components of the extension artifact, so we don't 
pollute the
-        // container with component descriptors or realms that don't have any 
meaning beyond discovery.
-        ClassRealm discoveryRealm = new ClassRealm( new ClassWorld(), 
"discovery", Thread.currentThread().getContextClassLoader() );
+        ClassWorld discoveryWorld = new ClassWorld();
         try
         {
-            discoveryRealm.addURL( extensionArtifact.getFile().toURL() );
-        }
-        catch ( MalformedURLException e )
-        {
-            throw new ExtensionManagerException( "Unable to generate URL from 
extension artifact for local-component discovery: " + 
extensionArtifact.getFile(), extensionArtifact, projectId, e );
-        }
+            // Create an entire new ClassWorld, ClassRealm for discovering
+            // the immediate components of the extension artifact, so we don't 
pollute the
+            // container with component descriptors or realms that don't have 
any meaning beyond discovery.
+            ClassRealm discoveryRealm = new ClassRealm( discoveryWorld, 
"discovery", Thread.currentThread().getContextClassLoader() );
+            try
+            {
+                discoveryRealm.addURL( extensionArtifact.getFile().toURL() );
+            }
+            catch ( MalformedURLException e )
+            {
+                throw new ExtensionManagerException( "Unable to generate URL 
from extension artifact for local-component discovery: " + 
extensionArtifact.getFile(), extensionArtifact, projectId, e );
+            }
 
-        ComponentDiscoverer discoverer = new DefaultComponentDiscoverer();
-        discoverer.setManager( new DummyDiscovererManager() );
+            ComponentDiscoverer discoverer = new DefaultComponentDiscoverer();
+            discoverer.setManager( new DummyDiscovererManager() );
 
-        ClassRealm projectRealm = projectSession.getProjectRealm();
-        try
-        {
-            // Find the extension component descriptors that exist ONLY in the 
immediate extension
-            // artifact...this prevents us from adding plexus-archiver 
components to the mix, for instance,
-            // when the extension uses that dependency.
-            List componentSetDescriptors = discoverer.findComponents( 
container.getContext(), discoveryRealm );
-            for ( Iterator it = componentSetDescriptors.iterator(); 
it.hasNext(); )
+            ClassRealm projectRealm = projectSession.getProjectRealm();
+            try
             {
-                ComponentSetDescriptor compSet = (ComponentSetDescriptor) 
it.next();
-                for ( Iterator compIt = compSet.getComponents().iterator(); 
compIt.hasNext(); )
+                // Find the extension component descriptors that exist ONLY in 
the immediate extension
+                // artifact...this prevents us from adding plexus-archiver 
components to the mix, for instance,
+                // when the extension uses that dependency.
+                List componentSetDescriptors = discoverer.findComponents( 
container.getContext(), discoveryRealm );
+                for ( Iterator it = componentSetDescriptors.iterator(); 
it.hasNext(); )
                 {
-                    // For each component in the extension artifact:
-                    ComponentDescriptor comp = (ComponentDescriptor) 
compIt.next();
-                    String implementation = comp.getImplementation();
-
-                    try
-                    {
-                        getLogger().debug( "Importing: " + implementation + 
"\nwith role: " + comp.getRole() + "\nand hint: " + comp.getRoleHint() + 
"\nfrom extension realm: " + extensionRealm.getId() + "\nto project realm: " + 
projectRealm.getId() );
-
-                        // Import the extension component's implementation 
class into the project-level
-                        // realm.
-                        projectRealm.importFrom( extensionRealm.getId(), 
implementation );
-
-                        // Set the realmId to be used in looking up this 
extension component to the
-                        // project-level realm, since we now have a restricted 
import
-                        // that allows most of the extension to stay hidden, 
and the
-                        // specific local extension components are still 
accessible
-                        // from the project-level realm.
-                        comp.setRealmId( projectRealm.getId() );
-
-                        // Finally, add the extension component's descriptor 
(with projectRealm
-                        // set as the lookup realm) to the container.
-                        container.addComponentDescriptor( comp );
-                    }
-                    catch ( NoSuchRealmException e )
+                    ComponentSetDescriptor compSet = (ComponentSetDescriptor) 
it.next();
+                    for ( Iterator compIt = 
compSet.getComponents().iterator(); compIt.hasNext(); )
                     {
-                        throw new ExtensionManagerException( "Failed to create 
import for component: " + implementation + " from extension realm: " + 
extensionRealm.getId() + " to project realm: " + projectRealm.getId(), 
extensionArtifact, projectId, e );
+                        // For each component in the extension artifact:
+                        ComponentDescriptor comp = (ComponentDescriptor) 
compIt.next();
+                        String implementation = comp.getImplementation();
+
+                        try
+                        {
+                            getLogger().debug( "Importing: " + implementation 
+ "\nwith role: " + comp.getRole() + "\nand hint: " + comp.getRoleHint() + 
"\nfrom extension realm: " + extensionRealm.getId() + "\nto project realm: " + 
projectRealm.getId() );
+
+                            // Import the extension component's implementation 
class into the project-level
+                            // realm.
+                            projectRealm.importFrom( extensionRealm.getId(), 
implementation );
+
+                            // Set the realmId to be used in looking up this 
extension component to the
+                            // project-level realm, since we now have a 
restricted import
+                            // that allows most of the extension to stay 
hidden, and the
+                            // specific local extension components are still 
accessible
+                            // from the project-level realm.
+                            comp.setRealmId( projectRealm.getId() );
+
+                            // Finally, add the extension component's 
descriptor (with projectRealm
+                            // set as the lookup realm) to the container.
+                            container.addComponentDescriptor( comp );
+                        }
+                        catch ( NoSuchRealmException e )
+                        {
+                            throw new ExtensionManagerException( "Failed to 
create import for component: " + implementation + " from extension realm: " + 
extensionRealm.getId() + " to project realm: " + projectRealm.getId(), 
extensionArtifact, projectId, e );
+                        }
                     }
                 }
             }
+            catch ( PlexusConfigurationException e )
+            {
+                throw new ExtensionManagerException( "Unable to discover 
extension components.", extensionArtifact, projectId, e );
+            }
+            catch ( ComponentRepositoryException e )
+            {
+                throw new ExtensionManagerException( "Unable to discover 
extension components from imports added to project-session realm.", 
extensionArtifact, projectId, e );
+            }
         }
-        catch ( PlexusConfigurationException e )
-        {
-            throw new ExtensionManagerException( "Unable to discover extension 
components.", extensionArtifact, projectId, e );
-        }
-        catch ( ComponentRepositoryException e )
+        finally
         {
-            throw new ExtensionManagerException( "Unable to discover extension 
components from imports added to project-session realm.", extensionArtifact, 
projectId, e );
+            Collection realms = discoveryWorld.getRealms();
+            for ( Iterator it = realms.iterator(); it.hasNext(); )
+            {
+                ClassRealm realm = (ClassRealm) it.next();
+                try
+                {
+                    discoveryWorld.disposeRealm( realm.getId() );
+                }
+                catch ( NoSuchRealmException e )
+                {
+                }
+            }
         }
     }
 


Reply via email to