Search in sources :

Example 6 with BeansException

use of cn.taketoday.beans.BeansException in project today-framework by TAKETODAY.

the class GenericFilterBean method init.

/**
 * Standard way of initializing this filter.
 * Map config parameters onto bean properties of this filter, and
 * invoke subclass initialization.
 *
 * @param filterConfig the configuration for this filter
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 * @see #initFilterBean
 */
@Override
public final void init(FilterConfig filterConfig) throws ServletException {
    Assert.notNull(filterConfig, "FilterConfig must not be null");
    this.filterConfig = filterConfig;
    // Set bean properties from init parameters.
    PropertyValues pvs = getFilterConfigPropertyValues(filterConfig, requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = BeanWrapper.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
            Environment env = this.environment;
            if (env == null) {
                env = new StandardServletEnvironment();
            }
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, env));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage();
            logger.error(msg, ex);
            throw new NestedServletException(msg, ex);
        }
    }
    // Let subclasses do whatever initialization they like.
    initFilterBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Filter '{}' configured for use", filterConfig.getFilterName());
    }
}
Also used : ResourceLoader(cn.taketoday.core.io.ResourceLoader) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) NestedServletException(cn.taketoday.web.servlet.NestedServletException) StandardServletEnvironment(cn.taketoday.web.context.support.StandardServletEnvironment) Environment(cn.taketoday.core.env.Environment) ResourceEditor(cn.taketoday.core.io.ResourceEditor) StandardServletEnvironment(cn.taketoday.web.context.support.StandardServletEnvironment) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) BeansException(cn.taketoday.beans.BeansException)

Example 7 with BeansException

use of cn.taketoday.beans.BeansException in project today-framework by TAKETODAY.

the class TestContextTransactionUtils method retrieveTransactionManager.

/**
 * Retrieve the {@linkplain PlatformTransactionManager transaction manager}
 * to use for the supplied {@linkplain TestContext test context}.
 * <p>The following algorithm is used to retrieve the transaction manager
 * from the {@link cn.taketoday.context.ApplicationContext ApplicationContext}
 * of the supplied test context:
 * <ol>
 * <li>Look up the transaction manager by type and explicit name, if the supplied
 * {@code name} is non-empty, throwing a {@link BeansException} if the named
 * transaction manager does not exist.
 * <li>Attempt to look up the transaction manager via a
 * {@link TransactionManagementConfigurer}, if present.
 * <li>Attempt to look up the single transaction manager by type.
 * <li>Attempt to look up the <em>primary</em> transaction manager by type.
 * <li>Attempt to look up the transaction manager by type and the
 * {@linkplain #DEFAULT_TRANSACTION_MANAGER_NAME default transaction manager
 * name}.
 * </ol>
 *
 * @param testContext the test context for which the transaction manager
 * should be retrieved; never {@code null}
 * @param name the name of the transaction manager to retrieve
 * (may be {@code null} or <em>empty</em>)
 * @return the transaction manager to use, or {@code null} if not found
 * @throws BeansException if an error occurs while retrieving an explicitly
 * named transaction manager
 * @throws IllegalStateException if more than one TransactionManagementConfigurer
 * exists in the ApplicationContext
 */
@Nullable
public static PlatformTransactionManager retrieveTransactionManager(TestContext testContext, @Nullable String name) {
    Assert.notNull(testContext, "TestContext must not be null");
    BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
    try {
        // Look up by type and explicit name
        if (StringUtils.hasText(name)) {
            return bf.getBean(name, PlatformTransactionManager.class);
        }
    } catch (BeansException ex) {
        logger.error("Failed to retrieve transaction manager named '{}' for test context {}", name, testContext, ex);
        throw ex;
    }
    try {
        // Look up single TransactionManagementConfigurer
        Map<String, TransactionManagementConfigurer> configurers = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, TransactionManagementConfigurer.class);
        Assert.state(configurers.size() <= 1, "Only one TransactionManagementConfigurer may exist in the ApplicationContext");
        if (configurers.size() == 1) {
            TransactionManager tm = configurers.values().iterator().next().annotationDrivenTransactionManager();
            Assert.state(tm instanceof PlatformTransactionManager, () -> "Transaction manager specified via TransactionManagementConfigurer " + "is not a PlatformTransactionManager: " + tm);
            return (PlatformTransactionManager) tm;
        }
        // Look up single bean by type
        Map<String, PlatformTransactionManager> txMgrs = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, PlatformTransactionManager.class);
        if (txMgrs.size() == 1) {
            return txMgrs.values().iterator().next();
        }
        try {
            // Look up single bean by type, with support for 'primary' beans
            return bf.getBean(PlatformTransactionManager.class);
        } catch (BeansException ex) {
            logBeansException(testContext, ex, PlatformTransactionManager.class);
        }
        // look up by type and default name
        return bf.getBean(DEFAULT_TRANSACTION_MANAGER_NAME, PlatformTransactionManager.class);
    } catch (BeansException ex) {
        logBeansException(testContext, ex, PlatformTransactionManager.class);
        return null;
    }
}
Also used : TransactionManager(cn.taketoday.transaction.TransactionManager) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) BeanFactory(cn.taketoday.beans.factory.BeanFactory) TransactionManagementConfigurer(cn.taketoday.transaction.annotation.TransactionManagementConfigurer) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) BeansException(cn.taketoday.beans.BeansException) Nullable(cn.taketoday.lang.Nullable)

