From 18d67e6fa854db9302e1a58a1b0de760dac9d017 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Fri, 9 Sep 2011 08:33:55 +0000 Subject: [PATCH] [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 --- .../admin/DefaultArchivaAdministration.java | 12 +- .../admin/FiletypeToMapClosure.java | 50 ++++ .../admin/ArchivaAdministrationTest.java | 51 ++++ .../scanning/RepositoryScanningAction.java | 233 ++++++++---------- .../RepositoryScanningActionTest.java | 90 ++++--- 5 files changed, 265 insertions(+), 171 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/admin/FiletypeToMapClosure.java diff --git a/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/admin/DefaultArchivaAdministration.java b/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/admin/DefaultArchivaAdministration.java index 6f3ae7652..968b9120e 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/admin/DefaultArchivaAdministration.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/admin/DefaultArchivaAdministration.java @@ -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 ); diff --git a/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/admin/FiletypeToMapClosure.java b/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/admin/FiletypeToMapClosure.java new file mode 100644 index 000000000..5465b6e71 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/admin/FiletypeToMapClosure.java @@ -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 map = new HashMap(); + + public void execute( Object input ) + { + if ( input instanceof FileType ) + { + FileType filetype = (FileType) input; + map.put( filetype.getId(), filetype ); + } + } + + public Map getMap() + { + return map; + } +} diff --git a/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/admin/ArchivaAdministrationTest.java b/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/admin/ArchivaAdministrationTest.java index 9a9629fe4..92eb20533 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/admin/ArchivaAdministrationTest.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/admin/ArchivaAdministrationTest.java @@ -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(); + } } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/scanning/RepositoryScanningAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/scanning/RepositoryScanningAction.java index a260234f7..759f66930 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/scanning/RepositoryScanningAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/scanning/RepositoryScanningAction.java @@ -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 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 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 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 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 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 oldList, List newList ) - { - for ( String consumer : newList ) - { - if ( !oldList.contains( consumer ) ) - { - triggerAuditEvent( AuditEvent.ENABLE_REPO_CONSUMER + " " + consumer ); - } - } - } - - private void filterRemovedConsumers( List oldList, List newList ) - { - for ( String consumer : oldList ) - { - if ( !newList.contains( consumer ) ) - { - triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer ); - } - } - } - - private void disableAllEnabledConsumers( List consumers ) - { - for ( String consumer : consumers ) - { - triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer ); - } - } public List 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; } } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/scanning/RepositoryScanningActionTest.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/scanning/RepositoryScanningActionTest.java index e53a8be0b..54fc408ea 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/scanning/RepositoryScanningActionTest.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/scanning/RepositoryScanningActionTest.java @@ -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 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 results = config.getRepositoryScanning().getKnownContentConsumers(); - + assertEquals( action.SUCCESS, returnString ); assertEquals( 0, results.size() ); } - + private void setEnabledKnownContentConsumers() { action.setEnabledKnownContentConsumers( createKnownContentConsumersList() ); } - - private List createKnownContentConsumersList( ) + + private List createKnownContentConsumersList() { List knownContentConsumers = new ArrayList(); 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; } }