Search in sources :

Example 51 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.

the class MessageBrokerBeanDefinitionParser method registerStompHandler.

private RuntimeBeanReference registerStompHandler(Element element, RuntimeBeanReference inChannel, RuntimeBeanReference outChannel, ParserContext context, Object source) {
    RootBeanDefinition stompHandlerDef = new RootBeanDefinition(StompSubProtocolHandler.class);
    registerBeanDef(stompHandlerDef, context, source);
    Element errorHandlerElem = DomUtils.getChildElementByTagName(element, "stomp-error-handler");
    if (errorHandlerElem != null) {
        RuntimeBeanReference errorHandlerRef = new RuntimeBeanReference(errorHandlerElem.getAttribute("ref"));
        stompHandlerDef.getPropertyValues().add("errorHandler", errorHandlerRef);
    }
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addIndexedArgumentValue(0, inChannel);
    cavs.addIndexedArgumentValue(1, outChannel);
    RootBeanDefinition handlerDef = new RootBeanDefinition(SubProtocolWebSocketHandler.class, cavs, null);
    handlerDef.getPropertyValues().addPropertyValue("protocolHandlers", stompHandlerDef);
    registerBeanDefByName(WEB_SOCKET_HANDLER_BEAN_NAME, handlerDef, context, source);
    RuntimeBeanReference result = new RuntimeBeanReference(WEB_SOCKET_HANDLER_BEAN_NAME);
    Element transportElem = DomUtils.getChildElementByTagName(element, "transport");
    if (transportElem != null) {
        if (transportElem.hasAttribute("message-size")) {
            stompHandlerDef.getPropertyValues().add("messageSizeLimit", transportElem.getAttribute("message-size"));
        }
        if (transportElem.hasAttribute("send-timeout")) {
            handlerDef.getPropertyValues().add("sendTimeLimit", transportElem.getAttribute("send-timeout"));
        }
        if (transportElem.hasAttribute("send-buffer-size")) {
            handlerDef.getPropertyValues().add("sendBufferSizeLimit", transportElem.getAttribute("send-buffer-size"));
        }
        Element factoriesElement = DomUtils.getChildElementByTagName(transportElem, "decorator-factories");
        if (factoriesElement != null) {
            ManagedList<Object> factories = extractBeanSubElements(factoriesElement, context);
            RootBeanDefinition factoryBean = new RootBeanDefinition(DecoratingFactoryBean.class);
            factoryBean.getConstructorArgumentValues().addIndexedArgumentValue(0, result);
            factoryBean.getConstructorArgumentValues().addIndexedArgumentValue(1, factories);
            result = new RuntimeBeanReference(registerBeanDef(factoryBean, context, source));
        }
    }
    return result;
}
Also used : Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 52 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.

the class MessageBrokerBeanDefinitionParser method registerMessageBroker.

