Search in sources :

Example 96 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project hudson-2.x by hudson.

the class BeanBuilder method manageListIfNecessary.

/**
	 * Checks whether there are any runtime refs inside the list and
	 * converts it to a ManagedList if necessary
	 *
	 * @param value The object that represents the list
	 * @return Either a new list or a managed one
	 */
private Object manageListIfNecessary(Object value) {
    List list = (List) value;
    boolean containsRuntimeRefs = false;
    for (ListIterator i = list.listIterator(); i.hasNext(); ) {
        Object e = i.next();
        if (e instanceof RuntimeBeanReference) {
            containsRuntimeRefs = true;
        }
        if (e instanceof BeanConfiguration) {
            BeanConfiguration c = (BeanConfiguration) e;
            i.set(c.getBeanDefinition());
            containsRuntimeRefs = true;
        }
    }
    if (containsRuntimeRefs) {
        List tmp = new ManagedList();
        tmp.addAll((List) value);
        value = tmp;
    }
    return value;
}
Also used : ManagedList(org.springframework.beans.factory.support.ManagedList) List(java.util.List) GroovyObject(groovy.lang.GroovyObject) ManagedList(org.springframework.beans.factory.support.ManagedList) ListIterator(java.util.ListIterator) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 97 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project grails-core by grails.

the class ChainedTransactionManagerPostProcessor method createTransactionManagerBeanReferences.

protected ManagedList<RuntimeBeanReference> createTransactionManagerBeanReferences(BeanDefinition chainedTransactionManagerBeanDefinition) {
    ManagedList<RuntimeBeanReference> transactionManagerRefs = new ManagedList<RuntimeBeanReference>();
    ConstructorArgumentValues constructorValues = chainedTransactionManagerBeanDefinition.getConstructorArgumentValues();
    constructorValues.addIndexedArgumentValue(0, transactionManagerRefs);
    transactionManagerRefs.add(new RuntimeBeanReference(PRIMARY_TRANSACTION_MANAGER));
    return transactionManagerRefs;
}
Also used : ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 98 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project grails-core by grails.

the class BeanBuilder method manageMapIfNecessary.

