From 36887ae75913554e08637e19e8da7e2fb7f13d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Wed, 16 Sep 2015 10:45:40 +0200 Subject: [PATCH] Registry-aware components The RegistryAware interface can be implemented in order to let the component know of the registry it is being bound to. --- TODO | 1 - .../info/ebenoit/ebul/cmp/ComponentState.java | 3 +++ .../info/ebenoit/ebul/cmp/RegistryAware.java | 22 ++++++++++++++++ .../ebenoit/ebul/cmp/TestComponentState.java | 25 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/main/java/info/ebenoit/ebul/cmp/RegistryAware.java diff --git a/TODO b/TODO index 49cf996..bf0ef94 100644 --- a/TODO +++ b/TODO @@ -2,7 +2,6 @@ To Do: * Implement "DriverFor" support * Scanning packages for annotated components * General usage documentation - * Registry awareness * Easier anonymous components * Uncouple component-provided names from the library diff --git a/src/main/java/info/ebenoit/ebul/cmp/ComponentState.java b/src/main/java/info/ebenoit/ebul/cmp/ComponentState.java index 149fe60..a78e17a 100644 --- a/src/main/java/info/ebenoit/ebul/cmp/ComponentState.java +++ b/src/main/java/info/ebenoit/ebul/cmp/ComponentState.java @@ -74,6 +74,9 @@ public final class ComponentState throw new ComponentCreationException( e.getCause( ) ); } } + if ( component instanceof RegistryAware ) { + ( (RegistryAware) component ).setComponentRegistry( registry ); + } this.component = component; String name; diff --git a/src/main/java/info/ebenoit/ebul/cmp/RegistryAware.java b/src/main/java/info/ebenoit/ebul/cmp/RegistryAware.java new file mode 100644 index 0000000..66ac899 --- /dev/null +++ b/src/main/java/info/ebenoit/ebul/cmp/RegistryAware.java @@ -0,0 +1,22 @@ +package info.ebenoit.ebul.cmp; + + +/** + * Interface for registry-aware components + *

+ * This interface can be implemented by a component that is aware of the component registry it is bound to. + * + * @author E. BenoƮt + */ +public interface RegistryAware +{ + + /** + * Sets the component registry + * + * @param registry + * the registry if the component is being bound, or null if the component is being detached. + */ + public void setComponentRegistry( ComponentRegistry registry ); + +} diff --git a/src/test/java/info/ebenoit/ebul/cmp/TestComponentState.java b/src/test/java/info/ebenoit/ebul/cmp/TestComponentState.java index 3a05f33..c1ecda3 100644 --- a/src/test/java/info/ebenoit/ebul/cmp/TestComponentState.java +++ b/src/test/java/info/ebenoit/ebul/cmp/TestComponentState.java @@ -65,6 +65,20 @@ public class TestComponentState } + /** Class used to test registry-aware components */ + private static class RACmpTest + implements RegistryAware + { + private ComponentRegistry registry; + + @Override + public void setComponentRegistry( ComponentRegistry registry ) + { + this.registry = registry; + } + + } + /** Class used to test methods that affect the component's lifecycle */ private static class LCATest { @@ -187,6 +201,17 @@ public class TestComponentState } + /** Test: initialising a {@link ComponentState} that implements {@link RegistryAware} */ + @Test + public void testInitialiseRegistryAware( ) + { + final RACmpTest object = new RACmpTest( ); + final NewComponentInfo< Object > ci = new NewComponentInfo< Object >( object ); + new ComponentState( this.reg , ci ); + Assert.assertSame( this.reg , object.registry ); + } + + /** Test: {@link ComponentState} copies autostart flag */ @Test public void testInitialiseAutostart( )