private RootBeanDefinition registerMessageBroker(Element brokerElement, RuntimeBeanReference inChannel, RuntimeBeanReference outChannel, RuntimeBeanReference brokerChannel, Object userDestHandler, RuntimeBeanReference brokerTemplate, RuntimeBeanReference userRegistry, ParserContext context, Object source) {
    Element simpleBrokerElem = DomUtils.getChildElementByTagName(brokerElement, "simple-broker");
    Element brokerRelayElem = DomUtils.getChildElementByTagName(brokerElement, "stomp-broker-relay");
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addIndexedArgumentValue(0, inChannel);
    cavs.addIndexedArgumentValue(1, outChannel);
    cavs.addIndexedArgumentValue(2, brokerChannel);
    RootBeanDefinition brokerDef;
    if (simpleBrokerElem != null) {
        String prefix = simpleBrokerElem.getAttribute("prefix");
        cavs.addIndexedArgumentValue(3, Arrays.asList(StringUtils.tokenizeToStringArray(prefix, ",")));
        brokerDef = new RootBeanDefinition(SimpleBrokerMessageHandler.class, cavs, null);
        if (brokerElement.hasAttribute("path-matcher")) {
            String pathMatcherRef = brokerElement.getAttribute("path-matcher");
            brokerDef.getPropertyValues().add("pathMatcher", new RuntimeBeanReference(pathMatcherRef));
        }
        if (simpleBrokerElem.hasAttribute("scheduler")) {
            String scheduler = simpleBrokerElem.getAttribute("scheduler");
            brokerDef.getPropertyValues().add("taskScheduler", new RuntimeBeanReference(scheduler));
        }
        if (simpleBrokerElem.hasAttribute("heartbeat")) {
            String heartbeatValue = simpleBrokerElem.getAttribute("heartbeat");
            brokerDef.getPropertyValues().add("heartbeatValue", heartbeatValue);
        }
    } else if (brokerRelayElem != null) {
        String prefix = brokerRelayElem.getAttribute("prefix");
        cavs.addIndexedArgumentValue(3, Arrays.asList(StringUtils.tokenizeToStringArray(prefix, ",")));
        MutablePropertyValues values = new MutablePropertyValues();
        if (brokerRelayElem.hasAttribute("relay-host")) {
            values.add("relayHost", brokerRelayElem.getAttribute("relay-host"));
        }
        if (brokerRelayElem.hasAttribute("relay-port")) {
            values.add("relayPort", brokerRelayElem.getAttribute("relay-port"));
        }
        if (brokerRelayElem.hasAttribute("client-login")) {
            values.add("clientLogin", brokerRelayElem.getAttribute("client-login"));
        }
        if (brokerRelayElem.hasAttribute("client-passcode")) {
            values.add("clientPasscode", brokerRelayElem.getAttribute("client-passcode"));
        }
        if (brokerRelayElem.hasAttribute("system-login")) {
            values.add("systemLogin", brokerRelayElem.getAttribute("system-login"));
        }
        if (brokerRelayElem.hasAttribute("system-passcode")) {
            values.add("systemPasscode", brokerRelayElem.getAttribute("system-passcode"));
        }
        if (brokerRelayElem.hasAttribute("heartbeat-send-interval")) {
            values.add("systemHeartbeatSendInterval", brokerRelayElem.getAttribute("heartbeat-send-interval"));
        }
        if (brokerRelayElem.hasAttribute("heartbeat-receive-interval")) {
            values.add("systemHeartbeatReceiveInterval", brokerRelayElem.getAttribute("heartbeat-receive-interval"));
        }
        if (brokerRelayElem.hasAttribute("virtual-host")) {
            values.add("virtualHost", brokerRelayElem.getAttribute("virtual-host"));
        }
        ManagedMap<String, Object> map = new ManagedMap<>();
        map.setSource(source);
        if (brokerRelayElem.hasAttribute("user-destination-broadcast")) {
            String destination = brokerRelayElem.getAttribute("user-destination-broadcast");
            map.put(destination, userDestHandler);
        }
        if (brokerRelayElem.hasAttribute("user-registry-broadcast")) {
            String destination = brokerRelayElem.getAttribute("user-registry-broadcast");
            map.put(destination, registerUserRegistryMessageHandler(userRegistry, brokerTemplate, destination, context, source));
        }
        if (!map.isEmpty()) {
            values.add("systemSubscriptions", map);
        }
        Class<?> handlerType = StompBrokerRelayMessageHandler.class;
        brokerDef = new RootBeanDefinition(handlerType, cavs, values);
    } else {
        // Should not happen
        throw new IllegalStateException("Neither <simple-broker> nor <stomp-broker-relay> elements found.");
    }
    registerBeanDef(brokerDef, context, source);
    return brokerDef;
}
Also used : Element(org.w3c.dom.Element) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ManagedMap(org.springframework.beans.factory.support.ManagedMap) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 53 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.

the class MessageBrokerBeanDefinitionParser method parse.

