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);
}
}
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);
}
}
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;
}
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;
}
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));
}
}
}
}
Aggregations