You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ji...@apache.org on 2023/10/05 14:00:25 UTC

[camel-quarkus] 30/45: Fix Quarkus recorder serialization issues for RouteTemplateDefinition resources

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

jiriondrusek pushed a commit to branch camel-main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git

commit 2e85b6034e7f363841b6ec7334693fbe95f3a9dc
Author: James Netherton <ja...@gmail.com>
AuthorDate: Wed Sep 20 16:02:46 2023 +0100

    Fix Quarkus recorder serialization issues for RouteTemplateDefinition resources
---
 .../kamelet/deployment/KameletProcessor.java       |  19 +++-
 .../kamelet/KameletClasspathResource.java          | 109 +++++++++++++++++++++
 2 files changed, 125 insertions(+), 3 deletions(-)

diff --git a/extensions/kamelet/deployment/src/main/java/org/apache/camel/quarkus/component/kamelet/deployment/KameletProcessor.java b/extensions/kamelet/deployment/src/main/java/org/apache/camel/quarkus/component/kamelet/deployment/KameletProcessor.java
index ad307b8936..142a4461ae 100644
--- a/extensions/kamelet/deployment/src/main/java/org/apache/camel/quarkus/component/kamelet/deployment/KameletProcessor.java
+++ b/extensions/kamelet/deployment/src/main/java/org/apache/camel/quarkus/component/kamelet/deployment/KameletProcessor.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.quarkus.component.kamelet.deployment;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -35,6 +36,7 @@ import org.apache.camel.RoutesBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.model.RouteTemplateDefinition;
+import org.apache.camel.quarkus.component.kamelet.KameletClasspathResource;
 import org.apache.camel.quarkus.component.kamelet.KameletConfiguration;
 import org.apache.camel.quarkus.component.kamelet.KameletRecorder;
 import org.apache.camel.quarkus.core.deployment.spi.CamelContextCustomizerBuildItem;
@@ -119,9 +121,20 @@ class KameletProcessor {
             }
         }
 
-        //quick workaround for #5230
-        //remove references to Resources, because the list is serialized; resources are loaded later in the recorder
-        definitions.stream().forEach(rd -> rd.setResource(null));
+        // Use Quarkus recorder serialization friendly KameletClasspathResource instead of the default Resource
+        definitions.forEach(definition -> {
+            try {
+                Resource originalResource = definition.getResource();
+                KameletClasspathResource kameletResource = new KameletClasspathResource();
+                kameletResource.setScheme(originalResource.getScheme());
+                kameletResource.setLocation(originalResource.getLocation());
+                kameletResource.setExists(originalResource.exists());
+                kameletResource.setData(originalResource.getInputStream().readAllBytes());
+                definition.setResource(kameletResource);
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        });
 
         return new CamelContextCustomizerBuildItem(
                 recorder.createTemplateLoaderCustomizer(definitions));
diff --git a/extensions/kamelet/runtime/src/main/java/org/apache/camel/quarkus/component/kamelet/KameletClasspathResource.java b/extensions/kamelet/runtime/src/main/java/org/apache/camel/quarkus/component/kamelet/KameletClasspathResource.java
new file mode 100644
index 0000000000..357e86c0e1
--- /dev/null
+++ b/extensions/kamelet/runtime/src/main/java/org/apache/camel/quarkus/component/kamelet/KameletClasspathResource.java
@@ -0,0 +1,109 @@
+/*
+ * 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.quarkus.component.kamelet;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Objects;
+
+import org.apache.camel.spi.Resource;
+
+/**
+ * Mutable & Quarkus recorder serialization friendly implementation for Kamelet classpath resources
+ */
+public class KameletClasspathResource implements Resource {
+    private String scheme;
+    private String location;
+    private boolean exists;
+    private byte[] data;
+    private InputStream inputStream;
+
+    @Override
+    public String getScheme() {
+        return scheme;
+    }
+
+    public void setScheme(String scheme) {
+        this.scheme = scheme;
+    }
+
+    @Override
+    public String getLocation() {
+        return this.location;
+    }
+
+    public void setLocation(String location) {
+        this.location = location;
+    }
+
+    @Override
+    public boolean exists() {
+        return exists;
+    }
+
+    public void setExists(boolean exists) {
+        this.exists = exists;
+    }
+
+    public byte[] getData() {
+        return this.data;
+    }
+
+    public void setData(byte[] data) {
+        this.inputStream = null;
+        this.data = data;
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        if (this.data == null) {
+            throw new IOException("No resource content was defined");
+        }
+        if (this.inputStream == null) {
+            this.inputStream = new ByteArrayInputStream(this.data);
+        }
+        return inputStream;
+    }
+
+    @Override
+    public String toString() {
+        String prefix = scheme + ":";
+        if (location.startsWith(prefix)) {
+            return "Resource[" + location + "]";
+        } else {
+            return "Resource[" + prefix + location + "]";
+        }
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        Resource that = (Resource) o;
+        return scheme.equals(that.getScheme()) && location.equals(that.getLocation());
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(scheme, location);
+    }
+}