Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-20667: Add ref error handler to model. camel-yaml-dsl: Avoid tw… #13840

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"properties": {
"id": { "index": 0, "kind": "attribute", "displayName": "Id", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The id of this node" },
"errorHandlerType": { "index": 1, "kind": "element", "displayName": "Error Handler Type", "required": true, "type": "object", "javaType": "org.apache.camel.ErrorHandlerFactory", "oneOf": [ "deadLetterChannel", "defaultErrorHandler", "jtaTransactionErrorHandler", "noErrorHandler", "springTransactionErrorHandler" ], "deprecated": false, "autowired": false, "secret": false, "description": "The specific error handler in use." }
"errorHandlerType": { "index": 1, "kind": "element", "displayName": "Error Handler Type", "required": true, "type": "object", "javaType": "org.apache.camel.ErrorHandlerFactory", "oneOf": [ "deadLetterChannel", "defaultErrorHandler", "jtaTransactionErrorHandler", "noErrorHandler", "refErrorHandler", "springTransactionErrorHandler" ], "deprecated": false, "autowired": false, "secret": false, "description": "The specific error handler in use." }
},
"exchangeProperties": {
"CamelExceptionCaught": { "index": 0, "kind": "exchangeProperty", "displayName": "Exception Caught", "label": "producer", "required": false, "javaType": "java.lang.Exception", "deprecated": false, "autowired": false, "secret": false, "description": "Stores the caught exception due to a processing error of the current Exchange" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"properties": {
"id": { "index": 0, "kind": "attribute", "displayName": "Id", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The id of this node" },
"errorHandlerType": { "index": 1, "kind": "element", "displayName": "Error Handler Type", "required": true, "type": "object", "javaType": "org.apache.camel.ErrorHandlerFactory", "oneOf": [ "deadLetterChannel", "defaultErrorHandler", "jtaTransactionErrorHandler", "noErrorHandler", "springTransactionErrorHandler" ], "deprecated": false, "autowired": false, "secret": false, "description": "The specific error handler in use." }
"errorHandlerType": { "index": 1, "kind": "element", "displayName": "Error Handler Type", "required": true, "type": "object", "javaType": "org.apache.camel.ErrorHandlerFactory", "oneOf": [ "deadLetterChannel", "defaultErrorHandler", "jtaTransactionErrorHandler", "noErrorHandler", "refErrorHandler", "springTransactionErrorHandler" ], "deprecated": false, "autowired": false, "secret": false, "description": "The specific error handler in use." }
},
"exchangeProperties": {
"CamelExceptionCaught": { "index": 0, "kind": "exchangeProperty", "displayName": "Exception Caught", "label": "producer", "required": false, "javaType": "java.lang.Exception", "deprecated": false, "autowired": false, "secret": false, "description": "Stores the caught exception due to a processing error of the current Exchange" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.camel.model.errorhandler.DefaultErrorHandlerDefinition;
import org.apache.camel.model.errorhandler.JtaTransactionErrorHandlerDefinition;
import org.apache.camel.model.errorhandler.NoErrorHandlerDefinition;
import org.apache.camel.model.errorhandler.RefErrorHandlerDefinition;
import org.apache.camel.model.errorhandler.SpringTransactionErrorHandlerDefinition;
import org.apache.camel.spi.Metadata;

Expand All @@ -42,6 +43,7 @@ public class ErrorHandlerDefinition extends IdentifiedType {
@XmlElement(name = "deadLetterChannel", type = DeadLetterChannelDefinition.class),
@XmlElement(name = "defaultErrorHandler", type = DefaultErrorHandlerDefinition.class),
@XmlElement(name = "noErrorHandler", type = NoErrorHandlerDefinition.class),
@XmlElement(name = "refErrorHandler", type = RefErrorHandlerDefinition.class),
@XmlElement(name = "jtaTransactionErrorHandler", type = JtaTransactionErrorHandlerDefinition.class),
@XmlElement(name = "springTransactionErrorHandler", type = SpringTransactionErrorHandlerDefinition.class) })
private ErrorHandlerFactory errorHandlerType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ protected ErrorHandlerDefinition doParseErrorHandlerDefinition() throws IOExcept
case "deadLetterChannel": def.setErrorHandlerType(doParseDeadLetterChannelDefinition()); yield true;
case "defaultErrorHandler": def.setErrorHandlerType(doParseDefaultErrorHandlerDefinition()); yield true;
case "noErrorHandler": def.setErrorHandlerType(doParseNoErrorHandlerDefinition()); yield true;
case "refErrorHandler": def.setErrorHandlerType(doParseRefErrorHandlerDefinition()); yield true;
case "jtaTransactionErrorHandler": def.setErrorHandlerType(doParseJtaTransactionErrorHandlerDefinition()); yield true;
case "springTransactionErrorHandler": def.setErrorHandlerType(doParseSpringTransactionErrorHandlerDefinition()); yield true;
default: yield false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ protected void doWriteErrorHandlerDefinition(String name, ErrorHandlerDefinition
case "DeadLetterChannelDefinition" -> doWriteDeadLetterChannelDefinition("deadLetterChannel", (DeadLetterChannelDefinition) v);
case "DefaultErrorHandlerDefinition" -> doWriteDefaultErrorHandlerDefinition("defaultErrorHandler", (DefaultErrorHandlerDefinition) v);
case "NoErrorHandlerDefinition" -> doWriteNoErrorHandlerDefinition("noErrorHandler", (NoErrorHandlerDefinition) v);
case "RefErrorHandlerDefinition" -> doWriteRefErrorHandlerDefinition("refErrorHandler", (RefErrorHandlerDefinition) v);
case "JtaTransactionErrorHandlerDefinition" -> doWriteJtaTransactionErrorHandlerDefinition("jtaTransactionErrorHandler", (JtaTransactionErrorHandlerDefinition) v);
case "SpringTransactionErrorHandlerDefinition" -> doWriteSpringTransactionErrorHandlerDefinition("springTransactionErrorHandler", (SpringTransactionErrorHandlerDefinition) v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ protected void doWriteErrorHandlerDefinition(String name, ErrorHandlerDefinition
case "DeadLetterChannelDefinition" -> doWriteDeadLetterChannelDefinition("deadLetterChannel", (DeadLetterChannelDefinition) v);
case "DefaultErrorHandlerDefinition" -> doWriteDefaultErrorHandlerDefinition("defaultErrorHandler", (DefaultErrorHandlerDefinition) v);
case "NoErrorHandlerDefinition" -> doWriteNoErrorHandlerDefinition("noErrorHandler", (NoErrorHandlerDefinition) v);
case "RefErrorHandlerDefinition" -> doWriteRefErrorHandlerDefinition("refErrorHandler", (RefErrorHandlerDefinition) v);
case "JtaTransactionErrorHandlerDefinition" -> doWriteJtaTransactionErrorHandlerDefinition("jtaTransactionErrorHandler", (JtaTransactionErrorHandlerDefinition) v);
case "SpringTransactionErrorHandlerDefinition" -> doWriteSpringTransactionErrorHandlerDefinition("springTransactionErrorHandler", (SpringTransactionErrorHandlerDefinition) v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5288,6 +5288,7 @@ protected boolean setProperty(EnrichDefinition target, String propertyKey,
@YamlProperty(name = "id", type = "string", description = "The id of this node", displayName = "Id"),
@YamlProperty(name = "jtaTransactionErrorHandler", type = "object:org.apache.camel.model.errorhandler.JtaTransactionErrorHandlerDefinition", oneOf = "errorHandlerType"),
@YamlProperty(name = "noErrorHandler", type = "object:org.apache.camel.model.errorhandler.NoErrorHandlerDefinition", oneOf = "errorHandlerType"),
@YamlProperty(name = "refErrorHandler", type = "object:org.apache.camel.model.errorhandler.RefErrorHandlerDefinition", oneOf = "errorHandlerType"),
@YamlProperty(name = "springTransactionErrorHandler", type = "object:org.apache.camel.model.errorhandler.SpringTransactionErrorHandlerDefinition", oneOf = "errorHandlerType")
}
)
Expand Down Expand Up @@ -5326,6 +5327,11 @@ protected boolean setProperty(ErrorHandlerDefinition target, String propertyKey,
target.setErrorHandlerType(val);
break;
}
case "refErrorHandler": {
org.apache.camel.model.errorhandler.RefErrorHandlerDefinition val = asType(node, org.apache.camel.model.errorhandler.RefErrorHandlerDefinition.class);
target.setErrorHandlerType(val);
break;
}
case "jtaTransactionErrorHandler": {
org.apache.camel.model.errorhandler.JtaTransactionErrorHandlerDefinition val = asType(node, org.apache.camel.model.errorhandler.JtaTransactionErrorHandlerDefinition.class);
target.setErrorHandlerType(val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ public ConstructNode resolve(String id) {
//
case "beans":
return beansDeserializer;
case "errorHandler":
case "org.apache.camel.model.ErrorHandlerDefinition":
return new ErrorHandlerDeserializer();
case "org.apache.camel.ErrorHandlerFactory":
return new ErrorHandlerBuilderDeserializer();
case "errorHandler":
// must be a global error handler
return new ErrorHandlerDeserializer(true);
case "org.apache.camel.model.ProcessorDefinition":
return new ProcessorDefinitionDeserializer();
case "kamelet":
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,30 @@

import org.apache.camel.CamelContext;
import org.apache.camel.ErrorHandlerFactory;
import org.apache.camel.dsl.yaml.common.YamlDeserializationContext;
import org.apache.camel.dsl.yaml.common.YamlDeserializerResolver;
import org.apache.camel.dsl.yaml.common.exception.UnsupportedFieldException;
import org.apache.camel.dsl.yaml.common.exception.YamlDeserializationException;
import org.apache.camel.model.ErrorHandlerDefinition;
import org.apache.camel.model.errorhandler.DeadLetterChannelDefinition;
import org.apache.camel.model.errorhandler.DefaultErrorHandlerDefinition;
import org.apache.camel.model.errorhandler.JtaTransactionErrorHandlerDefinition;
import org.apache.camel.model.errorhandler.NoErrorHandlerDefinition;
import org.apache.camel.model.errorhandler.RefErrorHandlerDefinition;
import org.apache.camel.spi.CamelContextCustomizer;
import org.apache.camel.spi.annotations.YamlIn;
import org.apache.camel.spi.annotations.YamlProperty;
import org.apache.camel.spi.annotations.YamlType;
import org.snakeyaml.engine.v2.api.ConstructNode;
import org.snakeyaml.engine.v2.nodes.MappingNode;
import org.snakeyaml.engine.v2.nodes.Node;
import org.snakeyaml.engine.v2.nodes.NodeTuple;

import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.asMappingNode;
import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.asText;
import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.asType;
import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.getDeserializationContext;
import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.setDeserializationContext;

@YamlIn
@YamlType(
Expand Down Expand Up @@ -53,19 +70,73 @@
})
public class ErrorHandlerDeserializer implements ConstructNode {

private final ErrorHandlerBuilderDeserializer delegate = new ErrorHandlerBuilderDeserializer();
private final boolean global;

public ErrorHandlerDeserializer() {
this(false);
}

private static CamelContextCustomizer customizer(ErrorHandlerFactory builder) {
public ErrorHandlerDeserializer(boolean global) {
this.global = global;
}

private static CamelContextCustomizer customizer(ErrorHandlerDefinition builder) {
return new CamelContextCustomizer() {
@Override
public void configure(CamelContext camelContext) {
camelContext.getCamelContextExtension().setErrorHandlerFactory(builder);
camelContext.getCamelContextExtension().setErrorHandlerFactory(builder.getErrorHandlerType());
}
};
}

@Override
public Object construct(Node node) {
return customizer((ErrorHandlerFactory) delegate.construct(node));
final MappingNode bn = asMappingNode(node);
final YamlDeserializationContext dc = getDeserializationContext(node);

ErrorHandlerFactory factory = null;
for (NodeTuple tuple : bn.getValue()) {
String key = asText(tuple.getKeyNode());
Node val = tuple.getValueNode();

setDeserializationContext(val, dc);

key = org.apache.camel.util.StringHelper.dashToCamelCase(key);
switch (key) {
case "deadLetterChannel":
factory = asType(val, DeadLetterChannelDefinition.class);
break;
case "defaultErrorHandler":
factory = asType(val, DefaultErrorHandlerDefinition.class);
break;
case "jtaTransactionErrorHandler":
case "springTransactionErrorHandler":
factory = asType(val, JtaTransactionErrorHandlerDefinition.class);
break;
case "noErrorHandler":
factory = asType(val, NoErrorHandlerDefinition.class);
break;
case "refErrorHandler":
factory = asType(val, RefErrorHandlerDefinition.class);
break;
default:
throw new UnsupportedFieldException(val, key);
}
}

if (factory == null) {
throw new YamlDeserializationException(node, "Unable to determine the error handler type for the node");
}

// wrap in model
ErrorHandlerDefinition answer = new ErrorHandlerDefinition();
answer.setErrorHandlerType(factory);

if (global) {
// global scoped should register factory on camel context via customizer
return customizer(answer);
}
return answer;
}

}
Loading
Loading