[MRM-1512] api to configure RepositoryScanningConfiguration : use it in webapp

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1167036 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2011-09-09 08:33:55 +00:00
parent ff43de7a65
commit 18d67e6fa8
5 changed files with 265 additions and 171 deletions

View File

@ -125,7 +125,7 @@ public class DefaultArchivaAdministration
fileType.removePattern( pattern );
saveConfiguration( configuration );
triggerAuditEvent( "", "", AuditEvent.REMOVE_PATTERN, auditInformation );
triggerAuditEvent( "", "", AuditEvent.REMOVE_PATTERN, auditInformation );
}
public FileType getFileType( String fileTypeId )
@ -170,6 +170,11 @@ public class DefaultArchivaAdministration
throws RepositoryAdminException
{
Configuration configuration = getArchivaConfiguration().getConfiguration();
if ( configuration.getRepositoryScanning().getKnownContentConsumers().contains( knownContentConsumer ) )
{
log.warn( "skip adding knownContentConsumer {} as already here", knownContentConsumer );
return;
}
configuration.getRepositoryScanning().addKnownContentConsumer( knownContentConsumer );
saveConfiguration( configuration );
triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
@ -188,6 +193,11 @@ public class DefaultArchivaAdministration
throws RepositoryAdminException
{
Configuration configuration = getArchivaConfiguration().getConfiguration();
if ( configuration.getRepositoryScanning().getInvalidContentConsumers().contains( invalidContentConsumer ) )
{
log.warn( "skip adding invalidContentConsumer {} as already here", invalidContentConsumer );
return;
}
configuration.getRepositoryScanning().addInvalidContentConsumer( invalidContentConsumer );
saveConfiguration( configuration );
triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );

View File

@ -0,0 +1,50 @@
package org.apache.archiva.admin.repository.admin;
/*
* 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.
*/
import org.apache.commons.collections.Closure;
import java.util.HashMap;
import java.util.Map;
/**
* FiletypeToMapClosure
*
* @since 1.4
*/
public class FiletypeToMapClosure
implements Closure
{
private Map<String, FileType> map = new HashMap<String, FileType>();
public void execute( Object input )
{
if ( input instanceof FileType )
{
FileType filetype = (FileType) input;
map.put( filetype.getId(), filetype );
}
}
public Map<String, FileType> getMap()
{
return map;
}
}

View File

@ -20,6 +20,7 @@ package org.apache.archiva.admin.repository.admin;
import org.apache.archiva.admin.repository.AbstractRepositoryAdminTest;
import org.apache.archiva.admin.repository.RepositoryAdminException;
import org.apache.archiva.audit.AuditEvent;
import org.junit.Test;
import javax.inject.Inject;
@ -61,6 +62,7 @@ public class ArchivaAdministrationTest
assertFalse(
archivaAdministration.getLegacyArtifactPaths().contains( new LegacyArtifactPath( "foo", "bar" ) ) );
assertEquals( initialSize, archivaAdministration.getLegacyArtifactPaths().size() );
mockAuditListener.clearEvents();
}
@Test
@ -101,5 +103,54 @@ public class ArchivaAdministrationTest
assertEquals( initialSize, archivaAdministration.getRepositoryScanning().getFileTypes().size() );
assertNull( archivaAdministration.getFileType( "foo" ) );
mockAuditListener.clearEvents();
}
@Test
public void knownContentConsumersTest()
throws Exception
{
int initialSize = archivaAdministration.getRepositoryScanning().getKnownContentConsumers().size();
archivaAdministration.addKnownContentConsumer( "foo", getFakeAuditInformation() );
assertEquals( initialSize + 1,
archivaAdministration.getRepositoryScanning().getKnownContentConsumers().size() );
assertTrue( archivaAdministration.getRepositoryScanning().getKnownContentConsumers().contains( "foo" ) );
// ensure we don't add it twice as it's an ArrayList as storage
archivaAdministration.addKnownContentConsumer( "foo", getFakeAuditInformation() );
assertEquals( initialSize + 1,
archivaAdministration.getRepositoryScanning().getKnownContentConsumers().size() );
assertTrue( archivaAdministration.getRepositoryScanning().getKnownContentConsumers().contains( "foo" ) );
mockAuditListener.clearEvents();
}
@Test
public void invalidContentConsumersTest()
throws Exception
{
int initialSize = archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().size();
archivaAdministration.addInvalidContentConsumer( "foo", getFakeAuditInformation() );
assertEquals( initialSize + 1,
archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().size() );
assertTrue( archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().contains( "foo" ) );
// ensure we don't add it twice as it's an ArrayList as storage
archivaAdministration.addInvalidContentConsumer( "foo", getFakeAuditInformation() );
assertEquals( initialSize + 1,
archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().size() );
assertTrue( archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().contains( "foo" ) );
assertEquals( 1, mockAuditListener.getAuditEvents().size() );
assertEquals( AuditEvent.ENABLE_REPO_CONSUMER, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
mockAuditListener.clearEvents();
}
}

