Example usage for org.springframework.beans.factory.support BeanDefinitionBuilder addConstructorArgReference

List of usage examples for org.springframework.beans.factory.support BeanDefinitionBuilder addConstructorArgReference

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support BeanDefinitionBuilder addConstructorArgReference.

Prototype

public BeanDefinitionBuilder addConstructorArgReference(String beanName) 

Source Link

Document

Add a reference to a named bean as a constructor arg.

Usage

From source file:biz.c24.io.spring.integration.config.XPathSelectorParser.java

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    String evaluationType = element.getAttribute("evaluation-result-type");

    String expression = element.getAttribute("xpath-statement");
    String expressionRef = element.getAttribute("xpath-statement-ref");

    boolean hasRef = StringUtils.hasText(expressionRef);
    Assert.isTrue(hasRef ^ StringUtils.hasText(expression),
            "Exactly one of the 'xpath-statement' or 'xpath-statement-ref' attributes is required.");
    if (hasRef) {
        builder.addConstructorArgReference(expressionRef);
    } else {/*from  w  ww  .  j ava 2 s .com*/
        builder.addConstructorArgValue(expression);
    }

    String stringTestValue = element.getAttribute("string-test-value");

    if (evaluationType.equals("boolean")) {
        builder.getBeanDefinition()
                .setBeanClass(biz.c24.io.spring.integration.selector.C24BooleanTestXPathMessageSelector.class);
        Assert.state(!StringUtils.hasText(stringTestValue),
                "'string-test-value' should not be specified when 'evaluation-result-type' is boolean");
    } else if (evaluationType.equals("string")) {
        Assert.hasText(stringTestValue,
                "'string-test-value' must be specified when 'evaluation-result-type' is string");
        builder.addPropertyValue("valueToTestFor", stringTestValue);
        builder.getBeanDefinition().setBeanClass(
                biz.c24.io.spring.integration.selector.C24StringValueTestXPathMessageSelector.class);
    } else {
        throw new IllegalArgumentException("Unsupported value [" + evaluationType
                + "] for 'evaluation-result-type', expected boolean or string.");
    }
}

From source file:biz.c24.io.spring.integration.config.XPathRouterParser.java

@Override
protected BeanDefinition doParseRouter(Element element, ParserContext parserContext) {

    BeanDefinitionBuilder xpathRouterBuilder = BeanDefinitionBuilder
            .genericBeanDefinition("biz.c24.io.spring.integration.router.C24XPathRouter");

    String expression = element.getAttribute("xpath-statement");
    String expressionRef = element.getAttribute("xpath-statement-ref");

    boolean hasRef = StringUtils.hasText(expressionRef);
    Assert.isTrue(hasRef ^ StringUtils.hasText(expression),
            "Exactly one of the 'xpath-statement' or 'xpath-statement-ref' attributes is required.");
    if (hasRef) {
        xpathRouterBuilder.addConstructorArgReference(expressionRef);
    } else {//  w  ww . j  a  va2 s  .  co  m
        xpathRouterBuilder.addConstructorArgValue(expression);
    }

    return xpathRouterBuilder.getBeanDefinition();
}

From source file:com.mtgi.analytics.aop.config.v11.BtHttpRequestsBeanDefinitionParser.java

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    //compile required constructor arguments from attributes and nested elements.  first up, event type.
    builder.addConstructorArg(element.getAttribute(ATT_EVENT_TYPE));

    //manager ID from enclosing tag, or ref attribute.
    String managerId = parserContext.isNested()
            ? (String) parserContext.getContainingBeanDefinition().getAttribute("id")
            : element.getAttribute(BtNamespaceConstants.ATT_TRACKING_MANAGER);
    builder.addConstructorArgReference(managerId);

    //parameter list to include in event data, if any.
    String paramList = element.getAttribute(ATT_PARAMETERS);
    builder.addConstructorArg(parseList(paramList));

    //parameter list to include in event name, if any
    String nameList = element.getAttribute(ATT_NAME_PARAMETERS);
    builder.addConstructorArg(parseList(nameList));

    //URI patterns, if any.  can be specified as attribute or nested elements.
    ArrayList<Pattern> accum = new ArrayList<Pattern>();
    if (element.hasAttribute(ATT_URI_PATTERN))
        accum.add(Pattern.compile(element.getAttribute(ATT_URI_PATTERN)));

    NodeList nl = element.getElementsByTagNameNS("*", ATT_URI_PATTERN);
    for (int i = 0; i < nl.getLength(); ++i) {
        Element e = (Element) nl.item(i);
        String pattern = e.getTextContent();
        if (StringUtils.hasText(pattern))
            accum.add(Pattern.compile(pattern));
    }// ww w.  ja v a 2s .  c om

    if (accum.isEmpty())
        builder.addConstructorArg(null);
    else
        builder.addConstructorArg(accum.toArray(new Pattern[accum.size()]));

    if (parserContext.isNested())
        parserContext.getReaderContext().registerWithGeneratedName(builder.getBeanDefinition());
}

