MRM-1015 - Move consumers and related API's to Spring

RepositoryContentConsumers needed to be ported over as a spring component so that we can use the ApplicationContextAware interface.



git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@719157 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James William Dumay 2008-11-20 04:13:50 +00:00
parent f578399872
commit 8152eb5642
12 changed files with 279 additions and 111 deletions

View File

@ -19,8 +19,6 @@ package org.apache.maven.archiva.consumers.core;
* under the License.
*/
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.ConfigurationNames;
import org.apache.maven.archiva.configuration.FileTypes;
@ -46,47 +44,23 @@ import java.util.List;
* ArtifactMissingChecksumsConsumer - Create missing checksums for the artifact.
*
* @version $Id$
* @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
* role-hint="create-missing-checksums"
* instantiation-strategy="per-lookup"
*/
public class ArtifactMissingChecksumsConsumer
extends AbstractMonitoredConsumer
implements KnownRepositoryContentConsumer, RegistryListener, Initializable
{
/**
* @plexus.configuration default-value="create-missing-checksums"
*/
private String id;
/**
* @plexus.configuration default-value="Create Missing Checksums (.sha1 & .md5)"
*/
private String description;
/**
* @plexus.requirement
*/
private ArchivaConfiguration configuration;
/**
* @plexus.requirement
*/
private FileTypes filetypes;
/**
* @plexus.requirement role-hint="sha1"
*/
private Digester digestSha1;
/**
* @plexus.requirement role-hint="md5";
*/
private Digester digestMd5;
/**
* @plexus.requirement
*/
private ChecksumFile checksum;
private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
@ -99,6 +73,22 @@ public class ArtifactMissingChecksumsConsumer
private List<String> includes = new ArrayList<String>();
public ArtifactMissingChecksumsConsumer(String id,
String description,
ArchivaConfiguration configuration,
FileTypes filetypes,
Digester digestSha1,
Digester digestMd5,
ChecksumFile checksum) {
this.id = id;
this.description = description;
this.configuration = configuration;
this.filetypes = filetypes;
this.digestSha1 = digestSha1;
this.digestMd5 = digestMd5;
this.checksum = checksum;
}
public String getId()
{
return this.id;

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="artifactMissingChecksumsConsumer" class="org.apache.maven.archiva.consumers.core.ArtifactMissingChecksumsConsumer" scope="prototype">
<constructor-arg>
<value>create-missing-checksums</value>
</constructor-arg>
<constructor-arg>
<value>Create Missing Checksums (.sha1, .md5)</value>
</constructor-arg>
<constructor-arg>
<ref bean="archivaConfiguration"/>
</constructor-arg>
<constructor-arg>
<ref bean="fileTypes"/>
</constructor-arg>
<constructor-arg>
<ref bean="digester#sha1"/>
</constructor-arg>
<constructor-arg>
<ref bean="digester#md5"/>
</constructor-arg>
<constructor-arg>
<ref bean="checksumFile"/>
</constructor-arg>
</bean>
</beans>

View File

@ -19,17 +19,15 @@ package org.apache.maven.archiva.consumers.core;
* under the License.
*/
import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
public class ArtifactMissingChecksumsConsumerTest
extends AbstractArtifactConsumerTest
{
@Override
protected void setUp()
throws Exception
{
super.setUp();
consumer = (ArtifactMissingChecksumsConsumer) lookup( KnownRepositoryContentConsumer.class.getName(),
"create-missing-checksums" );
consumer = (ArtifactMissingChecksumsConsumer) lookup( "artifactMissingChecksumsConsumer" );
}
}

View File

@ -56,6 +56,10 @@
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-xml-tools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>

View File

@ -38,39 +38,36 @@ import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* RepositoryContentConsumerUtil
*
* @version $Id$
*
* @plexus.component role="org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers"
*/
public class RepositoryContentConsumers
public class RepositoryContentConsumers implements ApplicationContextAware
{
private Logger log = LoggerFactory.getLogger( RepositoryContentConsumers.class );
private ApplicationContext applicationContext;
/**
* @plexus.requirement
*/
private ArchivaConfiguration archivaConfiguration;
/**
* @plexus.requirement role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
*/
private List<KnownRepositoryContentConsumer> availableKnownConsumers;
/**
* @plexus.requirement role="org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer"
*/
private List<InvalidRepositoryContentConsumer> availableInvalidConsumers;
private List<KnownRepositoryContentConsumer> selectedKnownConsumers;
private List<InvalidRepositoryContentConsumer> selectedInvalidConsumers;
public RepositoryContentConsumers(ArchivaConfiguration archivaConfiguration)
{
this.archivaConfiguration = archivaConfiguration;
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException
{
this.applicationContext = applicationContext;
}
/**
* <p>
* Get the list of Ids associated with those {@link KnownRepositoryContentConsumer} that have
@ -160,7 +157,7 @@ public class RepositoryContentConsumers
List<String> knownSelected = getSelectedKnownConsumerIds();
for ( KnownRepositoryContentConsumer consumer : availableKnownConsumers )
for ( KnownRepositoryContentConsumer consumer : getAvailableKnownConsumers() )
{
if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
{
@ -187,7 +184,7 @@ public class RepositoryContentConsumers
List<String> invalidSelected = getSelectedInvalidConsumerIds();
for ( InvalidRepositoryContentConsumer consumer : availableInvalidConsumers )
for ( InvalidRepositoryContentConsumer consumer : getAvailableInvalidConsumers() )
{
if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
{
@ -208,7 +205,7 @@ public class RepositoryContentConsumers
*/
public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
{
return availableKnownConsumers;
return new ArrayList(applicationContext.getBeansOfType(KnownRepositoryContentConsumer.class).values());
}
/**
@ -220,35 +217,7 @@ public class RepositoryContentConsumers
*/
public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
{
return availableInvalidConsumers;
}
/**
* Set the list of {@link KnownRepositoryContentConsumer} objects that are
* available.
*
* NOTE: This is an override for the base functionality as a component, this
* is used by archiva-cli and the unit testing framework.
*
* @return the list of available {@link KnownRepositoryContentConsumer}.
*/
public void setAvailableKnownConsumers( List<KnownRepositoryContentConsumer> availableKnownConsumers )
{
this.availableKnownConsumers = availableKnownConsumers;
}
/**
* Set the list of {@link InvalidRepositoryContentConsumer} objects that are
* available.
*
* NOTE: This is an override for the base functionality as a component, this
* is used by archiva-cli and the unit testing framework.
*
* @return the list of available {@link InvalidRepositoryContentConsumer}.
*/
public void setAvailableInvalidConsumers( List<InvalidRepositoryContentConsumer> availableInvalidConsumers )
{
this.availableInvalidConsumers = availableInvalidConsumers;
return new ArrayList(applicationContext.getBeansOfType(InvalidRepositoryContentConsumer.class).values());
}
/**

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="repositoryContentConsumers" class="org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers" scope="prototype">
<constructor-arg>
<ref bean="archivaConfiguration"/>
</constructor-arg>
</bean>
</beans>

View File

@ -1,10 +1,17 @@
package org.apache.maven.archiva.repository.scanner;
import java.util.Date;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
public class RepositoryContentConsumersStub
extends RepositoryContentConsumers
{
public RepositoryContentConsumersStub(ArchivaConfiguration archivaConfiguration)
{
super(archivaConfiguration);
}
@Override
public Date getStartTime()
{
Date startTimeForTest = new Date( System.currentTimeMillis() );

View File

@ -19,6 +19,8 @@ package org.apache.maven.archiva.repository.scanner;
* under the License.
*/
import java.io.IOException;
import java.util.Locale;
import org.apache.commons.lang.SystemUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
@ -31,8 +33,18 @@ import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.core.io.Resource;
/**
* RepositoryContentConsumersTest
@ -45,17 +57,18 @@ public class RepositoryContentConsumersTest
private RepositoryContentConsumers lookupRepositoryConsumers()
throws Exception
{
RepositoryContentConsumers consumerUtilStub = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
.getName(), "test" );
ArchivaConfiguration archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE );
ArchivaConfiguration configuration = (ArchivaConfiguration)lookup(ArchivaConfiguration.class);
RepositoryContentConsumers consumerUtilStub = new RepositoryContentConsumersStub(configuration);
RepositoryContentConsumers consumerUtil = (RepositoryContentConsumers) lookup( RepositoryContentConsumers.class
.getName() );
ApplicationContext context = new MockApplicationContext(consumerUtil.getAvailableKnownConsumers(), consumerUtil.getAvailableInvalidConsumers());
consumerUtilStub.setAvailableKnownConsumers( consumerUtil.getAvailableKnownConsumers() );
consumerUtilStub.setAvailableInvalidConsumers( consumerUtil.getAvailableInvalidConsumers() );
consumerUtilStub.setApplicationContext(context);
consumerUtilStub.setSelectedInvalidConsumers( consumerUtil.getSelectedInvalidConsumers() );
consumerUtilStub.setSelectedKnownConsumers( consumerUtil.getSelectedKnownConsumers() );
consumerUtilStub.setArchivaConfiguration( archivaConfiguration );
consumerUtilStub.setArchivaConfiguration( configuration );
assertNotNull( "RepositoryContentConsumers should not be null.", consumerUtilStub );
@ -206,7 +219,9 @@ public class RepositoryContentConsumersTest
KnownRepositoryContentConsumer unselectedKnownConsumer =
(KnownRepositoryContentConsumer) MockControl.createNiceControl(
KnownRepositoryContentConsumer.class ).getMock();
consumers.setAvailableKnownConsumers( Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ) );
consumers.setApplicationContext(new MockApplicationContext(Arrays.asList( selectedKnownConsumer, unselectedKnownConsumer ), null));
consumers.setSelectedKnownConsumers( Collections.singletonList( selectedKnownConsumer ) );
MockControl invalidControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
@ -215,7 +230,9 @@ public class RepositoryContentConsumersTest
InvalidRepositoryContentConsumer unselectedInvalidConsumer =
(InvalidRepositoryContentConsumer) MockControl.createControl(
InvalidRepositoryContentConsumer.class ).getMock();
consumers.setAvailableInvalidConsumers( Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer ) );
consumers.setApplicationContext( new MockApplicationContext(null, Arrays.asList( selectedInvalidConsumer, unselectedInvalidConsumer )));
consumers.setSelectedInvalidConsumers( Collections.singletonList( selectedInvalidConsumer ) );
ManagedRepositoryConfiguration repo = createRepository( "id", "name", getTestFile( "target/test-repo" ) );
@ -303,4 +320,155 @@ public class RepositoryContentConsumersTest
}
return path;
}
private static Map convertToMap(List objects)
{
HashMap map = new HashMap();
for (Object o : objects)
{
map.put(o, o);
}
return map;
}
public class MockApplicationContext implements ApplicationContext
{
private List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer;
private List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers;
public MockApplicationContext(List<KnownRepositoryContentConsumer> knownRepositoryContentConsumer, List<InvalidRepositoryContentConsumer> invalidRepositoryContentConsumers)
{
this.knownRepositoryContentConsumer = knownRepositoryContentConsumer;
this.invalidRepositoryContentConsumers = invalidRepositoryContentConsumers;
}
public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
throw new UnsupportedOperationException("Not supported yet.");
}
public String getDisplayName() {
throw new UnsupportedOperationException("Not supported yet.");
}
public String getId() {
throw new UnsupportedOperationException("Not supported yet.");
}
public ApplicationContext getParent() {
throw new UnsupportedOperationException("Not supported yet.");
}
public long getStartupDate() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean containsBeanDefinition(String beanName) {
throw new UnsupportedOperationException("Not supported yet.");
}
public int getBeanDefinitionCount() {
throw new UnsupportedOperationException("Not supported yet.");
}
public String[] getBeanDefinitionNames() {
throw new UnsupportedOperationException("Not supported yet.");
}
public String[] getBeanNamesForType(Class type) {
throw new UnsupportedOperationException("Not supported yet.");
}
public String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Map getBeansOfType(Class type) throws BeansException {
if (type == KnownRepositoryContentConsumer.class)
{
return convertToMap(knownRepositoryContentConsumer);
}
if (type == InvalidRepositoryContentConsumer.class)
{
return convertToMap(invalidRepositoryContentConsumers);
}
throw new UnsupportedOperationException("Should not have been called");
}
public Map getBeansOfType(Class type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean containsBean(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}
public String[] getAliases(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object getBean(String name) throws BeansException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object getBean(String name, Class requiredType) throws BeansException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Object getBean(String name, Object[] args) throws BeansException {
throw new UnsupportedOperationException("Not supported yet.");
}
public Class getType(String name) throws NoSuchBeanDefinitionException {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean containsLocalBean(String name) {
throw new UnsupportedOperationException("Not supported yet.");
}
public BeanFactory getParentBeanFactory() {
throw new UnsupportedOperationException("Not supported yet.");
}
public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
throw new UnsupportedOperationException("Not supported yet.");
}
public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
throw new UnsupportedOperationException("Not supported yet.");
}
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
throw new UnsupportedOperationException("Not supported yet.");
}
public void publishEvent(ApplicationEvent event) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Resource[] getResources(String locationPattern) throws IOException {
throw new UnsupportedOperationException("Not supported yet.");
}
public ClassLoader getClassLoader() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Resource getResource(String location) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}

View File

@ -86,7 +86,7 @@ public class DavResourceTest extends PlexusInSpringTestCase
resource = getDavResource(resourceLocator.getHref(false), myResource);
lockManager = new SimpleLockManager();
resource.addLockManager(lockManager);
consumers = new RepositoryContentConsumers();
consumers = (RepositoryContentConsumers)getApplicationContext().getBean("repositoryContentConsumers");
archivaXworkUser = (ArchivaXworkUser) getApplicationContext().getBean( PlexusToSpringUtils.buildSpringId( ArchivaXworkUser.class ) );
}

View File

@ -5,15 +5,23 @@ import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
import java.util.List;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
public class StubRepositoryContentConsumers
extends RepositoryContentConsumers
{
public StubRepositoryContentConsumers(ArchivaConfiguration configuration)
{
super(configuration);
}
@Override
public List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
{
return getAvailableKnownConsumers();
}
@Override
public synchronized List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
{
return getAvailableInvalidConsumers();

View File

@ -97,14 +97,6 @@
</configuration>
</component>
<!-- Don't drag in the world just to test this -->
<component>
<role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
<role-hint>default</role-hint>
<implementation>org.apache.maven.archiva.webdav.StubRepositoryContentConsumers</implementation>
</component>
<component>
<role>org.codehaus.plexus.redback.system.SecuritySystem</role>
<role-hint>default</role-hint>

View File

@ -97,14 +97,6 @@
</configuration>
</component>
<!-- Don't drag in the world just to test this -->
<component>
<role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
<role-hint>default</role-hint>
<implementation>org.apache.maven.archiva.webdav.StubRepositoryContentConsumers</implementation>
</component>
<component>
<role>org.codehaus.plexus.redback.system.SecuritySystem</role>
<role-hint>default</role-hint>