Search in sources :

Example 36 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project redisson by redisson.

the class RemoteInvocationOptionDecorator method decorate.

@Override
public void decorate(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, RedissonNamespaceParserSupport helper) {
    if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_INVOCATION_OPTIONS_ELEMENT)) {
        Element options = helper.getSingleElement(element, RedissonNamespaceParserSupport.REMOTE_INVOCATION_OPTIONS_ELEMENT);
        String optionBeanId = invokeOptions(options, parserContext, helper);
        if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_NO_ACK_ELEMENT)) {
            helper.invoker(optionBeanId, "noAck", null, parserContext);
        }
        if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_ACK_ELEMENT)) {
            Element remoteAck = helper.getSingleElement(element, RedissonNamespaceParserSupport.REMOTE_ACK_ELEMENT);
            Assert.state(helper.hasAttribute(remoteAck, RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE), "Missing \"" + RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE + "\" attribute in \"" + RedissonNamespaceParserSupport.REMOTE_ACK_ELEMENT + "\" element.");
            ArrayList args = new ArrayList(2);
            args.add(helper.getAttribute(remoteAck, RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE));
            if (helper.hasAttribute(remoteAck, RedissonNamespaceParserSupport.TIME_UNIT_ATTRIBUTE)) {
                args.add(helper.getAttribute(remoteAck, RedissonNamespaceParserSupport.TIME_UNIT_ATTRIBUTE));
            }
            helper.invoker(optionBeanId, "expectAckWithin", args.toArray(), parserContext);
        }
        if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_NO_RESULT_ELEMENT)) {
            helper.invoker(optionBeanId, "noResult", null, parserContext);
        }
        if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_RESULT_ELEMENT)) {
            Element remoteResult = helper.getSingleElement(element, RedissonNamespaceParserSupport.REMOTE_RESULT_ELEMENT);
            Assert.state(helper.hasAttribute(remoteResult, RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE), "Missing \"" + RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE + "\" attribute in \"" + RedissonNamespaceParserSupport.REMOTE_RESULT_ELEMENT + "\" element.");
            ArrayList args = new ArrayList(2);
            args.add(helper.getAttribute(remoteResult, RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE));
            if (helper.hasAttribute(remoteResult, RedissonNamespaceParserSupport.TIME_UNIT_ATTRIBUTE)) {
                args.add(helper.getAttribute(remoteResult, RedissonNamespaceParserSupport.TIME_UNIT_ATTRIBUTE));
            }
            helper.invoker(optionBeanId, "expectResultWithin", args.toArray(), parserContext);
        }
        MutablePropertyValues properties = builder.getRawBeanDefinition().getPropertyValues();
        PropertyValue propertyValue = properties.getPropertyValue("arguments");
        ManagedList<Object> args = new ManagedList();
        args.addAll(Arrays.asList((Object[]) propertyValue.getValue()));
        args.add(new RuntimeBeanReference(optionBeanId));
        properties.removePropertyValue("arguments");
        properties.addPropertyValue("arguments", args);
    }
}
Also used : Element(org.w3c.dom.Element) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) ArrayList(java.util.ArrayList) PropertyValue(org.springframework.beans.PropertyValue) ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 37 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.

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.hasLength(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(org.springframework.beans.BeanMetadataElement) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanMetadataElement(org.springframework.beans.BeanMetadataElement) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue)

Example 38 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.

the class GroovyBeanDefinitionReader method manageListIfNecessary.

/**
	 * Checks whether there are any {@link RuntimeBeanReference}s inside the {@link List}
	 * and converts it to a {@link ManagedList} if necessary.
	 * @param list the original List
	 * @return either the original list or a managed copy of it
	 */
