Search in sources :

Example 11 with ManagedList

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

the class ScheduledTasksBeanDefinitionParser method doParse.

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    // lazy scheduled tasks are a contradiction in terms -> force to false
    builder.setLazyInit(false);
    ManagedList<RuntimeBeanReference> cronTaskList = new ManagedList<>();
    ManagedList<RuntimeBeanReference> fixedDelayTaskList = new ManagedList<>();
    ManagedList<RuntimeBeanReference> fixedRateTaskList = new ManagedList<>();
    ManagedList<RuntimeBeanReference> triggerTaskList = new ManagedList<>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (!isScheduledElement(child, parserContext)) {
            continue;
        }
        Element taskElement = (Element) child;
        String ref = taskElement.getAttribute("ref");
        String method = taskElement.getAttribute("method");
        // Check that 'ref' and 'method' are specified
        if (!StringUtils.hasText(ref) || !StringUtils.hasText(method)) {
            parserContext.getReaderContext().error("Both 'ref' and 'method' are required", taskElement);
            // Continue with the possible next task element
            continue;
        }
        String cronAttribute = taskElement.getAttribute("cron");
        String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
        String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
        String triggerAttribute = taskElement.getAttribute("trigger");
        String initialDelayAttribute = taskElement.getAttribute("initial-delay");
        boolean hasCronAttribute = StringUtils.hasText(cronAttribute);
        boolean hasFixedDelayAttribute = StringUtils.hasText(fixedDelayAttribute);
        boolean hasFixedRateAttribute = StringUtils.hasText(fixedRateAttribute);
        boolean hasTriggerAttribute = StringUtils.hasText(triggerAttribute);
        boolean hasInitialDelayAttribute = StringUtils.hasText(initialDelayAttribute);
        if (!(hasCronAttribute || hasFixedDelayAttribute || hasFixedRateAttribute || hasTriggerAttribute)) {
            parserContext.getReaderContext().error("one of the 'cron', 'fixed-delay', 'fixed-rate', or 'trigger' attributes is required", taskElement);
            // with the possible next task element
            continue;
        }
        if (hasInitialDelayAttribute && (hasCronAttribute || hasTriggerAttribute)) {
            parserContext.getReaderContext().error("the 'initial-delay' attribute may not be used with cron and trigger tasks", taskElement);
            // with the possible next task element
            continue;
        }
        String runnableName = runnableReference(ref, method, taskElement, parserContext).getBeanName();
        if (hasFixedDelayAttribute) {
            fixedDelayTaskList.add(intervalTaskReference(runnableName, initialDelayAttribute, fixedDelayAttribute, taskElement, parserContext));
        }
        if (hasFixedRateAttribute) {
            fixedRateTaskList.add(intervalTaskReference(runnableName, initialDelayAttribute, fixedRateAttribute, taskElement, parserContext));
        }
        if (hasCronAttribute) {
            cronTaskList.add(cronTaskReference(runnableName, cronAttribute, taskElement, parserContext));
        }
        if (hasTriggerAttribute) {
            String triggerName = new RuntimeBeanReference(triggerAttribute).getBeanName();
            triggerTaskList.add(triggerTaskReference(runnableName, triggerName, taskElement, parserContext));
        }
    }
    String schedulerRef = element.getAttribute("scheduler");
    if (StringUtils.hasText(schedulerRef)) {
        builder.addPropertyReference("taskScheduler", schedulerRef);
    }
    builder.addPropertyValue("cronTasksList", cronTaskList);
    builder.addPropertyValue("fixedDelayTasksList", fixedDelayTaskList);
    builder.addPropertyValue("fixedRateTasksList", fixedRateTaskList);
    builder.addPropertyValue("triggerTasksList", triggerTaskList);
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 12 with ManagedList

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

the class ViewResolversBeanDefinitionParser method parse.

