maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ifedore...@apache.org
Subject git commit: MNG-5677 hooks to allow fine-grained cache management
Date Fri, 08 Aug 2014 18:28:54 GMT
Repository: maven
Updated Branches:
  refs/heads/master 8980f67b9 -> 693f8f660


MNG-5677 hooks to allow fine-grained cache management

Also fixed broken cache #flush() and missing #dispose() in
couple of places.

Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>


Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/693f8f66
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/693f8f66
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/693f8f66

Branch: refs/heads/master
Commit: 693f8f6604f24792b4dadbacf2fbc0eb79285915
Parents: 8980f67
Author: Igor Fedorenko <igor@ifedorenko.com>
Authored: Fri Aug 8 22:28:02 2014 +0400
Committer: Igor Fedorenko <igor@ifedorenko.com>
Committed: Fri Aug 8 22:28:06 2014 +0400

----------------------------------------------------------------------
 .../plugin/DefaultExtensionRealmCache.java      | 52 ++++++++++++++-----
 .../plugin/DefaultPluginArtifactsCache.java     |  8 +--
 .../maven/plugin/DefaultPluginRealmCache.java   |  2 +-
 .../maven/plugin/ExtensionRealmCache.java       | 16 ++++--
 .../maven/plugin/PluginArtifactsCache.java      |  2 +-
 .../apache/maven/plugin/PluginRealmCache.java   |  2 +-
 .../internal/DefaultMavenPluginManager.java     |  2 +-
 .../maven/project/DefaultProjectBuilder.java    |  1 +
 .../project/DefaultProjectBuildingHelper.java   | 20 +++++---
 .../maven/project/DefaultProjectRealmCache.java | 53 +++++++++++++++-----
 .../apache/maven/project/ProjectRealmCache.java | 17 +++++--
 11 files changed, 127 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/plugin/DefaultExtensionRealmCache.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultExtensionRealmCache.java
b/maven-core/src/main/java/org/apache/maven/plugin/DefaultExtensionRealmCache.java
index bc10223..bda84d0 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultExtensionRealmCache.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultExtensionRealmCache.java
@@ -21,14 +21,16 @@ package org.apache.maven.plugin;
 
 import java.io.File;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.maven.project.ExtensionDescriptor;
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
 import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
 import org.eclipse.aether.artifact.Artifact;
 
 /**
@@ -36,10 +38,11 @@ import org.eclipse.aether.artifact.Artifact;
  */
 @Component( role = ExtensionRealmCache.class )
 public class DefaultExtensionRealmCache
-    implements ExtensionRealmCache
+    implements ExtensionRealmCache, Disposable
 {
 
-    private static class CacheKey
+    protected static class CacheKey
+        implements Key
     {
 
         private final List<File> files;
@@ -97,28 +100,36 @@ public class DefaultExtensionRealmCache
                 && sizes.equals( other.sizes );
         }
 
+        @Override
+        public String toString()
+        {
+            return files.toString();
+        }
     }
 
-    private final Map<CacheKey, CacheRecord> cache = new HashMap<CacheKey, CacheRecord>();
+    protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<Key, CacheRecord>();
+
+    @Override
+    public Key createKey( List<? extends Artifact> extensionArtifacts )
+    {
+        return new CacheKey( extensionArtifacts );
+    }
 
-    public CacheRecord get( List<? extends Artifact> extensionArtifacts )
+    public CacheRecord get( Key key )
     {
-        return cache.get( new CacheKey( extensionArtifacts ) );
+        return cache.get( key );
     }
 
