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

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

Introduction

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

Prototype

public static BeanDefinitionBuilder rootBeanDefinition(Class<?> beanClass) 

Source Link

Document

Create a new BeanDefinitionBuilder used to construct a RootBeanDefinition .

Usage

From source file:org.jboss.seam.spring.namespace.CdiBeanImportBeanDefinitionParser.java

@Override
public AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder cdiBeanFactoryBuilder = BeanDefinitionBuilder
            .rootBeanDefinition(FACTORY_BEAN_CLASS_NAME);
    String beanManagerReference = element.getAttribute(BEAN_MANAGER_REFERENCE);
    if (StringUtils.hasText(beanManagerReference)) {
        cdiBeanFactoryBuilder.addPropertyReference("beanManager", beanManagerReference);
    } else {//from ww w.j  a v  a2 s .c  om
        cdiBeanFactoryBuilder.addPropertyReference("beanManager", DEFAULT_BEAN_MANAGER_ID);
    }

    String name = element.getAttribute(NAME_ATTRIBUTE);

    String type = element.getAttribute("type");

    if (!StringUtils.hasText(name) && !StringUtils.hasText(type)) {
        parserContext.getReaderContext()
                .error("A CDI bean reference must specify at least a name or the bean type", element);
    }

    ArrayList<Qualifier> qualifiers = new ArrayList<Qualifier>();

    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node childNode = children.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE && "qualifier".equals(childNode.getLocalName())) {
            Element qualifierElement = (Element) childNode;
            Qualifier qualifier = new Qualifier();
            Map<String, Object> attributes = new HashMap<String, Object>();
            qualifier.setClassName(qualifierElement.getAttribute("type"));

            if (qualifierElement.hasChildNodes()) {
                NodeList attributeNodes = qualifierElement.getChildNodes();
                for (int j = 0; j < attributeNodes.getLength(); j++) {
                    Node attributeNode = attributeNodes.item(j);
                    if (attributeNode.getNodeType() == Node.ELEMENT_NODE
                            && "attribute".equals(attributeNode.getLocalName())) {
                        Element attributeElement = (Element) attributeNode;
                        String attributeName = attributeElement.getAttribute("name");
                        String attributeValue = attributeElement.getAttribute("value");
                        if (!StringUtils.hasText(attributeName) || !StringUtils.hasText(attributeValue)) {
                            parserContext.getReaderContext().error(
                                    "Qualifier attributes must have both a name and a value", attributeElement);
                        }
                        attributes.put(attributeName, attributeValue);
                    }
                }
            }
            qualifier.setAttributes(attributes);
            qualifiers.add(qualifier);
        }
    }
    if (StringUtils.hasText(name) && !qualifiers.isEmpty()) {
        parserContext.getReaderContext()
                .error("A bean reference must either specify a name or qualifiers but not both", element);
    }

    BeanDefinitionBuilder lookupBuilder = BeanDefinitionBuilder
            .rootBeanDefinition(TYPE_SAFE_BEAN_LOOKUP_CLASS_NAME);
    lookupBuilder.addConstructorArgValue(type);
    lookupBuilder.addPropertyValue("qualifiers", qualifiers);
    AbstractBeanDefinition lookupBeanDefinition = lookupBuilder.getBeanDefinition();
    String lookupBeanName = parserContext.getReaderContext().generateBeanName(lookupBeanDefinition);
    BeanDefinitionReaderUtils.registerBeanDefinition(
            new BeanDefinitionHolder(lookupBeanDefinition, lookupBeanName), parserContext.getRegistry());
    cdiBeanFactoryBuilder.addPropertyReference("cdiBeanLookup", lookupBeanName);

    return cdiBeanFactoryBuilder.getBeanDefinition();
}

