MNG-5655 deduplicate WeakMojoExecutionListener instances

Signed-off-by: Igor Fedorenko <ifedorenko@apache.org>
This commit is contained in:
Igor Fedorenko 2014-06-26 09:07:24 -04:00
parent 4da87163f9
commit 963373726d
2 changed files with 89 additions and 12 deletions

View File

@ -19,6 +19,8 @@
* under the License. * under the License.
*/ */
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map; import java.util.Map;
@ -177,36 +179,42 @@ protected void configure()
public void beforeMojoExecution( MojoExecutionEvent event ) public void beforeMojoExecution( MojoExecutionEvent event )
throws MojoExecutionException throws MojoExecutionException
{ {
for ( Object provided : getScopeState().provided.values() ) for ( WeakMojoExecutionListener provided : getProvidedListeners() )
{ {
if ( provided instanceof WeakMojoExecutionListener ) provided.beforeMojoExecution( event );
{
( (WeakMojoExecutionListener) provided ).beforeMojoExecution( event );
}
} }
} }
public void afterMojoExecutionSuccess( MojoExecutionEvent event ) public void afterMojoExecutionSuccess( MojoExecutionEvent event )
throws MojoExecutionException throws MojoExecutionException
{ {
for ( Object provided : getScopeState().provided.values() ) for ( WeakMojoExecutionListener provided : getProvidedListeners() )
{ {
if ( provided instanceof WeakMojoExecutionListener ) provided.afterMojoExecutionSuccess( event );
{
( (WeakMojoExecutionListener) provided ).afterMojoExecutionSuccess( event );
}
} }
} }
public void afterExecutionFailure( MojoExecutionEvent 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() ) for ( Object provided : getScopeState().provided.values() )
{ {
if ( provided instanceof WeakMojoExecutionListener ) if ( provided instanceof WeakMojoExecutionListener )
{ {
( (WeakMojoExecutionListener) provided ).afterExecutionFailure( event ); listeners.put( (WeakMojoExecutionListener) provided, null );
} }
} }
return listeners.keySet();
} }
} }

View File

@ -14,9 +14,16 @@
*/ */
package org.apache.maven.execution.scope.internal; 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 junit.framework.TestCase;
import com.google.inject.Key; import com.google.inject.Key;
import com.google.inject.Provider;
public class MojoExecutionScopeTest public class MojoExecutionScopeTest
extends TestCase extends TestCase
@ -45,9 +52,71 @@ public void testNestedEnter()
try try
{ {
scope.exit(); scope.exit();
fail();
} }
catch ( IllegalStateException expected ) 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();
}
} }