-    public CacheRecord put( List<? extends Artifact> extensionArtifacts, ClassRealm
extensionRealm,
-                            ExtensionDescriptor extensionDescriptor )
+    public CacheRecord put( Key key, ClassRealm extensionRealm, ExtensionDescriptor extensionDescriptor
)
     {
         if ( extensionRealm == null )
         {
             throw new NullPointerException();
         }
 
-        CacheKey key = new CacheKey( extensionArtifacts );
-
         if ( cache.containsKey( key ) )
         {
-            throw new IllegalStateException( "Duplicate extension realm for extension " +
extensionArtifacts );
+            throw new IllegalStateException( "Duplicate extension realm for extension " +
key );
         }
 
         CacheRecord record = new CacheRecord( extensionRealm, extensionDescriptor );
@@ -130,12 +141,29 @@ public class DefaultExtensionRealmCache
 
     public void flush()
     {
+        for ( CacheRecord record : cache.values() )
+        {
+            ClassRealm realm = record.realm;
+            try
+            {
+                realm.getWorld().disposeRealm( realm.getId() );
+            }
+            catch ( NoSuchRealmException e )
+            {
+                // ignore
+            }
+        }
         cache.clear();
     }
 
-    public void register( MavenProject project, CacheRecord record )
+    public void register( MavenProject project, Key key, CacheRecord record )
     {
         // default cache does not track extension usage
     }
 
+    public void dispose()
+    {
+        flush();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginArtifactsCache.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginArtifactsCache.java
b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginArtifactsCache.java
index 145482d..6ee3463 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginArtifactsCache.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginArtifactsCache.java
@@ -21,9 +21,9 @@ package org.apache.maven.plugin;
 
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.maven.model.Plugin;
 import org.apache.maven.project.MavenProject;
@@ -44,7 +44,7 @@ public class DefaultPluginArtifactsCache
     implements PluginArtifactsCache
 {
 
-    private static class CacheKey
+    protected static class CacheKey
         implements Key
     {
 
@@ -133,7 +133,7 @@ public class DefaultPluginArtifactsCache
 
     }
 
-    protected final Map<Key, CacheRecord> cache = new HashMap<Key, CacheRecord>();
+    protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<Key, CacheRecord>();
 
     public Key createKey( Plugin plugin, DependencyFilter extensionFilter, List<RemoteRepository>
repositories,
                           RepositorySystemSession session )
@@ -210,7 +210,7 @@ public class DefaultPluginArtifactsCache
         return CacheUtils.pluginEquals( a, b );
     }
 
-    public void register( MavenProject project, CacheRecord record )
+    public void register( MavenProject project, Key cacheKey, CacheRecord record )
     {
         // default cache does not track record usage
     }

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java
b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java
index e459cbc..d867036 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginRealmCache.java
@@ -206,7 +206,7 @@ public class DefaultPluginRealmCache
         return CacheUtils.pluginEquals( a, b );
     }
 
-    public void register( MavenProject project, CacheRecord record )
+    public void register( MavenProject project, Key key, CacheRecord record )
     {
         // default cache does not track plugin usage
     }

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/plugin/ExtensionRealmCache.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/ExtensionRealmCache.java b/maven-core/src/main/java/org/apache/maven/plugin/ExtensionRealmCache.java
index 560e454..61d3058 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/ExtensionRealmCache.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/ExtensionRealmCache.java
@@ -36,6 +36,13 @@ import org.eclipse.aether.artifact.Artifact;
  */
 public interface ExtensionRealmCache
 {
+    /**
+     * A cache key.
+     */
+    interface Key
+    {
+        // marker interface for cache keys
+    }
 
     static class CacheRecord
     {
@@ -52,10 +59,11 @@ public interface ExtensionRealmCache
 
     }
 
-    CacheRecord get( List<? extends Artifact> extensionArtifacts );
+    Key createKey( List<? extends Artifact> extensionArtifacts );
+
+    CacheRecord get( Key key );
 
-    CacheRecord put( List<? extends Artifact> extensionArtifacts, ClassRealm extensionRealm,
-                     ExtensionDescriptor extensionDescriptor );
+    CacheRecord put( Key key, ClassRealm extensionRealm, ExtensionDescriptor extensionDescriptor
);
 
     void flush();
 
@@ -67,6 +75,6 @@ public interface ExtensionRealmCache
      * @param project The project that employs the plugin realm, must not be {@code null}.
      * @param record The cache record being used for the project, must not be {@code null}.
      */
-    void register( MavenProject project, CacheRecord record );
+    void register( MavenProject project, Key key, CacheRecord record );
 
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/plugin/PluginArtifactsCache.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginArtifactsCache.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginArtifactsCache.java
index fe0ef15..e4d256f 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/PluginArtifactsCache.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginArtifactsCache.java
@@ -86,6 +86,6 @@ public interface PluginArtifactsCache
      * @param project The project that employs the plugin realm, must not be {@code null}.
      * @param record The cache record being used for the project, must not be {@code null}.
      */
-    void register( MavenProject project, CacheRecord record );
+    void register( MavenProject project, Key cacheKey, CacheRecord record );
 
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/plugin/PluginRealmCache.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginRealmCache.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginRealmCache.java
index 11e981b..4eea338 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/PluginRealmCache.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginRealmCache.java
@@ -80,6 +80,6 @@ public interface PluginRealmCache
      * @param project The project that employs the plugin realm, must not be {@code null}.
      * @param record The cache record being used for the project, must not be {@code null}.
      */
-    void register( MavenProject project, CacheRecord record );
+    void register( MavenProject project, Key key, CacheRecord record );
 
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
index 8c8f507..90dd9ec 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
@@ -330,7 +330,7 @@ public class DefaultMavenPluginManager
                 pluginRealmCache.put( cacheKey, pluginDescriptor.getClassRealm(), pluginDescriptor.getArtifacts()
);
         }
 
-        pluginRealmCache.register( project, cacheRecord );
+        pluginRealmCache.register( project, cacheKey, cacheRecord );
     }
 
     private void createPluginRealm( PluginDescriptor pluginDescriptor, MavenSession session,
ClassLoader parent,

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
index 32786ae..8e1d51e 100644
--- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
+++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
@@ -143,6 +143,7 @@ public class DefaultProjectBuilder
                 ModelBuildingRequest request = getModelBuildingRequest( config );
 
                 project = new MavenProject();
+                project.setFile( pomFile );
 
                 DefaultModelBuildingListener listener =
                     new DefaultModelBuildingListener( project, projectBuildingHelper, projectBuildingRequest
);

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java
b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java
index 58a87fc..fccdcfe 100644
--- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java
+++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java
@@ -244,18 +244,20 @@ public class DefaultProjectBuildingHelper
                 {
                     pluginArtifactsCache.put( cacheKey, e );
 
-                    pluginArtifactsCache.register( project, recordArtifacts );
+                    pluginArtifactsCache.register( project, cacheKey, recordArtifacts );
 
                     throw e;
                 }
             }
 
