Reducing chattiness by adding ConfigurationListener to give option of event on configuration save as a whole, not each field.

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@582993 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-10-08 23:08:32 +00:00
parent 8af86cd884
commit 25bc854985
8 changed files with 248 additions and 59 deletions

View File

@ -49,10 +49,25 @@ public interface ArchivaConfiguration
throws RegistryException, IndeterminateConfigurationException;
/**
* Add a change listener so that registry changes are propogated.
* Add a configuration listener to notify of changes to the configuration.
*
* @param listener the listener
*/
void addListener( ConfigurationListener listener );
/**
* Remove a configuration listener to stop notifications of changes to the configuration.
*
* @param listener the listener
*/
void removeListener( ConfigurationListener listener );
/**
* Add a registry listener to notify of events in plexus-registry.
*
* @param listener the listener
* TODO: Remove in future.
*/
void addChangeListener( RegistryListener listener );
}

View File

@ -0,0 +1,77 @@
package org.apache.maven.archiva.configuration;
/*
* 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.
*/
/**
* ConfigurationEvent
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class ConfigurationEvent
{
public static final int SAVED = 1;
public static final int CHANGED = 2;
private int type;
public ConfigurationEvent( int type )
{
this.type = type;
}
public int getType()
{
return type;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + type;
return result;
}
@Override
public boolean equals( Object obj )
{
if ( this == obj )
{
return true;
}
if ( obj == null )
{
return false;
}
if ( getClass() != obj.getClass() )
{
return false;
}
final ConfigurationEvent other = (ConfigurationEvent) obj;
if ( type != other.type )
{
return false;
}
return true;
}
}

View File

@ -0,0 +1,34 @@
package org.apache.maven.archiva.configuration;
/*
* 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.
*/
/**
* ConfigurationListener
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public interface ConfigurationListener
{
/**
* Generic event point to notify components that something has happend in the configuration.
*/
public void configurationEvent(ConfigurationEvent event);
}

View File

@ -35,10 +35,11 @@ import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Implementation of configuration holder that retrieves it from the registry.
@ -84,9 +85,14 @@ public class DefaultArchivaConfiguration
private String userConfigFilename;
/**
* Listeners we've registered.
* Configuration Listeners we've registered.
*/
private List<RegistryListener> listeners = new LinkedList<RegistryListener>();
private Set<ConfigurationListener> listeners = new HashSet<ConfigurationListener>();
/**
* Registry Listeners we've registered.
*/
private Set<RegistryListener> registryListeners = new HashSet<RegistryListener>();
public String getFilteredUserConfigFilename()
{
@ -250,6 +256,8 @@ public class DefaultArchivaConfiguration
new ConfigurationRegistryWriter().write( configuration, section );
section.save();
triggerEvent( ConfigurationEvent.SAVED );
this.configuration = processExpressions( configuration );
}
@ -271,11 +279,9 @@ public class DefaultArchivaConfiguration
{
( (Initializable) registry ).initialize();
for ( Iterator<RegistryListener> i = listeners.iterator(); i.hasNext(); )
for ( RegistryListener regListener: registryListeners )
{
RegistryListener l = i.next();
addRegistryChangeListener( l );
addRegistryChangeListener( regListener );
}
}
catch ( InitializationException e )
@ -283,15 +289,53 @@ public class DefaultArchivaConfiguration
throw new RegistryException( "Unable to reinitialize configuration: " + e.getMessage(), e );
}
triggerEvent( ConfigurationEvent.SAVED );
return registry.getSection( KEY + ".user" );
}
private void triggerEvent( int type )
{
ConfigurationEvent evt = new ConfigurationEvent( type );
for ( ConfigurationListener listener : listeners )
{
try
{
listener.configurationEvent( evt );
}
catch ( Throwable t )
{
getLogger().warn( "Unable to notify of saved configuration event.", t );
}
}
}
public void addListener( ConfigurationListener listener )
{
if ( listener == null )
{
return;
}
listeners.add( listener );
}
public void removeListener( ConfigurationListener listener )
{
if ( listener == null )
{
return;
}
listeners.remove( listener );
}
public void addChangeListener( RegistryListener listener )
{
addRegistryChangeListener( listener );
// keep track for later
listeners.add( listener );
registryListeners.add( listener );
}
private void addRegistryChangeListener( RegistryListener listener )
@ -308,6 +352,7 @@ public class DefaultArchivaConfiguration
}
}
public void initialize()
throws InitializationException
{

View File

@ -21,9 +21,7 @@ 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;
@ -38,6 +36,7 @@ import java.util.Map;
public class ArchivaConfigurationTest
extends PlexusTestCase
{
@SuppressWarnings("unused")
private Registry registry;
protected void setUp()
@ -108,7 +107,7 @@ public class ArchivaConfigurationTest
assertEquals( "check known consumers", 9, repoScanning.getKnownContentConsumers().size() );
assertEquals( "check invalid consumers", 1, repoScanning.getInvalidContentConsumers().size() );
List patterns = filetypes.getFileTypePatterns( "artifacts" );
List<String> patterns = filetypes.getFileTypePatterns( "artifacts" );
assertNotNull( "check 'artifacts' file type", patterns );
assertEquals( "check 'artifacts' patterns", 13, patterns.size() );
@ -192,15 +191,12 @@ public class ArchivaConfigurationTest
configuration.getWebapp().getUi().setAppletFindEnabled( false );
// add a change listener
MockControl control = createRegistryListenerMockControl();
RegistryListener listener = (RegistryListener) control.getMock();
archivaConfiguration.addChangeListener( listener );
MockControl control = createConfigurationListenerMockControl();
ConfigurationListener listener = (ConfigurationListener) control.getMock();
archivaConfiguration.addListener( 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 );
listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
control.setVoidCallable();
control.replay();
@ -221,21 +217,9 @@ public class ArchivaConfigurationTest
assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() );
}
private static MockControl createRegistryListenerMockControl()
private static MockControl createConfigurationListenerMockControl()
{
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 ? "<any>" : super.argumentToString( object );
}
} );
MockControl control = MockControl.createControl( ConfigurationListener.class );
return control;
}
@ -291,12 +275,13 @@ public class ArchivaConfigurationTest
configuration.getWebapp().getUi().setAppletFindEnabled( false );
// add a change listener
MockControl control = createRegistryListenerMockControl();
RegistryListener listener = (RegistryListener) control.getMock();
archivaConfiguration.addChangeListener( listener );
MockControl control = createConfigurationListenerMockControl();
ConfigurationListener listener = (ConfigurationListener) control.getMock();
archivaConfiguration.addListener( listener );
listener.beforeConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE );
listener.afterConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE );
listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) );
// once from default creation, and again from manual call to save
control.setVoidCallable( 2 );
control.replay();

