diff --git a/maven-build-context/src/main/java/org/apache/maven/context/BuildContextManager.java b/maven-build-context/src/main/java/org/apache/maven/context/BuildContextManager.java index 1567ec096e..6132f4775b 100644 --- a/maven-build-context/src/main/java/org/apache/maven/context/BuildContextManager.java +++ b/maven-build-context/src/main/java/org/apache/maven/context/BuildContextManager.java @@ -1,5 +1,7 @@ package org.apache.maven.context; +import org.codehaus.plexus.context.Context; + /** * Manager interface used to store, read, and clear the BuildContext out of the container. * @@ -10,11 +12,6 @@ public interface BuildContextManager String ROLE = BuildContextManager.class.getName(); - /** - * Create a new instance of BuildContext - */ - BuildContext newUnstoredInstance(); - /** * Read the BuildContext from the container. If it doesn't already exist, optionally create it. */ @@ -31,4 +28,12 @@ public interface BuildContextManager */ void clearBuildContext(); + /** + * Re-orient this BuildContextManager to use the given Plexus Context instance, returning + * the original Context instance so it can be restored later. This can be important when the + * BuildContextManager is instantiated inside a Maven plugin, but the plugin needs to use the + * context associated with the core of Maven (in case multiple contexts are used). + */ + Context reorientToContext( Context context ); + } diff --git a/maven-build-context/src/main/java/org/apache/maven/context/DefaultBuildContextManager.java b/maven-build-context/src/main/java/org/apache/maven/context/DefaultBuildContextManager.java index a8aee84497..97801f1fe6 100644 --- a/maven-build-context/src/main/java/org/apache/maven/context/DefaultBuildContextManager.java +++ b/maven-build-context/src/main/java/org/apache/maven/context/DefaultBuildContextManager.java @@ -32,15 +32,6 @@ public DefaultBuildContextManager( Context context ) this.context = context; } - /** - * Create a new instance of DefaultBuildContext, and return it. Each method call creates a brand - * new instance. - */ - public BuildContext newUnstoredInstance() - { - return new DefaultBuildContext(); - } - /** * Clear the contents of the build context inside the container context. */ @@ -128,4 +119,12 @@ public void contextualize( Context context ) this.context = context; } + public Context reorientToContext( Context context ) + { + Context oldContext = this.context; + this.context = context; + + return oldContext; + } + } diff --git a/maven-build-context/src/test/java/org/apache/maven/context/AbstractBuildContextManagerTest.java b/maven-build-context/src/test/java/org/apache/maven/context/AbstractBuildContextManagerTest.java index 3fd59657e0..11dda82378 100644 --- a/maven-build-context/src/test/java/org/apache/maven/context/AbstractBuildContextManagerTest.java +++ b/maven-build-context/src/test/java/org/apache/maven/context/AbstractBuildContextManagerTest.java @@ -9,6 +9,8 @@ public abstract class AbstractBuildContextManagerTest protected abstract String getRoleHintBeforeSetUp(); + protected abstract BuildContext createBuildContext(); + protected BuildContextManager getBuildContextManager() { return mgr; @@ -21,28 +23,10 @@ public void setUp() throws Exception mgr = (BuildContextManager) lookup( BuildContextManager.ROLE, getRoleHintBeforeSetUp() ); } - public void testNewUnstoredInstance_ShouldReturnValidContextInstance() - { - BuildContext context = mgr.newUnstoredInstance(); - - assertNotNull( context ); - - String key = "key"; - String value = "value"; - - context.put( key, value ); - - assertEquals( value, context.get( key ) ); - - context.delete( key ); - - assertNull( context.get( key ) ); - } - public void testNewUnstoredInstance_SuccessiveCallsShouldReturnDistinctContextInstances() { - BuildContext context = mgr.newUnstoredInstance(); - BuildContext context2 = mgr.newUnstoredInstance(); + BuildContext context = createBuildContext(); + BuildContext context2 = createBuildContext(); assertNotNull( context ); assertNotNull( context2 ); @@ -51,7 +35,7 @@ public void testNewUnstoredInstance_SuccessiveCallsShouldReturnDistinctContextIn public void testStoreAndRead_ShouldRetrieveStoredValueAfterRead() { - BuildContext ctx = mgr.newUnstoredInstance(); + BuildContext ctx = createBuildContext(); String key = "key"; String value = "value"; @@ -68,7 +52,7 @@ public void testStoreAndRead_ShouldRetrieveStoredValueAfterRead() public void testStoreAndClear_ShouldNotRetrieveStoredValueAfterClear() { - BuildContext ctx = mgr.newUnstoredInstance(); + BuildContext ctx = createBuildContext(); String key = "key"; String value = "value"; diff --git a/maven-build-context/src/test/java/org/apache/maven/context/DefaultBuildContextManagerTest.java b/maven-build-context/src/test/java/org/apache/maven/context/DefaultBuildContextManagerTest.java index fa32d6b64d..51eabc7935 100644 --- a/maven-build-context/src/test/java/org/apache/maven/context/DefaultBuildContextManagerTest.java +++ b/maven-build-context/src/test/java/org/apache/maven/context/DefaultBuildContextManagerTest.java @@ -9,4 +9,9 @@ protected String getRoleHintBeforeSetUp() return DefaultBuildContextManager.ROLE_HINT; } + protected BuildContext createBuildContext() + { + return new DefaultBuildContext(); + } + }