Repository: maven
Updated Branches:
  refs/heads/master 3d2d8619b -> b80fb7d7c


MNG-5695 fixed inconsistent/incomplete custom guice scope bindings

MojoExecution scope was only available for maven plugins and could not
be used for components defined in maven core or maven code extensions.

Session scope was only available for maven core and core extensions
but did not work for components from maven plugins.

Made both custom scopes available available in all realms.

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


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

Branch: refs/heads/master
Commit: b80fb7d7ce46c67cc2caa4e136430add83535e23
Parents: 3d2d861
Author: Igor Fedorenko <ifedore...@apache.org>
Authored: Fri Sep 26 20:18:47 2014 -0400
Committer: Igor Fedorenko <ifedore...@apache.org>
Committed: Fri Sep 26 21:55:30 2014 -0400

----------------------------------------------------------------------
 .../java/org/apache/maven/DefaultMaven.java     |   1 +
 .../java/org/apache/maven/SessionModule.java    |  39 ------
 .../java/org/apache/maven/SessionScope.java     | 136 -------------------
 .../scope/internal/MojoExecutionScope.java      |  30 ----
 .../internal/MojoExecutionScopeCoreModule.java  |  45 ++++++
 .../internal/MojoExecutionScopeModule.java      |  56 ++++++++
 .../internal/LifecycleModuleBuilder.java        |   2 +-
 .../internal/DefaultMavenPluginManager.java     |   7 +-
 .../project/DefaultProjectBuildingHelper.java   |   6 +-
 .../session/scope/internal/SessionScope.java    | 136 +++++++++++++++++++
 .../scope/internal/SessionScopeModule.java      |  63 +++++++++
 11 files changed, 310 insertions(+), 211 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java 
b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
index f2feffb..fb70bfe 100644
--- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
+++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
@@ -61,6 +61,7 @@ import org.apache.maven.project.ProjectBuildingRequest;
 import org.apache.maven.project.ProjectBuildingResult;
 import org.apache.maven.repository.LocalRepositoryNotAccessibleException;
 import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