Example 8 with BeansException

use of cn.taketoday.beans.BeansException in project today-infrastructure by TAKETODAY.

the class GenericFilterBean method init.

/**
 * Standard way of initializing this filter.
 * Map config parameters onto bean properties of this filter, and
 * invoke subclass initialization.
 *
 * @param filterConfig the configuration for this filter
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 * @see #initFilterBean
 */
@Override
public final void init(FilterConfig filterConfig) throws ServletException {
    Assert.notNull(filterConfig, "FilterConfig must not be null");
    this.filterConfig = filterConfig;
    // Set bean properties from init parameters.
    PropertyValues pvs = getFilterConfigPropertyValues(filterConfig, requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = BeanWrapper.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
            Environment env = this.environment;
            if (env == null) {
                env = new StandardServletEnvironment();
            }
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, env));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage();
            logger.error(msg, ex);
            throw new NestedServletException(msg, ex);
        }
    }
    // Let subclasses do whatever initialization they like.
    initFilterBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Filter '{}' configured for use", filterConfig.getFilterName());
    }
}
Also used : ResourceLoader(cn.taketoday.core.io.ResourceLoader) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) NestedServletException(cn.taketoday.web.servlet.NestedServletException) StandardServletEnvironment(cn.taketoday.web.context.support.StandardServletEnvironment) Environment(cn.taketoday.core.env.Environment) ResourceEditor(cn.taketoday.core.io.ResourceEditor) StandardServletEnvironment(cn.taketoday.web.context.support.StandardServletEnvironment) ServletContextResourceLoader(cn.taketoday.web.context.support.ServletContextResourceLoader) BeansException(cn.taketoday.beans.BeansException)

Example 9 with BeansException

use of cn.taketoday.beans.BeansException in project today-infrastructure by TAKETODAY.

the class XmlListableBeanFactoryTests method setup.

@BeforeEach
public void setup() {
    parent = new StandardBeanFactory();
    Map map = new HashMap();
    map.put("name", "Albert");
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setPropertyValues(new PropertyValues(map));
    parent.registerBeanDefinition("father", bd1);
    map = new HashMap();
    map.put("name", "Roderick");
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setPropertyValues(new PropertyValues(map));
    parent.registerBeanDefinition("rod", bd2);
    this.factory = new StandardBeanFactory(parent);
    new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(new ClassPathResource("test.xml", getClass()));
    this.factory.addBeanPostProcessor(new InitializationBeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
            if (bean instanceof TestBean) {
                ((TestBean) bean).setPostProcessed(true);
            }
            if (bean instanceof DummyFactory) {
                ((DummyFactory) bean).setPostProcessed(true);
            }
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
            return bean;
        }
    });
    this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
    this.factory.addBeanPostProcessor(new ProtectedLifecycleBean.PostProcessor());
// this.factory.preInstantiateSingletons();
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) HashMap(java.util.HashMap) InitializationBeanPostProcessor(cn.taketoday.beans.factory.InitializationBeanPostProcessor) ClassPathResource(cn.taketoday.core.io.ClassPathResource) DummyFactory(cn.taketoday.beans.testfixture.beans.factory.DummyFactory) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) LifecycleBean(cn.taketoday.beans.testfixture.beans.LifecycleBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) HashMap(java.util.HashMap) Map(java.util.Map) BeansException(cn.taketoday.beans.BeansException) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with BeansException

use of cn.taketoday.beans.BeansException in project today-infrastructure by TAKETODAY.

the class TestContextTransactionUtils method retrieveTransactionManager.

