This is an automated email from the ASF dual-hosted git repository. ahuber pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push: new b58d9f6c26 ISIS-3167: test fixes: implem. bean lookup by id b58d9f6c26 is described below commit b58d9f6c268fcdb8107469fce6176dd1175dfbb0 Author: Andi Huber <ahu...@apache.org> AuthorDate: Wed Aug 31 10:20:24 2022 +0200 ISIS-3167: test fixes: implem. bean lookup by id --- .../applib/services/registry/ServiceRegistry.java | 5 ++- .../commons/internal/ioc/_ManagedBeanAdapter.java | 9 ++-- .../IsisBeanFactoryPostProcessorForSpring.java | 11 ++--- .../_testing/MetaModelContext_forTesting.java | 50 ++++++++++++++++++---- .../_testing/ServiceRegistry_forTesting.java | 24 +++++++++-- .../load/ObjectLoader_builtinHandlers.java | 8 ++-- .../services/registry/ServiceRegistryDefault.java | 5 ++- .../specimpl/dflt/ObjectSpecificationDefault.java | 30 +++++++------ .../factory/FactoryServiceDefault.java | 2 +- 9 files changed, 101 insertions(+), 43 deletions(-) diff --git a/api/applib/src/main/java/org/apache/isis/applib/services/registry/ServiceRegistry.java b/api/applib/src/main/java/org/apache/isis/applib/services/registry/ServiceRegistry.java index 3a469d6a70..5dc5ba01fe 100644 --- a/api/applib/src/main/java/org/apache/isis/applib/services/registry/ServiceRegistry.java +++ b/api/applib/src/main/java/org/apache/isis/applib/services/registry/ServiceRegistry.java @@ -26,6 +26,7 @@ import java.util.stream.Stream; import javax.annotation.Priority; +import org.apache.isis.applib.id.LogicalType; import org.apache.isis.commons.collections.Can; import org.apache.isis.commons.internal._Constants; import org.apache.isis.commons.internal.base._Reduction; @@ -83,14 +84,14 @@ public interface ServiceRegistry { * * @param id - corresponds to the ObjectSpecificationId of the bean's type */ - Optional<_ManagedBeanAdapter> lookupRegisteredBeanById(String id); + Optional<_ManagedBeanAdapter> lookupRegisteredBeanById(LogicalType id); /** * Returns a registered bean of given {@code name}, or throws when no such bean. * * @param id - corresponds to the ObjectSpecificationId of the bean's type */ - default _ManagedBeanAdapter lookupRegisteredBeanByIdElseFail(final String id) { + default _ManagedBeanAdapter lookupRegisteredBeanByIdElseFail(final LogicalType id) { return lookupRegisteredBeanById(id).orElseThrow( ()->_Exceptions.unrecoverable( "Failed to lookup BeanAdapter by id '" + id + "'")); diff --git a/commons/src/main/java/org/apache/isis/commons/internal/ioc/_ManagedBeanAdapter.java b/commons/src/main/java/org/apache/isis/commons/internal/ioc/_ManagedBeanAdapter.java index 8e29a5c60f..e04e9c8e17 100644 --- a/commons/src/main/java/org/apache/isis/commons/internal/ioc/_ManagedBeanAdapter.java +++ b/commons/src/main/java/org/apache/isis/commons/internal/ioc/_ManagedBeanAdapter.java @@ -35,13 +35,16 @@ public interface _ManagedBeanAdapter { // -- TEST FACTORIES - public static <T> _ManagedBeanAdapter forTestingLazy(Class<T> beanClass, Supplier<T> beanProvider) { + public static <T> _ManagedBeanAdapter forTestingLazy(final String logicalTypeName, final Class<T> beanClass, final Supplier<T> beanProvider) { + return _ManagedBeanAdapter_forTestingLazy.of(logicalTypeName, beanClass, beanProvider); + } + + public static <T> _ManagedBeanAdapter forTestingLazy(final Class<T> beanClass, final Supplier<T> beanProvider) { return _ManagedBeanAdapter_forTestingLazy.of(beanClass.getName(), beanClass, beanProvider); } - public static <T> _ManagedBeanAdapter forTesting(T bean) { + public static <T> _ManagedBeanAdapter forTesting(final T bean) { return _ManagedBeanAdapter_forTestingLazy.of(bean.getClass().getName(), bean.getClass(), ()->bean); } - } \ No newline at end of file diff --git a/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanFactoryPostProcessorForSpring.java b/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanFactoryPostProcessorForSpring.java index cc5ebb5b8d..194af48cd1 100644 --- a/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanFactoryPostProcessorForSpring.java +++ b/core/config/src/main/java/org/apache/isis/core/config/beans/IsisBeanFactoryPostProcessorForSpring.java @@ -100,19 +100,16 @@ implements isisComponentScanInterceptor.intercept(typeMetaData); - if(!typeMetaData.isVetoedForInjection()) { - + if(typeMetaData.isVetoedForInjection()) { + registry.removeBeanDefinition(beanDefinitionName); + log.debug("vetoing bean {}", beanDefinitionName); + } else { val beanNameOverride = typeMetaData.getBeanNameOverride(); if(_Strings.isNotEmpty(beanNameOverride)) { registry.removeBeanDefinition(beanDefinitionName); registry.registerBeanDefinition(beanNameOverride, beanDefinition); log.debug("renaming bean {} -> {}", beanDefinitionName, beanNameOverride); } - - - } else { - registry.removeBeanDefinition(beanDefinitionName); - log.debug("vetoing bean {}", beanDefinitionName); } } diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/_testing/MetaModelContext_forTesting.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/_testing/MetaModelContext_forTesting.java index b8665866a6..06b35a0a3a 100644 --- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/_testing/MetaModelContext_forTesting.java +++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/_testing/MetaModelContext_forTesting.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Stream; @@ -55,9 +56,9 @@ import org.apache.isis.applib.value.semantics.ValueSemanticsResolver; import org.apache.isis.commons.collections.Can; import org.apache.isis.commons.internal.base._Lazy; import org.apache.isis.commons.internal.base._NullSafe; -import org.apache.isis.commons.internal.base._Strings; import org.apache.isis.commons.internal.collections._Lists; import org.apache.isis.commons.internal.collections._Maps; +import org.apache.isis.commons.internal.collections._Sets; import org.apache.isis.commons.internal.collections._Streams; import org.apache.isis.commons.internal.exceptions._Exceptions; import org.apache.isis.commons.internal.ioc._ManagedBeanAdapter; @@ -92,6 +93,7 @@ import org.apache.isis.core.metamodel.services.grid.bootstrap.GridSystemServiceB import org.apache.isis.core.metamodel.services.layout.LayoutServiceDefault; import org.apache.isis.core.metamodel.services.message.MessageServiceNoop; import org.apache.isis.core.metamodel.services.title.TitleServiceDefault; +import org.apache.isis.core.metamodel.spec.ObjectSpecification; import org.apache.isis.core.metamodel.specloader.SpecificationLoader; import org.apache.isis.core.metamodel.specloader.SpecificationLoaderDefault; import org.apache.isis.core.metamodel.valuesemantics.BigDecimalValueSemantics; @@ -245,6 +247,7 @@ implements MetaModelContext { return _Streams.concat( streamSingletons().map(_ManagedBeanAdapter::forTesting), singletonProviders.stream(), + discoveredServices.stream(), Stream.of( // support for lazy bean providers, _ManagedBeanAdapter.forTestingLazy(GridLoaderService.class, this::getGridLoaderService), @@ -491,18 +494,49 @@ implements MetaModelContext { objectAdaptersForBeansOfKnownSort.clear(); } + @lombok.Value(staticConstructor = "of") + static class ServiceInstance { + final ObjectSpecification specification; + final Object pojo; + } + + @Builder.Default + private final Set<_ManagedBeanAdapter> discoveredServices = _Sets.newHashSet(); + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private void registerAsService(final ServiceInstance serviceInstance) { + val spec = serviceInstance.getSpecification(); + discoveredServices.add(_ManagedBeanAdapter.forTestingLazy( + spec.getLogicalTypeName(), + (Class)spec.getCorrespondingClass(), + serviceInstance::getPojo)); + } + private final Map<String, ManagedObject> collectBeansOfKnownSort() { + val map = _Maps.<String, ManagedObject>newLinkedHashMap(); - getServiceRegistry() + + // first pass: introspect them all + val services = getServiceRegistry() .streamRegisteredBeans() - .filter(bean->_Strings.isNotEmpty(bean.getId())) - .forEach(bean-> - toManagedObject(bean) - .ifPresent(serviceAdapter->map.put(bean.getId(), serviceAdapter))); + .map(this::toServiceInstance) + .map(op->op.orElse(null)) + .filter(_NullSafe::isPresent) + .peek(this::registerAsService) + .collect(Can.<ServiceInstance>toCan()); + + // reload registered beans + ((ServiceRegistry_forTesting)getServiceRegistry()).invalidateRegisteredBeans(); + + // second pass: adapt service objects + services.stream() + .map(service->ManagedObject.service(service.specification, service.pojo)) + .forEach(serviceAdapter-> + map.put(serviceAdapter.getSpecification().getLogicalTypeName(), serviceAdapter)); return map; } - private final Optional<ManagedObject> toManagedObject(final _ManagedBeanAdapter managedBeanAdapter) { + private final Optional<ServiceInstance> toServiceInstance(final _ManagedBeanAdapter managedBeanAdapter) { val servicePojo = managedBeanAdapter.getInstance().getFirst() .orElseThrow(()->_Exceptions.unrecoverable( "Cannot get service instance of type '%s'", @@ -513,7 +547,7 @@ implements MetaModelContext { } return getSpecificationLoader() .specForType(servicePojo.getClass()) - .map(serviceSpec->ManagedObject.service(serviceSpec, servicePojo)); + .map(serviceSpec->ServiceInstance.of(serviceSpec, servicePojo)); } // -- RECURSIVE INITIALIZATION FIX diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/_testing/ServiceRegistry_forTesting.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/_testing/ServiceRegistry_forTesting.java index a03700b642..a15f471f4a 100644 --- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/_testing/ServiceRegistry_forTesting.java +++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/_testing/ServiceRegistry_forTesting.java @@ -19,15 +19,20 @@ package org.apache.isis.core.metamodel._testing; import java.lang.annotation.Annotation; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Stream; +import org.apache.isis.applib.id.LogicalType; import org.apache.isis.applib.services.registry.ServiceRegistry; import org.apache.isis.commons.collections.Can; +import org.apache.isis.commons.internal.assertions._Assert; import org.apache.isis.commons.internal.base._Casts; import org.apache.isis.commons.internal.base._NullSafe; +import org.apache.isis.commons.internal.base._Strings; +import org.apache.isis.commons.internal.collections._Maps; import org.apache.isis.commons.internal.collections._Sets; import org.apache.isis.commons.internal.context._Context; import org.apache.isis.commons.internal.exceptions._Exceptions; @@ -81,14 +86,16 @@ class ServiceRegistry_forTesting implements ServiceRegistry { return Can.empty(); } + private final Map<String, _ManagedBeanAdapter> registeredBeanById = _Maps.newHashMap(); + @Override public Stream<_ManagedBeanAdapter> streamRegisteredBeans() { return registeredBeans().stream(); } @Override - public Optional<_ManagedBeanAdapter> lookupRegisteredBeanById(final String id) { - throw _Exceptions.notImplemented(); + public Optional<_ManagedBeanAdapter> lookupRegisteredBeanById(final LogicalType id) { + return Optional.ofNullable(registeredBeanById.get(id.getLogicalTypeName())); } @Override @@ -96,6 +103,13 @@ class ServiceRegistry_forTesting implements ServiceRegistry { throw _Exceptions.notImplemented(); } + void invalidateRegisteredBeans() { + synchronized(registeredBeans) { + registeredBeans.clear(); + } + streamRegisteredBeans().count(); + } + // -- HELPER private Set<_ManagedBeanAdapter> registeredBeans() { @@ -106,7 +120,11 @@ class ServiceRegistry_forTesting implements ServiceRegistry { if(registeredBeans.isEmpty()) { streamBeans() .filter(_NullSafe::isPresent) - .forEach(registeredBeans::add); + .peek(bean->_Assert.assertTrue(_Strings.isNotEmpty(bean.getId()))) + .forEach(bean->{ + registeredBeans.add(bean); + registeredBeanById.put(bean.getId(), bean); + }); triggerPostInit.set(true); } } diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/objectmanager/load/ObjectLoader_builtinHandlers.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/objectmanager/load/ObjectLoader_builtinHandlers.java index 6c54fcc56f..595cdccc1a 100644 --- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/objectmanager/load/ObjectLoader_builtinHandlers.java +++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/objectmanager/load/ObjectLoader_builtinHandlers.java @@ -89,15 +89,15 @@ final class ObjectLoader_builtinHandlers { public ManagedObject handle(final ObjectLoader.Request objectLoadRequest) { val spec = objectLoadRequest.getObjectSpecification(); - val logicalTypeName = spec.getLogicalTypeName(); + val logicalType = spec.getLogicalType(); val servicePojo = metaModelContext.getServiceRegistry() - .lookupRegisteredBeanById(logicalTypeName) + .lookupRegisteredBeanById(logicalType) .map(_ManagedBeanAdapter::getInstance) .flatMap(Can::getFirst) .orElseThrow(()->_Exceptions.noSuchElement( - "loader: %s loading beanName %s", - this.getClass().getName(), logicalTypeName)); + "loader: %s loading logicalType %s", + this.getClass().getName(), logicalType)); return ManagedObject.of(spec, servicePojo); } diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/registry/ServiceRegistryDefault.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/registry/ServiceRegistryDefault.java index 9979c4089b..7bb8ec18a9 100644 --- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/registry/ServiceRegistryDefault.java +++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/registry/ServiceRegistryDefault.java @@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.apache.isis.applib.annotation.PriorityPrecedence; +import org.apache.isis.applib.id.LogicalType; import org.apache.isis.applib.services.registry.ServiceRegistry; import org.apache.isis.commons.collections.Can; import org.apache.isis.commons.internal.base._Lazy; @@ -54,8 +55,8 @@ public final class ServiceRegistryDefault implements ServiceRegistry { @Inject private IsisBeanTypeRegistry isisBeanTypeRegistry; @Override - public Optional<_ManagedBeanAdapter> lookupRegisteredBeanById(final String id) { - return Optional.ofNullable(contributingDomainServicesById.get().get(id)); + public Optional<_ManagedBeanAdapter> lookupRegisteredBeanById(final LogicalType id) { + return Optional.ofNullable(contributingDomainServicesById.get().get(id.getLogicalTypeName())); } @Override diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/specloader/specimpl/dflt/ObjectSpecificationDefault.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/specloader/specimpl/dflt/ObjectSpecificationDefault.java index e7d6e74589..d2e2d0ccf6 100644 --- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/specloader/specimpl/dflt/ObjectSpecificationDefault.java +++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/specloader/specimpl/dflt/ObjectSpecificationDefault.java @@ -101,17 +101,7 @@ implements FacetHolder { typeMeta.getLogicalType().getLogicalTypeSimpleName(), typeMeta.getBeanSort(), facetProcessor, postProcessor); - this.injectable = !typeMeta.getManagedBy().isVetoedForInjection() - && !typeMeta.getBeanSort().isAbstract() - && !typeMeta.getBeanSort().isValue() - && !typeMeta.getBeanSort().isEntity() - && !typeMeta.getBeanSort().isViewModel() - && !typeMeta.getBeanSort().isMixin() - && (typeMeta.getBeanSort().isManagedBeanAny() - || mmc.getServiceRegistry() - .lookupRegisteredBeanById(typeMeta.getLogicalType().getLogicalTypeName()) - .isPresent()); - + this.isVetoedForInjection = typeMeta.getManagedBy().isVetoedForInjection(); this.classSubstitutorRegistry = classSubstitutorRegistry; // must install EncapsulationFacet (if any) and MemberAnnotationPolicyFacet (if any) @@ -131,8 +121,22 @@ implements FacetHolder { new FacetedMethodsBuilder(this, facetProcessor, classSubstitutorRegistry); } - @Getter(onMethod_ = {@Override}) - private final boolean injectable; + private boolean isVetoedForInjection; + + @Override + public boolean isInjectable() { + return !isVetoedForInjection + && !getBeanSort().isAbstract() + && !getBeanSort().isValue() + && !getBeanSort().isEntity() + && !getBeanSort().isViewModel() + && !getBeanSort().isMixin() + && (getBeanSort().isManagedBeanAny() + //|| typeMeta.getBeanSort().isUnknown()); + || getServiceRegistry() + .lookupRegisteredBeanById(getLogicalType()) + .isPresent()); + } @Override protected void introspectTypeHierarchy() { diff --git a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/factory/FactoryServiceDefault.java b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/factory/FactoryServiceDefault.java index 1869fc0131..0211ddb2c6 100644 --- a/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/factory/FactoryServiceDefault.java +++ b/core/runtimeservices/src/main/java/org/apache/isis/core/runtimeservices/factory/FactoryServiceDefault.java @@ -75,7 +75,7 @@ public class FactoryServiceDefault implements FactoryService { public <T> T get(final @NonNull Class<T> requiredType) { return isisSystemEnvironment.getIocContainer() .get(requiredType) - .orElseThrow(()->_Exceptions.noSuchElement("no an injectable type %s", requiredType)); + .orElseThrow(()->_Exceptions.noSuchElement("not an injectable type %s", requiredType)); } @Override