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 2022/05/23 09:48:47 UTC
[camel] 02/05: CAMEL-18117: camel-jbang - Use profile for commands such as run, uber-jar, which allows the profile to control which properties file to use with settings.
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 895cf7fc771ae717fa5c6dc37abb0d55e521b8b6
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon May 23 10:53:17 2022 +0200
CAMEL-18117: camel-jbang - Use profile for commands such as run, uber-jar, which allows the profile to control which properties file to use with settings.
---
.../dsl/jbang/core/commands/CamelCommand.java | 37 +++++++
.../dsl/jbang/core/commands/CamelJBangMain.java | 17 +++-
.../apache/camel/dsl/jbang/core/commands/Pipe.java | 8 +-
.../dsl/jbang/core/commands/ProfileHelper.java | 25 +++--
.../apache/camel/dsl/jbang/core/commands/Run.java | 108 +++++++++++----------
.../camel/dsl/jbang/core/commands/UberJar.java | 8 +-
6 files changed, 131 insertions(+), 72 deletions(-)
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java
new file mode 100644
index 00000000000..c6888b888a9
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java
@@ -0,0 +1,37 @@
+/*
+ * 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.dsl.jbang.core.commands;
+
+import java.util.concurrent.Callable;
+
+abstract class CamelCommand implements Callable<Integer> {
+
+ CamelJBangMain main;
+
+ public CamelCommand(CamelJBangMain main) {
+ this.main = main;
+ }
+
+ public CamelJBangMain getMain() {
+ return main;
+ }
+
+ public String getProfile() {
+ return main.getProfile();
+ }
+
+}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
index 979082ce800..fc22b313500 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
@@ -26,18 +26,20 @@ import picocli.CommandLine.Command;
@Command(name = "camel", description = "Apache Camel CLI", mixinStandardHelpOptions = true)
public class CamelJBangMain implements Callable<Integer> {
private static CommandLine commandLine;
+
@CommandLine.Option(names = { "--profile" }, scope = CommandLine.ScopeType.INHERIT, defaultValue = "application",
description = "Profile")
private String profile;
public static void run(String... args) {
- commandLine = new CommandLine(new CamelJBangMain())
- .addSubcommand("run", new CommandLine(new Run()))
+ CamelJBangMain main = new CamelJBangMain();
+ commandLine = new CommandLine(main)
+ .addSubcommand("run", new CommandLine(new Run(main)))
.addSubcommand("init", new CommandLine(new Init()))
.addSubcommand("bind", new CommandLine(new Bind()))
- .addSubcommand("pipe", new CommandLine(new Pipe()))
+ .addSubcommand("pipe", new CommandLine(new Pipe(main)))
.addSubcommand("package", new CommandLine(new Package())
- .addSubcommand("uber-jar", new UberJar()))
+ .addSubcommand("uber-jar", new UberJar(main)))
.addSubcommand("generate", new CommandLine(new CodeGenerator())
.addSubcommand("rest", new CodeRestGenerator()))
.addSubcommand("build", new CommandLine(new Build())
@@ -61,7 +63,8 @@ public class CamelJBangMain implements Callable<Integer> {
return new String[] { v };
});
- ProfileHelper.augmentWithProperties(commandLine, args);
+ String profile = ProfileHelper.getProfile(args);
+ ProfileHelper.augmentWithProperties(commandLine, profile, args);
int exitCode = commandLine.execute(args);
System.exit(exitCode);
}
@@ -71,4 +74,8 @@ public class CamelJBangMain implements Callable<Integer> {
commandLine.execute("--help");
return 0;
}
+
+ public String getProfile() {
+ return profile;
+ }
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Pipe.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Pipe.java
index 5b52fe8f62b..39d915d54b4 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Pipe.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Pipe.java
@@ -21,7 +21,7 @@ import java.util.concurrent.Callable;
import picocli.CommandLine;
@CommandLine.Command(name = "pipe", description = "Run Camel in pipe and filters mode for terminal scripting")
-class Pipe implements Callable<Integer> {
+class Pipe extends CamelCommand {
@CommandLine.Parameters(description = "Name of file", arity = "1")
String file;
@@ -57,6 +57,10 @@ class Pipe implements Callable<Integer> {
arity = "0")
String[] property;
+ public Pipe(CamelJBangMain main) {
+ super(main);
+ }
+
@Override
public Integer call() throws Exception {
// remove leading ./ when calling a script in pipe mode
@@ -64,7 +68,7 @@ class Pipe implements Callable<Integer> {
file = file.substring(2);
}
- Run run = new Run();
+ Run run = new Run(main);
run.logging = logging;
run.loggingLevel = loggingLevel;
run.loggingColor = false;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ProfileHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ProfileHelper.java
index 13cd2f42fd5..ded483eda04 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ProfileHelper.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ProfileHelper.java
@@ -47,8 +47,18 @@ public final class ProfileHelper {
private ProfileHelper() {
}
- public static void augmentWithProperties(CommandLine commandLine, String... args) {
- String profile = getProfile(args);
+ public static String getProfile(String... args) {
+ CommandLine.ParseResult results = new CommandLine(new Profile())
+ .setStopAtUnmatched(false)
+ .setStopAtPositional(false).parseArgs(args);
+ if (results.hasMatchedOption(PROFILE)) {
+ return results.matchedOption(PROFILE).getValue().toString();
+ } else {
+ return DEFAULT_PROFILE;
+ }
+ }
+
+ public static void augmentWithProperties(CommandLine commandLine, String profile, String... args) {
propertiesFilename = profile + PROPERTIES_FILE_EXTENSION;
Properties fileProperties = readProperties();
@@ -63,17 +73,6 @@ public final class ProfileHelper {
}
}
- private static String getProfile(String... args) {
- CommandLine.ParseResult results = new CommandLine(new Profile())
- .setStopAtUnmatched(false)
- .setStopAtPositional(false).parseArgs(args);
- if (results.hasMatchedOption(PROFILE)) {
- return results.matchedOption(PROFILE).getValue().toString();
- } else {
- return DEFAULT_PROFILE;
- }
- }
-
private static Properties augmentProperties(Properties properties, final CommandLine commandLine) {
List<String> commonArgumentList
= commonArgumentList(new ArrayList<>(), commandLine.getSubcommands(), commandLine.getCommandName());
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 c28f6793aa7..a1338b54303 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
@@ -36,7 +36,6 @@ import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import java.util.StringJoiner;
-import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -68,7 +67,7 @@ import static org.apache.camel.dsl.jbang.core.commands.GitHubHelper.asGithubSing
import static org.apache.camel.dsl.jbang.core.commands.GitHubHelper.fetchGithubUrls;
@Command(name = "run", description = "Run as local Camel application")
-class Run implements Callable<Integer> {
+class Run extends CamelCommand {
public static final String WORK_DIR = ".camel-jbang";
public static final String RUN_SETTINGS_FILE = "camel-jbang-run.properties";
@@ -169,6 +168,11 @@ class Run implements Callable<Integer> {
@Option(names = { "--open-api" }, description = "Add an OpenAPI spec from the given file")
String openapi;
+
+ public Run(CamelJBangMain main) {
+ super(main);
+ }
+
//CHECKSTYLE:ON
@Override
@@ -228,11 +232,11 @@ class Run implements Callable<Integer> {
return 0;
}
- private OrderedProperties loadApplicationProperties(File source) throws Exception {
+ private OrderedProperties loadProfileProperties(File source) throws Exception {
OrderedProperties prop = new OrderedProperties();
prop.load(new FileInputStream(source));
- // special for routes include pattern that we need to "fix" after reading from application.properties
+ // special for routes include pattern that we need to "fix" after reading from properties
// to make this work in run command
String value = prop.getProperty("camel.main.routesIncludePattern");
if (value != null) {
@@ -251,15 +255,6 @@ class Run implements Callable<Integer> {
return prop;
}
- private static String prefixFile(String line, String key) {
- String value = StringHelper.after(line, key + "=");
- if (value != null) {
- value = value.replaceAll("file:", "classpath:");
- line = key + "=" + value;
- }
- return line;
- }
-
private int run() throws Exception {
File work = new File(WORK_DIR);
removeDir(work);
@@ -270,24 +265,34 @@ class Run implements Callable<Integer> {
generateOpenApi();
}
- OrderedProperties applicationProperties = null;
+ OrderedProperties profileProperties = null;
+ File profilePropertiesFile = new File(getProfile() + ".properties");
+ if (profilePropertiesFile.exists()) {
+ profileProperties = loadProfileProperties(profilePropertiesFile);
+ // logging level/color may be configured in the properties file
+ loggingLevel = profileProperties.getProperty("loggingLevel", loggingLevel);
+ loggingColor
+ = "true".equals(profileProperties.getProperty("loggingColor", loggingColor ? "true" : "false"));
+ loggingJson
+ = "true".equals(profileProperties.getProperty("loggingJson", loggingJson ? "true" : "false"));
+ if (propertiesFiles == null) {
+ propertiesFiles = "file:" + profilePropertiesFile.getName();
+ } else {
+ propertiesFiles = propertiesFiles + ",file:" + profilePropertiesFile.getName();
+ }
+ }
+
+ // if no specific file to run then try to auto-detect
if (files == null || files.length == 0) {
- // special when no files have been specified then we use application.properties as source to know what to run
- File source = new File("application.properties");
- if (source.exists()) {
- applicationProperties = loadApplicationProperties(source);
- // logging level/color may be configured in the properties file
- loggingLevel = applicationProperties.getProperty("loggingLevel", loggingLevel);
- loggingColor
- = "true".equals(applicationProperties.getProperty("loggingColor", loggingColor ? "true" : "false"));
- loggingJson
- = "true".equals(applicationProperties.getProperty("loggingJson", loggingJson ? "true" : "false"));
- } else if (!silentRun && !source.exists()) {
- System.out.println("Cannot run because application.properties file does not exist");
- return 1;
- } else if (silentRun) {
- // silent-run then auto-detect all files
- files = new File(".").list();
+ String routes = profileProperties != null ? profileProperties.getProperty("camel.main.routesIncludePattern") : null;
+ if (routes == null) {
+ if (!silentRun) {
+ System.out.println("Cannot run because " + getProfile() + ".properties file does not exist");
+ return 1;
+ } else {
+ // silent-run then auto-detect all files
+ files = new File(".").list();
+ }
}
}
@@ -306,20 +311,20 @@ class Run implements Callable<Integer> {
});
main.setAppName("Apache Camel (JBang)");
- writeSetting(main, applicationProperties, "camel.main.name", name);
+ writeSetting(main, profileProperties, "camel.main.name", name);
if (dev) {
// allow quick shutdown during development
- writeSetting(main, applicationProperties, "camel.main.shutdownTimeout", "5");
+ writeSetting(main, profileProperties, "camel.main.shutdownTimeout", "5");
}
- writeSetting(main, applicationProperties, "camel.main.routesReloadEnabled", dev ? "true" : "false");
- writeSetting(main, applicationProperties, "camel.main.sourceLocationEnabled", "true");
- writeSetting(main, applicationProperties, "camel.main.tracing", trace ? "true" : "false");
- writeSetting(main, applicationProperties, "camel.main.modeline", modeline ? "true" : "false");
+ writeSetting(main, profileProperties, "camel.main.routesReloadEnabled", dev ? "true" : "false");
+ writeSetting(main, profileProperties, "camel.main.sourceLocationEnabled", "true");
+ writeSetting(main, profileProperties, "camel.main.tracing", trace ? "true" : "false");
+ writeSetting(main, profileProperties, "camel.main.modeline", modeline ? "true" : "false");
// allow java-dsl to compile to .class which we need in uber-jar mode
- writeSetting(main, applicationProperties, "camel.main.routesCompileDirectory", WORK_DIR);
- writeSetting(main, applicationProperties, "camel.jbang.dependencies", dependencies);
- writeSetting(main, applicationProperties, "camel.jbang.health", health ? "true" : "false");
- writeSetting(main, applicationProperties, "camel.jbang.console", console ? "true" : "false");
+ writeSetting(main, profileProperties, "camel.main.routesCompileDirectory", WORK_DIR);
+ writeSetting(main, profileProperties, "camel.jbang.dependencies", dependencies);
+ writeSetting(main, profileProperties, "camel.jbang.health", health ? "true" : "false");
+ writeSetting(main, profileProperties, "camel.jbang.console", console ? "true" : "false");
// command line arguments
if (property != null) {
@@ -343,16 +348,16 @@ class Run implements Callable<Integer> {
// auto terminate if being idle
main.addInitialProperty("camel.main.durationMaxIdleSeconds", "1");
}
- writeSetting(main, applicationProperties, "camel.main.durationMaxMessages",
+ writeSetting(main, profileProperties, "camel.main.durationMaxMessages",
() -> maxMessages > 0 ? String.valueOf(maxMessages) : null);
- writeSetting(main, applicationProperties, "camel.main.durationMaxSeconds",
+ writeSetting(main, profileProperties, "camel.main.durationMaxSeconds",
() -> maxSeconds > 0 ? String.valueOf(maxSeconds) : null);
- writeSetting(main, applicationProperties, "camel.main.durationMaxIdleSeconds",
+ writeSetting(main, profileProperties, "camel.main.durationMaxIdleSeconds",
() -> maxIdleSeconds > 0 ? String.valueOf(maxIdleSeconds) : null);
- writeSetting(main, applicationProperties, "camel.jbang.platform-http.port",
+ writeSetting(main, profileProperties, "camel.jbang.platform-http.port",
() -> port > 0 ? String.valueOf(port) : null);
- writeSetting(main, applicationProperties, "camel.jbang.jfr", jfr || jfrProfile != null ? "jfr" : null);
- writeSetting(main, applicationProperties, "camel.jbang.jfr-profile", jfrProfile != null ? jfrProfile : null);
+ writeSetting(main, profileProperties, "camel.jbang.jfr", jfr || jfrProfile != null ? "jfr" : null);
+ writeSetting(main, profileProperties, "camel.jbang.jfr-profile", jfrProfile != null ? jfrProfile : null);
if (fileLock) {
lockFile = createLockFile();
@@ -459,13 +464,13 @@ class Run implements Callable<Integer> {
main.addInitialProperty("camel.main.routesIncludePattern", js.toString());
writeSettings("camel.main.routesIncludePattern", js.toString());
} else {
- writeSetting(main, applicationProperties, "camel.main.routesIncludePattern", () -> null);
+ writeSetting(main, profileProperties, "camel.main.routesIncludePattern", () -> null);
}
if (sjClasspathFiles.length() > 0) {
main.addInitialProperty("camel.jbang.classpathFiles", sjClasspathFiles.toString());
writeSettings("camel.jbang.classpathFiles", sjClasspathFiles.toString());
} else {
- writeSetting(main, applicationProperties, "camel.jbang.classpathFiles", () -> null);
+ writeSetting(main, profileProperties, "camel.jbang.classpathFiles", () -> null);
}
if (sjKamelets.length() > 0) {
@@ -478,7 +483,7 @@ class Run implements Callable<Integer> {
main.addInitialProperty("camel.component.kamelet.location", loc);
writeSettings("camel.component.kamelet.location", loc);
} else {
- writeSetting(main, applicationProperties, "camel.component.kamelet.location", () -> null);
+ writeSetting(main, profileProperties, "camel.component.kamelet.location", () -> null);
}
// we can only reload if file based
@@ -501,7 +506,10 @@ class Run implements Callable<Integer> {
}
file = "file://" + file;
}
- locations.append(file).append(",");
+ if (locations.length() > 0) {
+ locations.append(",");
+ }
+ locations.append(file);
}
// there may be existing properties
String loc = main.getInitialProperties().getProperty("camel.component.properties.location");
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java
index 6a3e7f971f3..709167c4d9c 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java
@@ -47,7 +47,7 @@ import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Command(name = "uber-jar", description = "Package application as a single uber-jar")
-class UberJar implements Callable<Integer> {
+class UberJar extends CamelCommand {
private static final String BUILD_DIR = ".camel-jbang/work";
private static final String CLASSES_DIR = BUILD_DIR + "/classes";
@@ -72,6 +72,10 @@ class UberJar implements Callable<Integer> {
@Option(names = { "--fresh" }, description = "Make sure we use fresh (i.e. non-cached) resources")
private boolean fresh;
+ public UberJar(CamelJBangMain main) {
+ super(main);
+ }
+
@Override
public Integer call() throws Exception {
// the settings file has information what to package in uber-jar so we need to read it from the run command
@@ -174,7 +178,7 @@ class UberJar implements Callable<Integer> {
}
private Integer runSilently() throws Exception {
- Run run = new Run();
+ Run run = new Run(main);
Integer code = run.runSilent();
return code;
}