Changing mapper interface

This commit is contained in:
Martin Schreier 2022-01-11 21:30:42 +01:00
parent a0c5e5a0b0
commit 9a102721d6
16 changed files with 213 additions and 79 deletions

View File

@ -0,0 +1,46 @@
package org.apache.archiva.common;
/*
* 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.
*/
/**
* @author Martin Schreier <martin_s@apache.org>
*/
public abstract class AbstractMapper<B,T,R> implements MultiModelMapper<B,T,R>
{
@Override
public <S2, T2, R2> boolean supports( Class<S2> baseType, Class<T2> destinationType, Class<R2> reverseSourceType )
{
return (
baseType.isAssignableFrom( getBaseType( ) ) &&
destinationType.isAssignableFrom( getDestinationType( ) ) &&
reverseSourceType.isAssignableFrom( getReverseSourceType( ) )
);
}
@Override
public int hashCode( )
{
return getHash();
}
@Override
public boolean equals( Object obj )
{
return super.hashCode( ) == obj.hashCode( );
}
}

View File

@ -22,19 +22,21 @@ package org.apache.archiva.common;
*
* @author Martin Schreier <martin_s@apache.org>
*
* @param <SB> The base source type for the model mapper
* @param <TB> The base target type for the model mapper
* @param <B> The base type for the model mapper
* @param <T> The target type for the model mapper
* @param <R> The reverse source type for the model mapper
*/
public interface ModelMapperFactory<SB,TB>
public interface ModelMapperFactory<B,T,R>
{
/**
* Returns a mapper for the given source and target type. If no mapper is registered for this combination,
* it will throw a {@link IllegalArgumentException}
* @param sourceType the source type for the mapping
* @param targetType the destination type
* @param <S> source type
* @param <T> destination type
* @param baseType the source type for the mapping
* @param destinationType the destination type
* @param <B2> base type
* @param <T2> destination type
* @param <R2> Reverse source type
* @return the mapper instance
*/
<S extends SB, T extends TB> ModelMapper<S, T> getMapper( Class<S> sourceType, Class<T> targetType ) throws IllegalArgumentException;
<B2 extends B, T2 extends T, R2 extends R> MultiModelMapper<B2, T2, R2> getMapper( Class<B2> baseType, Class<T2> destinationType, Class<R2> reverseSourceType ) throws IllegalArgumentException;
}

View File

@ -22,57 +22,72 @@ package org.apache.archiva.common;
*
* @author Martin Schreier <martin_s@apache.org>
*/
public interface ModelMapper<S,T>
public interface MultiModelMapper<B,T,R>
{
/**
* Converts the source instance to a new instance of the target type.
* @param source the source instance
* @return a new instance of the target type
*/
T map(S source);
T map( B source);
/**
* Updates the target instance based on the source instance
* @param source the source instance
* @param target the target instance
*/
void update( S source, T target );
void update( B source, T target );
/**
* Converts the target instance back to the source type
* @param target the target instance
* @param source the target instance
* @return a new instance of the source type
*/
S reverseMap(T target);
B reverseMap( R source);
/**
* Updates the source instance based on the target instance
* @param target the target instance
* @param source the source instance
* @param source the target instance
* @param target the source instance
*/
void reverseUpdate( T target, S source);
void reverseUpdate( R source, B target);
/**
* Returns the class name of the source type
* @return the source type
*/
Class<S> getSourceType();
Class<B> getBaseType();
/**
* Returns the class name of the target type
* Returns the class name of type that is the goal for the mapping.
* @return the target type
*/
Class<T> getTargetType();
Class<T> getDestinationType();
/**
* Returns the class name of the source for the reverse mapping.
* @return
*/
Class<R> getReverseSourceType();
/**
* Returns <code>true</code>, if the given type are the same or supertype of the mapping types.
* @param sourceType
* @param targetType
* @param baseType
* @param destinationType
* @param reverseSourceType
* @param <S>
* @param <T>
* @return
*/
<S, T> boolean supports( Class<S> sourceType, Class<T> targetType );
<S, T, R> boolean supports( Class<S> baseType, Class<T> destinationType, Class<R> reverseSourceType);
default int getHash() {
return getHash(getBaseType( ), getDestinationType( ), getReverseSourceType( ) );
}
static int getHash(Class<?> baseType, Class<?> destinationType, Class<?> reverseSourceType) {
return baseType.hashCode( ) ^ destinationType.hashCode( ) ^ reverseSourceType.hashCode( );
}
}