From source file:org.constretto.spring.internal.ConstrettoNamespaceHandler.java

private void createAnnotationConfigBean(ConstrettoConfiguration configuration,
        AssemblyContextResolver assemblyContextResolver, ParserContext parserContext) {
    BeanDefinitionBuilder configurationAnnotationConfigurerBean = BeanDefinitionBuilder
            .rootBeanDefinition(ConfigurationAnnotationConfigurer.class);
    if (configuration != null) {
        configurationAnnotationConfigurerBean.addConstructorArgValue(configuration);
    } else {/*from  ww  w.  j a  v  a 2s .  com*/
        configurationAnnotationConfigurerBean.addConstructorArgReference(CONSTRETTO_CONFIGURATION_BEAN_NAME);
    }
    if (assemblyContextResolver != null) {
        configurationAnnotationConfigurerBean.addConstructorArgValue(assemblyContextResolver);
    } else {
        configurationAnnotationConfigurerBean.addConstructorArgReference(ENVIRONMENT_CONTEXT_RESOLVER_NAME);
    }
    parserContext.getRegistry().registerBeanDefinition(CONSTRETTO_CONFIGURATION_ANNOTATION_BEAN_NAME,
            configurationAnnotationConfigurerBean.getBeanDefinition());
    BeanDefinitionBuilder environmentAnnotationConfigurerBean = BeanDefinitionBuilder
            .rootBeanDefinition(EnvironmentAnnotationConfigurer.class);
    environmentAnnotationConfigurerBean.addConstructorArgValue(assemblyContextResolver);
    parserContext.getRegistry().registerBeanDefinition(CONSTRETTO_ENVIRONMENT_ANNOTATION_BEAN_NAME,
            environmentAnnotationConfigurerBean.getBeanDefinition());
}

From source file:org.springjutsu.validation.namespace.ValidationMVCAnnotationsDefinitionParser.java

/**
 * Do actual parsing./* ww w . ja va 2 s. c  o  m*/
 */
public BeanDefinition parse(Element configNode, ParserContext context) {

    BeanDefinitionBuilder successViewHandlerBuilder = BeanDefinitionBuilder
            .genericBeanDefinition(SuccessViewHandlerInterceptor.class);
    BeanDefinitionBuilder validationFailureViewHandlerBuilder = BeanDefinitionBuilder
            .genericBeanDefinition(ValidationFailureViewHandlerExceptionResolver.class);
    BeanDefinitionBuilder successViewHandlerMapperBuilder = BeanDefinitionBuilder
            .genericBeanDefinition(MappedInterceptor.class);

    // Register them beans.
    registerInfrastructureBean(configNode, context, validationFailureViewHandlerBuilder);
    String successViewBeanName = registerInfrastructureBean(configNode, context, successViewHandlerBuilder);
    String[] successViewMappings = new String[] { "/**" };
    // Map success view handler
    successViewHandlerMapperBuilder.addConstructorArgValue(successViewMappings);
    successViewHandlerMapperBuilder.addConstructorArgReference(successViewBeanName);
    registerInfrastructureBean(configNode, context, successViewHandlerMapperBuilder);

    return null;
}

From source file:org.echocat.jemoni.carbon.spring.Jmx2CarbonBridgeDefinitionParser.java