View File

@ -21,14 +21,14 @@ package org.apache.maven.archiva.indexer;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ConfigurationListener;
import org.codehaus.plexus.registry.Registry;
import org.codehaus.plexus.registry.RegistryException;
import org.codehaus.plexus.registry.RegistryListener;
import org.easymock.MockControl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
/**
* MockConfiguration
@ -43,7 +43,8 @@ public class MockConfiguration implements ArchivaConfiguration
{
private Configuration configuration = new Configuration();
private List listeners = new ArrayList();
private Set<RegistryListener> registryListeners = new HashSet<RegistryListener>();
private Set<ConfigurationListener> configListeners = new HashSet<ConfigurationListener>();
private MockControl registryControl;
@ -57,7 +58,7 @@ public class MockConfiguration implements ArchivaConfiguration
public void addChangeListener( RegistryListener listener )
{
listeners.add( listener );
registryListeners.add( listener );
}
public Configuration getConfiguration()
@ -73,10 +74,8 @@ public class MockConfiguration implements ArchivaConfiguration
public void triggerChange( String name, String value )
{
Iterator it = listeners.iterator();
while ( it.hasNext() )
for(RegistryListener listener: registryListeners)
{
RegistryListener listener = (RegistryListener) it.next();
try
{
listener.afterConfigurationChange( registryMock, name, value );
@ -87,4 +86,14 @@ public class MockConfiguration implements ArchivaConfiguration
}
}
}
public void addListener( ConfigurationListener listener )
{
configListeners.add(listener);
}
public void removeListener( ConfigurationListener listener )
{
configListeners.remove( listener );
}
}

View File

@ -21,13 +21,14 @@ package org.apache.maven.archiva.proxy;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ConfigurationListener;
import org.codehaus.plexus.registry.Registry;
import org.codehaus.plexus.registry.RegistryException;
import org.codehaus.plexus.registry.RegistryListener;
import org.easymock.MockControl;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
/**
* MockConfiguration
@ -43,7 +44,8 @@ public class MockConfiguration
{
private Configuration configuration = new Configuration();
private List<RegistryListener> listeners = new ArrayList<RegistryListener>();
private Set<RegistryListener> registryListeners = new HashSet<RegistryListener>();
private Set<ConfigurationListener> configListeners = new HashSet<ConfigurationListener>();
private MockControl registryControl;
@ -57,7 +59,7 @@ public class MockConfiguration
public void addChangeListener( RegistryListener listener )
{
listeners.add( listener );
registryListeners.add( listener );
}
public Configuration getConfiguration()
@ -73,7 +75,7 @@ public class MockConfiguration
public void triggerChange( String name, String value )
{
for( RegistryListener listener: listeners )
for(RegistryListener listener: registryListeners)
{
try
{
@ -85,4 +87,14 @@ public class MockConfiguration
}
}
}
public void addListener( ConfigurationListener listener )
{
configListeners.add(listener);
}
public void removeListener( ConfigurationListener listener )
{
configListeners.remove( listener );
}
}

View File

@ -21,14 +21,17 @@ package org.apache.maven.archiva.repository;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ConfigurationListener;
import org.codehaus.plexus.registry.Registry;
import org.codehaus.plexus.registry.RegistryException;
import org.codehaus.plexus.registry.RegistryListener;
import org.easymock.MockControl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* MockConfiguration
@ -44,7 +47,8 @@ public class MockConfiguration
{
private Configuration configuration = new Configuration();
private List<RegistryListener> listeners = new ArrayList<RegistryListener>();
private Set<RegistryListener> registryListeners = new HashSet<RegistryListener>();
private Set<ConfigurationListener> configListeners = new HashSet<ConfigurationListener>();
private MockControl registryControl;
@ -58,7 +62,7 @@ public class MockConfiguration
public void addChangeListener( RegistryListener listener )
{
listeners.add( listener );
registryListeners.add( listener );
}
public Configuration getConfiguration()
@ -74,10 +78,8 @@ public class MockConfiguration
public void triggerChange( String name, String value )
{
Iterator<RegistryListener> it = listeners.iterator();
while ( it.hasNext() )
for(RegistryListener listener: registryListeners)
{
RegistryListener listener = (RegistryListener) it.next();
try
{
listener.afterConfigurationChange( registryMock, name, value );
@ -88,4 +90,14 @@ public class MockConfiguration
}
}
}
public void addListener( ConfigurationListener listener )
{
configListeners.add(listener);
}
public void removeListener( ConfigurationListener listener )
{
configListeners.remove( listener );
}
}