-            pluginArtifactsCache.register( project, recordArtifacts );
+            pluginArtifactsCache.register( project, cacheKey, recordArtifacts );
 
             ClassRealm extensionRealm;
             ExtensionDescriptor extensionDescriptor = null;
+            
+            final ExtensionRealmCache.Key extensionKey = extensionRealmCache.createKey( artifacts
);
 
-            ExtensionRealmCache.CacheRecord recordRealm = extensionRealmCache.get( artifacts
);
+            ExtensionRealmCache.CacheRecord recordRealm = extensionRealmCache.get( extensionKey
);
 
             if ( recordRealm != null )
             {
@@ -295,10 +297,10 @@ public class DefaultProjectBuildingHelper
                     }
                 }
 
-                recordRealm = extensionRealmCache.put( artifacts, extensionRealm, extensionDescriptor
);
+                recordRealm = extensionRealmCache.put( extensionKey, extensionRealm, extensionDescriptor
);
             }
 
-            extensionRealmCache.register( project, recordRealm );
+            extensionRealmCache.register( project, extensionKey, recordRealm );
 
             extensionRealms.add( extensionRealm );
             if ( extensionDescriptor != null )
@@ -324,7 +326,9 @@ public class DefaultProjectBuildingHelper
             logger.debug( "Extension realms for project " + model.getId() + ": " + extensionRealms
);
         }
 
-        ProjectRealmCache.CacheRecord record = projectRealmCache.get( extensionRealms );
+        ProjectRealmCache.Key projectRealmKey = projectRealmCache.createKey( extensionRealms
);
+
+        ProjectRealmCache.CacheRecord record = projectRealmCache.get( projectRealmKey );
 
         if ( record == null )
         {
@@ -365,10 +369,10 @@ public class DefaultProjectBuildingHelper
                 extensionArtifactFilter = new ExclusionsDependencyFilter( exclusions );
             }
 
-            record = projectRealmCache.put( extensionRealms, projectRealm, extensionArtifactFilter
);
+            record = projectRealmCache.put( projectRealmKey, projectRealm, extensionArtifactFilter
);
         }
 
-        projectRealmCache.register( project, record );
+        projectRealmCache.register( project, projectRealmKey, record );
 
         return record;
     }

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/project/DefaultProjectRealmCache.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectRealmCache.java
b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectRealmCache.java
index 1c5dfa3..6ad8150 100644
--- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectRealmCache.java
+++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectRealmCache.java
@@ -20,12 +20,14 @@ package org.apache.maven.project;
  */
 
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
 import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
 import org.eclipse.aether.graph.DependencyFilter;
 
 /**
@@ -33,10 +35,11 @@ import org.eclipse.aether.graph.DependencyFilter;
  */
 @Component( role = ProjectRealmCache.class )
 public class DefaultProjectRealmCache
