You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/11/19 15:33:12 UTC
[camel] 01/02: CAMEL-17210: camel-jbang - Allow to run a file loaded via github resource loader
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit f342b67d0313eac9fc9cae576161442a23538d18
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Nov 19 16:28:02 2021 +0100
CAMEL-17210: camel-jbang - Allow to run a file loaded via github resource loader
---
.../apache/camel/dsl/jbang/core/commands/Run.java | 53 ++++++++++++++++------
dsl/camel-kamelet-main/pom.xml | 7 +++
.../java/org/apache/camel/main/KameletMain.java | 23 ++++++----
.../apache/camel/main/KameletMainGithubTest.java | 35 ++++++++++++++
.../org/apache/camel/main/KameletMainTest.java | 34 ++++++++++++++
.../src/test/resources/log4j2.properties | 28 ++++++++++++
.../src/test/resources/my-route.yaml | 27 +++++++++++
7 files changed, 186 insertions(+), 21 deletions(-)
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 1330c3f..fc0b072 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -25,6 +25,7 @@ import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
import org.apache.camel.main.KameletMain;
+import org.apache.camel.support.ResourceHelper;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@@ -38,7 +39,7 @@ class Run implements Callable<Integer> {
//CHECKSTYLE:OFF
@Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands")
- private boolean helpRequested = false;
+ private boolean helpRequested;
//CHECKSTYLE:ON
@Option(names = { "--debug-level" }, defaultValue = "info", description = "Default debug level")
@@ -53,6 +54,10 @@ class Run implements Callable<Integer> {
@Option(names = { "--reload" }, description = "Enables live reload when source file is changed (saved)")
private boolean reload;
+ @Option(names = { "--file-lock" },
+ description = "Whether to create a temporary file lock, which upon deleting triggers this process to terminate")
+ private boolean fileLock;
+
class ShutdownRoute extends RouteBuilder {
private File lockFile;
@@ -99,25 +104,47 @@ class Run implements Callable<Integer> {
System.setProperty("camel.main.durationMaxMessages", String.valueOf(maxMessages));
}
- System.setProperty("camel.main.routes-include-pattern", "file:" + binding);
- System.setProperty("camel.main.routes-reload-enabled", reload ? "true" : "false");
- System.setProperty("camel.main.routes-reload-directory", ".");
- System.setProperty("camel.main.routes-reload-pattern", binding);
-
RuntimeUtil.configureLog(debugLevel);
- File bindingFile = new File(binding);
- if (!bindingFile.exists()) {
- System.err.println("The binding file does not exist");
+ KameletMain main = new KameletMain() {
+ @Override
+ protected void configureInitialProperties() {
+ addInitialProperty("camel.component.kamelet.location", "classpath:/kamelets,github:apache:camel-kamelets");
+ // turn off lightweight if we have routes reload enabled
+ addInitialProperty("camel.main.lightweight", reload ? "false" : "true");
+ // shutdown quickly
+ addInitialProperty("camel.main.shutdown-timeout", "5");
+ }
+ };
+
+ // special route to trigger shutdown on watch file delete
+ if (fileLock) {
+ // TODO: use another way
+ main.configure().addRoutesBuilder(new ShutdownRoute(createLockFile()));
+ }
- return 1;
+ if (!ResourceHelper.hasScheme(binding) && !binding.startsWith("github:")) {
+ binding = "file:" + binding;
}
+ main.addInitialProperty("camel.main.routes-include-pattern", binding);
+
+ if (binding.startsWith("file:")) {
+ // check if file exist
+ File bindingFile = new File(binding.substring(5));
+ if (!bindingFile.exists() && !bindingFile.isFile()) {
+ System.err.println("The binding file does not exist");
+ return 1;
+ }
- System.out.println("Starting Camel JBang!");
- KameletMain main = new KameletMain();
+ // we can only reload if file based
+ main.addInitialProperty("camel.main.routes-reload-enabled", reload ? "true" : "false");
+ main.addInitialProperty("camel.main.routes-reload-directory", ".");
+ main.addInitialProperty("camel.main.routes-reload-pattern", binding);
+ }
- main.configure().addRoutesBuilder(new ShutdownRoute(createLockFile()));
+ System.out.println("Starting Camel JBang! ZZZ");
main.start();
+
context = main.getCamelContext();
main.run();
diff --git a/dsl/camel-kamelet-main/pom.xml b/dsl/camel-kamelet-main/pom.xml
index 1041be3..cf7bd5c 100644
--- a/dsl/camel-kamelet-main/pom.xml
+++ b/dsl/camel-kamelet-main/pom.xml
@@ -83,6 +83,13 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-slf4j-impl</artifactId>
+ <scope>test</scope>
+ </dependency>
+
</dependencies>
</project>
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
index 854c5f8..3274e80 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
@@ -147,14 +147,7 @@ public class KameletMain extends MainCommandLineSupport {
answer.setApplicationContextClassLoader(kameletClassLoader);
answer.setRegistry(registry);
- addInitialProperty("camel.component.kamelet.location", "classpath:/kamelets,github:apache:camel-kamelets");
- addInitialProperty("camel.main.routes-reload-enabled", "true");
- addInitialProperty("camel.main.routes-reload-directory", "src/main/resources");
- addInitialProperty("camel.main.routes-reload-pattern", "camel/*.yaml");
- // turn off lightweight as we have routes reload enabled
- addInitialProperty("camel.main.lightweight", "false");
- // shutdown quickly
- addInitialProperty("camel.main.shutdown-timeout", "5");
+ configureInitialProperties();
if (download) {
try {
@@ -171,4 +164,18 @@ public class KameletMain extends MainCommandLineSupport {
return answer;
}
+ /**
+ * Sets initial properties that are specific to camel-kamelet-main
+ */
+ protected void configureInitialProperties() {
+ addInitialProperty("camel.component.kamelet.location", "classpath:/kamelets,github:apache:camel-kamelets");
+ addInitialProperty("camel.main.routes-reload-enabled", "true");
+ addInitialProperty("camel.main.routes-reload-directory", "src/main/resources");
+ addInitialProperty("camel.main.routes-reload-pattern", "camel/*.yaml");
+ // turn off lightweight as we have routes reload enabled
+ addInitialProperty("camel.main.lightweight", "false");
+ // shutdown quickly
+ addInitialProperty("camel.main.shutdown-timeout", "5");
+ }
+
}
diff --git a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainGithubTest.java b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainGithubTest.java
new file mode 100644
index 0000000..d62e2e9
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainGithubTest.java
@@ -0,0 +1,35 @@
+/*
+ * 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.main;
+
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+
+@Disabled("Manual test")
+public class KameletMainGithubTest {
+
+ @Test
+ public void testKameletMain() throws Exception {
+ KameletMain main = new KameletMain();
+ main.setDownload(true);
+ main.configure().withDurationMaxSeconds(10);
+// main.configure().withRoutesIncludePattern("github:apache:camel-k:examples/languages/routes.yaml");
+ main.configure().withRoutesIncludePattern("github:apache:camel-examples:examples/main-yaml/src/main/resources/my-route.yaml");
+
+ main.run();
+ }
+}
diff --git a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTest.java b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTest.java
new file mode 100644
index 0000000..c3c163b
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTest.java
@@ -0,0 +1,34 @@
+/*
+ * 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.main;
+
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+
+@Disabled("Manual test")
+public class KameletMainTest {
+
+ @Test
+ public void testKameletMain() throws Exception {
+ KameletMain main = new KameletMain();
+ main.setDownload(true);
+ main.configure().withDurationMaxSeconds(5);
+ main.configure().withRoutesIncludePattern("file:src/test/resources/my-route.yaml");
+
+ main.run();
+ }
+}
diff --git a/dsl/camel-kamelet-main/src/test/resources/log4j2.properties b/dsl/camel-kamelet-main/src/test/resources/log4j2.properties
new file mode 100644
index 0000000..ceb0f20
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/test/resources/log4j2.properties
@@ -0,0 +1,28 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+appender.file.type = File
+appender.file.name = file
+appender.file.fileName = target/camel-kamelet-main-test.log
+appender.file.layout.type = PatternLayout
+appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+appender.out.type = Console
+appender.out.name = out
+appender.out.layout.type = PatternLayout
+appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
+rootLogger.level = INFO
+rootLogger.appenderRef.file.ref = out
diff --git a/dsl/camel-kamelet-main/src/test/resources/my-route.yaml b/dsl/camel-kamelet-main/src/test/resources/my-route.yaml
new file mode 100644
index 0000000..03909be
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/test/resources/my-route.yaml
@@ -0,0 +1,27 @@
+# ---------------------------------------------------------------------------
+# 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.
+# ---------------------------------------------------------------------------
+
+- from:
+ uri: "timer:tick"
+ parameters:
+ period: "1000"
+ steps:
+ - set-body:
+ constant: "Hello Yaml !!!"
+ - transform:
+ simple: "${body.toUpperCase()}"
+ - to: "log:info"
\ No newline at end of file