/**
 * Retrieve the {@linkplain PlatformTransactionManager transaction manager}
 * to use for the supplied {@linkplain TestContext test context}.
 * <p>The following algorithm is used to retrieve the transaction manager
 * from the {@link cn.taketoday.context.ApplicationContext ApplicationContext}
 * of the supplied test context:
 * <ol>
 * <li>Look up the transaction manager by type and explicit name, if the supplied
 * {@code name} is non-empty, throwing a {@link BeansException} if the named
 * transaction manager does not exist.
 * <li>Attempt to look up the transaction manager via a
 * {@link TransactionManagementConfigurer}, if present.
 * <li>Attempt to look up the single transaction manager by type.
 * <li>Attempt to look up the <em>primary</em> transaction manager by type.
 * <li>Attempt to look up the transaction manager by type and the
 * {@linkplain #DEFAULT_TRANSACTION_MANAGER_NAME default transaction manager
 * name}.
 * </ol>
 *
 * @param testContext the test context for which the transaction manager
 * should be retrieved; never {@code null}
 * @param name the name of the transaction manager to retrieve
 * (may be {@code null} or <em>empty</em>)
 * @return the transaction manager to use, or {@code null} if not found
 * @throws BeansException if an error occurs while retrieving an explicitly
 * named transaction manager
 * @throws IllegalStateException if more than one TransactionManagementConfigurer
 * exists in the ApplicationContext
 */
@Nullable
public static PlatformTransactionManager retrieveTransactionManager(TestContext testContext, @Nullable String name) {
    Assert.notNull(testContext, "TestContext must not be null");
    BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
    try {
        // Look up by type and explicit name
        if (StringUtils.hasText(name)) {
            return bf.getBean(name, PlatformTransactionManager.class);
        }
    } catch (BeansException ex) {
        logger.error("Failed to retrieve transaction manager named '{}' for test context {}", name, testContext, ex);
        throw ex;
    }
    try {
        // Look up single TransactionManagementConfigurer
        Map<String, TransactionManagementConfigurer> configurers = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, TransactionManagementConfigurer.class);
        Assert.state(configurers.size() <= 1, "Only one TransactionManagementConfigurer may exist in the ApplicationContext");
        if (configurers.size() == 1) {
            TransactionManager tm = configurers.values().iterator().next().annotationDrivenTransactionManager();
            Assert.state(tm instanceof PlatformTransactionManager, () -> "Transaction manager specified via TransactionManagementConfigurer " + "is not a PlatformTransactionManager: " + tm);
            return (PlatformTransactionManager) tm;
        }
        // Look up single bean by type
        Map<String, PlatformTransactionManager> txMgrs = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, PlatformTransactionManager.class);
        if (txMgrs.size() == 1) {
            return txMgrs.values().iterator().next();
        }
        try {
            // Look up single bean by type, with support for 'primary' beans
            return bf.getBean(PlatformTransactionManager.class);
        } catch (BeansException ex) {
            logBeansException(testContext, ex, PlatformTransactionManager.class);
        }
        // look up by type and default name
        return bf.getBean(DEFAULT_TRANSACTION_MANAGER_NAME, PlatformTransactionManager.class);
    } catch (BeansException ex) {
        logBeansException(testContext, ex, PlatformTransactionManager.class);
        return null;
    }
}
Also used : TransactionManager(cn.taketoday.transaction.TransactionManager) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) BeanFactory(cn.taketoday.beans.factory.BeanFactory) TransactionManagementConfigurer(cn.taketoday.transaction.annotation.TransactionManagementConfigurer) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager) BeansException(cn.taketoday.beans.BeansException) Nullable(cn.taketoday.lang.Nullable)

Aggregations

BeansException (cn.taketoday.beans.BeansException)11 PropertyValues (cn.taketoday.beans.PropertyValues)4 BeanFactory (cn.taketoday.beans.factory.BeanFactory)4 Nullable (cn.taketoday.lang.Nullable)4 PlatformTransactionManager (cn.taketoday.transaction.PlatformTransactionManager)4 TypeConverter (cn.taketoday.beans.TypeConverter)3 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)3 InjectionPoint (cn.taketoday.beans.factory.InjectionPoint)3 UnsatisfiedDependencyException (cn.taketoday.beans.factory.UnsatisfiedDependencyException)3 ValueHolder (cn.taketoday.beans.factory.config.ConstructorArgumentValues.ValueHolder)3 MethodParameter (cn.taketoday.core.MethodParameter)3 Constructor (java.lang.reflect.Constructor)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 BeanWrapper (cn.taketoday.beans.BeanWrapper)2 InitializationBeanPostProcessor (cn.taketoday.beans.factory.InitializationBeanPostProcessor)2 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)2 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)2 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 LifecycleBean (cn.taketoday.beans.testfixture.beans.LifecycleBean)2