From 9a102721d6f0e4ef951901427c43f5c0dc936fd7 Mon Sep 17 00:00:00 2001 From: Martin Schreier Date: Tue, 11 Jan 2022 21:30:42 +0100 Subject: [PATCH] Changing mapper interface --- .../apache/archiva/common/AbstractMapper.java | 46 +++++++++++++++ .../archiva/common/ModelMapperFactory.java | 18 +++--- ...ModelMapper.java => MultiModelMapper.java} | 43 +++++++++----- .../archiva-configuration-model/pom.xml | 2 +- .../model/ConfigurationModel.java | 2 + .../apache/archiva/repository/Repository.java | 5 +- .../repository/base/AbstractRepository.java | 6 +- .../repository/MavenManagedRepository.java | 8 +-- .../repository/MavenRemoteRepository.java | 6 +- .../archiva-rest/archiva-rest-api/pom.xml | 4 ++ .../api/v2/model/MavenManagedRepository.java | 2 +- .../v2/model/map/MavenRepositoryMapper.java | 59 +++++++++++++++---- ...RestMapper.java => RestServiceMapper.java} | 7 ++- .../v2/model/map/ServiceMapperFactory.java | 47 +++++++-------- .../resources/META-INF/spring-context.xml | 33 +++++++++++ .../DefaultMavenManagedRepositoryService.java | 4 +- 16 files changed, 213 insertions(+), 79 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/AbstractMapper.java rename archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/{ModelMapper.java => MultiModelMapper.java} (62%) rename archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/{RestMapper.java => RestServiceMapper.java} (75%) create mode 100644 archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/resources/META-INF/spring-context.xml diff --git a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/AbstractMapper.java b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/AbstractMapper.java new file mode 100644 index 000000000..c77c91cbe --- /dev/null +++ b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/AbstractMapper.java @@ -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 + */ +public abstract class AbstractMapper implements MultiModelMapper +{ + @Override + public boolean supports( Class baseType, Class destinationType, Class 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( ); + } +} diff --git a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/ModelMapperFactory.java b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/ModelMapperFactory.java index 30ee0c4c3..159b95d19 100644 --- a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/ModelMapperFactory.java +++ b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/ModelMapperFactory.java @@ -22,19 +22,21 @@ package org.apache.archiva.common; * * @author Martin Schreier * - * @param The base source type for the model mapper - * @param The base target type for the model mapper + * @param The base type for the model mapper + * @param The target type for the model mapper + * @param The reverse source type for the model mapper */ -public interface ModelMapperFactory +public interface ModelMapperFactory { /** * 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 source type - * @param destination type + * @param baseType the source type for the mapping + * @param destinationType the destination type + * @param base type + * @param destination type + * @param Reverse source type * @return the mapper instance */ - ModelMapper getMapper( Class sourceType, Class targetType ) throws IllegalArgumentException; + MultiModelMapper getMapper( Class baseType, Class destinationType, Class reverseSourceType ) throws IllegalArgumentException; } diff --git a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/ModelMapper.java b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/MultiModelMapper.java similarity index 62% rename from archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/ModelMapper.java rename to archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/MultiModelMapper.java index 1f7fda93b..d28b6da67 100644 --- a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/ModelMapper.java +++ b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/archiva/common/MultiModelMapper.java @@ -22,57 +22,72 @@ package org.apache.archiva.common; * * @author Martin Schreier */ -public interface ModelMapper +public interface MultiModelMapper { /** * 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 getSourceType(); + Class 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 getTargetType(); + Class getDestinationType(); + + /** + * Returns the class name of the source for the reverse mapping. + * @return + */ + Class getReverseSourceType(); /** * Returns true, if the given type are the same or supertype of the mapping types. - * @param sourceType - * @param targetType + * @param baseType + * @param destinationType + * @param reverseSourceType * @param * @param * @return */ - boolean supports( Class sourceType, Class targetType ); + boolean supports( Class baseType, Class destinationType, Class 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( ); + } } diff --git a/archiva-modules/archiva-base/archiva-configuration/archiva-configuration-model/pom.xml b/archiva-modules/archiva-base/archiva-configuration/archiva-configuration-model/pom.xml index bd2591722..988c6f598 100644 --- a/archiva-modules/archiva-base/archiva-configuration/archiva-configuration-model/pom.xml +++ b/archiva-modules/archiva-base/archiva-configuration/archiva-configuration-model/pom.xml @@ -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"> - archiva-configuration org.apache.archiva.configuration + archiva-configuration 3.0.0-SNAPSHOT 4.0.0 diff --git a/archiva-modules/archiva-base/archiva-configuration/archiva-configuration-model/src/main/java/org/apache/archiva/configuration/model/ConfigurationModel.java b/archiva-modules/archiva-base/archiva-configuration/archiva-configuration-model/src/main/java/org/apache/archiva/configuration/model/ConfigurationModel.java index 6a9520aa7..0fbcd3054 100644 --- a/archiva-modules/archiva-base/archiva-configuration/archiva-configuration-model/src/main/java/org/apache/archiva/configuration/model/ConfigurationModel.java +++ b/archiva-modules/archiva-base/archiva-configuration/archiva-configuration-model/src/main/java/org/apache/archiva/configuration/model/ConfigurationModel.java @@ -18,6 +18,8 @@ package org.apache.archiva.configuration.model; */ /** + * Marker interface that is used for configuration model classes. + * * @author Martin Schreier */ public interface ConfigurationModel diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/Repository.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/Repository.java index ac20ed536..323b071ef 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/Repository.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/Repository.java @@ -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 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 */ - > RepositoryFeature getFeature(Class clazz) throws UnsupportedFeatureException; + > T getFeature( Class clazz) throws UnsupportedFeatureException; /** diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/AbstractRepository.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/AbstractRepository.java index b01efafd7..694b36dd1 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/AbstractRepository.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/base/AbstractRepository.java @@ -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 > RepositoryFeature getFeature( Class clazz ) throws UnsupportedFeatureException + public > T getFeature( Class clazz ) throws UnsupportedFeatureException { if (featureMap.containsKey( clazz )) { - return (RepositoryFeature) featureMap.get(clazz); + return (T) featureMap.get(clazz); } else { throw new UnsupportedFeatureException( "Feature " + clazz + " not supported" ); diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/maven/repository/MavenManagedRepository.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/maven/repository/MavenManagedRepository.java index 5e81f868a..388c69cea 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/maven/repository/MavenManagedRepository.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/maven/repository/MavenManagedRepository.java @@ -96,15 +96,15 @@ public class MavenManagedRepository extends AbstractManagedRepository @SuppressWarnings( "unchecked" ) @Override - public > RepositoryFeature getFeature( Class clazz ) throws UnsupportedFeatureException + public > T getFeature( Class clazz ) throws UnsupportedFeatureException { if (ArtifactCleanupFeature.class.equals( clazz )) { - return (RepositoryFeature) artifactCleanupFeature; + return (T) artifactCleanupFeature; } else if (IndexCreationFeature.class.equals(clazz)) { - return (RepositoryFeature) indexCreationFeature; + return (T) indexCreationFeature; } else if (StagingRepositoryFeature.class.equals(clazz)) { - return (RepositoryFeature) stagingRepositoryFeature; + return (T) stagingRepositoryFeature; } else { throw new UnsupportedFeatureException( ); } diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/maven/repository/MavenRemoteRepository.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/maven/repository/MavenRemoteRepository.java index 1be681ece..95ad5a809 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/maven/repository/MavenRemoteRepository.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/maven/repository/MavenRemoteRepository.java @@ -91,12 +91,12 @@ public class MavenRemoteRepository extends AbstractRemoteRepository @SuppressWarnings( "unchecked" ) @Override - public > RepositoryFeature getFeature( Class clazz ) throws UnsupportedFeatureException + public > T getFeature( Class clazz ) throws UnsupportedFeatureException { if (RemoteIndexFeature.class.equals( clazz )) { - return (RepositoryFeature) remoteIndexFeature; + return (T) remoteIndexFeature; } else if (IndexCreationFeature.class.equals(clazz)) { - return (RepositoryFeature) indexCreationFeature; + return (T) indexCreationFeature; } else { throw new UnsupportedFeatureException( ); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml index 3075d15be..eeed333a3 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml @@ -68,6 +68,10 @@ org.apache.archiva archiva-scheduler-indexing + + org.apache.archiva.configuration + archiva-configuration-model + org.apache.archiva diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/MavenManagedRepository.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/MavenManagedRepository.java index e82db108d..ba76efb85 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/MavenManagedRepository.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/MavenManagedRepository.java @@ -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; } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/MavenRepositoryMapper.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/MavenRepositoryMapper.java index d95dae4e3..305e8e798 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/MavenRepositoryMapper.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/MavenRepositoryMapper.java @@ -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 */ -public class MavenRepositoryMapper implements RestMapper +@Service("mapper#managed_repository#maven") +public class MavenRepositoryMapper extends RestServiceMapper { + + 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 getSourceType( ) + public Class getBaseType( ) { return MavenManagedRepository.class; } @Override - public Class getTargetType( ) + public Class getDestinationType( ) { return ManagedRepositoryConfiguration.class; } @Override - public boolean supports( Class sourceType, Class targetType ) + public Class getReverseSourceType( ) { - return ( - sourceType.isAssignableFrom( getSourceType() ) && - targetType.isAssignableFrom( getTargetType() ) ); + return ManagedRepository.class; } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/RestMapper.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/RestServiceMapper.java similarity index 75% rename from archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/RestMapper.java rename to archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/RestServiceMapper.java index 36e6ee192..bccd8da21 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/RestMapper.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/RestServiceMapper.java @@ -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 */ -public interface RestMapper extends ModelMapper +public abstract class RestServiceMapper extends AbstractMapper + implements MultiModelMapper { } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/ServiceMapperFactory.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/ServiceMapperFactory.java index 7d9ee87a7..0a9b55d4c 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/ServiceMapperFactory.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/v2/model/map/ServiceMapperFactory.java @@ -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 */ +@SuppressWarnings( "unchecked" ) @Service("modelMapperFactory#rest") -public class ServiceMapperFactory implements ModelMapperFactory +public class ServiceMapperFactory implements ModelMapperFactory { + List modelMapperList; + @Inject - List modelMapperList; + public ServiceMapperFactory( List modelMapperList ) + { + this.modelMapperList = modelMapperList; + initMapper(); + } - Map, Map,ModelMapper>> modelMap; + Map> 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 sType = (Class) mapper.getSourceType( ); - Class tType = (Class) mapper.getTargetType( ); - Map, ModelMapper> tMap; - if (modelMap.containsKey( sType )) { - tMap = modelMap.get( sType ); - } else { - tMap = new HashMap<>( ); - } - tMap.put( tType, (ModelMapper) mapper ); + Integer key = mapper.hashCode( ); + modelMap.put( key, (MultiModelMapper) mapper ); } } @Override - public ModelMapper getMapper( Class sourceType, Class targetType ) throws IllegalArgumentException + public MultiModelMapper getMapper( Class baseType, Class destinationType, Class reverseSourceType ) throws IllegalArgumentException { - if (!modelMap.containsKey( sourceType )) { - throw new IllegalArgumentException( "No mapper defined for the given source type "+sourceType ); - } - Map, ModelMapper> tMap = modelMap.get( sourceType ); - if ( !tMap.containsKey( targetType ) ) - { - throw new IllegalArgumentException( "No mapper defined for the given target type "+targetType ); + Integer key = MultiModelMapper.getHash( baseType, destinationType, reverseSourceType ); + if (!modelMap.containsKey( key )) { + throw new IllegalArgumentException( "No mapper defined for the given source type "+baseType ); } - return (ModelMapper) tMap.get( targetType ); + return (MultiModelMapper) modelMap.get( key ); } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/resources/META-INF/spring-context.xml new file mode 100644 index 000000000..c8c8d6acf --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/resources/META-INF/spring-context.xml @@ -0,0 +1,33 @@ + + + + + + + + + diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/maven/DefaultMavenManagedRepositoryService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/maven/DefaultMavenManagedRepositoryService.java index f62fef2e0..5bec54dff 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/maven/DefaultMavenManagedRepositoryService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/maven/DefaultMavenManagedRepositoryService.java @@ -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 ) ); -- 2.39.5