View File

@ -21,8 +21,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>archiva-configuration</artifactId>
<groupId>org.apache.archiva.configuration</groupId>
<artifactId>archiva-configuration</artifactId>
<version>3.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -18,6 +18,8 @@ package org.apache.archiva.configuration.model;
*/
/**
* Marker interface that is used for configuration model classes.
*
* @author Martin Schreier <martin_s@apache.org>
*/
public interface ConfigurationModel

View File

@ -23,7 +23,6 @@ import org.apache.archiva.indexer.ArchivaIndexingContext;
import org.apache.archiva.event.EventSource;
import org.apache.archiva.repository.storage.RepositoryStorage;
import org.apache.archiva.repository.features.RepositoryFeature;
import org.apache.archiva.repository.storage.StorageAsset;
import java.net.URI;
import java.util.Locale;
@ -136,12 +135,12 @@ public interface Repository extends EventSource, RepositoryStorage {
* Extension method that allows to provide different features that are not supported by all
* repository types.
*
* @param clazz The feature class that is requested
* @param <T> This is the class of the feature
* @param clazz The feature class that is requested
* @return The feature implementation for this repository instance, if it is supported
* @throws UnsupportedFeatureException if the feature is not supported by this repository type
*/
<T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature(Class<T> clazz) throws UnsupportedFeatureException;
<T extends RepositoryFeature<T>> T getFeature( Class<T> clazz) throws UnsupportedFeatureException;
/**

View File

@ -22,7 +22,6 @@ package org.apache.archiva.repository.base;
import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;
import org.apache.archiva.event.Event;
import org.apache.archiva.event.EventHandler;
import org.apache.archiva.event.EventManager;
@ -38,7 +37,6 @@ import org.apache.archiva.repository.storage.RepositoryStorage;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.archiva.repository.features.RepositoryFeature;
import org.apache.archiva.repository.features.StagingRepositoryFeature;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -200,10 +198,10 @@ public abstract class AbstractRepository implements EditableRepository, EventHan
@SuppressWarnings( "unchecked" )
@Override
public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature( Class<T> clazz ) throws UnsupportedFeatureException
public <T extends RepositoryFeature<T>> T getFeature( Class<T> clazz ) throws UnsupportedFeatureException
{
if (featureMap.containsKey( clazz )) {
return (RepositoryFeature<T>) featureMap.get(clazz);
return (T) featureMap.get(clazz);
} else
{
throw new UnsupportedFeatureException( "Feature " + clazz + " not supported" );

View File

@ -96,15 +96,15 @@ public class MavenManagedRepository extends AbstractManagedRepository
@SuppressWarnings( "unchecked" )
@Override
public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature( Class<T> clazz ) throws UnsupportedFeatureException
public <T extends RepositoryFeature<T>> T getFeature( Class<T> clazz ) throws UnsupportedFeatureException
{
if (ArtifactCleanupFeature.class.equals( clazz ))
{
return (RepositoryFeature<T>) artifactCleanupFeature;
return (T) artifactCleanupFeature;
} else if (IndexCreationFeature.class.equals(clazz)) {
return (RepositoryFeature<T>) indexCreationFeature;
return (T) indexCreationFeature;
} else if (StagingRepositoryFeature.class.equals(clazz)) {
return (RepositoryFeature<T>) stagingRepositoryFeature;
return (T) stagingRepositoryFeature;
} else {
throw new UnsupportedFeatureException( );
}

View File

@ -91,12 +91,12 @@ public class MavenRemoteRepository extends AbstractRemoteRepository
@SuppressWarnings( "unchecked" )
@Override
public <T extends RepositoryFeature<T>> RepositoryFeature<T> getFeature( Class<T> clazz ) throws UnsupportedFeatureException
public <T extends RepositoryFeature<T>> T getFeature( Class<T> clazz ) throws UnsupportedFeatureException
{
if (RemoteIndexFeature.class.equals( clazz )) {
return (RepositoryFeature<T>) remoteIndexFeature;
return (T) remoteIndexFeature;
} else if (IndexCreationFeature.class.equals(clazz)) {
return (RepositoryFeature<T>) indexCreationFeature;
return (T) indexCreationFeature;
} else {
throw new UnsupportedFeatureException( );
}

View File

@ -68,6 +68,10 @@
<groupId>org.apache.archiva</groupId>
<artifactId>archiva-scheduler-indexing</artifactId>
</dependency>
<dependency>
<groupId>org.apache.archiva.configuration</groupId>
<artifactId>archiva-configuration-model</artifactId>
</dependency>
<dependency>
<groupId>org.apache.archiva</groupId>

View File

@ -200,7 +200,7 @@ public class MavenManagedRepository extends Repository
}
@Schema(name="has_staging_repository", description = "True, if this repository has a staging repository assigned")
public boolean isHasStagingRepository( )
public boolean hasStagingRepository( )
{
return hasStagingRepository;
}

View File

@ -17,56 +17,93 @@ package org.apache.archiva.rest.api.v2.model.map;
* under the License.
*/
import org.apache.archiva.common.ModelMapper;
import org.apache.archiva.configuration.model.ManagedRepositoryConfiguration;
import org.apache.archiva.repository.ManagedRepository;
import org.apache.archiva.repository.ReleaseScheme;
import org.apache.archiva.repository.RepositoryType;
import org.apache.archiva.repository.features.ArtifactCleanupFeature;
import org.apache.archiva.repository.features.StagingRepositoryFeature;
import org.apache.archiva.rest.api.v2.model.MavenManagedRepository;
import org.springframework.stereotype.Service;
/**
* Mapping implementation for Maven managed repository to managed repository configuration
*
* @author Martin Schreier <martin_s@apache.org>
*/
public class MavenRepositoryMapper implements RestMapper<MavenManagedRepository, ManagedRepositoryConfiguration>
@Service("mapper#managed_repository#maven")
public class MavenRepositoryMapper extends RestServiceMapper<MavenManagedRepository, ManagedRepositoryConfiguration, ManagedRepository>
{
private static final String TYPE = RepositoryType.MAVEN.name( );
@Override
public ManagedRepositoryConfiguration map( MavenManagedRepository source )
{
return null;
ManagedRepositoryConfiguration target = new ManagedRepositoryConfiguration( );
update( source, target );
return target;
}
@Override
public void update( MavenManagedRepository source, ManagedRepositoryConfiguration target )
{
target.setId( source.getId() );
target.setName( source.getName() );
target.setDescription( source.getDescription( ) );
target.setType( TYPE );
target.setBlockRedeployments( source.isBlocksRedeployments() );
target.setDeleteReleasedSnapshots( source.isDeleteSnapshotsOfRelease() );
target.setIndexDir( source.getIndexPath() );
target.setLayout( source.getLayout() );
target.setLocation( source.getLocation() );
target.setPackedIndexDir( source.getPackedIndexPath() );
target.setRefreshCronExpression( source.getSchedulingDefinition() );
target.setReleases( source.getReleaseSchemes( ).contains( ReleaseScheme.RELEASE ) );
target.setRetentionCount( source.getRetentionCount() );
target.setRetentionPeriod( source.getRetentionPeriod().getDays() );
target.setScanned( source.isScanned() );
target.setSkipPackedIndexCreation( source.isSkipPackedIndexCreation() );
target.setSnapshots( source.getReleaseSchemes( ).contains( ReleaseScheme.SNAPSHOT ) );
target.setStageRepoNeeded( source.hasStagingRepository() );
}
@Override
public MavenManagedRepository reverseMap( ManagedRepositoryConfiguration target )
public MavenManagedRepository reverseMap( ManagedRepository source )
{
MavenManagedRepository result = new MavenManagedRepository( );
StagingRepositoryFeature srf = source.getFeature( StagingRepositoryFeature.class ).get( );
ArtifactCleanupFeature acf = source.getFeature( ArtifactCleanupFeature.class ).get( );
result.setHasStagingRepository( srf.isStageRepoNeeded() );
result.setBlocksRedeployments( source.blocksRedeployments() );
result.setIndex( source.hasIndex() );
result.setStagingRepository( srf.getStagingRepository().getId() );
return null;
}
@Override
public void reverseUpdate( ManagedRepositoryConfiguration target, MavenManagedRepository source )
public void reverseUpdate( ManagedRepository source, MavenManagedRepository target )
{
}
@Override
public Class<MavenManagedRepository> getSourceType( )
public Class<MavenManagedRepository> getBaseType( )
{
return MavenManagedRepository.class;
}
@Override
public Class<ManagedRepositoryConfiguration> getTargetType( )
public Class<ManagedRepositoryConfiguration> getDestinationType( )
{
return ManagedRepositoryConfiguration.class;
}
@Override
public <S, T> boolean supports( Class<S> sourceType, Class<T> targetType )
public Class<ManagedRepository> getReverseSourceType( )
{
return (
sourceType.isAssignableFrom( getSourceType() ) &&
targetType.isAssignableFrom( getTargetType() ) );
return ManagedRepository.class;
}
}

View File

@ -17,13 +17,16 @@ package org.apache.archiva.rest.api.v2.model.map;
* under the License.
*/
import org.apache.archiva.common.ModelMapper;
import org.apache.archiva.common.AbstractMapper;
import org.apache.archiva.common.MultiModelMapper;
import org.apache.archiva.configuration.model.ConfigurationModel;
import org.apache.archiva.repository.Repository;
import org.apache.archiva.rest.api.v2.model.RestModel;
/**
* @author Martin Schreier <martin_s@apache.org>
*/
public interface RestMapper<S extends RestModel, T extends ConfigurationModel> extends ModelMapper<S,T>
public abstract class RestServiceMapper<S extends RestModel, T extends ConfigurationModel, R extends Repository> extends AbstractMapper<S,T,R>
implements MultiModelMapper<S,T,R>
{
}

View File

@ -17,13 +17,13 @@ package org.apache.archiva.rest.api.v2.model.map;
* under the License.
*/
import org.apache.archiva.common.ModelMapper;
import org.apache.archiva.common.ModelMapperFactory;
import org.apache.archiva.common.MultiModelMapper;
import org.apache.archiva.configuration.model.ConfigurationModel;
import org.apache.archiva.repository.Repository;
import org.apache.archiva.rest.api.v2.model.RestModel;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
@ -32,45 +32,40 @@ import java.util.Map;
/**
* @author Martin Schreier <martin_s@apache.org>
*/
@SuppressWarnings( "unchecked" )
@Service("modelMapperFactory#rest")
public class ServiceMapperFactory implements ModelMapperFactory<RestModel, ConfigurationModel>
public class ServiceMapperFactory implements ModelMapperFactory<RestModel, ConfigurationModel, Repository>
{
List<MultiModelMapper> modelMapperList;
@Inject
List<ModelMapper> modelMapperList;
public ServiceMapperFactory( List<MultiModelMapper> modelMapperList )
{
this.modelMapperList = modelMapperList;
initMapper();
}
Map<Class<? extends RestModel>, Map<Class<? extends ConfigurationModel>,ModelMapper<? extends RestModel, ? extends ConfigurationModel>>> modelMap;
Map<Integer, MultiModelMapper<? extends RestModel, ? extends ConfigurationModel, ? extends Repository>> modelMap = new HashMap<>( );
@PostConstruct
void initMapper() {
modelMap = new HashMap<>( );
for ( ModelMapper<?, ?> mapper : modelMapperList )
for ( MultiModelMapper<?, ?, ?> mapper : modelMapperList )
{
if (!mapper.supports( RestModel.class, ConfigurationModel.class )) {
if (!mapper.supports( RestModel.class, ConfigurationModel.class, Repository.class )) {
continue;
}
Class<? extends RestModel> sType = (Class<? extends RestModel>) mapper.getSourceType( );
Class<? extends ConfigurationModel> tType = (Class<? extends ConfigurationModel>) mapper.getTargetType( );
Map<Class<? extends ConfigurationModel>, ModelMapper<? extends RestModel, ? extends ConfigurationModel>> tMap;
if (modelMap.containsKey( sType )) {
tMap = modelMap.get( sType );
} else {
tMap = new HashMap<>( );
}
tMap.put( tType, (ModelMapper<? extends RestModel, ? extends ConfigurationModel>) mapper );
Integer key = mapper.hashCode( );
modelMap.put( key, (MultiModelMapper<? extends RestModel, ? extends ConfigurationModel, ? extends Repository>) mapper );
}
}
@Override
public <S extends RestModel, T extends ConfigurationModel> ModelMapper<S, T> getMapper( Class<S> sourceType, Class<T> targetType ) throws IllegalArgumentException
public <S extends RestModel, T extends ConfigurationModel, R extends Repository> MultiModelMapper<S, T, R> getMapper( Class<S> baseType, Class<T> destinationType, Class<R> reverseSourceType ) throws IllegalArgumentException
{
if (!modelMap.containsKey( sourceType )) {
throw new IllegalArgumentException( "No mapper defined for the given source type "+sourceType );
Integer key = MultiModelMapper.getHash( baseType, destinationType, reverseSourceType );
if (!modelMap.containsKey( key )) {
throw new IllegalArgumentException( "No mapper defined for the given source type "+baseType );
}
Map<Class<? extends ConfigurationModel>, ModelMapper<? extends RestModel, ? extends ConfigurationModel>> tMap = modelMap.get( sourceType );
if ( !tMap.containsKey( targetType ) )
{
throw new IllegalArgumentException( "No mapper defined for the given target type "+targetType );
}
return (ModelMapper<S, T>) tMap.get( targetType );
return (MultiModelMapper<S, T, R>) modelMap.get( key );
}
}

View File

@ -0,0 +1,33 @@
<?xml version="1.0"?>
<!--
~ 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="true">
<context:component-scan
base-package="org.apache.archiva.rest.api.v2.model.map"/>
</beans>

View File

@ -220,7 +220,7 @@ public class DefaultMavenManagedRepositoryService implements MavenManagedReposit
}
try
{
managedRepositoryAdmin.addManagedRepository( convert( managedRepository ), managedRepository.isHasStagingRepository(), getAuditInformation() );
managedRepositoryAdmin.addManagedRepository( convert( managedRepository ), managedRepository.hasStagingRepository(), getAuditInformation() );
httpServletResponse.setStatus( 201 );
return MavenManagedRepository.of( repositoryRegistry.getManagedRepository( repoId ) );
}
@ -236,7 +236,7 @@ public class DefaultMavenManagedRepositoryService implements MavenManagedReposit
org.apache.archiva.admin.model.beans.ManagedRepository repo = convert( managedRepository );
try
{
managedRepositoryAdmin.updateManagedRepository( repo, managedRepository.isHasStagingRepository( ), getAuditInformation( ), managedRepository.isResetStats( ) );
managedRepositoryAdmin.updateManagedRepository( repo, managedRepository.hasStagingRepository( ), getAuditInformation( ), managedRepository.isResetStats( ) );
ManagedRepository newRepo = repositoryRegistry.getManagedRepository( managedRepository.getId( ) );
if (newRepo==null) {
throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_UPDATE_FAILED, repositoryId ) );