org.mule.runtime.config.internal.ObjectProviderAwareBeanFactory Maven / Gradle / Ivy
/*
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.runtime.config.internal;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableMap;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import org.mule.runtime.api.exception.MuleRuntimeException;
import org.mule.runtime.api.ioc.ConfigurableObjectProvider;
import org.mule.runtime.api.ioc.ObjectProvider;
import org.mule.runtime.core.api.util.func.CheckedSupplier;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
/**
* {@link org.springframework.beans.factory.ListableBeanFactory} implementation that will resolve beans using a list of
* {@link ObjectProvider}s if it was not able to find a bean on itself.
*
* @since 4.0
*/
public class ObjectProviderAwareBeanFactory extends DefaultListableBeanFactory {
private List objectProviders = emptyList();
public ObjectProviderAwareBeanFactory(BeanFactory parentBeanFactory) {
super(parentBeanFactory);
}
public void setObjectProviders(List objectProviders) {
this.objectProviders = objectProviders;
}
@Override
public T getBean(Class requiredType) throws BeansException {
boolean doNotFail = false;
try {
T bean = super.getBean(requiredType);
if (bean == null) {
doNotFail = true;
throw new NoSuchBeanDefinitionException(requiredType);
}
return bean;
} catch (NoSuchBeanDefinitionException e) {
Optional