maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ifedore...@apache.org
Subject git commit: MNG-5655 deduplicate WeakMojoExecutionListener instances
Date Thu, 26 Jun 2014 13:20:15 GMT
Repository: maven
Updated Branches:
  refs/heads/master 4da87163f -> 963373726


MNG-5655 deduplicate WeakMojoExecutionListener instances

Signed-off-by: Igor Fedorenko <ifedorenko@apache.org>


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

Branch: refs/heads/master
Commit: 963373726d900c8e093931af7baa94f3a08cb44d
Parents: 4da8716
Author: Igor Fedorenko <ifedorenko@apache.org>
Authored: Thu Jun 26 09:07:24 2014 -0400
Committer: Igor Fedorenko <ifedorenko@apache.org>
Committed: Thu Jun 26 09:07:30 2014 -0400

----------------------------------------------------------------------
 .../scope/internal/MojoExecutionScope.java      | 32 +++++----
 .../scope/internal/MojoExecutionScopeTest.java  | 69 ++++++++++++++++++++
 2 files changed, 89 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/96337372/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScope.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScope.java
b/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScope.java
index fc8b1e6..d8a5f6c 100644
--- a/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScope.java
+++ b/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScope.java
@@ -19,6 +19,8 @@ package org.apache.maven.execution.scope.internal;
  * under the License.
  */
 
+import java.util.Collection;
+import java.util.IdentityHashMap;
 import java.util.LinkedList;
 import java.util.Map;
 
@@ -177,36 +179,42 @@ public class MojoExecutionScope
     public void beforeMojoExecution( MojoExecutionEvent event )
         throws MojoExecutionException
     {
-        for ( Object provided : getScopeState().provided.values() )
+        for ( WeakMojoExecutionListener provided : getProvidedListeners() )
         {
-            if ( provided instanceof WeakMojoExecutionListener )
-            {
-                ( (WeakMojoExecutionListener) provided ).beforeMojoExecution( event );
-            }
+            provided.beforeMojoExecution( event );
         }
     }
 
     public void afterMojoExecutionSuccess( MojoExecutionEvent event )
         throws MojoExecutionException
     {
-        for ( Object provided : getScopeState().provided.values() )
+        for ( WeakMojoExecutionListener provided : getProvidedListeners() )
         {
-            if ( provided instanceof WeakMojoExecutionListener )
-            {
-                ( (WeakMojoExecutionListener) provided ).afterMojoExecutionSuccess( event
);
-            }
+            provided.afterMojoExecutionSuccess( event );
         }
     }
 
     public void afterExecutionFailure( MojoExecutionEvent event )
     {
+        for ( WeakMojoExecutionListener provided : getProvidedListeners() )
+        {
+            provided.afterExecutionFailure( event );
+        }
+    }
+
+    private Collection<WeakMojoExecutionListener> getProvidedListeners()
+    {
+        // the same instance can be provided multiple times under different Key's
+        // deduplicate instances to avoid redundant beforeXXX/afterXXX callbacks
+        IdentityHashMap<WeakMojoExecutionListener, Object> listeners =
+            new IdentityHashMap<WeakMojoExecutionListener, Object>();
         for ( Object provided : getScopeState().provided.values() )
         {
             if ( provided instanceof WeakMojoExecutionListener )
             {
-                ( (WeakMojoExecutionListener) provided ).afterExecutionFailure( event );
+                listeners.put( (WeakMojoExecutionListener) provided, null );
             }
         }
+        return listeners.keySet();
     }
-
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/96337372/maven-core/src/test/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeTest.java
----------------------------------------------------------------------
diff --git a/maven-core/src/test/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeTest.java
b/maven-core/src/test/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeTest.java
index 6e6f9fe..a4eb354 100644
--- a/maven-core/src/test/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeTest.java
+++ b/maven-core/src/test/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeTest.java
@@ -14,9 +14,16 @@
  */
 package org.apache.maven.execution.scope.internal;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.maven.execution.MojoExecutionEvent;
+import org.apache.maven.execution.scope.WeakMojoExecutionListener;
+import org.apache.maven.plugin.MojoExecutionException;
+
 import junit.framework.TestCase;
 
 import com.google.inject.Key;
+import com.google.inject.Provider;
 
 public class MojoExecutionScopeTest
     extends TestCase
@@ -45,9 +52,71 @@ public class MojoExecutionScopeTest
         try
         {
             scope.exit();
+            fail();
         }
         catch ( IllegalStateException expected )
         {
         }
     }
+
+    public void testMultiKeyInstance()
+        throws Exception
+    {
+        MojoExecutionScope scope = new MojoExecutionScope();
+        scope.enter();
+
+        final AtomicInteger beforeExecution = new AtomicInteger();
+        final AtomicInteger afterExecutionSuccess = new AtomicInteger();
+        final AtomicInteger afterExecutionFailure = new AtomicInteger();
+        final WeakMojoExecutionListener instance = new WeakMojoExecutionListener()
+        {
+            @Override
+            public void beforeMojoExecution( MojoExecutionEvent event )
+                throws MojoExecutionException
+            {
+                beforeExecution.incrementAndGet();
+            }
+
+            @Override
+            public void afterMojoExecutionSuccess( MojoExecutionEvent event )
+                throws MojoExecutionException
+            {
+                afterExecutionSuccess.incrementAndGet();
+            }
+
+            @Override
+            public void afterExecutionFailure( MojoExecutionEvent event )
+            {
+                afterExecutionFailure.incrementAndGet();
+            }
+        };
+        assertSame( instance, scope.scope( Key.get( Object.class ), new Provider<Object>()
+        {
+            @Override
+            public Object get()
+            {
+                return instance;
+            }
+        } ).get() );
+        assertSame( instance,
+                    scope.scope( Key.get( WeakMojoExecutionListener.class ), new Provider<WeakMojoExecutionListener>()
+                    {
+                        @Override
+                        public WeakMojoExecutionListener get()
+                        {
+                            return instance;
+                        }
+                    } ).get() );
+
+        final MojoExecutionEvent event = new MojoExecutionEvent( null, null, null, null );
+        scope.beforeMojoExecution( event );
+        scope.afterMojoExecutionSuccess( event );
+        scope.afterExecutionFailure( event );
+
+        assertEquals( 1, beforeExecution.get() );
+        assertEquals( 1, afterExecutionSuccess.get() );
+        assertEquals( 1, afterExecutionFailure.get() );
+
+        scope.exit();
+    }
 }


Mime
View raw message