Search in sources :

Example 1 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-framework by spring-projects.

the class SimpleConstructorNamespaceHandler method decorate.

@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
    if (node instanceof Attr) {
        Attr attr = (Attr) node;
        String argName = StringUtils.trimWhitespace(parserContext.getDelegate().getLocalName(attr));
        String argValue = StringUtils.trimWhitespace(attr.getValue());
        ConstructorArgumentValues cvs = definition.getBeanDefinition().getConstructorArgumentValues();
        boolean ref = false;
        // handle -ref arguments
        if (argName.endsWith(REF_SUFFIX)) {
            ref = true;
            argName = argName.substring(0, argName.length() - REF_SUFFIX.length());
        }
        ValueHolder valueHolder = new ValueHolder(ref ? new RuntimeBeanReference(argValue) : argValue);
        valueHolder.setSource(parserContext.getReaderContext().extractSource(attr));
        // handle "escaped"/"_" arguments
        if (argName.startsWith(DELIMITER_PREFIX)) {
            String arg = argName.substring(1).trim();
            // fast default check
            if (!StringUtils.hasText(arg)) {
                cvs.addGenericArgumentValue(valueHolder);
            } else // assume an index otherwise
            {
                int index = -1;
                try {
                    index = Integer.parseInt(arg);
                } catch (NumberFormatException ex) {
                    parserContext.getReaderContext().error("Constructor argument '" + argName + "' specifies an invalid integer", attr);
                }
                if (index < 0) {
                    parserContext.getReaderContext().error("Constructor argument '" + argName + "' specifies a negative index", attr);
                }
                if (cvs.hasIndexedArgumentValue(index)) {
                    parserContext.getReaderContext().error("Constructor argument '" + argName + "' with index " + index + " already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr);
                }
                cvs.addIndexedArgumentValue(index, valueHolder);
            }
        } else // no escaping -> ctr name
        {
            String name = Conventions.attributeNameToPropertyName(argName);
            if (containsArgWithName(name, cvs)) {
                parserContext.getReaderContext().error("Constructor argument '" + argName + "' already defined using <constructor-arg>." + " Only one approach may be used per argument.", attr);
            }
            valueHolder.setName(Conventions.attributeNameToPropertyName(argName));
            cvs.addGenericArgumentValue(valueHolder);
        }
    }
    return definition;
}
Also used : ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Attr(org.w3c.dom.Attr) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 2 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-data-mongodb by spring-projects.

the class MongoDbFactoryParserIntegrationTests method setsUpMongoDbFactoryUsingAMongoClientUri.

// DATAMONGO-1218
@Test
public void setsUpMongoDbFactoryUsingAMongoClientUri() {
    reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-client-uri.xml"));
    BeanDefinition definition = factory.getBeanDefinition("mongoDbFactory");
    ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();
    assertThat(constructorArguments.getArgumentCount(), is(1));
    ValueHolder argument = constructorArguments.getArgumentValue(0, MongoClientURI.class);
    assertThat(argument, is(notNullValue()));
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) ClassPathResource(org.springframework.core.io.ClassPathResource) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 3 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-data-mongodb by spring-projects.

the class MongoDbFactoryParserIntegrationTests method setsUpUriWithId.

// DATAMONGO-1293
@Test
public void setsUpUriWithId() {
    reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-uri-and-id.xml"));
    BeanDefinition definition = factory.getBeanDefinition("testMongo");
    ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();
    assertThat(constructorArguments.getArgumentCount(), is(1));
    ValueHolder argument = constructorArguments.getArgumentValue(0, MongoClientURI.class);
    assertThat(argument, is(notNullValue()));
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) ClassPathResource(org.springframework.core.io.ClassPathResource) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 4 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project spring-integration by spring-projects.

the class AbstractConsumerEndpointParser method parseInternal.

