-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yaml-loader: add support for OnException
- Loading branch information
1 parent
af1fdce
commit 83c24fe
Showing
9 changed files
with
322 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
...aml-common/src/main/java/org/apache/camel/k/loader/yaml/parser/OnExceptionStepParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.camel.k.loader.yaml.parser; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAlias; | ||
import org.apache.camel.k.annotation.yaml.YAMLNodeDefinition; | ||
import org.apache.camel.k.annotation.yaml.YAMLStepParser; | ||
import org.apache.camel.k.loader.yaml.model.Step; | ||
import org.apache.camel.k.loader.yaml.spi.ProcessorStepParser; | ||
import org.apache.camel.k.loader.yaml.spi.StartStepParser; | ||
import org.apache.camel.k.loader.yaml.spi.StepParserSupport; | ||
import org.apache.camel.model.ExpressionSubElementDefinition; | ||
import org.apache.camel.model.OnExceptionDefinition; | ||
import org.apache.camel.model.ProcessorDefinition; | ||
import org.apache.camel.model.RedeliveryPolicyDefinition; | ||
import org.apache.camel.model.WhenDefinition; | ||
import org.apache.camel.model.language.ExpressionDefinition; | ||
import org.apache.camel.reifier.OnExceptionReifier; | ||
import org.apache.camel.spi.ClassResolver; | ||
import org.apache.camel.util.ObjectHelper; | ||
|
||
import static org.apache.camel.util.ObjectHelper.ifNotEmpty; | ||
|
||
@YAMLStepParser("on-exception") | ||
public class OnExceptionStepParser implements StartStepParser, ProcessorStepParser { | ||
@Override | ||
public ProcessorDefinition<?> toStartProcessor(Context context) { | ||
final ClassResolver resolver = context.getCamelContext().getClassResolver(); | ||
final Definition definition = context.node(Definition.class); | ||
|
||
if (ObjectHelper.isEmpty(definition.exceptions)) { | ||
definition.exceptions = List.of(Exception.class.getName()); | ||
} | ||
|
||
OnExceptionDefinition onException = null; | ||
|
||
// | ||
// don0t know if it is correct but builder.onException(A, B, C) seems to | ||
// do the same, so it loops over the given exception classes and creates | ||
// a OnExceptionDefinition for each exception. | ||
// | ||
for (String className: definition.exceptions) { | ||
onException = context.builder().onException(resolver.resolveClass(className, Throwable.class)); | ||
onException.setRouteScoped(false); | ||
|
||
mapToOnException(context, definition, onException); | ||
} | ||
|
||
return StepParserSupport.convertSteps( | ||
context, | ||
onException, | ||
definition.steps); | ||
} | ||
|
||
@Override | ||
public ProcessorDefinition<?> toProcessor(Context context) { | ||
final Definition definition = context.node(Definition.class); | ||
final OnExceptionDefinition onException = new OnExceptionDefinition(); | ||
|
||
if (ObjectHelper.isEmpty(definition.exceptions)) { | ||
definition.exceptions = List.of(Exception.class.getName()); | ||
} | ||
|
||
onException.setExceptions(definition.exceptions); | ||
onException.setRouteScoped(true); | ||
|
||
mapToOnException(context, definition, onException); | ||
|
||
return StepParserSupport.convertSteps( | ||
context, | ||
onException, | ||
definition.steps); | ||
} | ||
|
||
private static void mapToOnException(Context context, Definition definition, OnExceptionDefinition onException) { | ||
ifNotEmpty(definition.retryWhile, onException::setRetryWhile); | ||
ifNotEmpty(definition.handled, onException::setHandled); | ||
ifNotEmpty(definition.continued, onException::setContinued); | ||
ifNotEmpty(definition.continued, onException::setContinued); | ||
ifNotEmpty(definition.redeliveryPolicyType, onException::setRedeliveryPolicyType); | ||
ifNotEmpty(definition.redeliveryPolicyRef, onException::setRedeliveryPolicyRef); | ||
ifNotEmpty(definition.onRedeliveryRef, onException::setOnRedeliveryRef); | ||
ifNotEmpty(definition.onExceptionOccurredRef, onException::setOnExceptionOccurredRef); | ||
ifNotEmpty(definition.useOriginalMessage, val -> onException.setUseOriginalMessage(Boolean.toString(val))); | ||
ifNotEmpty(definition.useOriginalBody, val -> onException.setUseOriginalBody(Boolean.toString(val))); | ||
|
||
if (definition.onWhen != null) { | ||
StepParserSupport.notNull(definition.onWhen.steps, "onWhen.steps"); | ||
|
||
// | ||
// This is needed as when processing the start step, the same definition | ||
// is applied to multiple OnExceptionDefinition | ||
// | ||
WhenDefinition whenDefinition = new WhenDefinition(); | ||
whenDefinition.setExpression(definition.onWhen.getExpression()); | ||
|
||
StepParserSupport.convertSteps( | ||
context, | ||
whenDefinition, | ||
definition.onWhen.steps | ||
); | ||
|
||
onException.setOnWhen(whenDefinition); | ||
} | ||
} | ||
|
||
@YAMLNodeDefinition(reifiers = OnExceptionReifier.class) | ||
public static final class Definition { | ||
public List<Step> steps; | ||
|
||
@JsonAlias("exceptions") | ||
public List<String> exceptions; | ||
|
||
@JsonAlias({"when", "on-when"}) | ||
public When onWhen; | ||
@JsonAlias("retry-while") | ||
public ExpressionElement retryWhile; | ||
@JsonAlias("handled") | ||
public ExpressionElement handled; | ||
@JsonAlias("continued") | ||
public ExpressionElement continued; | ||
|
||
@JsonAlias("redelivery-policy") | ||
public RedeliveryPolicyDefinition redeliveryPolicyType; | ||
@JsonAlias("redelivery-policy-ref") | ||
public String redeliveryPolicyRef; | ||
|
||
@JsonAlias("on-redelivery-ref") | ||
public String onRedeliveryRef; | ||
@JsonAlias("on-exception-occurred-ref") | ||
public String onExceptionOccurredRef; | ||
@JsonAlias("use-original-message") | ||
public boolean useOriginalMessage; | ||
@JsonAlias("use-original-body") | ||
public boolean useOriginalBody; | ||
|
||
public static final class When extends WhenDefinition implements HasExpression { | ||
public List<Step> steps; | ||
} | ||
|
||
public static final class ExpressionElement extends ExpressionSubElementDefinition implements HasExpression { | ||
@Override | ||
public void setExpression(ExpressionDefinition expressionDefinition) { | ||
super.setExpressionType(expressionDefinition); | ||
} | ||
|
||
@Override | ||
public ExpressionDefinition getExpression() { | ||
return super.getExpressionType(); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.