This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch release-1.4.x
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 42cd200a7a3967aaa1051235e57b50bd7c3ea126
Author: Pasquale Congiusti <pasquale.congiu...@gmail.com>
AuthorDate: Tue Apr 27 10:44:19 2021 +0200

    refactor(trait): error handler language to yaml
---
 .../kamelet-binding-error-handler.yaml             | 11 +++-
 pkg/trait/error_handler.go                         | 61 ++++++++--------------
 2 files changed, 31 insertions(+), 41 deletions(-)

diff --git a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml 
b/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml
index 5f62adc..6c44ca7 100644
--- a/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml
+++ b/examples/kamelets/error-handler/kamelet-binding-error-handler.yaml
@@ -30,7 +30,16 @@ spec:
       kind: Kamelet
       apiVersion: camel.apache.org/v1alpha1
       name: log-sink
-  errorHandler:
+  errorHandler:   
+#    log:
+#      parameters:
+#        maximumRedeliveries: 3
+#        redeliveryDelay: 2000      
+#    ref: "something"
+#    bean:
+#      type: "org.apache.camel.builder.DeadLetterChannelBuilder"
+#      parameters:
+#        deadLetterUri: log:error
     dead-letter-channel:
       endpoint:
         ref:
diff --git a/pkg/trait/error_handler.go b/pkg/trait/error_handler.go
index fbd757f..dd6e218 100644
--- a/pkg/trait/error_handler.go
+++ b/pkg/trait/error_handler.go
@@ -18,10 +18,9 @@ limitations under the License.
 package trait
 
 import (
-       "fmt"
-
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+       "gopkg.in/yaml.v2"
 )
 
 // The error-handler is a platform trait used to inject Error Handler source 
into the integration runtime.
@@ -29,6 +28,8 @@ import (
 // +camel-k:trait=error-handler
 type errorHandlerTrait struct {
        BaseTrait `property:",squash"`
+       // The error handler ref name found in application properties
+       ErrorHandlerRef string `property:",omitempty"`
 }
 
 func newErrorHandlerTrait() Trait {
@@ -51,57 +52,37 @@ func (t *errorHandlerTrait) Configure(e *Environment) 
(bool, error) {
                return false, nil
        }
 
-       return 
e.Integration.Spec.GetConfigurationProperty(v1alpha1.ErrorHandlerRefName) != 
"", nil
+       t.ErrorHandlerRef = 
e.Integration.Spec.GetConfigurationProperty(v1alpha1.ErrorHandlerRefName)
+
+       return t.ErrorHandlerRef != "", nil
 }
 
 func (t *errorHandlerTrait) Apply(e *Environment) error {
        if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
-               err := addErrorHandlerAsSource(e)
-               if err != nil {
-                       return err
-               }
+               return t.addErrorHandlerAsSource(e)
        }
        return nil
 }
 
-func addErrorHandlerAsSource(e *Environment) error {
-       errorHandlerRefName := 
e.Integration.Spec.GetConfigurationProperty(v1alpha1.ErrorHandlerRefName)
-       // TODO change to yaml flow when we fix 
https://issues.apache.org/jira/browse/CAMEL-16486
+func (t *errorHandlerTrait) addErrorHandlerAsSource(e *Environment) error {
+       flowErrorHandler := map[string]interface{}{
+               "error-handler": map[string]string{
+                       "ref": t.ErrorHandlerRef,
+               },
+       }
+       encodedFlowErrorHandler, err := 
yaml.Marshal([]map[string]interface{}{flowErrorHandler})
+       if err != nil {
+               return err
+       }
        errorHandlerSource := v1.SourceSpec{
                DataSpec: v1.DataSpec{
-                       Name: "ErrorHandlerSource.java",
-                       Content: fmt.Sprintf(`
-                       import org.apache.camel.builder.RouteBuilder;
-                       public class ErrorHandlerSource extends RouteBuilder {
-                       @Override
-                       public void configure() throws Exception {
-                               errorHandler("%s");
-                         }
-                       }
-                       `, errorHandlerRefName),
+                       Name:    "camel-k-embedded-error-handler.yaml",
+                       Content: string(encodedFlowErrorHandler),
                },
-               Language: v1.LanguageJavaSource,
+               Language: v1.LanguageYaml,
                Type:     v1.SourceTypeErrorHandler,
        }
-       /*
-               flowErrorHandler := map[string]interface{}{
-                       "error-handler": map[string]string{
-                               "ref": errorHandlerRefName,
-                       },
-               }
-               encodedFlowErrorHandler, err := yaml.Marshal(flowErrorHandler)
-               if err != nil {
-                       return err
-               }
-               errorHandlerSource := v1.SourceSpec{
-                       DataSpec: v1.DataSpec{
-                               Name:    "ErrorHandlerSource.yaml",
-                               Content: string(encodedFlowErrorHandler),
-                       },
-                       Language: v1.LanguageYaml,
-                       Type:     v1.SourceTypeErrorHandler,
-               }
-       */
+
        e.Integration.Status.AddOrReplaceGeneratedSources(errorHandlerSource)
 
        return nil

Reply via email to