diff --git a/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java b/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java index 540a86539..7c007bb6e 100644 --- a/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java +++ b/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java @@ -33,6 +33,8 @@ import org.codehaus.plexus.util.StringUtils; import java.io.File; import java.io.IOException; import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; /** * Implementation of configuration holder that retrieves it from the registry. @@ -77,9 +79,14 @@ public class DefaultArchivaConfiguration */ private String userConfigFilename; + /** + * Listeners we've registered. + */ + private List listeners = new LinkedList(); + public String getFilteredUserConfigFilename() { - return StringUtils.replace( userConfigFilename, "${user.home}", System.getProperty( "user.home" + "" ) ); + return StringUtils.replace( userConfigFilename, "${user.home}", System.getProperty( "user.home" ) ); } public synchronized Configuration getConfiguration() @@ -176,6 +183,13 @@ public class DefaultArchivaConfiguration try { ( (Initializable) registry ).initialize(); + + for ( Iterator i = listeners.iterator(); i.hasNext(); ) + { + RegistryListener l = (RegistryListener) i.next(); + + addRegistryChangeListener( l ); + } } catch ( InitializationException e ) { @@ -186,6 +200,14 @@ public class DefaultArchivaConfiguration } public void addChangeListener( RegistryListener listener ) + { + addRegistryChangeListener( listener ); + + // keep track for later + listeners.add( listener ); + } + + private void addRegistryChangeListener( RegistryListener listener ) { Registry section = registry.getSection( KEY + ".user" ); if ( section != null ) diff --git a/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java b/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java index 7c6dd36c3..d3a485b89 100644 --- a/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java +++ b/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java @@ -20,7 +20,11 @@ package org.apache.maven.archiva.configuration; */ import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.registry.Registry; +import org.codehaus.plexus.registry.RegistryListener; import org.codehaus.plexus.util.FileUtils; +import org.easymock.AbstractMatcher; +import org.easymock.MockControl; import java.io.File; import java.util.List; @@ -33,6 +37,16 @@ import java.util.List; public class ArchivaConfigurationTest extends PlexusTestCase { + private Registry registry; + + protected void setUp() + throws Exception + { + super.setUp(); + + registry = (Registry) lookup( Registry.ROLE, "commons-configuration" ); + } + public void testGetConfigurationFromRegistryWithASingleNamedConfigurationResource() throws Exception { @@ -162,8 +176,8 @@ public class ArchivaConfigurationTest file.getParentFile().mkdirs(); FileUtils.fileWrite( file.getAbsolutePath(), "" ); - ArchivaConfiguration archivaConfiguration = - (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save" ); + DefaultArchivaConfiguration archivaConfiguration = + (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save" ); Configuration configuration = new Configuration(); configuration.setVersion( "1" ); @@ -171,8 +185,23 @@ public class ArchivaConfigurationTest configuration.getWebapp().setUi( new UserInterfaceOptions() ); configuration.getWebapp().getUi().setAppletFindEnabled( false ); + // add a change listener + MockControl control = createRegistryListenerMockControl(); + RegistryListener listener = (RegistryListener) control.getMock(); + archivaConfiguration.addChangeListener( listener ); + + listener.beforeConfigurationChange( registry, "version", "1" ); + listener.beforeConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE ); + + listener.afterConfigurationChange( registry, "version", "1" ); + listener.afterConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE ); + + control.replay(); + archivaConfiguration.save( configuration ); + control.verify(); + assertTrue( "Check file exists", file.exists() ); // check it @@ -180,11 +209,30 @@ public class ArchivaConfigurationTest assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() ); // read it back - archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-read-saved" ); + archivaConfiguration = + (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-read-saved" ); configuration = archivaConfiguration.getConfiguration(); assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() ); } + private static MockControl createRegistryListenerMockControl() + { + MockControl control = MockControl.createControl( RegistryListener.class ); + control.setDefaultMatcher( new AbstractMatcher() + { + protected boolean argumentMatches( Object object, Object object1 ) + { + return object instanceof Registry || super.argumentMatches( object, object1 ); + } + + protected String argumentToString( Object object ) + { + return object instanceof Registry ? "" : super.argumentToString( object ); + } + } ); + return control; + } + public void testStoreConfigurationUser() throws Exception { @@ -236,8 +284,20 @@ public class ArchivaConfigurationTest configuration.getWebapp().setUi( new UserInterfaceOptions() ); configuration.getWebapp().getUi().setAppletFindEnabled( false ); + // add a change listener + MockControl control = createRegistryListenerMockControl(); + RegistryListener listener = (RegistryListener) control.getMock(); + archivaConfiguration.addChangeListener( listener ); + + listener.beforeConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE ); + listener.afterConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE ); + + control.replay(); + archivaConfiguration.save( configuration ); + control.verify(); + assertTrue( "Check file exists", userFile.exists() ); assertFalse( "Check file not created", baseFile.exists() );