/**
     * Checks whether there are any runtime refs inside a Map and converts
     * it to a ManagedMap if necessary.
     *
     * @param value The current map
     * @return A ManagedMap or a normal map
     */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object manageMapIfNecessary(Object value) {
    Map map = (Map) value;
    boolean containsRuntimeRefs = false;
    for (Object e : map.values()) {
        if (e instanceof RuntimeBeanReference) {
            containsRuntimeRefs = true;
            break;
        }
    }
    if (containsRuntimeRefs) {
        Map managedMap = new ManagedMap();
        managedMap.putAll(map);
        return managedMap;
    }
    return value;
}
Also used : GroovyObject(groovy.lang.GroovyObject) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Map(java.util.Map) HashMap(java.util.HashMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 99 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project grails-core by grails.

the class BeanBuilder method invokeMethod.

/**
     * Overrides method invocation to create beans for each method name that takes a class argument.
     */
@Override
public Object invokeMethod(String name, Object arg) {
    Object[] args = (Object[]) arg;
    if (CREATE_APPCTX.equals(name)) {
        return createApplicationContext();
    }
    if (REGISTER_BEANS.equals(name) && args.length == 1 && args[0] instanceof GenericApplicationContext) {
        registerBeans((GenericApplicationContext) args[0]);
        return null;
    }
    if (BEANS.equals(name) && args.length == 1 && args[0] instanceof Closure) {
        return beans((Closure<?>) args[0]);
    }
    if (REF.equals(name)) {
        String refName;
        Assert.notNull(args[0], "Argument to ref() is not a valid bean or was not found");
        if (args[0] instanceof RuntimeBeanReference) {
            refName = ((RuntimeBeanReference) args[0]).getBeanName();
        } else {
            refName = args[0].toString();
        }
        boolean parentRef = false;
        if (args.length > 1) {
            if (args[1] instanceof Boolean) {
                parentRef = (Boolean) args[1];
            }
        }
        return new RuntimeBeanReference(refName, parentRef);
    }
    if (namespaceHandlers.containsKey(name) && args.length > 0 && (args[0] instanceof Closure)) {
        createDynamicElementReader(name, true).invokeMethod("doCall", args);
        return this;
    }
    if (args.length > 0 && args[0] instanceof Closure) {
        // abstract bean definition
        return invokeBeanDefiningMethod(name, args);
    }
    if (args.length > 0 && args[0] instanceof Class || args.length > 0 && args[0] instanceof RuntimeBeanReference || args.length > 0 && args[0] instanceof Map) {
        return invokeBeanDefiningMethod(name, args);
    }
    if (args.length > 1 && args[args.length - 1] instanceof Closure) {
        return invokeBeanDefiningMethod(name, args);
    }
    ApplicationContext ctx = springConfig.getUnrefreshedApplicationContext();
    MetaClass mc = DefaultGroovyMethods.getMetaClass(ctx);
    if (!mc.respondsTo(ctx, name, args).isEmpty()) {
        return mc.invokeMethod(ctx, name, args);
    }
    return this;
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Closure(groovy.lang.Closure) MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject) MetaClass(groovy.lang.MetaClass) GString(groovy.lang.GString) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Map(java.util.Map) HashMap(java.util.HashMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 100 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project redisson by redisson.

the class RedissonRPCServerDefinitionParser method parseNested.

@Override
protected void parseNested(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, BeanDefinition bd) {
    Class<?> apiClass;
    try {
        apiClass = Class.forName(helper.getAttribute(element, RedissonNamespaceParserSupport.API_CLASS_ATTRIBUTE));
    } catch (ClassNotFoundException ex) {
        throw new IllegalArgumentException("The class [" + helper.getAttribute(element, RedissonNamespaceParserSupport.API_CLASS_ATTRIBUTE) + "] specified in \"" + RedissonNamespaceParserSupport.API_CLASS_ATTRIBUTE + "\" attribute has not " + "found. Please check the class path.", ex);
    }
    builder.addPropertyValue("targetObject", new RuntimeBeanReference(helper.getAttribute(element, RedissonNamespaceParserSupport.REMOTE_SERVICE_REF_ATTRIBUTE)));
    builder.addPropertyValue("targetMethod", "register");
    ManagedList args = new ManagedList();
    args.add(apiClass);
    args.add(new RuntimeBeanReference(helper.getAttribute(element, BeanDefinitionParserDelegate.BEAN_REF_ATTRIBUTE)));
    String workers = null;
    if (helper.hasAttribute(element, RedissonNamespaceParserSupport.CONCURRENT_WORKERS_ATTRIBUTE)) {
        workers = helper.getAttribute(element, RedissonNamespaceParserSupport.CONCURRENT_WORKERS_ATTRIBUTE);
    }
    if (StringUtils.hasText(workers)) {
        args.add(Integer.parseInt(workers));
    }
    if (helper.hasAttribute(element, RedissonNamespaceParserSupport.EXECUTOR_REF_ATTRIBUTE)) {
        Assert.state(helper.hasAttribute(element, RedissonNamespaceParserSupport.CONCURRENT_WORKERS_ATTRIBUTE), "The \"" + RedissonNamespaceParserSupport.CONCURRENT_WORKERS_ATTRIBUTE + "\" attribute in \"" + RedissonNamespaceParserSupport.RPC_SERVER_ELEMENT + "\" element is required when \"" + RedissonNamespaceParserSupport.EXECUTOR_REF_ATTRIBUTE + "\" attribute is specified.");
        args.add(new RuntimeBeanReference(helper.getAttribute(element, RedissonNamespaceParserSupport.EXECUTOR_REF_ATTRIBUTE)));
    }
    builder.addPropertyValue("arguments", args);
}
Also used : ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)156 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)86 Element (org.w3c.dom.Element)47 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)39 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)33 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)32 ManagedList (org.springframework.beans.factory.support.ManagedList)27 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)24 Test (org.junit.Test)21 ManagedMap (org.springframework.beans.factory.support.ManagedMap)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)15 GroovyObject (groovy.lang.GroovyObject)12 TestBean (org.springframework.tests.sample.beans.TestBean)12 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 Map (java.util.Map)10 Node (org.w3c.dom.Node)10 HashMap (java.util.HashMap)9 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)9 ITestBean (org.springframework.tests.sample.beans.ITestBean)8