[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

@ -170,6 +170,11 @@ public class DefaultArchivaAdministration
throws RepositoryAdminException throws RepositoryAdminException
{ {
Configuration configuration = getArchivaConfiguration().getConfiguration(); Configuration configuration = getArchivaConfiguration().getConfiguration();
if ( configuration.getRepositoryScanning().getKnownContentConsumers().contains( knownContentConsumer ) )
{
log.warn( "skip adding knownContentConsumer {} as already here", knownContentConsumer );
return;
}
configuration.getRepositoryScanning().addKnownContentConsumer( knownContentConsumer ); configuration.getRepositoryScanning().addKnownContentConsumer( knownContentConsumer );
saveConfiguration( configuration ); saveConfiguration( configuration );
triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation ); triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation );
@ -188,6 +193,11 @@ public class DefaultArchivaAdministration
throws RepositoryAdminException throws RepositoryAdminException
{ {
Configuration configuration = getArchivaConfiguration().getConfiguration(); Configuration configuration = getArchivaConfiguration().getConfiguration();
if ( configuration.getRepositoryScanning().getInvalidContentConsumers().contains( invalidContentConsumer ) )
{
log.warn( "skip adding invalidContentConsumer {} as already here", invalidContentConsumer );
return;
}
configuration.getRepositoryScanning().addInvalidContentConsumer( invalidContentConsumer ); configuration.getRepositoryScanning().addInvalidContentConsumer( invalidContentConsumer );
saveConfiguration( configuration ); saveConfiguration( configuration );
triggerAuditEvent( "", "", AuditEvent.ENABLE_REPO_CONSUMER, auditInformation ); 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.AbstractRepositoryAdminTest;
import org.apache.archiva.admin.repository.RepositoryAdminException; import org.apache.archiva.admin.repository.RepositoryAdminException;
import org.apache.archiva.audit.AuditEvent;
import org.junit.Test; import org.junit.Test;
import javax.inject.Inject; import javax.inject.Inject;
@ -61,6 +62,7 @@ public class ArchivaAdministrationTest
assertFalse( assertFalse(
archivaAdministration.getLegacyArtifactPaths().contains( new LegacyArtifactPath( "foo", "bar" ) ) ); archivaAdministration.getLegacyArtifactPaths().contains( new LegacyArtifactPath( "foo", "bar" ) ) );
assertEquals( initialSize, archivaAdministration.getLegacyArtifactPaths().size() ); assertEquals( initialSize, archivaAdministration.getLegacyArtifactPaths().size() );
mockAuditListener.clearEvents();
} }
@Test @Test
@ -101,5 +103,54 @@ public class ArchivaAdministrationTest
assertEquals( initialSize, archivaAdministration.getRepositoryScanning().getFileTypes().size() ); assertEquals( initialSize, archivaAdministration.getRepositoryScanning().getFileTypes().size() );
assertNull( archivaAdministration.getFileType( "foo" ) ); 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.Preparable;
import com.opensymphony.xwork2.Validateable; 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.audit.Auditable;
import org.apache.archiva.repository.scanner.RepositoryContentConsumers; import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
import org.apache.archiva.security.common.ArchivaRoleConstants; import org.apache.archiva.security.common.ArchivaRoleConstants;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils; 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.apache.maven.archiva.web.action.AbstractActionSupport;
import org.codehaus.plexus.redback.rbac.Resource; 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.SecureAction;
import org.codehaus.redback.integration.interceptor.SecureActionBundle; import org.codehaus.redback.integration.interceptor.SecureActionBundle;
import org.codehaus.redback.integration.interceptor.SecureActionException; import org.codehaus.redback.integration.interceptor.SecureActionException;
@ -62,10 +58,10 @@ public class RepositoryScanningAction
{ {
@Inject @Inject
private ArchivaConfiguration archivaConfiguration; private RepositoryContentConsumers repoconsumerUtil;
@Inject @Inject
private RepositoryContentConsumers repoconsumerUtil; private ArchivaAdministration archivaAdministration;
private Map<String, FileType> fileTypeMap; private Map<String, FileType> fileTypeMap;
@ -109,58 +105,46 @@ public class RepositoryScanningAction
public String addFiletypePattern() public String addFiletypePattern()
{ {
log.info( "Add New File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" ); log.info( "Add New File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
if ( !isValidFiletypeCommand() ) if ( !isValidFiletypeCommand() )
{ {
return INPUT; return INPUT;
} }
String id = getFileTypeId(); try
String pattern = getPattern();
FileType filetype = findFileType( id );
if ( filetype == null )
{ {
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; return INPUT;
} }
return SUCCESS;
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();
} }
public String removeFiletypePattern() public String removeFiletypePattern()
throws RepositoryAdminException
{ {
log.info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" ); log.info( "Remove File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
if ( !isValidFiletypeCommand() ) if ( !isValidFiletypeCommand() )
{ {
return INPUT; return INPUT;
} }
FileType filetype = findFileType( getFileTypeId() ); try
if ( filetype == null )
{ {
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; return INPUT;
} }
filetype.removePattern( getPattern() ); return SUCCESS;
triggerAuditEvent( AuditEvent.REMOVE_PATTERN + " " + pattern );
return saveConfiguration();
} }
public String getFileTypeId() public String getFileTypeId()
@ -207,8 +191,7 @@ public class RepositoryScanningAction
public void prepare() public void prepare()
throws Exception throws Exception
{ {
Configuration config = archivaConfiguration.getConfiguration(); RepositoryScanning reposcanning = archivaAdministration.getRepositoryScanning();
RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure(); FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
@ -243,54 +226,90 @@ public class RepositoryScanningAction
public String updateInvalidConsumers() public String updateInvalidConsumers()
{ {
addActionMessage( "Update Invalid Consumers" );
List<String> oldConsumers = try
archivaConfiguration.getConfiguration().getRepositoryScanning().getInvalidContentConsumers(); {
List<String> oldConsumers = getArchivaAdministration().getRepositoryScanning().getInvalidContentConsumers();
archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
enabledInvalidContentConsumers );
if ( enabledInvalidContentConsumers != null ) if ( enabledInvalidContentConsumers != null )
{ {
filterAddedConsumers( oldConsumers, enabledInvalidContentConsumers ); for ( String oldConsumer : oldConsumers )
filterRemovedConsumers( oldConsumers, enabledInvalidContentConsumers ); {
if ( !enabledInvalidContentConsumers.contains( oldConsumer ) )
{
getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
}
}
for ( String enabledKnowContentConsumer : enabledInvalidContentConsumers )
{
getArchivaAdministration().addInvalidContentConsumer( enabledKnowContentConsumer,
getAuditInformation() );
}
} }
else else
{ {
disableAllEnabledConsumers( oldConsumers ); 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" );
return saveConfiguration(); return SUCCESS;
} }
public String updateKnownConsumers() public String updateKnownConsumers()
{ {
addActionMessage( "Update Known Consumers" );
List<String> oldConsumers = try
archivaConfiguration.getConfiguration().getRepositoryScanning().getKnownContentConsumers(); {
List<String> oldConsumers = getArchivaAdministration().getRepositoryScanning().getKnownContentConsumers();
archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
enabledKnownContentConsumers );
if ( enabledKnownContentConsumers != null ) if ( enabledKnownContentConsumers != null )
{ {
filterAddedConsumers( oldConsumers, enabledKnownContentConsumers ); for ( String oldConsumer : oldConsumers )
filterRemovedConsumers( oldConsumers, enabledKnownContentConsumers ); {
if ( !enabledKnownContentConsumers.contains( oldConsumer ) )
{
getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
}
}
for ( String enabledKnowContentConsumer : enabledKnownContentConsumers )
{
getArchivaAdministration().addKnownContentConsumer( enabledKnowContentConsumer,
getAuditInformation() );
}
} }
else else
{ {
disableAllEnabledConsumers( oldConsumers ); 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" );
return saveConfiguration(); return SUCCESS;
} }
private FileType findFileType( String id ) private FileType findFileType( String id )
throws RepositoryAdminException
{ {
RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning(); return getArchivaAdministration().getFileType( id );
return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
} }
private boolean isValidFiletypeCommand() private boolean isValidFiletypeCommand()
@ -308,56 +327,6 @@ public class RepositoryScanningAction
return !hasActionErrors(); 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() public List<String> getEnabledInvalidContentConsumers()
{ {
@ -379,13 +348,13 @@ public class RepositoryScanningAction
this.enabledKnownContentConsumers = enabledKnownContentConsumers; 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; 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.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration; import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
@ -52,22 +53,30 @@ public class RepositoryScanningActionTest
config = new Configuration(); config = new Configuration();
RepositoryScanningConfiguration repositoryScanningConfig = new RepositoryScanningConfiguration( ); RepositoryScanningConfiguration repositoryScanningConfig = new RepositoryScanningConfiguration();
repositoryScanningConfig.setKnownContentConsumers( createKnownContentConsumersList() ); repositoryScanningConfig.setKnownContentConsumers( createKnownContentConsumersList() );
config.setRepositoryScanning( repositoryScanningConfig ); config.setRepositoryScanning( repositoryScanningConfig );
action.setArchivaConfiguration( archivaConfig ); DefaultArchivaAdministration archivaAdministration = new DefaultArchivaAdministration();
archivaAdministration.setArchivaConfiguration( archivaConfig );
action.setArchivaAdministration( archivaAdministration );
} }
public void testUpdateKnownConsumers() public void testUpdateKnownConsumers()
throws Exception throws Exception
{ {
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 10 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfig.save( config );
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(); archivaConfigControl.replay();
@ -78,16 +87,21 @@ public class RepositoryScanningActionTest
List<String> results = config.getRepositoryScanning().getKnownContentConsumers(); List<String> results = config.getRepositoryScanning().getKnownContentConsumers();
assertEquals( action.SUCCESS, returnString ); assertEquals( action.SUCCESS, returnString );
assertEquals( 8, results.size() ); assertEquals( "results " + results, 8, results.size() );
} }
public void testDisableAllKnownConsumers() public void testDisableAllKnownConsumers()
throws Exception throws Exception
{ {
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 10 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfig.save( config );
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(); archivaConfigControl.replay();
@ -106,7 +120,7 @@ public class RepositoryScanningActionTest
action.setEnabledKnownContentConsumers( createKnownContentConsumersList() ); action.setEnabledKnownContentConsumers( createKnownContentConsumersList() );
} }
private List<String> createKnownContentConsumersList( ) private List<String> createKnownContentConsumersList()
{ {
List<String> knownContentConsumers = new ArrayList<String>(); List<String> knownContentConsumers = new ArrayList<String>();
knownContentConsumers.add( "auto-remove" ); knownContentConsumers.add( "auto-remove" );