From source file:com.github.bjoern2.yolotyrion.spring.xml.RepositorySingleBeanDefinitionParser.java

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    // Scan classpath:
    String basePackage = element.getAttribute("basePackage");

    // Scan for TemplateRepositories
    ClassPathScanningCandidateComponentProvider provider = generateScanner(TemplateRepository.class);
    Set<BeanDefinition> candidates = provider.findCandidateComponents(basePackage);
    for (BeanDefinition candidate : candidates) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TemplateRepoFactoryBean.class);
        builder.addPropertyValue("repositoryInterface", candidate.getBeanClassName());
        builder.addPropertyValue("handler", new TemplateRepositoryInvocationHandler());
        AbstractBeanDefinition def = builder.getRawBeanDefinition();

        String beanName = buildDefaultBeanName(def);
        registerBean(def, beanName, parserContext);
    }//  www.  ja v  a 2  s .co m

    // Scan for PropertyRepositories
    provider = generateScanner(PropertyRepository.class);
    candidates = provider.findCandidateComponents(basePackage);
    for (BeanDefinition candidate : candidates) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(PropertyRepoFactoryBean.class);
        builder.addPropertyValue("repositoryInterface", candidate.getBeanClassName());
        builder.addPropertyValue("handler", new PropertyRepositoryInvocationHandler());
        AbstractBeanDefinition def = builder.getRawBeanDefinition();

        String beanName = buildDefaultBeanName(def);
        registerBean(def, beanName, parserContext);
    }

    return null;
}

From source file:fr.xebia.management.config.ThreadPoolExecutorFactoryDefinitionParser.java

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ThreadPoolExecutorFactory.class);

    // Mark as infrastructure bean and attach source location.
    builder.setRole(BeanDefinition.ROLE_APPLICATION);
    builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));

    String poolSize = element.getAttribute(POOL_SIZE_ATTRIBUTE);
    if (StringUtils.hasText(poolSize)) {
        builder.addPropertyValue("poolSize", poolSize);
    }//from  www .  j  a v a2  s. c  o m

    String queueCapacity = element.getAttribute(QUEUE_CAPACITY_ATTRIBUTE);
    if (StringUtils.hasText(queueCapacity)) {
        builder.addPropertyValue("queueCapacity", queueCapacity);
    }

    String keepAlive = element.getAttribute(KEEP_ALIVE_ATTRIBUTE);
    if (StringUtils.hasText(keepAlive)) {
        builder.addPropertyValue("keepAliveTimeInSeconds", keepAlive);
    }

    String rejectionPolicy = element.getAttribute(REJECTION_POLICY_ATTRIBUTE);

    Class<? extends RejectedExecutionHandler> rejectedExecutionHandlerClass;
    if ("ABORT".equals(rejectionPolicy)) {
        rejectedExecutionHandlerClass = AbortPolicy.class;
    } else if ("CALLER_RUNS".equals(rejectionPolicy)) {
        rejectedExecutionHandlerClass = CallerRunsPolicy.class;
    } else if ("DISCARD".equals(rejectionPolicy)) {
        rejectedExecutionHandlerClass = DiscardPolicy.class;
    } else if ("DISCARD_OLDEST".equals(rejectionPolicy)) {
        rejectedExecutionHandlerClass = DiscardOldestPolicy.class;
    } else {
        throw new IllegalArgumentException(
                "Unsupported '" + REJECTION_POLICY_ATTRIBUTE + "': '" + rejectionPolicy + "'");
    }
    builder.addPropertyValue("rejectedExecutionHandlerClass", rejectedExecutionHandlerClass);

    return builder.getBeanDefinition();
}

From source file:org.bigtester.ate.xmlschema.FindByIdBeanDefinitionParser.java

/**
 * {@inheritDoc}//  ww w .  j a  va2 s.c om
 */
@Override
protected @Nullable AbstractBeanDefinition parseInternal(@Nullable Element element,
        @Nullable ParserContext parserContext) {
    // Here we parse the Spring elements such as < property>
    if (parserContext == null || element == null)
        throw GlobalUtils.createNotInitializedException("element and parserContext");
    // this will never be null since the schema explicitly requires that a value be supplied
    String findbyValue = element.getAttribute(XsdElementConstants.ATTR_ELEMENTFINDBYID_FINDBYVALUE);
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(ElementFindById.class);
    if (StringUtils.hasText(findbyValue))
        factory.addConstructorArgValue(findbyValue);

    return factory.getBeanDefinition();
}