private Object manageListIfNecessary(List<?> list) {
    boolean containsRuntimeRefs = false;
    for (Object element : list) {
        if (element instanceof RuntimeBeanReference) {
            containsRuntimeRefs = true;
            break;
        }
    }
    if (containsRuntimeRefs) {
        List<Object> managedList = new ManagedList<>();
        managedList.addAll(list);
        return managedList;
    }
    return list;
}
Also used : GroovyObject(groovy.lang.GroovyObject) ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 39 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.

the class PropertyResourceConfigurerTests method doTestPropertyPlaceholderConfigurer.

private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) {
    Map singletonMap = Collections.singletonMap("myKey", "myValue");
    if (parentChildSeparation) {
        MutablePropertyValues pvs1 = new MutablePropertyValues();
        pvs1.add("age", "${age}");
        MutablePropertyValues pvs2 = new MutablePropertyValues();
        pvs2.add("name", "name${var}${var}${");
        pvs2.add("spouse", new RuntimeBeanReference("${ref}"));
        pvs2.add("someMap", singletonMap);
        RootBeanDefinition parent = new RootBeanDefinition(TestBean.class);
        parent.setPropertyValues(pvs1);
        ChildBeanDefinition bd = new ChildBeanDefinition("${parent}", pvs2);
        factory.registerBeanDefinition("parent1", parent);
        factory.registerBeanDefinition("tb1", bd);
    } else {
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.add("age", "${age}");
        pvs.add("name", "name${var}${var}${");
        pvs.add("spouse", new RuntimeBeanReference("${ref}"));
        pvs.add("someMap", singletonMap);
        RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
        bd.setPropertyValues(pvs);
        factory.registerBeanDefinition("tb1", bd);
    }
    ConstructorArgumentValues cas = new ConstructorArgumentValues();
    cas.addIndexedArgumentValue(1, "${age}");
    cas.addGenericArgumentValue("${var}name${age}");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("stringArray", new String[] { "${os.name}", "${age}" });
    List<Object> friends = new ManagedList<>();
    friends.add("na${age}me");
    friends.add(new RuntimeBeanReference("${ref}"));
    pvs.add("friends", friends);
    Set<Object> someSet = new ManagedSet<>();
    someSet.add("na${age}me");
    someSet.add(new RuntimeBeanReference("${ref}"));
    someSet.add(new TypedStringValue("${age}", Integer.class));
    pvs.add("someSet", someSet);
    Map<Object, Object> someMap = new ManagedMap<>();
    someMap.put(new TypedStringValue("key${age}"), new TypedStringValue("${age}"));
    someMap.put(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}"));
    someMap.put("key1", new RuntimeBeanReference("${ref}"));
    someMap.put("key2", "${age}name");
    MutablePropertyValues innerPvs = new MutablePropertyValues();
    innerPvs.add("country", "${os.name}");
    RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);
    innerBd.setPropertyValues(innerPvs);
    someMap.put("key3", innerBd);
    MutablePropertyValues innerPvs2 = new MutablePropertyValues(innerPvs);
    someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child"));
    pvs.add("someMap", someMap);
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs);
    factory.registerBeanDefinition("tb2", bd);
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("age", "98");
    props.setProperty("var", "${m}var");
    props.setProperty("ref", "tb2");
    props.setProperty("m", "my");
    props.setProperty("key4", "mykey4");
    props.setProperty("parent", "parent1");
    ppc.setProperties(props);
    ppc.postProcessBeanFactory(factory);
    TestBean tb1 = (TestBean) factory.getBean("tb1");
    TestBean tb2 = (TestBean) factory.getBean("tb2");
    assertEquals(98, tb1.getAge());
    assertEquals(98, tb2.getAge());
    assertEquals("namemyvarmyvar${", tb1.getName());
    assertEquals("myvarname98", tb2.getName());
    assertEquals(tb2, tb1.getSpouse());
    assertEquals(1, tb1.getSomeMap().size());
    assertEquals("myValue", tb1.getSomeMap().get("myKey"));
    assertEquals(2, tb2.getStringArray().length);
    assertEquals(System.getProperty("os.name"), tb2.getStringArray()[0]);
    assertEquals("98", tb2.getStringArray()[1]);
    assertEquals(2, tb2.getFriends().size());
    assertEquals("na98me", tb2.getFriends().iterator().next());
    assertEquals(tb2, tb2.getFriends().toArray()[1]);
    assertEquals(3, tb2.getSomeSet().size());
    assertTrue(tb2.getSomeSet().contains("na98me"));
    assertTrue(tb2.getSomeSet().contains(tb2));
    assertTrue(tb2.getSomeSet().contains(new Integer(98)));
    assertEquals(6, tb2.getSomeMap().size());
    assertEquals("98", tb2.getSomeMap().get("key98"));
    assertEquals(tb2, tb2.getSomeMap().get("key98ref"));
    assertEquals(tb2, tb2.getSomeMap().get("key1"));
    assertEquals("98name", tb2.getSomeMap().get("key2"));
    TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3");
    TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4");
    assertEquals(0, inner1.getAge());
    assertEquals(null, inner1.getName());
    assertEquals(System.getProperty("os.name"), inner1.getCountry());
    assertEquals(98, inner2.getAge());
    assertEquals("namemyvarmyvar${", inner2.getName());
    assertEquals(System.getProperty("os.name"), inner2.getCountry());
}
Also used : ManagedSet(org.springframework.beans.factory.support.ManagedSet) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) Properties(java.util.Properties) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) HashMap(java.util.HashMap) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 40 with ManagedList