+import org.apache.maven.session.scope.internal.SessionScope;
 import org.apache.maven.settings.Mirror;
 import org.apache.maven.settings.Proxy;
 import org.apache.maven.settings.Server;

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/maven-core/src/main/java/org/apache/maven/SessionModule.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/SessionModule.java 
b/maven-core/src/main/java/org/apache/maven/SessionModule.java
deleted file mode 100644
index ee853ce..0000000
--- a/maven-core/src/main/java/org/apache/maven/SessionModule.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package org.apache.maven;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import javax.inject.Named;
-
-import org.apache.maven.execution.MavenSession;
-
-import com.google.inject.AbstractModule;
-
-@Named
-public class SessionModule extends AbstractModule
-{            
-    @Override
-    protected void configure()
-    {
-        SessionScope scope = new SessionScope();
-        bindScope( SessionScoped.class, scope );
-        bind( SessionScope.class ).toInstance( scope );
-        bind( MavenSession.class ).toProvider( 
SessionScope.<MavenSession>seededKeyProvider() ).in( scope );
-    }
-}

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/maven-core/src/main/java/org/apache/maven/SessionScope.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/SessionScope.java 
b/maven-core/src/main/java/org/apache/maven/SessionScope.java
deleted file mode 100644
index 9c16fec..0000000
--- a/maven-core/src/main/java/org/apache/maven/SessionScope.java
+++ /dev/null
@@ -1,136 +0,0 @@
-package org.apache.maven;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import java.util.LinkedList;
-import java.util.Map;
-
-import com.google.common.collect.Maps;
-import com.google.inject.Key;
-import com.google.inject.OutOfScopeException;
-import com.google.inject.Provider;
-import com.google.inject.Scope;
-import com.google.inject.util.Providers;
-
-public class SessionScope
-    implements Scope
-{    
-    private static final Provider<Object> SEEDED_KEY_PROVIDER = new 
Provider<Object>()
-    {
-        public Object get()
-        {
-            throw new IllegalStateException();
-        }
-    };
-
-    private static final class ScopeState
-    {
-        public final Map<Key<?>, Provider<?>> seeded = Maps.newHashMap();
-
-        public final Map<Key<?>, Object> provided = Maps.newHashMap();
-    }
-
-    private final ThreadLocal<LinkedList<ScopeState>> values = new 
ThreadLocal<LinkedList<ScopeState>>();
-
-    public void enter()
-    {
-        LinkedList<ScopeState> stack = values.get();
-        if ( stack == null )
-        {
-            stack = new LinkedList<ScopeState>();
-            values.set( stack );
-        }
-        stack.addFirst( new ScopeState() );
-    }
-
-    private ScopeState getScopeState()
-    {
-        LinkedList<ScopeState> stack = values.get();
-        if ( stack == null || stack.isEmpty() )
-        {
-            throw new IllegalStateException();
-        }
-        return stack.getFirst();
-    }
-
-    public void exit()
-    {
-        final LinkedList<ScopeState> stack = values.get();
-        if ( stack == null || stack.isEmpty() )
-        {
-            throw new IllegalStateException();
-        }
-        stack.removeFirst();
-        if ( stack.isEmpty() )
-        {
-            values.remove();
-        }
-    }
-
-    public <T> void seed( Class<T> clazz, Provider<T> value )
-    {
-        getScopeState().seeded.put( Key.get( clazz ), value );
-    }
-
-    public <T> void seed( Class<T> clazz, final T value )
-    {
-        getScopeState().seeded.put( Key.get( clazz ), Providers.of( value ) );
-    }
-
-    public <T> Provider<T> scope( final Key<T> key, final Provider<T> unscoped 
)
-    {
-        return new Provider<T>()
-        {
-            @SuppressWarnings( "unchecked" )
-            public T get()
-            {
-                LinkedList<ScopeState> stack = values.get();
-                if ( stack == null || stack.isEmpty() )
-                {
-                    throw new OutOfScopeException( "Cannot access " + key + " 
outside of a scoping block" );
-                }
-
-                ScopeState state = stack.getFirst();
-
-                Provider<?> seeded = state.seeded.get( key );
-
-                if ( seeded != null )
-                {
-                    return (T) seeded.get();
-                }
-
-                T provided = (T) state.provided.get( key );
-                if ( provided == null && unscoped != null )
-                {
-                    provided = unscoped.get();
-                    state.provided.put( key, provided );
-                }
-
-                return provided;
-            }
-        };
-    }
-
-    @SuppressWarnings( { "unchecked" } )
-    public static <T> Provider<T> seededKeyProvider()
-    {
-        return (Provider<T>) SEEDED_KEY_PROVIDER;
-    }
-}

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/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 d8a5f6c..8a98daf 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
@@ -24,30 +24,18 @@ import java.util.IdentityHashMap;
 import java.util.LinkedList;
 import java.util.Map;
 
-import javax.inject.Named;
-import javax.inject.Singleton;
-
 import org.apache.maven.execution.MojoExecutionEvent;
 import org.apache.maven.execution.MojoExecutionListener;
-import org.apache.maven.execution.scope.MojoExecutionScoped;
 import org.apache.maven.execution.scope.WeakMojoExecutionListener;
-import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.PlexusContainer;
-import 
org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 
 import com.google.common.collect.Maps;
-import com.google.inject.AbstractModule;
 import com.google.inject.Key;
-import com.google.inject.Module;
 import com.google.inject.OutOfScopeException;
 import com.google.inject.Provider;
 import com.google.inject.Scope;
 import com.google.inject.util.Providers;
 
-@Named
-@Singleton
 public class MojoExecutionScope
     implements Scope, MojoExecutionListener
 {
@@ -158,24 +146,6 @@ public class MojoExecutionScope
         return (Provider<T>) SEEDED_KEY_PROVIDER;
     }
 
-    public static Module getScopeModule( PlexusContainer container )
-        throws ComponentLookupException
-    {
-        final MojoExecutionScope scope = container.lookup( 
MojoExecutionScope.class );
-        return new AbstractModule()
-        {
-            @Override
-            protected void configure()
-            {
-                bindScope( MojoExecutionScoped.class, scope );
-
-                // standard scope bindings
-                bind( MavenProject.class ).toProvider( 
MojoExecutionScope.<MavenProject> seededKeyProvider() ).in( scope );
-                bind( MojoExecution.class ).toProvider( 
MojoExecutionScope.<MojoExecution> seededKeyProvider() ).in( scope );
-            }
-        };
-    }
-
     public void beforeMojoExecution( MojoExecutionEvent event )
         throws MojoExecutionException
     {

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeCoreModule.java
----------------------------------------------------------------------
diff --git 
a/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeCoreModule.java
 
b/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeCoreModule.java
new file mode 100644
index 0000000..8208c3c
--- /dev/null
+++ 
b/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeCoreModule.java
@@ -0,0 +1,45 @@
+package org.apache.maven.execution.scope.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.maven.execution.MojoExecutionListener;
+
+@Named
+public class MojoExecutionScopeCoreModule
+    extends MojoExecutionScopeModule
+{
+
+    @Inject
+    public MojoExecutionScopeCoreModule()
+    {
+        super( new MojoExecutionScope() );
+    }
+
+    @Override
+    protected void configure()
+    {
+        super.configure();
+        bind( MojoExecutionListener.class ).toInstance( scope );
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeModule.java
----------------------------------------------------------------------
diff --git 
a/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeModule.java
 
b/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeModule.java
new file mode 100644
index 0000000..49adf48
--- /dev/null
+++ 
b/maven-core/src/main/java/org/apache/maven/execution/scope/internal/MojoExecutionScopeModule.java
@@ -0,0 +1,56 @@
+package org.apache.maven.execution.scope.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.execution.scope.MojoExecutionScoped;
+import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.PlexusContainer;
+import 
org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+
+import com.google.inject.AbstractModule;
+
+public class MojoExecutionScopeModule
+    extends AbstractModule
+{
+    protected final MojoExecutionScope scope;
+
+    public MojoExecutionScopeModule( PlexusContainer container )
+        throws ComponentLookupException
+    {
+        this( container.lookup( MojoExecutionScope.class ) );
+    }
+
+    protected MojoExecutionScopeModule( MojoExecutionScope scope )
+    {
+        this.scope = scope;
+    }
+
+    @Override
+    protected void configure()
+    {
+        bindScope( MojoExecutionScoped.class, scope );
+        bind( MojoExecutionScope.class ).toInstance( scope );
+
+        bind( MavenProject.class ).toProvider( 
MojoExecutionScope.<MavenProject> seededKeyProvider() ).in( scope );
+        bind( MojoExecution.class ).toProvider( 
MojoExecutionScope.<MojoExecution> seededKeyProvider() ).in( scope );
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleModuleBuilder.java
----------------------------------------------------------------------
diff --git 
a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleModuleBuilder.java
 
b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleModuleBuilder.java
index a332c36..5d72ca3 100644
--- 
a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleModuleBuilder.java
+++ 
b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleModuleBuilder.java
@@ -22,7 +22,6 @@ package org.apache.maven.lifecycle.internal;
 import java.util.HashSet;
 import java.util.List;
 
-import org.apache.maven.SessionScope;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.execution.BuildSuccess;
 import org.apache.maven.execution.ExecutionEvent;
@@ -33,6 +32,7 @@ import org.apache.maven.lifecycle.MavenExecutionPlan;
 import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
 import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.session.scope.internal.SessionScope;
 import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.component.annotations.Requirement;
 

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/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 94b4e6f..020d6b1 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
@@ -41,7 +41,7 @@ import org.apache.maven.RepositoryUtils;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.classrealm.ClassRealmManager;
 import org.apache.maven.execution.MavenSession;
-import org.apache.maven.execution.scope.internal.MojoExecutionScope;
+import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.monitor.logging.DefaultLog;
 import org.apache.maven.plugin.ContextEnabled;
@@ -67,6 +67,7 @@ import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.rtinfo.RuntimeInformation;
+import org.apache.maven.session.scope.internal.SessionScopeModule;
 import org.codehaus.plexus.DefaultPlexusContainer;
 import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.classworlds.realm.ClassRealm;
@@ -392,8 +393,8 @@ public class DefaultMavenPluginManager
                 container.addComponentDescriptor( componentDescriptor );
             }
 
-            ( (DefaultPlexusContainer) container ).discoverComponents( 
pluginRealm,
-                                                                       
MojoExecutionScope.getScopeModule( container ) );
+            ( (DefaultPlexusContainer) container ).discoverComponents( 
pluginRealm, new SessionScopeModule( container ),
+                                                                       new 
MojoExecutionScopeModule( container ) );
         }
         catch ( ComponentLookupException e )
         {

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/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 fccdcfe..3d04637 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
@@ -33,7 +33,7 @@ import java.util.Set;
 import org.apache.maven.artifact.InvalidRepositoryException;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.classrealm.ClassRealmManager;
-import org.apache.maven.execution.scope.internal.MojoExecutionScope;
+import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule;
 import org.apache.maven.model.Build;
 import org.apache.maven.model.Extension;
 import org.apache.maven.model.Model;
@@ -48,6 +48,7 @@ import org.apache.maven.plugin.version.PluginVersionRequest;
 import org.apache.maven.plugin.version.PluginVersionResolutionException;
 import org.apache.maven.plugin.version.PluginVersionResolver;
 import org.apache.maven.repository.RepositorySystem;
+import org.apache.maven.session.scope.internal.SessionScopeModule;
 import org.codehaus.plexus.DefaultPlexusContainer;
 import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.classworlds.realm.ClassRealm;
@@ -271,7 +272,8 @@ public class DefaultProjectBuildingHelper
                 try
                 {
                     ( (DefaultPlexusContainer) container ).discoverComponents( 
extensionRealm,
-                                                                               
MojoExecutionScope.getScopeModule( container ) );
+                                                                               
new SessionScopeModule( container ),
+                                                                               
new MojoExecutionScopeModule( container ) );
                 }
                 catch ( Exception e )
                 {

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/maven-core/src/main/java/org/apache/maven/session/scope/internal/SessionScope.java
----------------------------------------------------------------------
diff --git 
a/maven-core/src/main/java/org/apache/maven/session/scope/internal/SessionScope.java
 
b/maven-core/src/main/java/org/apache/maven/session/scope/internal/SessionScope.java
new file mode 100644
index 0000000..ea8b677
--- /dev/null
+++ 
b/maven-core/src/main/java/org/apache/maven/session/scope/internal/SessionScope.java
@@ -0,0 +1,136 @@
+package org.apache.maven.session.scope.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.LinkedList;
+import java.util.Map;
+
+import com.google.common.collect.Maps;
+import com.google.inject.Key;
+import com.google.inject.OutOfScopeException;
+import com.google.inject.Provider;
+import com.google.inject.Scope;
+import com.google.inject.util.Providers;
+
+public class SessionScope
+    implements Scope
+{    
+    private static final Provider<Object> SEEDED_KEY_PROVIDER = new 
Provider<Object>()
+    {
+        public Object get()
+        {
+            throw new IllegalStateException();
+        }
+    };
+
+    private static final class ScopeState
+    {
+        public final Map<Key<?>, Provider<?>> seeded = Maps.newHashMap();
+
+        public final Map<Key<?>, Object> provided = Maps.newHashMap();
+    }
+
+    private final ThreadLocal<LinkedList<ScopeState>> values = new 
ThreadLocal<LinkedList<ScopeState>>();
+
+    public void enter()
+    {
+        LinkedList<ScopeState> stack = values.get();
+        if ( stack == null )
+        {
+            stack = new LinkedList<ScopeState>();
+            values.set( stack );
+        }
+        stack.addFirst( new ScopeState() );
+    }
+
+    private ScopeState getScopeState()
+    {
+        LinkedList<ScopeState> stack = values.get();
+        if ( stack == null || stack.isEmpty() )
+        {
+            throw new IllegalStateException();
+        }
+        return stack.getFirst();
+    }
+
+    public void exit()
+    {
+        final LinkedList<ScopeState> stack = values.get();
+        if ( stack == null || stack.isEmpty() )
+        {
+            throw new IllegalStateException();
+        }
+        stack.removeFirst();
+        if ( stack.isEmpty() )
+        {
+            values.remove();
+        }
+    }
+
+    public <T> void seed( Class<T> clazz, Provider<T> value )
+    {
+        getScopeState().seeded.put( Key.get( clazz ), value );
+    }
+
+    public <T> void seed( Class<T> clazz, final T value )
+    {
+        getScopeState().seeded.put( Key.get( clazz ), Providers.of( value ) );
+    }
+
+    public <T> Provider<T> scope( final Key<T> key, final Provider<T> unscoped 
)
+    {
+        return new Provider<T>()
+        {
+            @SuppressWarnings( "unchecked" )
+            public T get()
+            {
+                LinkedList<ScopeState> stack = values.get();
+                if ( stack == null || stack.isEmpty() )
+                {
+                    throw new OutOfScopeException( "Cannot access " + key + " 
outside of a scoping block" );
+                }
+
+                ScopeState state = stack.getFirst();
+
+                Provider<?> seeded = state.seeded.get( key );
+
+                if ( seeded != null )
+                {
+                    return (T) seeded.get();
+                }
+
+                T provided = (T) state.provided.get( key );
+                if ( provided == null && unscoped != null )
+                {
+                    provided = unscoped.get();
+                    state.provided.put( key, provided );
+                }
+
+                return provided;
+            }
+        };
+    }
+
+    @SuppressWarnings( { "unchecked" } )
+    public static <T> Provider<T> seededKeyProvider()
+    {
+        return (Provider<T>) SEEDED_KEY_PROVIDER;
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven/blob/b80fb7d7/maven-core/src/main/java/org/apache/maven/session/scope/internal/SessionScopeModule.java
----------------------------------------------------------------------
diff --git 
a/maven-core/src/main/java/org/apache/maven/session/scope/internal/SessionScopeModule.java
 
b/maven-core/src/main/java/org/apache/maven/session/scope/internal/SessionScopeModule.java
new file mode 100644
index 0000000..9b8c0ff
--- /dev/null
+++ 
b/maven-core/src/main/java/org/apache/maven/session/scope/internal/SessionScopeModule.java
@@ -0,0 +1,63 @@
+package org.apache.maven.session.scope.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.maven.SessionScoped;
+import org.apache.maven.execution.MavenSession;
+import org.codehaus.plexus.PlexusContainer;
+import 
org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+
+import com.google.inject.AbstractModule;
+
+@Named
+public class SessionScopeModule
+    extends AbstractModule
+{
+    private final SessionScope scope;
+
+    @Inject
+    public SessionScopeModule()
+    {
+        this( new SessionScope() );
+    }
+
+    public SessionScopeModule( PlexusContainer container )
+        throws ComponentLookupException
+    {
+        this( container.lookup( SessionScope.class ) );
+    }
+
+    private SessionScopeModule( SessionScope scope )
+    {
+        this.scope = scope;
+    }
+
+    @Override
+    protected void configure()
+    {
+        bindScope( SessionScoped.class, scope );
+        bind( SessionScope.class ).toInstance( scope );
+
+        bind( MavenSession.class ).toProvider( SessionScope.<MavenSession> 
seededKeyProvider() ).in( scope );
+    }
+}

Reply via email to