From source file:com.tacitknowledge.flip.spring.config.FeatureServiceHandlerParser.java

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder factoryBuilder = BeanDefinitionBuilder
            .rootBeanDefinition(FeatureServiceDirectFactory.class);
    RootBeanDefinition factoryBean = (RootBeanDefinition) factoryBuilder.getBeanDefinition();
    parserContext.getRegistry().registerBeanDefinition(FlipSpringAspect.FEATURE_SERVICE_FACTORY_BEAN_NAME,
            factoryBean);/*from   w ww .  j  a  v  a  2s. com*/

    MutablePropertyValues factoryPropertyValues = new MutablePropertyValues();
    factoryBean.setPropertyValues(factoryPropertyValues);

    String environmentBean = element.getAttribute("environment");
    if (environmentBean != null && !environmentBean.isEmpty()) {
        factoryPropertyValues.addPropertyValue("environment", new RuntimeBeanNameReference(environmentBean));
    }

    Element contextProvidersElement = DomUtils.getChildElementByTagName(element, "context-providers");
    if (contextProvidersElement != null) {
        List contextProvidersList = parserContext.getDelegate().parseListElement(contextProvidersElement,
                factoryBean);
        if (contextProvidersList != null && !contextProvidersList.isEmpty()) {
            factoryPropertyValues.addPropertyValue("contextProviders", contextProvidersList);
        }
    }

    Element propertyReadersElement = DomUtils.getChildElementByTagName(element, "property-readers");
    if (propertyReadersElement != null && propertyReadersElement.hasChildNodes()) {
        List propertyReadersList = parserContext.getDelegate().parseListElement(propertyReadersElement,
                factoryBean);
        if (propertyReadersList != null && !propertyReadersList.isEmpty()) {
            factoryPropertyValues.addPropertyValue("propertyReaders", propertyReadersList);
        }
    }

    Element propertiesElement = DomUtils.getChildElementByTagName(element, "properties");
    if (propertiesElement != null && propertiesElement.hasChildNodes()) {
        Properties properties = parserContext.getDelegate().parsePropsElement(propertiesElement);
        if (properties != null && !properties.isEmpty()) {
            factoryPropertyValues.addPropertyValue("properties", properties);
        }
    }

    BeanDefinitionBuilder featureServiceBuilder = BeanDefinitionBuilder.genericBeanDefinition();
    BeanDefinition featureServiceRawBean = featureServiceBuilder.getRawBeanDefinition();
    featureServiceRawBean.setFactoryBeanName(FlipSpringAspect.FEATURE_SERVICE_FACTORY_BEAN_NAME);
    featureServiceRawBean.setFactoryMethodName("createFeatureService");
    parserContext.getRegistry().registerBeanDefinition(FlipSpringAspect.FEATURE_SERVICE_BEAN_NAME,
            featureServiceBuilder.getBeanDefinition());

    return null;
}

From source file:org.bigtester.ate.xmlschema.WindowFindByTitleBeanDefinitionParser.java

/**
 * {@inheritDoc}//from   ww w . ja  v  a2 s  .  com
 */
@Override
protected @Nullable AbstractBeanDefinition parseInternal(@Nullable Element element,
        @Nullable ParserContext parserContext) {
    // Here we parse the Spring elements such as < property>
    if (parserContext == null || element == null)
        throw GlobalUtils.createNotInitializedException("element and parserContext");
    // this will never be null since the schema explicitly requires that a value be supplied
    String title = element.getAttribute(XsdElementConstants.ATTR_BROWSERWINDOWFINDBYTITLE_TITLE);
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(TestWindowFindByTitle.class);
    if (StringUtils.hasText(title))
        factory.addConstructorArgValue(title);

    return factory.getBeanDefinition();
}

From source file:org.bigtester.ate.xmlschema.XmlTestCaseBeanDefinitionParser.java

/**
 * {@inheritDoc}//from w ww  .  ja  va 2 s.com
 */
@Override
protected @Nullable AbstractBeanDefinition parseInternal(@Nullable Element element,
        @Nullable ParserContext parserContext) {
    // this will never be null since the schema explicitly requires that a value be supplied
    if (parserContext == null || element == null)
        throw GlobalUtils.createNotInitializedException("element and parserContext");
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(XmlTestCase.class);
    String testCaseName = element.getAttribute(XsdElementConstants.ATTR_XMLTESTCASE_TESTCASEFILEPATHNAME);
    if (StringUtils.hasText(testCaseName)) {
        factory.addConstructorArgValue(testCaseName);
    }
    List<Element> dependencies = (List<Element>) DomUtils.getChildElementsByTagName(element,
            XsdElementConstants.ELEMENT_CASEDEPENDENCIES);
    if (null != dependencies && dependencies.size() == 1) {
        List<Element> allDependencies = (List<Element>) DomUtils.getChildElementsByTagName(dependencies.get(0),
                XsdElementConstants.ELEMENT_CASEDEPENDENCY);

        if (allDependencies != null && !allDependencies.isEmpty()) {
            if (null == factory)
                throw GlobalUtils.createNotInitializedException("factory");
            parseCaseDependenciesInnerComponents(allDependencies, factory, parserContext);
        }
    }
    return factory.getBeanDefinition();
}