-    implements ProjectRealmCache
+    implements ProjectRealmCache, Disposable
 {
 
-    private static class CacheKey
+    protected static class CacheKey
+        implements Key
     {
 
         private final List<? extends ClassRealm> extensionRealms;
@@ -74,28 +77,36 @@ public class DefaultProjectRealmCache
             return extensionRealms.equals( other.extensionRealms );
         }
 
+        @Override
+        public String toString()
+        {
+            return extensionRealms.toString();
+        }
     }
 
-    private final Map<CacheKey, CacheRecord> cache = new HashMap<CacheKey, CacheRecord>();
+    protected final Map<Key, CacheRecord> cache = new ConcurrentHashMap<Key, CacheRecord>();
+
+    @Override
+    public Key createKey( List<? extends ClassRealm> extensionRealms )
+    {
+        return new CacheKey( extensionRealms );
+    }
 
-    public CacheRecord get( List<? extends ClassRealm> extensionRealms )
+    public CacheRecord get( Key key )
     {
-        return cache.get( new CacheKey( extensionRealms ) );
+        return cache.get( key );
     }
 
-    public CacheRecord put( List<? extends ClassRealm> extensionRealms, ClassRealm
projectRealm,
-                            DependencyFilter extensionArtifactFilter )
+    public CacheRecord put( Key key, ClassRealm projectRealm, DependencyFilter extensionArtifactFilter
)
     {
         if ( projectRealm == null )
         {
             throw new NullPointerException();
         }
 
-        CacheKey key = new CacheKey( extensionRealms );
-
         if ( cache.containsKey( key ) )
         {
-            throw new IllegalStateException( "Duplicate project realm for extensions " +
extensionRealms );
+            throw new IllegalStateException( "Duplicate project realm for extensions " +
key );
         }
 
         CacheRecord record = new CacheRecord( projectRealm, extensionArtifactFilter );
@@ -107,12 +118,30 @@ public class DefaultProjectRealmCache
 
     public void flush()
     {
+        for ( CacheRecord record : cache.values() )
+        {
+            ClassRealm realm = record.realm;
+            try
+            {
+                realm.getWorld().disposeRealm( realm.getId() );
+            }
+            catch ( NoSuchRealmException e )
+            {
+                // ignore
+            }
+        }
         cache.clear();
     }
 
-    public void register( MavenProject project, CacheRecord record )
+    public void register( MavenProject project, Key key, CacheRecord record )
     {
         // default cache does not track record usage
     }
 
+    @Override
+    public void dispose()
+    {
+        flush();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/693f8f66/maven-core/src/main/java/org/apache/maven/project/ProjectRealmCache.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectRealmCache.java b/maven-core/src/main/java/org/apache/maven/project/ProjectRealmCache.java
index 415eab9..779d96b 100644
--- a/maven-core/src/main/java/org/apache/maven/project/ProjectRealmCache.java
+++ b/maven-core/src/main/java/org/apache/maven/project/ProjectRealmCache.java
@@ -35,6 +35,14 @@ import org.eclipse.aether.graph.DependencyFilter;
 public interface ProjectRealmCache
 {
 
+    /**
+     * A cache key.
+     */
+    interface Key
+    {
+        // marker interface for cache keys
+    }
+
     static class CacheRecord
     {
 
@@ -50,10 +58,11 @@ public interface ProjectRealmCache
 
     }
 
-    CacheRecord get( List<? extends ClassRealm> extensionRealms );
+    Key createKey( List<? extends ClassRealm> extensionRealms );
+
+    CacheRecord get( Key key );
 
-    CacheRecord put( List<? extends ClassRealm> extensionRealms, ClassRealm projectRealm,
-                     DependencyFilter extensionArtifactFilter );
+    CacheRecord put( Key key, ClassRealm projectRealm, DependencyFilter extensionArtifactFilter
);
 
     void flush();
 
@@ -65,6 +74,6 @@ public interface ProjectRealmCache
      * @param project The project that employs the plugin realm, must not be {@code null}.
      * @param record The cache record being used for the project, must not be {@code null}.
      */
-    void register( MavenProject project, CacheRecord record );
+    void register( MavenProject project, Key key, CacheRecord record );
 
 }


Mime
View raw message