@Override
protected void doParse(@Nonnull Element element, @Nonnull BeanDefinitionBuilder bean) {
    final String jmxRegistryRef = element.getAttribute(JMX_REGISTRY_REF_ATTRIBUTE);
    final String mBeanServerRef = element.getAttribute(MBEAN_SERVER_REF_ATTRIBUTE);
    if (hasText(jmxRegistryRef)) {
        if (hasText(mBeanServerRef)) {
            throw new IllegalArgumentException("The " + JMX_REGISTRY_REF_ATTRIBUTE + " and "
                    + MBEAN_SERVER_REF_ATTRIBUTE + " attributes could not be used at the same time.");
        }/*  w  ww .  j  a  v  a 2  s  .co  m*/
        bean.addConstructorArgReference(jmxRegistryRef);
    } else if (hasText(mBeanServerRef)) {
        bean.addConstructorArgReference(mBeanServerRef);
    }
    bean.addConstructorArgReference(element.getAttribute(WRITER_REF_ATTRIBUTE));

    final String classLoaderRef = element.getAttribute(CLASS_LOADER_REF_ATTRIBUTE);
    if (hasText(classLoaderRef)) {
        bean.addPropertyReference("classLoader", classLoaderRef);
    }

    final String pathPrefix = element.getAttribute(PATH_PREFIX_ATTRIBUTE);
    if (hasText(pathPrefix)) {
        bean.addPropertyValue("pathPrefix", pathPrefix);
    }

    Configuration configuration = null;
    final NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (CONFIGURATION_ELEMENT.equals(child.getLocalName())
                && SCHEMA_NAMESPACE.equals(child.getNamespaceURI())) {
            configuration = unmarshall(child);
        }
    }

    final String rulesRef = element.getAttribute(CONFIGURATION_REF_ATTRIBUTE);
    if (hasText(rulesRef)) {
        if (configuration != null) {
            throw new IllegalArgumentException("The " + CONFIGURATION_ELEMENT + " element and "
                    + CONFIGURATION_REF_ATTRIBUTE + " attribute could not be used at the same time.");
        }
        bean.addPropertyReference("configuration", rulesRef);
    } else {
        bean.addPropertyValue("configuration", configuration);
    }
}

From source file:org.smf4j.spring.RegistryNodeTemplateDefinitionParser.java

private String createCounter(ParserContext context, Element element, CounterConfig config) {
    String name = getName(context, element);

    if (config.getContentionType() == ContentionType.UNKNOWN) {
        context.getReaderContext().error("Unknown concurrency type.", context.extractSource(element));
        return null;
    }/*from  www  .jav a2  s. c  om*/
    if (config.getDurationType() == DurationType.UNKNOWN) {
        context.getReaderContext().error("Unknown duration type.", context.extractSource(element));
        return null;
    }
    if (config.getIntervalsType() == IntervalsType.UNKNOWN) {
        context.getReaderContext().error("Unknown intervals type.", context.extractSource(element));
        return null;
    }

    // Create bean definition for the mutator factory
    String mutatorFactoryId;
    switch (config.getDurationType()) {
    case NA:
    case UNBOUNDED:
        mutatorFactoryId = createUnboundedMutatorFactory(context, element, config);
        break;
    case WINDOWED:
        mutatorFactoryId = createWindowedMutatorFactory(context, element, config);
        break;
    default:
        context.getReaderContext().error("Unexpected duration type.", context.extractSource(element));
        return null;
    }

    if (mutatorFactoryId == null) {
        // Something went wrong creating the factory
        return null;
    }

    // Create bean definition for accumulator
    String accumulatorClass;
    switch (config.getContentionType()) {
    case HIGH:
    case NA:
        accumulatorClass = HC_ACCUMULATOR_CLASS;
        break;
    case LOW:
        accumulatorClass = LC_ACCUMULATOR_CLASS;
        break;
    default:
        context.getReaderContext().error("Unexpected concurrency type.", context.extractSource(element));
        return null;
    }
    BeanDefinitionBuilder accBdb = getBdb(accumulatorClass);
    accBdb.addConstructorArgReference(mutatorFactoryId);
    String accBeanId = context.getReaderContext().registerWithGeneratedName(accBdb.getBeanDefinition());

    // Create proxy that carries name
    BeanDefinitionBuilder accProxyBdb = getBdb(RegistryNodeChildProxy.class);
    accProxyBdb.addPropertyValue(NAME_ATTR, name);
    accProxyBdb.addPropertyValue(CHILD_ATTR, accBeanId);
    return context.getReaderContext().registerWithGeneratedName(accProxyBdb.getBeanDefinition());
}