From source file:org.bigtester.ate.xmlschema.WindowFindByOpenSequenceBeanDefinitionParser.java

/**
 * {@inheritDoc}/* w  w  w. j  ava 2 s.c  o  m*/
 */
@Override
protected @Nullable AbstractBeanDefinition parseInternal(@Nullable Element element,
        @Nullable ParserContext parserContext) {
    // Here we parse the Spring elements such as < property>
    if (parserContext == null || element == null)
        throw GlobalUtils.createNotInitializedException("element and parserContext");
    // this will never be null since the schema explicitly requires that a value be supplied
    String openSeq = element
            .getAttribute(XsdElementConstants.ATTR_BROWSERWINDOWFINDBYOPENSEQUENCE_OPENSEQUENCE);
    BeanDefinitionBuilder factory = BeanDefinitionBuilder
            .rootBeanDefinition(TestWindowFindByOpenSequence.class);
    if (StringUtils.hasText(openSeq))
        factory.addConstructorArgValue(Integer.parseInt(openSeq));

    return factory.getBeanDefinition();
}

From source file:org.bigtester.ate.xmlschema.TestProjectBeanDefinitionParser.java

/**
 * {@inheritDoc}/*from w  w  w .  ja v a2  s .com*/
 */
@Override
protected @Nullable AbstractBeanDefinition parseInternal(@Nullable Element element,
        @Nullable ParserContext parserContext) {
    // Here we parse the Spring elements such as < property>
    if (parserContext == null || element == null)
        throw GlobalUtils.createNotInitializedException("element and parserContext");
    // this will never be null since the schema explicitly requires that a
    // value be supplied

    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(TestProject.class);

    int stepThinkTime = Integer
            .parseInt(element.getAttribute(XsdElementConstants.ATTR_TESTPROJECT_STEPTHINKTIME));
    factory.addPropertyValue(XsdElementConstants.ATTR_TESTPROJECT_STEPTHINKTIME, stepThinkTime);

    String globalInitXml = element.getAttribute(XsdElementConstants.ATTR_TESTPROJECT_GLOBALINITXMLFILE);

    factory.addConstructorArgValue(globalInitXml);

    List<Element> suiteListElements = (List<Element>) DomUtils.getChildElementsByTagName(element,
            XsdElementConstants.ELEMENT_TESTSUITE);

    if (suiteListElements != null && !suiteListElements.isEmpty()) {
        parseSuiteComponents(suiteListElements, factory, parserContext);
    }

    return factory.getBeanDefinition();
}

From source file:org.bigtester.ate.xmlschema.CaseDependencyBeanDefinitionParser.java

/**
 * {@inheritDoc}//from   w w w . j  a va2s  .  c o m
 */
@Override
protected @Nullable AbstractBeanDefinition parseInternal(@Nullable Element element,
        @Nullable ParserContext parserContext) {
    // Here we parse the Spring elements such as < property>
    if (parserContext == null || element == null)
        throw GlobalUtils.createNotInitializedException("element and parserContext");
    // this will never be null since the schema explicitly requires that a
    // value be supplied
    String dependOnTestCaseID = element
            .getAttribute(XsdElementConstants.ATTR_CASEDEPENDENCY_DEPENDONTESTCASEID);
    BeanDefinitionBuilder factory = BeanDefinitionBuilder//NOPMD
            .rootBeanDefinition(CaseDependency.class);
    if (StringUtils.hasText(dependOnTestCaseID))
        factory.addConstructorArgValue(dependOnTestCaseID);

    String dependencyType = element.getAttribute(XsdElementConstants.ATTR_CASEDEPENDENCY_DEPENDENCYTYPE);
    if (StringUtils.hasText(dependencyType)) {
        String dependencyType2 = dependencyType.toUpperCase(); //NOPMD
        if (null == dependencyType2) {
            throw GlobalUtils.createInternalError("java string operation internal error!");
        } else {
            factory.addConstructorArgValue(EnumCaseDependencyType.valueOf(dependencyType2));
        }
    }

    return factory.getBeanDefinition();
}