Search in sources :

Example 1 with BeanMetadataElement

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

the class DatabasePopulatorConfigUtils method createDatabasePopulator.

private static BeanDefinition createDatabasePopulator(Element element, List<Element> scripts, String execution) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CompositeDatabasePopulator.class);
    boolean ignoreFailedDrops = element.getAttribute("ignore-failures").equals("DROPS");
    boolean continueOnError = element.getAttribute("ignore-failures").equals("ALL");
    ManagedList<BeanMetadataElement> delegates = new ManagedList<>();
    for (Element scriptElement : scripts) {
        String executionAttr = scriptElement.getAttribute("execution");
        if (!StringUtils.hasText(executionAttr)) {
            executionAttr = "INIT";
        }
        if (!execution.equals(executionAttr)) {
            continue;
        }
        BeanDefinitionBuilder delegate = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class);
        delegate.addPropertyValue("ignoreFailedDrops", ignoreFailedDrops);
        delegate.addPropertyValue("continueOnError", continueOnError);
        // Use a factory bean for the resources so they can be given an order if a pattern is used
        BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder.genericBeanDefinition(SortedResourcesFactoryBean.class);
        resourcesFactory.addConstructorArgValue(new TypedStringValue(scriptElement.getAttribute("location")));
        delegate.addPropertyValue("scripts", resourcesFactory.getBeanDefinition());
        if (StringUtils.isNotEmpty(scriptElement.getAttribute("encoding"))) {
            delegate.addPropertyValue("sqlScriptEncoding", new TypedStringValue(scriptElement.getAttribute("encoding")));
        }
        String separator = getSeparator(element, scriptElement);
        if (separator != null) {
            delegate.addPropertyValue("separator", new TypedStringValue(separator));
        }
        delegates.add(delegate.getBeanDefinition());
    }
    builder.addPropertyValue("populators", delegates);
    return builder.getBeanDefinition();
}
Also used : BeanMetadataElement(cn.taketoday.beans.BeanMetadataElement) BeanDefinitionBuilder(cn.taketoday.beans.factory.support.BeanDefinitionBuilder) Element(org.w3c.dom.Element) BeanMetadataElement(cn.taketoday.beans.BeanMetadataElement) ManagedList(cn.taketoday.beans.factory.support.ManagedList) TypedStringValue(cn.taketoday.beans.factory.config.TypedStringValue)

Example 2 with BeanMetadataElement

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

the class ConstructorResolver method resolvePreparedArguments.

/**
 * Resolve the prepared arguments stored in the given bean definition.
 */
private Object[] resolvePreparedArguments(String beanName, BeanDefinition merged, Executable executable, Object[] argsToResolve, BeanWrapper bw) {
    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = customConverter != null ? customConverter : bw;
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(beanFactory, beanName, merged, converter);
    Class<?>[] paramTypes = executable.getParameterTypes();
    Object[] resolvedArgs = new Object[argsToResolve.length];
    for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
        Object argValue = argsToResolve[argIndex];
        MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
        if (argValue == autowiredArgumentMarker) {
            argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, true);
        } else if (argValue instanceof BeanMetadataElement) {
            argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
        } else if (argValue instanceof String) {
            argValue = beanFactory.evaluateBeanDefinitionString((String) argValue, merged);
        }
        Class<?> paramType = paramTypes[argIndex];
        try {
            resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
        } catch (TypeMismatchException ex) {
            throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue) + "] to required type [" + paramType.getName() + "]: " + ex.getMessage());
        }
    }
    return resolvedArgs;
}
Also used : InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeConverter(cn.taketoday.beans.TypeConverter) BeanMetadataElement(cn.taketoday.beans.BeanMetadataElement) UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) MethodParameter(cn.taketoday.core.MethodParameter)

Example 3 with BeanMetadataElement

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

the class DatabasePopulatorConfigUtils method createDatabasePopulator.