@Override
protected final AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder handlerBuilder = this.parseHandler(element, parserContext);
    IntegrationNamespaceUtils.setReferenceIfAttributeDefined(handlerBuilder, element, "output-channel");
    IntegrationNamespaceUtils.setValueIfAttributeDefined(handlerBuilder, element, "order");
    Element txElement = DomUtils.getChildElementByTagName(element, "transactional");
    Element adviceChainElement = DomUtils.getChildElementByTagName(element, IntegrationNamespaceUtils.REQUEST_HANDLER_ADVICE_CHAIN);
    @SuppressWarnings("rawtypes") ManagedList adviceChain = IntegrationNamespaceUtils.configureAdviceChain(adviceChainElement, txElement, true, handlerBuilder.getRawBeanDefinition(), parserContext);
    if (!CollectionUtils.isEmpty(adviceChain)) {
        handlerBuilder.addPropertyValue("adviceChain", adviceChain);
    }
    AbstractBeanDefinition handlerBeanDefinition = handlerBuilder.getBeanDefinition();
    String inputChannelAttributeName = this.getInputChannelAttributeName();
    boolean hasInputChannelAttribute = element.hasAttribute(inputChannelAttributeName);
    if (parserContext.isNested()) {
        String elementDescription = IntegrationNamespaceUtils.createElementDescription(element);
        if (hasInputChannelAttribute) {
            parserContext.getReaderContext().error("The '" + inputChannelAttributeName + "' attribute isn't allowed for a nested (e.g. inside a <chain/>) endpoint element: " + elementDescription + ".", element);
        }
        if (!replyChannelInChainAllowed(element)) {
            if (StringUtils.hasText(element.getAttribute("reply-channel"))) {
                parserContext.getReaderContext().error("The 'reply-channel' attribute isn't" + " allowed for a nested (e.g. inside a <chain/>) outbound gateway element: " + elementDescription + ".", element);
            }
        }
        return handlerBeanDefinition;
    } else {
        if (!hasInputChannelAttribute) {
            String elementDescription = IntegrationNamespaceUtils.createElementDescription(element);
            parserContext.getReaderContext().error("The '" + inputChannelAttributeName + "' attribute is required for the top-level endpoint element: " + elementDescription + ".", element);
        }
    }
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ConsumerEndpointFactoryBean.class);
    if (!CollectionUtils.isEmpty(adviceChain)) {
        builder.addPropertyValue("adviceChain", adviceChain);
    }
    String handlerBeanName = BeanDefinitionReaderUtils.generateBeanName(handlerBeanDefinition, parserContext.getRegistry());
    String[] handlerAlias = IntegrationNamespaceUtils.generateAlias(element);
    parserContext.registerBeanComponent(new BeanComponentDefinition(handlerBeanDefinition, handlerBeanName, handlerAlias));
    builder.addPropertyReference("handler", handlerBeanName);
    String inputChannelName = element.getAttribute(inputChannelAttributeName);
    if (!parserContext.getRegistry().containsBeanDefinition(inputChannelName)) {
        if (parserContext.getRegistry().containsBeanDefinition(IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME)) {
            BeanDefinition channelRegistry = parserContext.getRegistry().getBeanDefinition(IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME);
            ConstructorArgumentValues caValues = channelRegistry.getConstructorArgumentValues();
            ValueHolder vh = caValues.getArgumentValue(0, Collection.class);
            if (vh == null) {
                // although it should never happen if it does we can fix it
                caValues.addIndexedArgumentValue(0, new ManagedSet<String>());
            }
            @SuppressWarnings("unchecked") Collection<String> channelCandidateNames = (Collection<String>) caValues.getArgumentValue(0, Collection.class).getValue();
            channelCandidateNames.add(inputChannelName);
        } else {
            parserContext.getReaderContext().error("Failed to locate '" + IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME + "'", parserContext.getRegistry());
        }
    }
    IntegrationNamespaceUtils.checkAndConfigureFixedSubscriberChannel(element, parserContext, inputChannelName, handlerBeanName);
    builder.addPropertyValue("inputChannelName", inputChannelName);
    List<Element> pollerElementList = DomUtils.getChildElementsByTagName(element, "poller");
    if (!CollectionUtils.isEmpty(pollerElementList)) {
        if (pollerElementList.size() != 1) {
            parserContext.getReaderContext().error("at most one poller element may be configured for an endpoint", element);
        }
        IntegrationNamespaceUtils.configurePollerMetadata(pollerElementList.get(0), builder, parserContext);
    }
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.AUTO_STARTUP);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.PHASE);
    IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.ROLE);
    AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
    String beanName = this.resolveId(element, beanDefinition, parserContext);
    parserContext.registerBeanComponent(new BeanComponentDefinition(beanDefinition, beanName));
    return null;
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Collection(java.util.Collection) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Example 5 with ValueHolder

use of org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder in project openolat by klemens.

the class ExtensionsAdminController method getBeanDefListFor.

private Map<String, GenericBeanDefinition> getBeanDefListFor(Class<?> clazz) {
    boolean debug = log.isDebug();
    ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(CoreSpringFactory.servletContext);
    XmlWebApplicationContext context = (XmlWebApplicationContext) applicationContext;
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    Map<String, GenericBeanDefinition> beanDefinitionList = new HashMap<String, GenericBeanDefinition>();
    String[] beanNames = beanFactory.getBeanNamesForType(clazz);
    for (int i = 0; i < beanNames.length; i++) {
        try {
            if (debug)
                log.debug(">>> beanNames=" + beanNames[i]);
            GenericBeanDefinition beanDef = (GenericBeanDefinition) beanFactory.getBeanDefinition(beanNames[i]);
            ConstructorArgumentValues args = beanDef.getConstructorArgumentValues();
            List<ValueHolder> values = args.getGenericArgumentValues();
            for (Iterator<ValueHolder> iterator = values.iterator(); iterator.hasNext(); ) {
                ValueHolder valueHolder = iterator.next();
                if (debug) {
                    log.debug("valueHolder=" + valueHolder);
                    log.debug("valueHolder.getType()=" + valueHolder.getType());
                    log.debug("valueHolder.getName()=" + valueHolder.getName());
                    log.debug("valueHolder.getValue()=" + valueHolder.getValue());
                }
            }
            beanDefinitionList.put(beanNames[i], beanDef);
        } catch (NoSuchBeanDefinitionException e) {
            log.warn("Error while trying to analyze bean with name: " + beanNames[i] + " :" + e);
        } catch (Exception e) {
            log.warn("Error while trying to analyze bean with name: " + beanNames[i] + " :" + e);
        }
    }
    return beanDefinitionList;
}
Also used : HashMap(java.util.HashMap) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ApplicationContext(org.springframework.context.ApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

ValueHolder (org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)24 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)14 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)9 BeansException (org.springframework.beans.BeansException)6 LinkedHashSet (java.util.LinkedHashSet)5 InjectionPoint (org.springframework.beans.factory.InjectionPoint)5 Test (org.junit.Test)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 Element (org.w3c.dom.Element)4 IOException (java.io.IOException)3 GeneralSecurityException (java.security.GeneralSecurityException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 TypeConverter (org.springframework.beans.TypeConverter)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)3 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)3 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3