View File

@ -21,22 +21,18 @@ package org.apache.maven.archiva.web.action.admin.scanning;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Validateable;
import org.apache.archiva.audit.AuditEvent;
import org.apache.archiva.admin.repository.RepositoryAdminException;
import org.apache.archiva.admin.repository.admin.ArchivaAdministration;
import org.apache.archiva.admin.repository.admin.FileType;
import org.apache.archiva.admin.repository.admin.FiletypeToMapClosure;
import org.apache.archiva.admin.repository.admin.RepositoryScanning;
import org.apache.archiva.audit.Auditable;
import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
import org.apache.archiva.security.common.ArchivaRoleConstants;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.FileType;
import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
import org.apache.maven.archiva.configuration.functors.FiletypeToMapClosure;
import org.apache.maven.archiva.web.action.AbstractActionSupport;
import org.codehaus.plexus.redback.rbac.Resource;
import org.codehaus.plexus.registry.RegistryException;
import org.codehaus.redback.integration.interceptor.SecureAction;
import org.codehaus.redback.integration.interceptor.SecureActionBundle;
import org.codehaus.redback.integration.interceptor.SecureActionException;
@ -62,10 +58,10 @@ public class RepositoryScanningAction
{
@Inject
private ArchivaConfiguration archivaConfiguration;
private RepositoryContentConsumers repoconsumerUtil;
@Inject
private RepositoryContentConsumers repoconsumerUtil;
private ArchivaAdministration archivaAdministration;
private Map<String, FileType> fileTypeMap;
@ -109,58 +105,46 @@ public class RepositoryScanningAction
public String addFiletypePattern()
{
log.info( "Add New File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
log.info( "Add New File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
if ( !isValidFiletypeCommand() )
{
return INPUT;
}
String id = getFileTypeId();
String pattern = getPattern();
FileType filetype = findFileType( id );
if ( filetype == null )
try
{
addActionError( "Pattern not added, unable to find filetype " + id );
getArchivaAdministration().addFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
}
catch ( RepositoryAdminException e )
{
addActionError( "error adding file type pattern " + e.getMessage() );
return INPUT;
}
if ( filetype.getPatterns().contains( pattern ) )
{
addActionError( "Not adding pattern \"" + pattern + "\" to filetype " + id + " as it already exists." );
return INPUT;
}
filetype.addPattern( pattern );
addActionMessage( "Added pattern \"" + pattern + "\" to filetype " + id );
triggerAuditEvent( AuditEvent.ADD_PATTERN + " " + pattern );
return saveConfiguration();
return SUCCESS;
}
public String removeFiletypePattern()
throws RepositoryAdminException
{
log.info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
log.info( "Remove File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
if ( !isValidFiletypeCommand() )
{
return INPUT;
}
FileType filetype = findFileType( getFileTypeId() );
if ( filetype == null )
try
{
addActionError( "Pattern not removed, unable to find filetype " + getFileTypeId() );
getArchivaAdministration().removeFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
}
catch ( RepositoryAdminException e )
{
addActionError( "error adding file type pattern " + e.getMessage() );
return INPUT;
}
filetype.removePattern( getPattern() );
triggerAuditEvent( AuditEvent.REMOVE_PATTERN + " " + pattern );
return saveConfiguration();
return SUCCESS;
}
public String getFileTypeId()
@ -207,8 +191,7 @@ public class RepositoryScanningAction
public void prepare()
throws Exception
{
Configuration config = archivaConfiguration.getConfiguration();
RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
RepositoryScanning reposcanning = archivaAdministration.getRepositoryScanning();
FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
@ -243,54 +226,90 @@ public class RepositoryScanningAction
public String updateInvalidConsumers()
{
try
{
List<String> oldConsumers = getArchivaAdministration().getRepositoryScanning().getInvalidContentConsumers();
if ( enabledInvalidContentConsumers != null )
{
for ( String oldConsumer : oldConsumers )
{
if ( !enabledInvalidContentConsumers.contains( oldConsumer ) )
{
getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
}
}
for ( String enabledKnowContentConsumer : enabledInvalidContentConsumers )
{
getArchivaAdministration().addInvalidContentConsumer( enabledKnowContentConsumer,
getAuditInformation() );
}
}
else
{
for ( String oldConsumer : oldConsumers )
{
getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
}
}
}
catch ( RepositoryAdminException e )
{
log.error( e.getMessage(), e );
addActionError( "Error update invalidContentConsumers " + e.getMessage() );
return INPUT;
}
addActionMessage( "Update Invalid Consumers" );
List<String> oldConsumers =
archivaConfiguration.getConfiguration().getRepositoryScanning().getInvalidContentConsumers();
archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
enabledInvalidContentConsumers );
if ( enabledInvalidContentConsumers != null )
{
filterAddedConsumers( oldConsumers, enabledInvalidContentConsumers );
filterRemovedConsumers( oldConsumers, enabledInvalidContentConsumers );
}
else
{
disableAllEnabledConsumers( oldConsumers );
}
return saveConfiguration();
return SUCCESS;
}
public String updateKnownConsumers()
{
try
{
List<String> oldConsumers = getArchivaAdministration().getRepositoryScanning().getKnownContentConsumers();
if ( enabledKnownContentConsumers != null )
{
for ( String oldConsumer : oldConsumers )
{
if ( !enabledKnownContentConsumers.contains( oldConsumer ) )
{
getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
}
}
for ( String enabledKnowContentConsumer : enabledKnownContentConsumers )
{
getArchivaAdministration().addKnownContentConsumer( enabledKnowContentConsumer,
getAuditInformation() );
}
}
else
{
for ( String oldConsumer : oldConsumers )
{
getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
}
}
}
catch ( RepositoryAdminException e )
{
log.error( e.getMessage(), e );
addActionError( "Error update knowContentConsumers " + e.getMessage() );
return INPUT;
}
addActionMessage( "Update Known Consumers" );
List<String> oldConsumers =
archivaConfiguration.getConfiguration().getRepositoryScanning().getKnownContentConsumers();
archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
enabledKnownContentConsumers );
if ( enabledKnownContentConsumers != null )
{
filterAddedConsumers( oldConsumers, enabledKnownContentConsumers );
filterRemovedConsumers( oldConsumers, enabledKnownContentConsumers );
}
else
{
disableAllEnabledConsumers( oldConsumers );
}
return saveConfiguration();
return SUCCESS;
}
private FileType findFileType( String id )
throws RepositoryAdminException
{
RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
return getArchivaAdministration().getFileType( id );
}
private boolean isValidFiletypeCommand()
@ -308,56 +327,6 @@ public class RepositoryScanningAction
return !hasActionErrors();
}
private String saveConfiguration()
{
try
{
archivaConfiguration.save( archivaConfiguration.getConfiguration() );
addActionMessage( "Successfully saved configuration" );
}
catch ( RegistryException e )
{
addActionError( "Unable to save configuration: " + e.getMessage() );
return INPUT;
}
catch ( IndeterminateConfigurationException e )
{
addActionError( e.getMessage() );
return INPUT;
}
return SUCCESS;
}
private void filterAddedConsumers( List<String> oldList, List<String> newList )
{
for ( String consumer : newList )
{
if ( !oldList.contains( consumer ) )
{
triggerAuditEvent( AuditEvent.ENABLE_REPO_CONSUMER + " " + consumer );
}
}
}
private void filterRemovedConsumers( List<String> oldList, List<String> newList )
{
for ( String consumer : oldList )
{
if ( !newList.contains( consumer ) )
{
triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
}
}
}
private void disableAllEnabledConsumers( List<String> consumers )
{
for ( String consumer : consumers )
{
triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
}
}
public List<String> getEnabledInvalidContentConsumers()
{
@ -379,13 +348,13 @@ public class RepositoryScanningAction
this.enabledKnownContentConsumers = enabledKnownContentConsumers;
}
public ArchivaConfiguration getArchivaConfiguration()
public ArchivaAdministration getArchivaAdministration()
{
return archivaConfiguration;
return archivaAdministration;
}
public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
{
this.archivaConfiguration = archivaConfiguration;
this.archivaAdministration = archivaAdministration;
}
}

View File

@ -1,5 +1,6 @@
package org.apache.maven.archiva.web.action.admin.scanning;
import org.apache.archiva.admin.repository.admin.DefaultArchivaAdministration;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
@ -32,81 +33,94 @@ public class RepositoryScanningActionTest
extends AbstractActionTestCase
{
private RepositoryScanningAction action;
private MockControl archivaConfigControl;
private ArchivaConfiguration archivaConfig;
private Configuration config;
protected void setUp()
throws Exception
protected void setUp()
throws Exception
{
super.setUp();
archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
action = new RepositoryScanningAction();
config = new Configuration();
RepositoryScanningConfiguration repositoryScanningConfig = new RepositoryScanningConfiguration( );
RepositoryScanningConfiguration repositoryScanningConfig = new RepositoryScanningConfiguration();
repositoryScanningConfig.setKnownContentConsumers( createKnownContentConsumersList() );
config.setRepositoryScanning( repositoryScanningConfig );
action.setArchivaConfiguration( archivaConfig );
DefaultArchivaAdministration archivaAdministration = new DefaultArchivaAdministration();
archivaAdministration.setArchivaConfiguration( archivaConfig );
action.setArchivaAdministration( archivaAdministration );
}
public void testUpdateKnownConsumers()
throws Exception
{
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 10 );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfigControl.replay();
setEnabledKnownContentConsumers();
String returnString = action.updateKnownConsumers();
List<String> results = config.getRepositoryScanning().getKnownContentConsumers();
assertEquals( action.SUCCESS, returnString );
assertEquals( 8, results.size() );
assertEquals( "results " + results, 8, results.size() );
}
public void testDisableAllKnownConsumers()
throws Exception
{
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 10 );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfig.save( config );
archivaConfigControl.replay();
action.setEnabledKnownContentConsumers( null );
String returnString = action.updateKnownConsumers();
List<String> results = config.getRepositoryScanning().getKnownContentConsumers();
assertEquals( action.SUCCESS, returnString );
assertEquals( 0, results.size() );
}
private void setEnabledKnownContentConsumers()
{
action.setEnabledKnownContentConsumers( createKnownContentConsumersList() );
}
private List<String> createKnownContentConsumersList( )
private List<String> createKnownContentConsumersList()
{
List<String> knownContentConsumers = new ArrayList<String>();
knownContentConsumers.add( "auto-remove" );
@ -117,7 +131,7 @@ public class RepositoryScanningActionTest
knownContentConsumers.add( "repository-purge" );
knownContentConsumers.add( "update-db-artifact" );
knownContentConsumers.add( "validate-checksums" );
return knownContentConsumers;
}
}