mechanism to carryover session scope seeds from one thread to another

Signed-off-by: Igor Fedorenko <ifedorenko@apache.org>
This commit is contained in:
Igor Fedorenko 2015-02-05 12:07:41 -05:00
parent 117df855e2
commit 45563ff5cb
4 changed files with 53 additions and 3 deletions

View File

@ -89,7 +89,7 @@ public class LifecycleModuleBuilder
// session may be different from rootSession seeded in DefaultMaven
// explicitly seed the right session here to make sure it is used by Guice
sessionScope.enter();
sessionScope.enter( reactorContext.getSessionScopeMemento() );
sessionScope.seed( MavenSession.class, session );
try
{

View File

@ -30,6 +30,7 @@ import org.apache.maven.lifecycle.MissingProjectException;
import org.apache.maven.lifecycle.NoGoalSpecifiedException;
import org.apache.maven.lifecycle.internal.builder.Builder;
import org.apache.maven.lifecycle.internal.builder.BuilderNotFoundException;
import org.apache.maven.session.scope.internal.SessionScope;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
@ -64,6 +65,9 @@ public class LifecycleStarter
@Requirement
private Map<String, Builder> builders;
@Requirement
private SessionScope sessionScope;
public void execute( MavenSession session )
{
@ -102,7 +106,9 @@ public class LifecycleStarter
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
ReactorBuildStatus reactorBuildStatus = new ReactorBuildStatus( session.getProjectDependencyGraph() );
reactorContext = new ReactorContext( result, projectIndex, oldContextClassLoader, reactorBuildStatus );
reactorContext =
new ReactorContext( result, projectIndex, oldContextClassLoader, reactorBuildStatus,
sessionScope.memento() );
String builderId = session.getRequest().getBuilderId();
Builder builder = builders.get( builderId );

View File

@ -20,6 +20,7 @@ package org.apache.maven.lifecycle.internal;
*/
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.session.scope.internal.SessionScope;
/**
* Context that is fixed for the entire reactor build.
@ -39,14 +40,17 @@ public class ReactorContext
private final ReactorBuildStatus reactorBuildStatus;
private final SessionScope.Memento sessionScope;
public ReactorContext( MavenExecutionResult result, ProjectIndex projectIndex,
ClassLoader originalContextClassLoader, ReactorBuildStatus reactorBuildStatus )
ClassLoader originalContextClassLoader, ReactorBuildStatus reactorBuildStatus,
SessionScope.Memento sessionScope )
{
this.result = result;
this.projectIndex = projectIndex;
this.originalContextClassLoader = originalContextClassLoader;
this.reactorBuildStatus = reactorBuildStatus;
this.sessionScope = sessionScope;
}
public ReactorBuildStatus getReactorBuildStatus()
@ -69,4 +73,11 @@ public class ReactorContext
return originalContextClassLoader;
}
/**
* @since 3.2.6
*/
public SessionScope.Memento getSessionScopeMemento()
{
return sessionScope;
}
}

View File

@ -19,9 +19,11 @@ package org.apache.maven.session.scope.internal;
* under the License.
*/
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.inject.Key;
import com.google.inject.OutOfScopeException;
@ -32,6 +34,19 @@ import com.google.inject.util.Providers;
public class SessionScope
implements Scope
{
/**
* @since 3.2.6
*/
public static class Memento
{
final Map<Key<?>, Provider<?>> seeded;
Memento( final Map<Key<?>, Provider<?>> seeded )
{
this.seeded = ImmutableMap.copyOf( seeded );
}
}
private static final Provider<Object> SEEDED_KEY_PROVIDER = new Provider<Object>()
{
public Object get()
@ -60,6 +75,15 @@ public class SessionScope
stack.addFirst( new ScopeState() );
}
/**
* @since 3.2.6
*/
public void enter( Memento memento )
{
enter();
getScopeState().seeded.putAll( memento.seeded );
}
private ScopeState getScopeState()
{
LinkedList<ScopeState> stack = values.get();
@ -84,6 +108,15 @@ public class SessionScope
}
}
/**
* @since 3.2.6
*/
public Memento memento()
{
LinkedList<ScopeState> stack = values.get();
return new Memento( stack != null ? stack.getFirst().seeded : Collections.<Key<?>, Provider<?>>emptyMap() );
}
public <T> void seed( Class<T> clazz, Provider<T> value )
{
getScopeState().seeded.put( Key.get( clazz ), value );