private static BeanDefinition createDatabasePopulator(Element element, List<Element> scripts, String execution) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CompositeDatabasePopulator.class);
    boolean ignoreFailedDrops = element.getAttribute("ignore-failures").equals("DROPS");
    boolean continueOnError = element.getAttribute("ignore-failures").equals("ALL");
    ManagedList<BeanMetadataElement> delegates = new ManagedList<>();
    for (Element scriptElement : scripts) {
        String executionAttr = scriptElement.getAttribute("execution");
        if (!StringUtils.hasText(executionAttr)) {
            executionAttr = "INIT";
        }
        if (!execution.equals(executionAttr)) {
            continue;
        }
        BeanDefinitionBuilder delegate = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class);
        delegate.addPropertyValue("ignoreFailedDrops", ignoreFailedDrops);
        delegate.addPropertyValue("continueOnError", continueOnError);
        // Use a factory bean for the resources so they can be given an order if a pattern is used
        BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder.genericBeanDefinition(SortedResourcesFactoryBean.class);
        resourcesFactory.addConstructorArgValue(new TypedStringValue(scriptElement.getAttribute("location")));
        delegate.addPropertyValue("scripts", resourcesFactory.getBeanDefinition());
        if (StringUtils.isNotEmpty(scriptElement.getAttribute("encoding"))) {
            delegate.addPropertyValue("sqlScriptEncoding", new TypedStringValue(scriptElement.getAttribute("encoding")));
        }
        String separator = getSeparator(element, scriptElement);
        if (separator != null) {
            delegate.addPropertyValue("separator", new TypedStringValue(separator));
        }
        delegates.add(delegate.getBeanDefinition());
    }
    builder.addPropertyValue("populators", delegates);
    return builder.getBeanDefinition();
}
Also used : BeanMetadataElement(cn.taketoday.beans.BeanMetadataElement) BeanDefinitionBuilder(cn.taketoday.beans.factory.support.BeanDefinitionBuilder) Element(org.w3c.dom.Element) BeanMetadataElement(cn.taketoday.beans.BeanMetadataElement) ManagedList(cn.taketoday.beans.factory.support.ManagedList) TypedStringValue(cn.taketoday.beans.factory.config.TypedStringValue)

Example 4 with BeanMetadataElement

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

the class ConstructorResolver method resolvePreparedArguments.

/**
 * Resolve the prepared arguments stored in the given bean definition.
 */
private Object[] resolvePreparedArguments(String beanName, BeanDefinition merged, Executable executable, Object[] argsToResolve, BeanWrapper bw) {
    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = customConverter != null ? customConverter : bw;
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(beanFactory, beanName, merged, converter);
    Class<?>[] paramTypes = executable.getParameterTypes();
    Object[] resolvedArgs = new Object[argsToResolve.length];
    for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
        Object argValue = argsToResolve[argIndex];
        MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
        if (argValue == autowiredArgumentMarker) {
            argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, true);
        } else if (argValue instanceof BeanMetadataElement) {
            argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
        } else if (argValue instanceof String) {
            argValue = beanFactory.evaluateBeanDefinitionString((String) argValue, merged);
        }
        Class<?> paramType = paramTypes[argIndex];
        try {
            resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
        } catch (TypeMismatchException ex) {
            throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue) + "] to required type [" + paramType.getName() + "]: " + ex.getMessage());
        }
    }
    return resolvedArgs;
}
Also used : InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeConverter(cn.taketoday.beans.TypeConverter) BeanMetadataElement(cn.taketoday.beans.BeanMetadataElement) UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) MethodParameter(cn.taketoday.core.MethodParameter)

Aggregations

BeanMetadataElement (cn.taketoday.beans.BeanMetadataElement)4 TypeConverter (cn.taketoday.beans.TypeConverter)2 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)2 InjectionPoint (cn.taketoday.beans.factory.InjectionPoint)2 UnsatisfiedDependencyException (cn.taketoday.beans.factory.UnsatisfiedDependencyException)2 TypedStringValue (cn.taketoday.beans.factory.config.TypedStringValue)2 BeanDefinitionBuilder (cn.taketoday.beans.factory.support.BeanDefinitionBuilder)2 ManagedList (cn.taketoday.beans.factory.support.ManagedList)2 MethodParameter (cn.taketoday.core.MethodParameter)2 Element (org.w3c.dom.Element)2