@Override
public BeanDefinition parse(Element element, ParserContext context) {
    Object source = context.extractSource(element);
    CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
    context.pushContainingComponent(compDefinition);
    Element channelElem = DomUtils.getChildElementByTagName(element, "client-inbound-channel");
    RuntimeBeanReference inChannel = getMessageChannel("clientInboundChannel", channelElem, context, source);
    channelElem = DomUtils.getChildElementByTagName(element, "client-outbound-channel");
    RuntimeBeanReference outChannel = getMessageChannel("clientOutboundChannel", channelElem, context, source);
    channelElem = DomUtils.getChildElementByTagName(element, "broker-channel");
    RuntimeBeanReference brokerChannel = getMessageChannel("brokerChannel", channelElem, context, source);
    RuntimeBeanReference userRegistry = registerUserRegistry(element, context, source);
    Object userDestHandler = registerUserDestHandler(element, userRegistry, inChannel, brokerChannel, context, source);
    RuntimeBeanReference converter = registerMessageConverter(element, context, source);
    RuntimeBeanReference template = registerMessagingTemplate(element, brokerChannel, converter, context, source);
    registerAnnotationMethodMessageHandler(element, inChannel, outChannel, converter, template, context, source);
    RootBeanDefinition broker = registerMessageBroker(element, inChannel, outChannel, brokerChannel, userDestHandler, template, userRegistry, context, source);
    // WebSocket and sub-protocol handling
    ManagedMap<String, Object> urlMap = registerHandlerMapping(element, context, source);
    RuntimeBeanReference stompHandler = registerStompHandler(element, inChannel, outChannel, context, source);
    for (Element endpointElem : DomUtils.getChildElementsByTagName(element, "stomp-endpoint")) {
        RuntimeBeanReference requestHandler = registerRequestHandler(endpointElem, stompHandler, context, source);
        String pathAttribute = endpointElem.getAttribute("path");
        Assert.state(StringUtils.hasText(pathAttribute), "Invalid <stomp-endpoint> (no path mapping)");
        List<String> paths = Arrays.asList(StringUtils.tokenizeToStringArray(pathAttribute, ","));
        for (String path : paths) {
            path = path.trim();
            Assert.state(StringUtils.hasText(path), "Invalid <stomp-endpoint> path attribute: " + pathAttribute);
            if (DomUtils.getChildElementByTagName(endpointElem, "sockjs") != null) {
                path = path.endsWith("/") ? path + "**" : path + "/**";
            }
            urlMap.put(path, requestHandler);
        }
    }
    Map<String, Object> scopeMap = Collections.<String, Object>singletonMap("websocket", new SimpSessionScope());
    RootBeanDefinition scopeConfigurer = new RootBeanDefinition(CustomScopeConfigurer.class);
    scopeConfigurer.getPropertyValues().add("scopes", scopeMap);
    registerBeanDefByName("webSocketScopeConfigurer", scopeConfigurer, context, source);
    registerWebSocketMessageBrokerStats(broker, inChannel, outChannel, context, source);
    context.popAndRegisterContainingComponent();
    return null;
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) SimpSessionScope(org.springframework.messaging.simp.SimpSessionScope) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 54 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.

the class MessageBrokerBeanDefinitionParser method registerUserRegistry.

private RuntimeBeanReference registerUserRegistry(Element element, ParserContext context, Object source) {
    Element relayElement = DomUtils.getChildElementByTagName(element, "stomp-broker-relay");
    boolean multiServer = (relayElement != null && relayElement.hasAttribute("user-registry-broadcast"));
    if (multiServer) {
        RootBeanDefinition localRegistryBeanDef = new RootBeanDefinition(DefaultSimpUserRegistry.class);
        RootBeanDefinition beanDef = new RootBeanDefinition(MultiServerUserRegistry.class);
        beanDef.getConstructorArgumentValues().addIndexedArgumentValue(0, localRegistryBeanDef);
        String beanName = registerBeanDef(beanDef, context, source);
        return new RuntimeBeanReference(beanName);
    } else {
        RootBeanDefinition beanDef = new RootBeanDefinition(DefaultSimpUserRegistry.class);
        String beanName = registerBeanDef(beanDef, context, source);
        return new RuntimeBeanReference(beanName);
    }
}
Also used : Element(org.w3c.dom.Element) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 55 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference 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

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