Skip to content

Commit

Permalink
mechanism to carryover session scope seeds from one thread to another
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Fedorenko <[email protected]>
  • Loading branch information
ifedorenko committed Feb 20, 2015
1 parent 117df85 commit 45563ff
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void buildProject( MavenSession session, MavenSession rootSession, Reacto

// 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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -64,6 +65,9 @@ public class LifecycleStarter

@Requirement
private Map<String, Builder> builders;

@Requirement
private SessionScope sessionScope;

public void execute( MavenSession session )
{
Expand Down Expand Up @@ -102,7 +106,9 @@ public void execute( MavenSession session )

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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.session.scope.internal.SessionScope;

/**
* Context that is fixed for the entire reactor build.
Expand All @@ -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()
Expand All @@ -69,4 +73,11 @@ public ClassLoader getOriginalContextClassLoader()
return originalContextClassLoader;
}

/**
* @since 3.2.6
*/
public SessionScope.Memento getSessionScopeMemento()
{
return sessionScope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
* 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;
Expand All @@ -32,6 +34,19 @@
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()
Expand Down Expand Up @@ -60,6 +75,15 @@ public void enter()
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();
Expand All @@ -84,6 +108,15 @@ public void exit()
}
}

/**
* @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 );
Expand Down

0 comments on commit 45563ff

Please sign in to comment.