git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@582993 13f79535-47bb-0310-9956-ffa450edef68tags/archiva-1.0-beta-3
throws RegistryException, IndeterminateConfigurationException; | 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 | * @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 ); | void addChangeListener( RegistryListener listener ); | ||||
} | } | ||||
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; | |||||
} | |||||
} |
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); | |||||
} |
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.Collection; | import java.util.Collection; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.HashSet; | |||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.LinkedList; | |||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.Set; | |||||
/** | /** | ||||
* Implementation of configuration holder that retrieves it from the registry. | * Implementation of configuration holder that retrieves it from the registry. | ||||
private String userConfigFilename; | 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() | public String getFilteredUserConfigFilename() | ||||
{ | { | ||||
new ConfigurationRegistryWriter().write( configuration, section ); | new ConfigurationRegistryWriter().write( configuration, section ); | ||||
section.save(); | section.save(); | ||||
triggerEvent( ConfigurationEvent.SAVED ); | |||||
this.configuration = processExpressions( configuration ); | this.configuration = processExpressions( configuration ); | ||||
} | } | ||||
try | try | ||||
{ | { | ||||
( (Initializable) registry ).initialize(); | ( (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 ) | catch ( InitializationException e ) | ||||
{ | { | ||||
throw new RegistryException( "Unable to reinitialize configuration: " + e.getMessage(), e ); | throw new RegistryException( "Unable to reinitialize configuration: " + e.getMessage(), e ); | ||||
} | } | ||||
triggerEvent( ConfigurationEvent.SAVED ); | |||||
return registry.getSection( KEY + ".user" ); | 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 ) | public void addChangeListener( RegistryListener listener ) | ||||
{ | { | ||||
addRegistryChangeListener( listener ); | addRegistryChangeListener( listener ); | ||||
// keep track for later | // keep track for later | ||||
listeners.add( listener ); | |||||
registryListeners.add( listener ); | |||||
} | } | ||||
private void addRegistryChangeListener( RegistryListener listener ) | private void addRegistryChangeListener( RegistryListener listener ) | ||||
} | } | ||||
} | } | ||||
public void initialize() | public void initialize() | ||||
throws InitializationException | throws InitializationException | ||||
{ | { |
import org.codehaus.plexus.PlexusTestCase; | import org.codehaus.plexus.PlexusTestCase; | ||||
import org.codehaus.plexus.registry.Registry; | import org.codehaus.plexus.registry.Registry; | ||||
import org.codehaus.plexus.registry.RegistryListener; | |||||
import org.codehaus.plexus.util.FileUtils; | import org.codehaus.plexus.util.FileUtils; | ||||
import org.easymock.AbstractMatcher; | |||||
import org.easymock.MockControl; | import org.easymock.MockControl; | ||||
import java.io.File; | import java.io.File; | ||||
public class ArchivaConfigurationTest | public class ArchivaConfigurationTest | ||||
extends PlexusTestCase | extends PlexusTestCase | ||||
{ | { | ||||
@SuppressWarnings("unused") | |||||
private Registry registry; | private Registry registry; | ||||
protected void setUp() | protected void setUp() | ||||
assertEquals( "check known consumers", 9, repoScanning.getKnownContentConsumers().size() ); | assertEquals( "check known consumers", 9, repoScanning.getKnownContentConsumers().size() ); | ||||
assertEquals( "check invalid consumers", 1, repoScanning.getInvalidContentConsumers().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 ); | assertNotNull( "check 'artifacts' file type", patterns ); | ||||
assertEquals( "check 'artifacts' patterns", 13, patterns.size() ); | assertEquals( "check 'artifacts' patterns", 13, patterns.size() ); | ||||
configuration.getWebapp().getUi().setAppletFindEnabled( false ); | configuration.getWebapp().getUi().setAppletFindEnabled( false ); | ||||
// add a change listener | // 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 ); | |||||
MockControl control = createConfigurationListenerMockControl(); | |||||
ConfigurationListener listener = (ConfigurationListener) control.getMock(); | |||||
archivaConfiguration.addListener( listener ); | |||||
listener.configurationEvent( new ConfigurationEvent( ConfigurationEvent.SAVED ) ); | |||||
control.setVoidCallable(); | |||||
control.replay(); | control.replay(); | ||||
assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() ); | 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; | return control; | ||||
} | } | ||||
configuration.getWebapp().getUi().setAppletFindEnabled( false ); | configuration.getWebapp().getUi().setAppletFindEnabled( false ); | ||||
// add a change listener | // 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(); | control.replay(); | ||||
import org.apache.maven.archiva.configuration.ArchivaConfiguration; | import org.apache.maven.archiva.configuration.ArchivaConfiguration; | ||||
import org.apache.maven.archiva.configuration.Configuration; | 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.Registry; | ||||
import org.codehaus.plexus.registry.RegistryException; | import org.codehaus.plexus.registry.RegistryException; | ||||
import org.codehaus.plexus.registry.RegistryListener; | import org.codehaus.plexus.registry.RegistryListener; | ||||
import org.easymock.MockControl; | 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 | * MockConfiguration | ||||
{ | { | ||||
private Configuration configuration = new Configuration(); | 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; | private MockControl registryControl; | ||||
public void addChangeListener( RegistryListener listener ) | public void addChangeListener( RegistryListener listener ) | ||||
{ | { | ||||
listeners.add( listener ); | |||||
registryListeners.add( listener ); | |||||
} | } | ||||
public Configuration getConfiguration() | public Configuration getConfiguration() | ||||
public void triggerChange( String name, String value ) | public void triggerChange( String name, String value ) | ||||
{ | { | ||||
Iterator it = listeners.iterator(); | |||||
while ( it.hasNext() ) | |||||
for(RegistryListener listener: registryListeners) | |||||
{ | { | ||||
RegistryListener listener = (RegistryListener) it.next(); | |||||
try | try | ||||
{ | { | ||||
listener.afterConfigurationChange( registryMock, name, value ); | listener.afterConfigurationChange( registryMock, name, value ); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
public void addListener( ConfigurationListener listener ) | |||||
{ | |||||
configListeners.add(listener); | |||||
} | |||||
public void removeListener( ConfigurationListener listener ) | |||||
{ | |||||
configListeners.remove( listener ); | |||||
} | |||||
} | } |
import org.apache.maven.archiva.configuration.ArchivaConfiguration; | import org.apache.maven.archiva.configuration.ArchivaConfiguration; | ||||
import org.apache.maven.archiva.configuration.Configuration; | 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.Registry; | ||||
import org.codehaus.plexus.registry.RegistryException; | import org.codehaus.plexus.registry.RegistryException; | ||||
import org.codehaus.plexus.registry.RegistryListener; | import org.codehaus.plexus.registry.RegistryListener; | ||||
import org.easymock.MockControl; | import org.easymock.MockControl; | ||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
import java.util.HashSet; | |||||
import java.util.Set; | |||||
/** | /** | ||||
* MockConfiguration | * MockConfiguration | ||||
{ | { | ||||
private Configuration configuration = new Configuration(); | 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; | private MockControl registryControl; | ||||
public void addChangeListener( RegistryListener listener ) | public void addChangeListener( RegistryListener listener ) | ||||
{ | { | ||||
listeners.add( listener ); | |||||
registryListeners.add( listener ); | |||||
} | } | ||||
public Configuration getConfiguration() | public Configuration getConfiguration() | ||||
public void triggerChange( String name, String value ) | public void triggerChange( String name, String value ) | ||||
{ | { | ||||
for( RegistryListener listener: listeners ) | |||||
for(RegistryListener listener: registryListeners) | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
public void addListener( ConfigurationListener listener ) | |||||
{ | |||||
configListeners.add(listener); | |||||
} | |||||
public void removeListener( ConfigurationListener listener ) | |||||
{ | |||||
configListeners.remove( listener ); | |||||
} | |||||
} | } |
import org.apache.maven.archiva.configuration.ArchivaConfiguration; | import org.apache.maven.archiva.configuration.ArchivaConfiguration; | ||||
import org.apache.maven.archiva.configuration.Configuration; | 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.Registry; | ||||
import org.codehaus.plexus.registry.RegistryException; | import org.codehaus.plexus.registry.RegistryException; | ||||
import org.codehaus.plexus.registry.RegistryListener; | import org.codehaus.plexus.registry.RegistryListener; | ||||
import org.easymock.MockControl; | import org.easymock.MockControl; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.HashSet; | |||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Set; | |||||
/** | /** | ||||
* MockConfiguration | * MockConfiguration | ||||
{ | { | ||||
private Configuration configuration = new Configuration(); | 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; | private MockControl registryControl; | ||||
public void addChangeListener( RegistryListener listener ) | public void addChangeListener( RegistryListener listener ) | ||||
{ | { | ||||
listeners.add( listener ); | |||||
registryListeners.add( listener ); | |||||
} | } | ||||
public Configuration getConfiguration() | public Configuration getConfiguration() | ||||
public void triggerChange( String name, String value ) | 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 | try | ||||
{ | { | ||||
listener.afterConfigurationChange( registryMock, name, value ); | listener.afterConfigurationChange( registryMock, name, value ); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
public void addListener( ConfigurationListener listener ) | |||||
{ | |||||
configListeners.add(listener); | |||||
} | |||||
public void removeListener( ConfigurationListener listener ) | |||||
{ | |||||
configListeners.remove( listener ); | |||||
} | |||||
} | } |