Java 类org.springframework.beans.factory.xml.AbstractBeanDefinitionParser 实例源码

项目:cloud-config    文件:ZkCacheResourceBeanDefinitionParser.java   
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    String path = element.getAttribute("path");
    builder.addPropertyValue("path", path);
    // set bean alias
    element.setAttribute(AbstractBeanDefinitionParser.NAME_ATTRIBUTE, BeanIdGenerator.getMemcachedBeanId(path));

    String cacheName = element.getAttribute("cache-name");
    builder.addPropertyValue("cacheName", cacheName);

    String resolverBeanName = element.getAttribute("resolver-ref");
    if(StringUtils.hasLength(resolverBeanName) && parserContext.getRegistry().containsBeanDefinition(resolverBeanName)) {
        builder.addPropertyReference("resolver", resolverBeanName);
    }

    String validatorBeanName = element.getAttribute("validator-ref");
    if(StringUtils.hasLength(validatorBeanName) && parserContext.getRegistry().containsBeanDefinition(validatorBeanName)) {
        builder.addPropertyReference("validator", validatorBeanName);
    }
}
项目:gomall.la    文件:NamespaceHandler.java   
@Override
protected void doParse(final Element element, final ParserContext parserContext, final BeanDefinitionBuilder builder) {
    String listClass = element.getAttribute("list-class");
    List<?> currentParsedList = parserContext.getDelegate().parseListElement(element, builder.getRawBeanDefinition());

    String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);
    if (parserContext.getRegistry().isBeanNameInUse(id)) {
        PropertyValue propValue = parserContext.getRegistry().getBeanDefinition(id).getPropertyValues()
                .getPropertyValue("sourceList");
        ManagedList oldList = (ManagedList) propValue.getValue();

        currentParsedList.addAll(oldList);
    }

    builder.addPropertyValue("sourceList", currentParsedList);
    if (StringUtils.hasText(listClass)) {
        builder.addPropertyValue("targetListClass", listClass);
    }
    String scope = element.getAttribute(NamespaceHandler.SCOPE_ATTRIBUTE);
    if (StringUtils.hasLength(scope)) {
        builder.setScope(scope);
    }
}
项目:spring-ldap    文件:LdapTemplateParser.java   
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(LdapTemplate.class);

    String contextSourceRef = getString(element, ATT_CONTEXT_SOURCE_REF, ContextSourceParser.DEFAULT_ID);
    builder.addPropertyReference("contextSource", contextSourceRef);
    builder.addPropertyValue("defaultCountLimit", getInt(element, ATT_COUNT_LIMIT, DEFAULT_COUNT_LIMIT));
    builder.addPropertyValue("defaultTimeLimit", getInt(element, ATT_TIME_LIMIT, DEFAULT_TIME_LIMIT));

    String searchScope = getString(element, ATT_SEARCH_SCOPE, SearchScope.SUBTREE.toString());
    builder.addPropertyValue("defaultSearchScope", SearchScope.valueOf(searchScope).getId());
    builder.addPropertyValue("ignorePartialResultException", getBoolean(element, ATT_IGNORE_PARTIAL_RESULT, false));
    builder.addPropertyValue("ignoreNameNotFoundException", getBoolean(element, ATT_IGNORE_NAME_NOT_FOUND, false));

    String odmRef = element.getAttribute(ATT_ODM_REF);
    if(StringUtils.hasText(odmRef)) {
        builder.addPropertyReference("objectDirectoryMapper", odmRef);
    }

    String id = getString(element, AbstractBeanDefinitionParser.ID_ATTRIBUTE, DEFAULT_ID);

    BeanDefinition beanDefinition = builder.getBeanDefinition();
    parserContext.registerBeanComponent(new BeanComponentDefinition(beanDefinition, id));

    return beanDefinition;
}
项目:gemini.blueprint    文件:AbstractReferenceDefinitionParser.java   
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();

    Class<?> beanClass = getBeanClass(element);
    Assert.notNull(beanClass);

    if (beanClass != null) {
        builder.getRawBeanDefinition().setBeanClass(beanClass);
    }

    builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));

    OsgiDefaultsDefinition defaults = resolveDefaults(element.getOwnerDocument(), parserContext);
    applyDefaults(parserContext, defaults, builder);

    doParse(element, parserContext, builder);

    AbstractBeanDefinition def = builder.getBeanDefinition();

    // check whether the bean is mandatory (and if it is, make it top-level
    // bean)
    if (parserContext.isNested()) {
        String value = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);
        value = (StringUtils.hasText(value) ? value + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR : "");
        String generatedName = generateBeanName(value, def, parserContext);
        // make the bean lazy (since it is an inner bean initiallly)
        def.setLazyInit(true);
        // disable autowiring for promoted bean
        def.setAutowireCandidate(false);
        def.setAttribute(PROMOTED_REF, Boolean.TRUE);

        BeanDefinitionHolder holder = new BeanDefinitionHolder(def, generatedName);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
        return createBeanReferenceDefinition(generatedName, def);
    }

    return def;
}
项目:cloud-config    文件:ZkJdbcDataSourceBeanDefinitionParser.java   
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    String path = element.getAttribute("path");
    // add alias for data source bean
    element.setAttribute(AbstractBeanDefinitionParser.NAME_ATTRIBUTE, BeanIdGenerator.getDataSourceBeanId(path));
    String resourceType = element.getAttribute("resource-type");
    boolean autoReload = getSafeBoolean(element.getAttribute("auto-reload"));
    boolean routingSupport = getSafeBoolean(element.getAttribute("routing-support"));
    String validatorBeanName = element.getAttribute("validator-ref");
    if (routingSupport) {
        String fallbackDsPath = element.getAttribute("fallback");
        String resolverBeanName = element.getAttribute("resolver-ref");
        if(!parserContext.getRegistry().containsBeanDefinition(resolverBeanName)) {
            throw new IllegalArgumentException("Undefined routing key resolver");
        }
        if ("BoneCP".equals(resourceType)) {
            builder.addPropertyValue("resourceFactoryBeanClass", BoneCPDataSourceFactoryBean.class.getName());
        } else if ("C3P0".equals(resourceType)) {
            builder.addPropertyValue("resourceFactoryBeanClass", C3P0DataSourceFactoryBean.class.getName());
        } else if ("Druid".equals(resourceType)) {
            builder.addPropertyValue("resourceFactoryBeanClass", DruidDataSourceFactoryBean.class.getName());
        } else {
            throw new UnsupportedOperationException("Unsupported resource type "+resourceType);
        }

        builder.addPropertyReference("resolver", resolverBeanName);
        builder.addPropertyValue("path", path);
        builder.addPropertyReference("client", CloudConfigCommon.ZK_CLIENT_BEAN_NAME);
        if(StringUtils.hasLength(fallbackDsPath)) {
            builder.addPropertyValue("fallbackDataSourcePath", fallbackDsPath);
        }
    } else {
        builder.addPropertyValue("configPath", path);
    }
    if(StringUtils.hasText(validatorBeanName) && parserContext.getRegistry().containsBeanDefinition(validatorBeanName)) {
        builder.addPropertyReference("validator", validatorBeanName);
    }
    builder.addPropertyValue("autoReload", autoReload);
}
项目:cloud-config    文件:ZkResourceBeanDefinitionParser.java   
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    String path = element.getAttribute("path");
    String resourceType = element.getAttribute("resource-type");
    element.setAttribute(AbstractBeanDefinitionParser.NAME_ATTRIBUTE, getBeanAliasByType(resourceType, path));

    boolean autoReload = getSafeBoolean(element.getAttribute("auto-reload"));
    boolean routingSupport = getSafeBoolean(element.getAttribute("routing-support"));
    String validatorBeanName = element.getAttribute("validator-ref");
    if(routingSupport) {
        String fallbackRsPath = element.getAttribute("fallback");
        String resolverBeanName = element.getAttribute("resolver-ref");
        if(!parserContext.getRegistry().containsBeanDefinition(resolverBeanName)) {
            throw new IllegalArgumentException("Undefined routing key resolver");
        }
        builder.addPropertyValue("path", path);
        builder.addPropertyReference("client", CloudConfigCommon.ZK_CLIENT_BEAN_NAME);
        builder.addPropertyReference("resolver", resolverBeanName);
        builder.addPropertyValue("resourceFactoryBeanClass", getFactoryBeanClassByType(resourceType, false));
        if(StringUtils.hasLength(fallbackRsPath)) {
            builder.addPropertyValue("fallbackResourcePath", fallbackRsPath);
        }
    } else {
        builder.addPropertyValue("configPath", path);
    }
    if(StringUtils.hasLength(validatorBeanName) && parserContext.getRegistry().containsBeanDefinition(validatorBeanName)) {
        builder.addPropertyReference("validator", validatorBeanName);
    }
    builder.addPropertyValue("autoReload", autoReload);
}
项目:gomall.la    文件:NamespaceHandler.java   
@Override
protected void doParse(final Element element, final ParserContext parserContext, final BeanDefinitionBuilder builder) {
    Map<?, ?> parsedMap = parserContext.getDelegate().parseMapElement(element, builder.getRawBeanDefinition());

    String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);
    String defaultKey = element.getAttribute(DEFAULT_KEY);
    String originalDefaultKey = null;
    // Merge the entries from another map which has the same bean id
    // Retrieve the previous default key if provided
    if (parserContext.getRegistry().isBeanNameInUse(id)) {
        PropertyValue sourceMapPv = parserContext.getRegistry().getBeanDefinition(id).getPropertyValues()
                .getPropertyValue("sourceMap");
        PropertyValue defaultKeyPv = parserContext.getRegistry().getBeanDefinition(id).getPropertyValues()
                .getPropertyValue("defaultKey");

        ManagedMap oldMap = (ManagedMap) sourceMapPv.getValue();
        originalDefaultKey = (String) defaultKeyPv.getValue();

        parsedMap.putAll(oldMap);
    }

    builder.addPropertyValue("sourceMap", parsedMap);
    builder.addPropertyValue("targetMapClass", MAP_CLASS_NAME);

    // 1) throw exception when different default key were provided
    // 2) the first converted bean has no default key, but next one does
    // 3) no default key provided and same default key provided
    if (StringUtils.hasText(defaultKey)) {
        if (StringUtils.hasText(originalDefaultKey) && !originalDefaultKey.equals(defaultKey)) {
            throw new RuntimeException("Default key must be unique in one map!");
        }
    } else if (StringUtils.hasText(originalDefaultKey)) {
        defaultKey = originalDefaultKey;
    }
    builder.addPropertyValue("defaultKey", defaultKey);

    String scope = element.getAttribute(SCOPE_ATTRIBUTE);
    if (StringUtils.hasLength(scope)) {
        builder.setScope(scope);
    }
}
项目:spring-osgi    文件:AbstractReferenceDefinitionParser.java   
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();

    Class beanClass = getBeanClass(element);
    Assert.notNull(beanClass);

    if (beanClass != null) {
        builder.getRawBeanDefinition().setBeanClass(beanClass);
    }
    builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

    builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
    if (parserContext.isNested()) {
        // Inner bean definition must receive same scope as containing bean.
        builder.setScope(parserContext.getContainingBeanDefinition().getScope());
    }
    if (parserContext.isDefaultLazyInit()) {
        // Default-lazy-init applies to custom bean definitions as well.
        builder.setLazyInit(true);
    }
    doParse(element, parserContext, builder);

    // check whether the bean is mandatory (and if it is, make it top-level
    // bean)

    AbstractBeanDefinition def = builder.getBeanDefinition();

    if (parserContext.isNested()) {
        StringBuffer id = new StringBuffer();
        String value = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);
        if (StringUtils.hasText(value)) {
            id.append(value);
            id.append(BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR);
        }
        // disable autowiring for promoted bean
        def.setAutowireCandidate(false);

        id.append(parserContext.getReaderContext().generateBeanName(def));
        BeanDefinitionHolder holder = new BeanDefinitionHolder(def, id.toString());
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
        return createBeanReferenceDefinition(id.toString());
    }

    return def;
}
项目:spring-ldap    文件:TransactionManagerParser.java   
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {

    String contextSourceRef = getString(element, ATT_CONTEXT_SOURCE_REF, ContextSourceParser.DEFAULT_ID);
    String dataSourceRef = element.getAttribute(ATT_DATA_SOURCE_REF);
    String sessionFactoryRef = element.getAttribute(ATT_SESSION_FACTORY_REF);

    if(StringUtils.hasText(dataSourceRef) && StringUtils.hasText(sessionFactoryRef)) {
        throw new IllegalArgumentException(
                String.format("Only one of %s and %s can be specified",
                        ATT_DATA_SOURCE_REF, ATT_SESSION_FACTORY_REF));
    }

    BeanDefinitionBuilder builder;
    if(StringUtils.hasText(dataSourceRef)) {
        builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceAndDataSourceTransactionManager.class);
        builder.addPropertyReference("dataSource", dataSourceRef);
    } else if(StringUtils.hasText(sessionFactoryRef)) {
        builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceAndHibernateTransactionManager.class);
        builder.addPropertyReference("sessionFactory", sessionFactoryRef);
    } else {
        // Standard transaction manager
        builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceTransactionManager.class);
    }

    builder.addPropertyReference("contextSource", contextSourceRef);

    Element defaultStrategyChild = DomUtils.getChildElementByTagName(element, Elements.DEFAULT_RENAMING_STRATEGY);
    Element differentSubtreeChild = DomUtils.getChildElementByTagName(element, Elements.DIFFERENT_SUBTREE_RENAMING_STRATEGY);

    if(defaultStrategyChild != null) {
        builder.addPropertyValue("renamingStrategy", parseDefaultRenamingStrategy(defaultStrategyChild));
    }

    if(differentSubtreeChild != null) {
        builder.addPropertyValue("renamingStrategy", parseDifferentSubtreeRenamingStrategy(differentSubtreeChild));
    }

    String id = getString(element, AbstractBeanDefinitionParser.ID_ATTRIBUTE, DEFAULT_ID);

    BeanDefinition beanDefinition = builder.getBeanDefinition();
    parserContext.registerBeanComponent(new BeanComponentDefinition(beanDefinition, id));

    return beanDefinition;
}
项目:spring-ldap    文件:ContextSourceParser.java   
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(LdapContextSource.class);

    String username = element.getAttribute(ATT_USERNAME);
    String password = element.getAttribute(ATT_PASSWORD);
    String url = element.getAttribute(ATT_URL);
    Assert.hasText(url, "url attribute must be specified");

    builder.addPropertyValue("userDn", username);
    builder.addPropertyValue("password", password);

    BeanDefinitionBuilder urlsBuilder = BeanDefinitionBuilder
            .rootBeanDefinition(UrlsFactory.class)
            .setFactoryMethod("urls")
            .addConstructorArgValue(url);

    builder.addPropertyValue("urls", urlsBuilder.getBeanDefinition());
    builder.addPropertyValue("base", getString(element, ATT_BASE, ""));
    builder.addPropertyValue("referral", getString(element, ATT_REFERRAL, null));

    boolean anonymousReadOnly = getBoolean(element, ATT_ANONYMOUS_READ_ONLY, false);
    builder.addPropertyValue("anonymousReadOnly", anonymousReadOnly);
    boolean nativePooling = getBoolean(element, ATT_NATIVE_POOLING, false);
    builder.addPropertyValue("pooled", nativePooling);

    String authStrategyRef = element.getAttribute(ATT_AUTHENTICATION_STRATEGY_REF);
    if(StringUtils.hasText(authStrategyRef)) {
        builder.addPropertyReference("authenticationStrategy", authStrategyRef);
    }

    String authSourceRef = element.getAttribute(ATT_AUTHENTICATION_SOURCE_REF);
    if(StringUtils.hasText(authSourceRef)) {
        builder.addPropertyReference("authenticationSource", authSourceRef);
    } else {
        Assert.hasText(username, "username attribute must be specified unless an authentication-source-ref explicitly configured");
        Assert.hasText(password, "password attribute must be specified unless an authentication-source-ref explicitly configured");
    }

    String baseEnvPropsRef = element.getAttribute(ATT_BASE_ENV_PROPS_REF);
    if(StringUtils.hasText(baseEnvPropsRef)) {
        builder.addPropertyReference("baseEnvironmentProperties", baseEnvPropsRef);
    }

    BeanDefinition targetContextSourceDefinition = builder.getBeanDefinition();
    targetContextSourceDefinition = applyPoolingIfApplicable(targetContextSourceDefinition, element, nativePooling);

    BeanDefinition actualContextSourceDefinition = targetContextSourceDefinition;
    if (!anonymousReadOnly) {
        BeanDefinitionBuilder proxyBuilder = BeanDefinitionBuilder.rootBeanDefinition(TransactionAwareContextSourceProxy.class);
        proxyBuilder.addConstructorArgValue(targetContextSourceDefinition);
        actualContextSourceDefinition = proxyBuilder.getBeanDefinition();
    }

    String id = getString(element, AbstractBeanDefinitionParser.ID_ATTRIBUTE, DEFAULT_ID);
    parserContext.registerBeanComponent(new BeanComponentDefinition(actualContextSourceDefinition, id));

    return actualContextSourceDefinition;
}