public BeanDefinition parse(Element element, ParserContext context) {
    Object source = context.extractSource(element);
    context.pushContainingComponent(new CompositeComponentDefinition(element.getTagName(), source));
    ManagedList<Object> resolvers = new ManagedList<>(4);
    resolvers.setSource(context.extractSource(element));
    String[] names = new String[] { "jsp", "tiles", "bean-name", "freemarker", "groovy", "script-template", "bean", "ref" };
    for (Element resolverElement : DomUtils.getChildElementsByTagName(element, names)) {
        String name = resolverElement.getLocalName();
        if ("bean".equals(name) || "ref".equals(name)) {
            resolvers.add(context.getDelegate().parsePropertySubElement(resolverElement, null));
            continue;
        }
        RootBeanDefinition resolverBeanDef;
        if ("jsp".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(InternalResourceViewResolver.class);
            resolverBeanDef.getPropertyValues().add("prefix", "/WEB-INF/");
            resolverBeanDef.getPropertyValues().add("suffix", ".jsp");
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("tiles".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(TilesViewResolver.class);
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("freemarker".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(FreeMarkerViewResolver.class);
            resolverBeanDef.getPropertyValues().add("suffix", ".ftl");
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("groovy".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(GroovyMarkupViewResolver.class);
            resolverBeanDef.getPropertyValues().add("suffix", ".tpl");
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("script-template".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(ScriptTemplateViewResolver.class);
            addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
        } else if ("bean-name".equals(name)) {
            resolverBeanDef = new RootBeanDefinition(BeanNameViewResolver.class);
        } else {
            // Should never happen
            throw new IllegalStateException("Unexpected element name: " + name);
        }
        resolverBeanDef.setSource(source);
        resolverBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        resolvers.add(resolverBeanDef);
    }
    String beanName = VIEW_RESOLVER_BEAN_NAME;
    RootBeanDefinition compositeResolverBeanDef = new RootBeanDefinition(ViewResolverComposite.class);
    compositeResolverBeanDef.setSource(source);
    compositeResolverBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    names = new String[] { "content-negotiation" };
    List<Element> contentnNegotiationElements = DomUtils.getChildElementsByTagName(element, names);
    if (contentnNegotiationElements.isEmpty()) {
        compositeResolverBeanDef.getPropertyValues().add("viewResolvers", resolvers);
    } else if (contentnNegotiationElements.size() == 1) {
        BeanDefinition beanDef = createContentNegotiatingViewResolver(contentnNegotiationElements.get(0), context);
        beanDef.getPropertyValues().add("viewResolvers", resolvers);
        ManagedList<Object> list = new ManagedList<>(1);
        list.add(beanDef);
        compositeResolverBeanDef.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
        compositeResolverBeanDef.getPropertyValues().add("viewResolvers", list);
    } else if (contentnNegotiationElements.size() > 1) {
        throw new IllegalArgumentException("Only one <content-negotiation> element is allowed.");
    }
    if (element.hasAttribute("order")) {
        compositeResolverBeanDef.getPropertyValues().add("order", element.getAttribute("order"));
    }
    context.getReaderContext().getRegistry().registerBeanDefinition(beanName, compositeResolverBeanDef);
    context.registerComponent(new BeanComponentDefinition(compositeResolverBeanDef, beanName));
    context.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver)

Example 13 with ManagedList

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

the class InterceptorsBeanDefinitionParser method getIncludePatterns.

private ManagedList<String> getIncludePatterns(Element interceptor, String elementName) {
    List<Element> paths = DomUtils.getChildElementsByTagName(interceptor, elementName);
    ManagedList<String> patterns = new ManagedList<>(paths.size());
    for (Element path : paths) {
        patterns.add(path.getAttribute("path"));
    }
    return patterns;
}
Also used : Element(org.w3c.dom.Element) ManagedList(org.springframework.beans.factory.support.ManagedList)

Example 14 with ManagedList

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

the class ComplexWebApplicationContext method refresh.

@Override
public void refresh() throws BeansException {
    registerSingleton(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, SessionLocaleResolver.class);
    registerSingleton(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, SessionThemeResolver.class);
    LocaleChangeInterceptor interceptor1 = new LocaleChangeInterceptor();
    LocaleChangeInterceptor interceptor2 = new LocaleChangeInterceptor();
    interceptor2.setParamName("locale2");
    ThemeChangeInterceptor interceptor3 = new ThemeChangeInterceptor();
    ThemeChangeInterceptor interceptor4 = new ThemeChangeInterceptor();
    interceptor4.setParamName("theme2");
    UserRoleAuthorizationInterceptor interceptor5 = new UserRoleAuthorizationInterceptor();
    interceptor5.setAuthorizedRoles("role1", "role2");
    List<Object> interceptors = new ArrayList<>();
    interceptors.add(interceptor5);
    interceptors.add(interceptor1);
    interceptors.add(interceptor2);
    interceptors.add(interceptor3);
    interceptors.add(interceptor4);
    interceptors.add(new MyHandlerInterceptor1());
    interceptors.add(new MyHandlerInterceptor2());
    interceptors.add(new MyWebRequestInterceptor());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("mappings", "/view.do=viewHandler\n/locale.do=localeHandler\nloc.do=anotherLocaleHandler");
    pvs.add("interceptors", interceptors);
    registerSingleton("myUrlMapping1", SimpleUrlHandlerMapping.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("mappings", "/form.do=localeHandler\n/unknown.do=unknownHandler\nservlet.do=myServlet");
    pvs.add("order", "2");
    registerSingleton("myUrlMapping2", SimpleUrlHandlerMapping.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("mappings", "/head.do=headController\n" + "body.do=bodyController\n/noview*=noviewController\n/noview/simple*=noviewController");
    pvs.add("order", "1");
    registerSingleton("handlerMapping", SimpleUrlHandlerMapping.class, pvs);
    registerSingleton("myDummyAdapter", MyDummyAdapter.class);
    registerSingleton("myHandlerAdapter", MyHandlerAdapter.class);
    registerSingleton("standardHandlerAdapter", SimpleControllerHandlerAdapter.class);
    registerSingleton("noviewController", NoViewController.class);
    pvs = new MutablePropertyValues();
    pvs.add("order", 0);
    pvs.add("basename", "org.springframework.web.servlet.complexviews");
    registerSingleton("viewResolver", ResourceBundleViewResolver.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("suffix", ".jsp");
    registerSingleton("viewResolver2", InternalResourceViewResolver.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("viewName", "form");
    registerSingleton("viewHandler", ParameterizableViewController.class, pvs);
    registerSingleton("localeHandler", ComplexLocaleChecker.class);
    registerSingleton("anotherLocaleHandler", ComplexLocaleChecker.class);
    registerSingleton("unknownHandler", Object.class);
    registerSingleton("headController", HeadController.class);
    registerSingleton("bodyController", BodyController.class);
    registerSingleton("servletPostProcessor", SimpleServletPostProcessor.class);
    registerSingleton("handlerAdapter", SimpleServletHandlerAdapter.class);
    registerSingleton("myServlet", MyServlet.class);
    pvs = new MutablePropertyValues();
    pvs.add("order", "1");
    pvs.add("exceptionMappings", "java.lang.IllegalAccessException=failed2\n" + "ServletRequestBindingException=failed3");
    pvs.add("defaultErrorView", "failed0");
    registerSingleton("exceptionResolver1", SimpleMappingExceptionResolver.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("order", "0");
    pvs.add("exceptionMappings", "java.lang.Exception=failed1");
    List<RuntimeBeanReference> mappedHandlers = new ManagedList<>();
    mappedHandlers.add(new RuntimeBeanReference("anotherLocaleHandler"));
    pvs.add("mappedHandlers", mappedHandlers);
    pvs.add("defaultStatusCode", "500");
    pvs.add("defaultErrorView", "failed2");
    registerSingleton("handlerExceptionResolver", SimpleMappingExceptionResolver.class, pvs);
    registerSingleton("multipartResolver", MockMultipartResolver.class);
    registerSingleton("testListener", TestApplicationListener.class);
    addMessage("test", Locale.ENGLISH, "test message");
    addMessage("test", Locale.CANADA, "Canadian & test message");
    super.refresh();
}
Also used : UserRoleAuthorizationInterceptor(org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor) ArrayList(java.util.ArrayList) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) ManagedList(org.springframework.beans.factory.support.ManagedList) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 15 with ManagedList

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

the class MessageBrokerBeanDefinitionParser method registerMessageConverter.

private RuntimeBeanReference registerMessageConverter(Element element, ParserContext context, Object source) {
    Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters");
    ManagedList<? super Object> converters = new ManagedList<>();
    if (convertersElement != null) {
        converters.setSource(source);
        for (Element beanElement : DomUtils.getChildElementsByTagName(convertersElement, "bean", "ref")) {
            Object object = context.getDelegate().parsePropertySubElement(beanElement, null);
            converters.add(object);
        }
    }
    if (convertersElement == null || Boolean.valueOf(convertersElement.getAttribute("register-defaults"))) {
        converters.setSource(source);
        converters.add(new RootBeanDefinition(StringMessageConverter.class));
        converters.add(new RootBeanDefinition(ByteArrayMessageConverter.class));
        if (jackson2Present) {
            RootBeanDefinition jacksonConverterDef = new RootBeanDefinition(MappingJackson2MessageConverter.class);
            RootBeanDefinition resolverDef = new RootBeanDefinition(DefaultContentTypeResolver.class);
            resolverDef.getPropertyValues().add("defaultMimeType", MimeTypeUtils.APPLICATION_JSON);
            jacksonConverterDef.getPropertyValues().add("contentTypeResolver", resolverDef);
            // Use Jackson factory in order to have JSR-310 and Joda-Time modules registered automatically
            GenericBeanDefinition jacksonFactoryDef = new GenericBeanDefinition();
            jacksonFactoryDef.setBeanClass(Jackson2ObjectMapperFactoryBean.class);
            jacksonFactoryDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
            jacksonFactoryDef.setSource(source);
            jacksonConverterDef.getPropertyValues().add("objectMapper", jacksonFactoryDef);
            converters.add(jacksonConverterDef);
        }
    }
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addIndexedArgumentValue(0, converters);
    RootBeanDefinition messageConverterDef = new RootBeanDefinition(CompositeMessageConverter.class, cavs, null);
    String name = MESSAGE_CONVERTER_BEAN_NAME;
    registerBeanDefByName(name, messageConverterDef, context, source);
    return new RuntimeBeanReference(name);
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ByteArrayMessageConverter(org.springframework.messaging.converter.ByteArrayMessageConverter) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

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