Search in sources :

Example 41 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project camel by apache.

the class ErrorHandlerDefinitionParser method doParse.

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    super.doParse(element, parserContext, builder);
    String id = element.getAttribute("id");
    ErrorHandlerType type = ErrorHandlerType.DefaultErrorHandler;
    if (ObjectHelper.isNotEmpty(element.getAttribute("type"))) {
        type = ErrorHandlerType.valueOf(element.getAttribute("type"));
    }
    if (type.equals(ErrorHandlerType.DefaultErrorHandler) || type.equals(ErrorHandlerType.DeadLetterChannel) || type.equals(ErrorHandlerType.TransactionErrorHandler)) {
        NodeList list = element.getChildNodes();
        int size = list.getLength();
        for (int i = 0; i < size; i++) {
            Node child = list.item(i);
            if (child instanceof Element) {
                Element childElement = (Element) child;
                String localName = child.getLocalName();
                // set the redeliveryPolicy
                if (localName.equals("redeliveryPolicy")) {
                    // cannot have redeliveryPolicyRef attribute as well, only one is allowed
                    if (ObjectHelper.isNotEmpty(element.getAttribute("redeliveryPolicyRef"))) {
                        throw new IllegalArgumentException("Cannot set both redeliveryPolicyRef and redeliveryPolicy," + " only one allowed, in error handler with id: " + id);
                    }
                    BeanDefinition redeliveryPolicyDefinition = redeliveryPolicyParser.parse(childElement, parserContext);
                    builder.addPropertyValue(localName, redeliveryPolicyDefinition);
                }
            }
        }
        parserRefAttribute(element, "onRedeliveryRef", "onRedelivery", builder);
        parserRefAttribute(element, "onRetryWhileRef", "onRetryWhile", builder);
        parserRefAttribute(element, "onPrepareFailureRef", "onPrepareFailure", builder);
        parserRefAttribute(element, "onExceptionOccurredRef", "onExceptionOccurred", builder);
        parserRefAttribute(element, "redeliveryPolicyRef", "redeliveryPolicy", builder);
        if (type.equals(ErrorHandlerType.TransactionErrorHandler)) {
            parserRefAttribute(element, "transactionTemplateRef", "transactionTemplate", builder);
            parserRefAttribute(element, "transactionManagerRef", "transactionManager", builder);
        }
    }
    // validate attributes according to type
    String deadLetterUri = element.getAttribute("deadLetterUri");
    if (ObjectHelper.isNotEmpty(deadLetterUri) && !type.equals(ErrorHandlerType.DeadLetterChannel)) {
        throw new IllegalArgumentException("Attribute deadLetterUri can only be used if type is " + ErrorHandlerType.DeadLetterChannel.name() + ", in error handler with id: " + id);
    }
    String deadLetterHandleNewException = element.getAttribute("deadLetterHandleNewException");
    if (ObjectHelper.isNotEmpty(deadLetterHandleNewException) && !type.equals(ErrorHandlerType.DeadLetterChannel)) {
        throw new IllegalArgumentException("Attribute deadLetterHandleNewException can only be used if type is " + ErrorHandlerType.DeadLetterChannel.name() + ", in error handler with id: " + id);
    }
    String transactionTemplateRef = element.getAttribute("transactionTemplateRef");
    if (ObjectHelper.isNotEmpty(transactionTemplateRef) && !type.equals(ErrorHandlerType.TransactionErrorHandler)) {
        throw new IllegalArgumentException("Attribute transactionTemplateRef can only be used if type is " + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id);
    }
    String transactionManagerRef = element.getAttribute("transactionManagerRef");
    if (ObjectHelper.isNotEmpty(transactionManagerRef) && !type.equals(ErrorHandlerType.TransactionErrorHandler)) {
        throw new IllegalArgumentException("Attribute transactionManagerRef can only be used if type is " + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id);
    }
    String rollbackLoggingLevel = element.getAttribute("rollbackLoggingLevel");
    if (ObjectHelper.isNotEmpty(rollbackLoggingLevel) && (!type.equals(ErrorHandlerType.TransactionErrorHandler))) {
        throw new IllegalArgumentException("Attribute rollbackLoggingLevel can only be used if type is " + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id);
    }
    String useOriginalMessage = element.getAttribute("useOriginalMessage");
    if (ObjectHelper.isNotEmpty(useOriginalMessage) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute useOriginalMessage is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String onRedeliveryRef = element.getAttribute("onRedeliveryRef");
    if (ObjectHelper.isNotEmpty(onRedeliveryRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute onRedeliveryRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String onExceptionOccurredRef = element.getAttribute("onExceptionOccurredRef");
    if (ObjectHelper.isNotEmpty(onExceptionOccurredRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute onExceptionOccurredRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String onPrepareFailureRef = element.getAttribute("onPrepareFailureRef");
    if (ObjectHelper.isNotEmpty(onPrepareFailureRef) && (type.equals(ErrorHandlerType.TransactionErrorHandler) || type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute onPrepareFailureRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String retryWhileRef = element.getAttribute("retryWhileRef");
    if (ObjectHelper.isNotEmpty(retryWhileRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute retryWhileRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String redeliveryPolicyRef = element.getAttribute("redeliveryPolicyRef");
    if (ObjectHelper.isNotEmpty(redeliveryPolicyRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute redeliveryPolicyRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String executorServiceRef = element.getAttribute("executorServiceRef");
    if (ObjectHelper.isNotEmpty(executorServiceRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute executorServiceRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String logName = element.getAttribute("logName");
    if (ObjectHelper.isNotEmpty(logName) && (!type.equals(ErrorHandlerType.LoggingErrorHandler))) {
        throw new IllegalArgumentException("Attribute logName is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String level = element.getAttribute("level");
    if (ObjectHelper.isNotEmpty(level) && (!type.equals(ErrorHandlerType.LoggingErrorHandler))) {
        throw new IllegalArgumentException("Attribute level is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ErrorHandlerType(org.apache.camel.spring.ErrorHandlerType)

Example 42 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project camel by apache.

the class CamelNamespaceHandler method autoRegisterBeanDefinition.

private void autoRegisterBeanDefinition(String id, BeanDefinition definition, ParserContext parserContext, String contextId) {
    // it is a bit cumbersome to work with the spring bean definition parser
    // as we kinda need to eagerly register the bean definition on the parser context
    // and then later we might find out that we should not have done that in case we have multiple camel contexts
    // that would have a id clash by auto registering the same bean definition with the same id such as a producer template
    // see if we have already auto registered this id
    BeanDefinition existing = autoRegisterMap.get(id);
    if (existing == null) {
        // no then add it to the map and register it
        autoRegisterMap.put(id, definition);
        parserContext.registerComponent(new BeanComponentDefinition(definition, id));
        if (LOG.isDebugEnabled()) {
            LOG.debug("Registered default: {} with id: {} on camel context: {}", new Object[] { definition.getBeanClassName(), id, contextId });
        }
    } else {
        // ups we have already registered it before with same id, but on another camel context
        // this is not good so we need to remove all traces of this auto registering.
        // end user must manually add the needed XML elements and provide unique ids access all camel context himself.
        LOG.debug("Unregistered default: {} with id: {} as we have multiple camel contexts and they must use unique ids." + " You must define the definition in the XML file manually to avoid id clashes when using multiple camel contexts", definition.getBeanClassName(), id);
        parserContext.getRegistry().removeBeanDefinition(id);
    }
}
Also used : BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 43 with BeanDefinition

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

the class ViewControllerBeanDefinitionParser method parse.

@Override
@SuppressWarnings("unchecked")
public BeanDefinition parse(Element element, ParserContext parserContext) {
    Object source = parserContext.extractSource(element);
    // Register SimpleUrlHandlerMapping for view controllers
    BeanDefinition hm = registerHandlerMapping(parserContext, source);
    // Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
    MvcNamespaceUtils.registerDefaultComponents(parserContext, source);
    // Create view controller bean definition
    RootBeanDefinition controller = new RootBeanDefinition(ParameterizableViewController.class);
    controller.setSource(source);
    HttpStatus statusCode = null;
    if (element.hasAttribute("status-code")) {
        int statusValue = Integer.valueOf(element.getAttribute("status-code"));
        statusCode = HttpStatus.valueOf(statusValue);
    }
    String name = element.getLocalName();
    if (name.equals("view-controller")) {
        if (element.hasAttribute("view-name")) {
            controller.getPropertyValues().add("viewName", element.getAttribute("view-name"));
        }
        if (statusCode != null) {
            controller.getPropertyValues().add("statusCode", statusCode);
        }
    } else if (name.equals("redirect-view-controller")) {
        controller.getPropertyValues().add("view", getRedirectView(element, statusCode, source));
    } else if (name.equals("status-controller")) {
        controller.getPropertyValues().add("statusCode", statusCode);
        controller.getPropertyValues().add("statusOnly", true);
    } else {
        // Should never happen...
        throw new IllegalStateException("Unexpected tag name: " + name);
    }
    Map<String, BeanDefinition> urlMap;
    if (hm.getPropertyValues().contains("urlMap")) {
        urlMap = (Map<String, BeanDefinition>) hm.getPropertyValues().getPropertyValue("urlMap").getValue();
    } else {
        urlMap = new ManagedMap<>();
        hm.getPropertyValues().add("urlMap", urlMap);
    }
    urlMap.put(element.getAttribute("path"), controller);
    return null;
}
Also used : HttpStatus(org.springframework.http.HttpStatus) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 44 with BeanDefinition

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

the class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests method createContext.

private ApplicationContext createContext(final ScopedProxyMode scopedProxyMode) {
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    scanner.setIncludeAnnotationConfig(false);
    scanner.setScopeMetadataResolver(new ScopeMetadataResolver() {

        @Override
        public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
            ScopeMetadata metadata = new ScopeMetadata();
            if (definition instanceof AnnotatedBeanDefinition) {
                AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
                for (String type : annDef.getMetadata().getAnnotationTypes()) {
                    if (type.equals(javax.inject.Singleton.class.getName())) {
                        metadata.setScopeName(BeanDefinition.SCOPE_SINGLETON);
                        break;
                    } else if (annDef.getMetadata().getMetaAnnotationTypes(type).contains(javax.inject.Scope.class.getName())) {
                        metadata.setScopeName(type.substring(type.length() - 13, type.length() - 6).toLowerCase());
                        metadata.setScopedProxyMode(scopedProxyMode);
                        break;
                    } else if (type.startsWith("javax.inject")) {
                        metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
                    }
                }
            }
            return metadata;
        }
    });
    // Scan twice in order to find errors in the bean definition compatibility check.
    scanner.scan(getClass().getPackage().getName());
    scanner.scan(getClass().getPackage().getName());
    context.registerAlias("classPathBeanDefinitionScannerJsr330ScopeIntegrationTests.SessionScopedTestBean", "session");
    context.refresh();
    return context;
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) ScopeMetadata(org.springframework.context.annotation.ScopeMetadata) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) ScopeMetadataResolver(org.springframework.context.annotation.ScopeMetadataResolver) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 45 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project camel by apache.

the class AbstractCamelContextBeanDefinitionParser method doParse.

protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
    // Parser the id attribute
    bean.setAbstract(true);
    // Parser the camelContextId attribute
    final String camelContextId = element.getAttribute("camelContextId");
    if (!StringUtils.isEmpty(camelContextId)) {
        wireCamelContext(bean, getContextId(camelContextId));
        // Don't need to do further parsing here
        return;
    }
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            String name = n.getLocalName();
            if ("camelContext".equals(name)) {
                // Parser the camel context
                BeanDefinition bd = ctx.getDelegate().parseCustomElement((Element) n);
                // Get the inner camel context id
                String contextId = (String) bd.getPropertyValues().getPropertyValue("id").getValue();
                wireCamelContext(bean, getContextId(contextId));
            } else if ("camelContextRef".equals(name)) {
                String contextId = n.getTextContent();
                wireCamelContext(bean, getContextId(contextId));
            }
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)593 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)188 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)117 Test (org.junit.jupiter.api.Test)105 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)89 Element (org.w3c.dom.Element)73 Test (org.junit.Test)70 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)54 ManagedList (org.springframework.beans.factory.support.ManagedList)40 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)39 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)36 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)35 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)33 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)33 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)30 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)29 ArrayList (java.util.ArrayList)28 Method (java.lang.reflect.Method)27 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)27