use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.

the class ResourcesBeanDefinitionParser method registerResourceHandler.

private String registerResourceHandler(ParserContext parserContext, Element element, Object source) {
    String locationAttr = element.getAttribute("location");
    if (!StringUtils.hasText(locationAttr)) {
        parserContext.getReaderContext().error("The 'location' attribute is required.", parserContext.extractSource(element));
        return null;
    }
    ManagedList<String> locations = new ManagedList<>();
    locations.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(locationAttr)));
    RootBeanDefinition resourceHandlerDef = new RootBeanDefinition(ResourceHttpRequestHandler.class);
    resourceHandlerDef.setSource(source);
    resourceHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    MutablePropertyValues values = resourceHandlerDef.getPropertyValues();
    values.add("locations", locations);
    String cacheSeconds = element.getAttribute("cache-period");
    if (StringUtils.hasText(cacheSeconds)) {
        values.add("cacheSeconds", cacheSeconds);
    }
    Element cacheControlElement = DomUtils.getChildElementByTagName(element, "cache-control");
    if (cacheControlElement != null) {
        CacheControl cacheControl = parseCacheControl(cacheControlElement);
        values.add("cacheControl", cacheControl);
    }
    Element resourceChainElement = DomUtils.getChildElementByTagName(element, "resource-chain");
    if (resourceChainElement != null) {
        parseResourceChain(resourceHandlerDef, parserContext, resourceChainElement, source);
    }
    Object manager = MvcNamespaceUtils.getContentNegotiationManager(parserContext);
    if (manager != null) {
        values.add("contentNegotiationManager", manager);
    }
    String beanName = parserContext.getReaderContext().generateBeanName(resourceHandlerDef);
    parserContext.getRegistry().registerBeanDefinition(beanName, resourceHandlerDef);
    parserContext.registerComponent(new BeanComponentDefinition(resourceHandlerDef, beanName));
    return beanName;
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) CacheControl(org.springframework.http.CacheControl)

Aggregations

ManagedList (org.springframework.beans.factory.support.ManagedList)60 Element (org.w3c.dom.Element)37 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)27 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)26 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)20 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)18 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)10 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)10 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)6 Node (org.w3c.dom.Node)6 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)4 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)4 GroovyObject (groovy.lang.GroovyObject)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BeanReference (org.springframework.beans.factory.config.BeanReference)3 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)3 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)3 ManagedMap (org.springframework.beans.factory.support.ManagedMap)3