From source file:org.smf4j.spring.RegistryNodeTemplateDefinitionParser.java

private String createWindowedMutatorFactory(ParserContext context, Element element, CounterConfig config) {

    // Find the mutator factory class for this type of windowed counter
    String mutatorFactoryClass;/*  w w  w  .j  a  v  a2 s.c o  m*/
    switch (config.getCounterType()) {
    case ADD:
        mutatorFactoryClass = (config.getContentionType() == ContentionType.HIGH)
                ? HC_WINDOWED_ADD_MUTATOR_CLASS
                : LC_WINDOWED_ADD_MUTATOR_CLASS;
        break;
    case MIN:
        mutatorFactoryClass = (config.getContentionType() == ContentionType.HIGH)
                ? HC_WINDOWED_MIN_MUTATOR_CLASS
                : LC_WINDOWED_MIN_MUTATOR_CLASS;
        break;
    case MAX:
        mutatorFactoryClass = (config.getContentionType() == ContentionType.HIGH)
                ? HC_WINDOWED_MAX_MUTATOR_CLASS
                : LC_WINDOWED_MAX_MUTATOR_CLASS;
        break;
    default:
        context.getReaderContext().error("Unexpected counter type.", context.extractSource(element));
        return null;
    }

    // Find the interval strategy class for this kind of interval
    String intervalStrategyClass;
    switch (config.getIntervalsType()) {
    case SECONDS:
    case NA: // Seconds is the default
        intervalStrategyClass = SECONDS_INTERVAL_STRATEGY_CLASS;
        break;
    case POWERSOFTWO:
        intervalStrategyClass = POWERS_OF_TWO_INTERVAL_STRATEGY_CLASS;
        break;
    default:
        context.getReaderContext().error("Unexpected 'intervals' type.", context.extractSource(element));
        return null;
    }

    // Make sure the timewindow and interval are specified correctly
    if (config.getTimeWindow() == null) {
        context.getReaderContext().error("'windowed' counters must " + "specify a valid 'time-window'.",
                context.extractSource(element));
        return null;
    }
    if (config.getNumIntervals() == null) {
        context.getReaderContext().error("'windowed' counters must " + "specify an 'intervals'.",
                context.extractSource(element));
        return null;
    }

    // Create a strategy instance for the strategy selected
    BeanDefinitionBuilder strategyBdb = getBdb(intervalStrategyClass);
    strategyBdb.addConstructorArgValue(config.getTimeWindow());
    strategyBdb.addConstructorArgValue(config.getNumIntervals());
    String strategyBeanId = context.getReaderContext()
            .registerWithGeneratedName(strategyBdb.getBeanDefinition());

    // Create a mutator factory referring to the strategy
    BeanDefinitionBuilder mutatorFactoryBdb = getBdb(mutatorFactoryClass);
    mutatorFactoryBdb.addConstructorArgReference(strategyBeanId);
    return context.getReaderContext().registerWithGeneratedName(mutatorFactoryBdb.getBeanDefinition());
}

From source file:ar.com.zauber.commons.conversion.spring.schema.ConversionNamespaceHandler.java

@Override
protected void doParse(final Element element, final ParserContext parserContext,
        final BeanDefinitionBuilder builder) {
    final String ref = element.getAttribute("element-converter-ref");
    if (StringUtils.isEmpty(ref)) {
        final List<?> l = parserContext.getDelegate().parseListElement(element, builder.getBeanDefinition());
        if (l.size() != 1) {
            throw new IllegalStateException("Se esperaba un solo converter." + " Se especificaron " + l.size());
        }//from   w ww.  j a  v a  2  s  . c o  m
        builder.addConstructorArgValue(l.iterator().next());
    } else {
        builder.addConstructorArgReference(ref);
    }
}