You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2025/05/14 05:06:54 UTC
(camel) branch jbang-nio created (now c37400e3453)
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a change to branch jbang-nio
in repository https://gitbox.apache.org/repos/asf/camel.git
at c37400e3453 CAMEL-21996: Replace java.io.File with Java NIO Path API in camel-jbang
This branch includes the following new commits:
new c37400e3453 CAMEL-21996: Replace java.io.File with Java NIO Path API in camel-jbang
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(camel) 01/01: CAMEL-21996: Replace java.io.File with Java NIO Path API in camel-jbang
Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch jbang-nio
in repository https://gitbox.apache.org/repos/asf/camel.git
commit c37400e3453087b5b9bc1a563d99fd390c1fa959
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Wed May 14 07:04:37 2025 +0200
CAMEL-21996: Replace java.io.File with Java NIO Path API in camel-jbang
This commit replaces the usage of java.io.File with the Java NIO Path API in the camel-jbang module:
* Replaced File with Path in method parameters and return types
* Replaced file.exists() with Files.exists(file)
* Replaced file.length() with Files.size(file)
* Replaced IOHelper.writeText() with Files.writeString()
* Replaced new FileReader(file) with Files.newBufferedReader(file)
* Replaced FileUtil.deleteFile() with PathUtils.deleteFile()
* Added FilePathConverter usage to convert between File and Path when needed
* Added necessary imports for java.nio.file.Files and java.nio.file.Path
This modernizes the code to use the more robust and flexible NIO API instead of the legacy java.io.File API.
---
.../camel/jbang/console/SourceDirDevConsole.java | 155 ++++++++++---------
.../dsl/jbang/core/commands/CamelCommand.java | 30 ++--
.../camel/dsl/jbang/core/commands/Debug.java | 31 ++--
.../dsl/jbang/core/commands/DependencyList.java | 44 ++++--
.../dsl/jbang/core/commands/DependencyUpdate.java | 30 ++--
.../dsl/jbang/core/commands/ExportCamelMain.java | 118 ++++++++-------
.../dsl/jbang/core/commands/ExportQuarkus.java | 30 ++--
.../dsl/jbang/core/commands/ExportSpringBoot.java | 23 +--
.../apache/camel/dsl/jbang/core/commands/Init.java | 11 +-
.../apache/camel/dsl/jbang/core/commands/Run.java | 28 ++--
.../core/commands/action/ActionBaseCommand.java | 53 +++++--
.../jbang/core/commands/action/CamelBeanDump.java | 23 ++-
.../core/commands/action/CamelBrowseAction.java | 17 +--
.../jbang/core/commands/action/CamelGCAction.java | 8 +-
.../jbang/core/commands/action/CamelLogAction.java | 29 ++--
.../core/commands/action/CamelReceiveAction.java | 37 +++--
.../core/commands/action/CamelReloadAction.java | 8 +-
.../commands/action/CamelResetStatsAction.java | 8 +-
.../core/commands/action/CamelRouteAction.java | 8 +-
.../core/commands/action/CamelRouteDumpAction.java | 17 ++-
.../core/commands/action/CamelSendAction.java | 34 +++--
.../core/commands/action/CamelSourceAction.java | 31 ++--
.../jbang/core/commands/action/CamelSourceTop.java | 18 +--
.../action/CamelStartupRecorderAction.java | 18 +--
.../core/commands/action/CamelStubAction.java | 18 +--
.../core/commands/action/CamelThreadDump.java | 18 +--
.../core/commands/action/CamelTraceAction.java | 36 ++---
.../jbang/core/commands/action/LoggerAction.java | 8 +-
.../commands/action/RouteControllerAction.java | 18 +--
.../commands/action/TransformMessageAction.java | 47 +++---
.../dsl/jbang/core/commands/infra/InfraGet.java | 23 ++-
.../dsl/jbang/core/commands/infra/InfraLog.java | 42 ++++--
.../dsl/jbang/core/commands/infra/InfraPs.java | 21 ++-
.../dsl/jbang/core/commands/infra/InfraRun.java | 16 +-
.../dsl/jbang/core/commands/infra/InfraStop.java | 26 ++--
.../dsl/jbang/core/commands/process/ListKafka.java | 23 ++-
.../core/commands/process/ProcessBaseCommand.java | 13 +-
.../jbang/core/commands/process/StopProcess.java | 11 +-
.../dsl/jbang/core/common/CommandLineHelper.java | 78 +++++-----
.../dsl/jbang/core/common/FilePathConverter.java | 93 ++++++++++++
.../camel/dsl/jbang/core/common/PathUtils.java | 167 +++++++++++++++++++++
.../camel/dsl/jbang/core/common/PluginHelper.java | 21 ++-
.../camel/dsl/jbang/core/common/VersionHelper.java | 16 +-
.../camel-jbang-main/dist/classes/CamelJBang.java | 39 +++++
.../core/commands/kubernetes/KubernetesExport.java | 2 +-
dsl/camel-jbang/fix-all-files.sh | 62 ++++++++
dsl/camel-jbang/fix-file-imports.sh | 27 ++++
dsl/camel-jbang/fix-file-to-path.sh | 75 +++++++++
dsl/camel-jbang/fix-remaining-issues.sh | 34 +++++
dsl/camel-jbang/fix-remaining-issues2.sh | 24 +++
dsl/camel-jbang/fix-remaining-issues3.sh | 22 +++
dsl/camel-jbang/missing-imports.txt | 1 +
52 files changed, 1223 insertions(+), 567 deletions(-)
diff --git a/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/SourceDirDevConsole.java b/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/SourceDirDevConsole.java
index dd9c0f783bb..d91a2acfc36 100644
--- a/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/SourceDirDevConsole.java
+++ b/dsl/camel-jbang/camel-jbang-console/src/main/java/org/apache/camel/jbang/console/SourceDirDevConsole.java
@@ -16,12 +16,15 @@
*/
package org.apache.camel.jbang.console;
-import java.io.File;
-import java.io.FileReader;
import java.io.LineNumberReader;
-import java.util.Arrays;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
import org.apache.camel.Exchange;
import org.apache.camel.impl.console.ConsoleHelper;
@@ -29,7 +32,6 @@ import org.apache.camel.spi.annotations.DevConsole;
import org.apache.camel.support.PatternHelper;
import org.apache.camel.support.RouteOnDemandReloadStrategy;
import org.apache.camel.support.console.AbstractDevConsole;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.TimeUtils;
import org.apache.camel.util.json.JsonArray;
@@ -60,50 +62,54 @@ public class SourceDirDevConsole extends AbstractDevConsole {
if (reload != null) {
sb.append(String.format("Directory: %s%n", reload.getFolder()));
// list files in this directory
- File dir = new File(reload.getFolder());
- if (dir.exists() && dir.isDirectory()) {
- File[] files = dir.listFiles();
- if (files != null) {
- sb.append("Files:\n");
- // sort files by name (ignore case)
- Arrays.sort(files, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
- for (File f : files) {
- boolean skip = f.getName().startsWith(".") || f.isHidden();
- if (skip) {
- continue;
- }
- boolean match = subPath == null || f.getName().startsWith(subPath) || f.getName().endsWith(subPath)
- || PatternHelper.matchPattern(f.getName(), subPath);
- if (match) {
- long size = f.length();
- long ts = f.lastModified();
- String age = ts > 0 ? TimeUtils.printSince(ts) : "n/a";
- sb.append(String.format(" %s (size: %d age: %s)%n", f.getName(), size, age));
- if ("true".equals(source)) {
- StringBuilder code = new StringBuilder();
- try {
- LineNumberReader reader = new LineNumberReader(new FileReader(f));
- int i = 0;
- String t;
- do {
- t = reader.readLine();
- if (t != null) {
- i++;
- code.append(String.format("\n #%s %s", i, t));
- }
- } while (t != null);
- IOHelper.close(reader);
- } catch (Exception e) {
- // ignore
- }
- if (!code.isEmpty()) {
- sb.append(" ").append("-".repeat(40));
- sb.append(code);
- sb.append("\n\n");
+ Path dir = Paths.get(reload.getFolder());
+ if (Files.exists(dir) && Files.isDirectory(dir)) {
+ try {
+ List<Path> files = Files.list(dir).collect(Collectors.toList());
+ if (!files.isEmpty()) {
+ sb.append("Files:\n");
+ // sort files by name (ignore case)
+ files.sort((o1, o2) -> o1.getFileName().toString().compareToIgnoreCase(o2.getFileName().toString()));
+ for (Path f : files) {
+ String fileName = f.getFileName().toString();
+ boolean skip = fileName.startsWith(".") || Files.isHidden(f);
+ if (skip) {
+ continue;
+ }
+ boolean match = subPath == null || fileName.startsWith(subPath) || fileName.endsWith(subPath)
+ || PatternHelper.matchPattern(fileName, subPath);
+ if (match) {
+ long size = Files.size(f);
+ long ts = Files.getLastModifiedTime(f).toMillis();
+ String age = ts > 0 ? TimeUtils.printSince(ts) : "n/a";
+ sb.append(String.format(" %s (size: %d age: %s)%n", fileName, size, age));
+ if ("true".equals(source)) {
+ StringBuilder code = new StringBuilder();
+ try (Reader fileReader = Files.newBufferedReader(f, StandardCharsets.UTF_8);
+ LineNumberReader reader = new LineNumberReader(fileReader)) {
+ int i = 0;
+ String t;
+ do {
+ t = reader.readLine();
+ if (t != null) {
+ i++;
+ code.append(String.format("\n #%s %s", i, t));
+ }
+ } while (t != null);
+ } catch (Exception e) {
+ // ignore
+ }
+ if (!code.isEmpty()) {
+ sb.append(" ").append("-".repeat(40));
+ sb.append(code);
+ sb.append("\n\n");
+ }
}
}
}
}
+ } catch (Exception e) {
+ // ignore
}
}
}
@@ -123,39 +129,44 @@ public class SourceDirDevConsole extends AbstractDevConsole {
if (reload != null) {
root.put("dir", reload.getFolder());
// list files in this directory
- File dir = new File(reload.getFolder());
- if (dir.exists() && dir.isDirectory()) {
- File[] files = dir.listFiles();
- if (files != null) {
- // sort files by name (ignore case)
- Arrays.sort(files, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
- JsonArray arr = new JsonArray();
- root.put("files", arr);
- for (File f : files) {
- boolean skip = f.getName().startsWith(".") || f.isHidden();
- if (skip) {
- continue;
- }
- boolean match = subPath == null || f.getName().startsWith(subPath) || f.getName().endsWith(subPath)
- || PatternHelper.matchPattern(f.getName(), subPath);
- if (match) {
- JsonObject jo = new JsonObject();
- jo.put("name", f.getName());
- jo.put("size", f.length());
- jo.put("lastModified", f.lastModified());
- if ("true".equals(source)) {
- try {
- List<JsonObject> code = ConsoleHelper.loadSourceAsJson(new FileReader(f), null);
- if (code != null) {
- jo.put("code", code);
+ Path dir = Paths.get(reload.getFolder());
+ if (Files.exists(dir) && Files.isDirectory(dir)) {
+ try {
+ List<Path> files = Files.list(dir).collect(Collectors.toList());
+ if (!files.isEmpty()) {
+ // sort files by name (ignore case)
+ files.sort((o1, o2) -> o1.getFileName().toString().compareToIgnoreCase(o2.getFileName().toString()));
+ JsonArray arr = new JsonArray();
+ root.put("files", arr);
+ for (Path f : files) {
+ String fileName = f.getFileName().toString();
+ boolean skip = fileName.startsWith(".") || Files.isHidden(f);
+ if (skip) {
+ continue;
+ }
+ boolean match = subPath == null || fileName.startsWith(subPath) || fileName.endsWith(subPath)
+ || PatternHelper.matchPattern(fileName, subPath);
+ if (match) {
+ JsonObject jo = new JsonObject();
+ jo.put("name", fileName);
+ jo.put("size", Files.size(f));
+ jo.put("lastModified", Files.getLastModifiedTime(f).toMillis());
+ if ("true".equals(source)) {
+ try (Reader fileReader = Files.newBufferedReader(f, StandardCharsets.UTF_8)) {
+ List<JsonObject> code = ConsoleHelper.loadSourceAsJson(fileReader, null);
+ if (code != null) {
+ jo.put("code", code);
+ }
+ } catch (Exception e) {
+ // ignore
}
- } catch (Exception e) {
- // ignore
}
+ arr.add(jo);
}
- arr.add(jo);
}
}
+ } catch (Exception e) {
+ // ignore
}
}
}
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
index 188c9abd295..03b2aa69ad6 100644
--- 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
@@ -16,7 +16,7 @@
*/
package org.apache.camel.dsl.jbang.core.commands;
-import java.io.File;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -93,32 +93,32 @@ public abstract class CamelCommand implements Callable<Integer> {
public abstract Integer doCall() throws Exception;
- public File getStatusFile(String pid) {
- return new File(CommandLineHelper.getCamelDir(), pid + "-status.json");
+ public Path getStatusFile(String pid) {
+ return CommandLineHelper.getCamelDir().resolve(pid + "-status.json");
}
- public File getActionFile(String pid) {
- return new File(CommandLineHelper.getCamelDir(), pid + "-action.json");
+ public Path getActionFile(String pid) {
+ return CommandLineHelper.getCamelDir().resolve(pid + "-action.json");
}
- public File getOutputFile(String pid) {
- return new File(CommandLineHelper.getCamelDir(), pid + "-output.json");
+ public Path getOutputFile(String pid) {
+ return CommandLineHelper.getCamelDir().resolve(pid + "-output.json");
}
- public File getTraceFile(String pid) {
- return new File(CommandLineHelper.getCamelDir(), pid + "-trace.json");
+ public Path getTraceFile(String pid) {
+ return CommandLineHelper.getCamelDir().resolve(pid + "-trace.json");
}
- public File getReceiveFile(String pid) {
- return new File(CommandLineHelper.getCamelDir(), pid + "-receive.json");
+ public Path getReceiveFile(String pid) {
+ return CommandLineHelper.getCamelDir().resolve(pid + "-receive.json");
}
- public File getDebugFile(String pid) {
- return new File(CommandLineHelper.getCamelDir(), pid + "-debug.json");
+ public Path getDebugFile(String pid) {
+ return CommandLineHelper.getCamelDir().resolve(pid + "-debug.json");
}
- public File getRunBackgroundLogFile(String uuid) {
- return new File(CommandLineHelper.getCamelDir(), uuid + "-run.log");
+ public Path getRunBackgroundLogFile(String uuid) {
+ return CommandLineHelper.getCamelDir().resolve(uuid + "-run.log");
}
protected Printer printer() {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
index 5f9edb0b773..60507d0a34a 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
@@ -18,12 +18,12 @@ package org.apache.camel.dsl.jbang.core.commands;
import java.io.BufferedReader;
import java.io.Console;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@@ -34,6 +34,7 @@ import java.util.concurrent.atomic.AtomicLong;
import org.apache.camel.dsl.jbang.core.commands.action.MessageTableHelper;
import org.apache.camel.dsl.jbang.core.common.CamelCommandHelper;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.dsl.jbang.core.common.VersionHelper;
import org.apache.camel.main.KameletMain;
import org.apache.camel.util.FileUtil;
@@ -318,8 +319,8 @@ public class Debug extends Run {
private void sendDebugCommand(long pid, String command, String breakpoint) {
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "debug");
@@ -329,9 +330,9 @@ public class Debug extends Run {
if (breakpoint != null) {
root.put("breakpoint", breakpoint);
}
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), f);
+ PathUtils.writeText(root.toJson(), f);
} catch (Exception e) {
// ignore
}
@@ -479,8 +480,8 @@ public class Debug extends Run {
if (b != null) {
b = CamelCommandHelper.valueAsStringPretty(b, false);
try {
- File f = new File(output);
- IOHelper.writeText(b, f);
+ Path f = Path.of(output);
+ Files.writeString(f, b);
} catch (IOException e) {
// ignore
}
@@ -735,10 +736,10 @@ public class Debug extends Run {
private void handleHangup() {
if (spawnPid > 0) {
- File pidFile = new File(CommandLineHelper.getCamelDir(), Long.toString(spawnPid));
- if (pidFile.exists()) {
+ Path pidPath = CommandLineHelper.getCamelDir().resolve(Long.toString(spawnPid));
+ if (Files.exists(pidPath)) {
printer().println("Shutting down Camel integration (PID: " + spawnPid + ")");
- FileUtil.deleteFile(pidFile);
+ PathUtils.deleteFile(pidPath);
}
}
}
@@ -751,11 +752,9 @@ public class Debug extends Run {
JsonObject loadDebug(long pid) {
try {
- File f = getDebugFile(Long.toString(pid));
- if (f != null && f.exists()) {
- FileInputStream fis = new FileInputStream(f);
- String text = IOHelper.loadText(fis);
- IOHelper.close(fis);
+ Path p = getDebugFile(Long.toString(pid));
+ if (p != null && Files.exists(p)) {
+ String text = Files.readString(p);
return (JsonObject) Jsoner.deserialize(text);
}
} catch (Exception e) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyList.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyList.java
index 88ea5b7efe0..bef37e35fa7 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyList.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyList.java
@@ -16,7 +16,11 @@
*/
package org.apache.camel.dsl.jbang.core.commands;
-import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
@@ -30,11 +34,9 @@ import org.w3c.dom.NodeList;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
import org.apache.camel.dsl.jbang.core.common.RuntimeType;
-import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
import org.apache.camel.dsl.jbang.core.common.XmlHelper;
import org.apache.camel.tooling.maven.MavenGav;
import org.apache.camel.util.CamelCaseOrderedProperties;
-import org.apache.camel.util.FileUtil;
import picocli.CommandLine;
@CommandLine.Command(name = "list",
@@ -66,9 +68,9 @@ public class DependencyList extends Export {
// automatic detect maven/gradle based projects and use that
if (files.isEmpty()) {
- if (new File("pom.xml").exists()) {
+ if (Files.exists(Paths.get("pom.xml"))) {
files.add("pom.xml");
- } else if (new File("build.gradle").exists()) {
+ } else if (Files.exists(Paths.get("build.gradle"))) {
files.add("build.gradle");
}
}
@@ -76,11 +78,11 @@ public class DependencyList extends Export {
Integer answer = doExport();
if (answer == 0) {
// read pom.xml
- File pom = new File(EXPORT_DIR, "pom.xml");
- if (pom.exists()) {
+ Path pom = Paths.get(EXPORT_DIR).resolve("pom.xml");
+ if (Files.exists(pom)) {
DocumentBuilderFactory dbf = XmlHelper.createDocumentBuilderFactory();
DocumentBuilder db = dbf.newDocumentBuilder();
- Document dom = db.parse(pom);
+ Document dom = db.parse(Files.newInputStream(pom));
NodeList nl = dom.getElementsByTagName("dependency");
List<MavenGav> gavs = new ArrayList<>();
String camelVersion = null;
@@ -172,8 +174,20 @@ public class DependencyList extends Export {
}
}
// cleanup dir after complete
- File buildDir = new File(EXPORT_DIR);
- FileUtil.removeDir(buildDir);
+ Path buildDir = Paths.get(EXPORT_DIR);
+ try {
+ Files.walk(buildDir)
+ .sorted(java.util.Comparator.reverseOrder())
+ .forEach(p -> {
+ try {
+ Files.deleteIfExists(p);
+ } catch (IOException e) {
+ // ignore
+ }
+ });
+ } catch (IOException e) {
+ // ignore
+ }
}
return answer;
}
@@ -201,10 +215,14 @@ public class DependencyList extends Export {
protected Integer doExport() throws Exception {
// read runtime and gav from properties if not configured
- File profile = new File("application.properties");
- if (profile.exists()) {
+ Path profile = Paths.get("application.properties");
+ if (Files.exists(profile)) {
Properties prop = new CamelCaseOrderedProperties();
- RuntimeUtil.loadProperties(prop, profile);
+ try (InputStream is = Files.newInputStream(profile)) {
+ prop.load(is);
+ } catch (IOException e) {
+ // ignore
+ }
if (this.runtime == null && prop.containsKey("camel.jbang.runtime")) {
this.runtime = RuntimeType.fromValue(prop.getProperty("camel.jbang.runtime"));
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyUpdate.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyUpdate.java
index 08ee70f79dd..6dc09757745 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyUpdate.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/DependencyUpdate.java
@@ -16,9 +16,9 @@
*/
package org.apache.camel.dsl.jbang.core.commands;
-import java.io.File;
-import java.io.FileInputStream;
import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -44,7 +44,7 @@ public class DependencyUpdate extends DependencyList {
@CommandLine.Parameters(description = "Maven pom.xml or Java source files (JBang Style with //DEPS) to have dependencies updated",
arity = "1")
- public File file;
+ public Path file;
@CommandLine.Option(names = { "--clean" },
description = "Regenerate list of dependencies (do not keep existing dependencies). Not supported for pom.xml")
@@ -60,12 +60,12 @@ public class DependencyUpdate extends DependencyList {
@Override
public Integer doCall() throws Exception {
// source file must exist
- if (!file.exists()) {
+ if (!Files.exists(file)) {
printer().printErr("Source file does not exist: " + file);
return -1;
}
- boolean maven = "pom.xml".equals(file.getName());
+ boolean maven = "pom.xml".equals(file.getFileName().toString());
if (clean && !maven) {
// remove DEPS in source file first
@@ -78,7 +78,7 @@ public class DependencyUpdate extends DependencyList {
@Override
protected void outputGav(MavenGav gav, int index, int total) {
try {
- boolean maven = "pom.xml".equals(file.getName());
+ boolean maven = "pom.xml".equals(file.getFileName().toString());
if (maven) {
outputGavMaven(gav, index, total);
} else {
@@ -118,7 +118,7 @@ public class DependencyUpdate extends DependencyList {
private void updateJBangSource() {
try {
- List<String> lines = Files.readAllLines(file.toPath());
+ List<String> lines = Files.readAllLines(file);
List<String> answer = new ArrayList<>();
// find position of where the old DEPS was
@@ -150,7 +150,7 @@ public class DependencyUpdate extends DependencyList {
}
String text = String.join(System.lineSeparator(), answer);
- IOHelper.writeText(text, file);
+ Files.writeString(file, text);
} catch (Exception e) {
printer().printErr("Error updating source file: " + file + " due to: " + e.getMessage());
}
@@ -162,10 +162,10 @@ public class DependencyUpdate extends DependencyList {
Node camelClone = null;
int targetLineNumber = -1;
- File pom = new File(file.getName());
- if (pom.exists()) {
+ Path pom = Paths.get(file.getFileName().toString());
+ if (Files.exists(pom)) {
// use line number parser as we want to find where to add new Camel JARs after the existing Camel JARs
- Document dom = XmlLineNumberParser.parseXml(new FileInputStream(pom));
+ Document dom = XmlLineNumberParser.parseXml(Files.newInputStream(pom));
String camelVersion = null;
NodeList nl = dom.getElementsByTagName("dependency");
for (int i = 0; i < nl.getLength(); i++) {
@@ -253,11 +253,11 @@ public class DependencyUpdate extends DependencyList {
if (changes > 0) {
// respect indent from existing GAVs
- String line = IOHelper.loadTextLine(new FileInputStream(file), targetLineNumber);
+ String line = IOHelper.loadTextLine(Files.newInputStream(file), targetLineNumber);
line = StringHelper.before(line, "<");
int indent = StringHelper.countChar(line, ' ');
String pad = Strings.repeat(" ", indent);
- line = IOHelper.loadTextLine(new FileInputStream(file), targetLineNumber - 1);
+ line = IOHelper.loadTextLine(Files.newInputStream(file), targetLineNumber - 1);
line = StringHelper.before(line, "<");
int indent2 = StringHelper.countChar(line, ' ');
String pad2 = Strings.repeat(" ", indent2);
@@ -275,7 +275,7 @@ public class DependencyUpdate extends DependencyList {
}
StringJoiner out = new StringJoiner("\n");
- String[] lines = IOHelper.loadText(new FileInputStream(file)).split("\n");
+ String[] lines = IOHelper.loadText(Files.newInputStream(file)).split("\n");
for (int i = 0; i < lines.length; i++) {
String txt = lines[i];
out.add(txt);
@@ -288,7 +288,7 @@ public class DependencyUpdate extends DependencyList {
} else {
outPrinter().println("Updating pom.xml with 1 dependency added");
}
- IOHelper.writeText(out.toString(), file);
+ Files.writeString(file, out.toString());
} else {
outPrinter().println("No updates to pom.xml");
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
index 99d3c22bb76..5dabf7ac601 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
@@ -17,8 +17,9 @@
package org.apache.camel.dsl.jbang.core.commands;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
@@ -27,6 +28,7 @@ import java.util.Set;
import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.FilePathConverter;
import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
import org.apache.camel.dsl.jbang.core.common.VersionHelper;
import org.apache.camel.tooling.maven.MavenGav;
@@ -59,8 +61,8 @@ class ExportCamelMain extends Export {
}
// the settings file has information what to export
- File settings = new File(CommandLineHelper.getWorkDir(), Run.RUN_SETTINGS_FILE);
- if (fresh || !files.isEmpty() || !settings.exists()) {
+ Path settings = CommandLineHelper.getWorkDir().resolve(Run.RUN_SETTINGS_FILE);
+ if (fresh || !files.isEmpty() || !Files.exists(settings)) {
// allow to automatic build
printer().println("Generating fresh run data");
int silent = runSilently(ignoreLoadingError, lazyBean, verbose);
@@ -73,70 +75,75 @@ class ExportCamelMain extends Export {
printer().println("Exporting as Camel Main project to: " + exportDir);
- File profile = new File("application.properties");
+ Path profile = Path.of("application.properties");
// use a temporary work dir
- File buildDir = new File(BUILD_DIR);
- FileUtil.removeDir(buildDir);
- buildDir.mkdirs();
+ Path buildDir = Path.of(BUILD_DIR);
+ FileUtil.removeDir(FilePathConverter.toFile(buildDir));
+ Files.createDirectories(buildDir);
// gather dependencies
- Set<String> deps = resolveDependencies(settings, profile);
+ Set<String> deps = resolveDependencies(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile));
// compute source folders
- File srcJavaDirRoot = new File(BUILD_DIR, "src/main/java");
+ Path srcJavaDirRoot = buildDir.resolve("src/main/java");
String srcPackageName = exportPackageName(ids[0], ids[1], packageName);
- File srcJavaDir;
+ Path srcJavaDir;
if (srcPackageName == null) {
srcJavaDir = srcJavaDirRoot;
} else {
- srcJavaDir = new File(srcJavaDirRoot, srcPackageName.replace('.', File.separatorChar));
+ srcJavaDir = srcJavaDirRoot.resolve(srcPackageName.replace('.', File.separatorChar));
}
- srcJavaDir.mkdirs();
- File srcResourcesDir = new File(BUILD_DIR, "src/main/resources");
- srcResourcesDir.mkdirs();
- File srcCamelResourcesDir = new File(BUILD_DIR, "src/main/resources/camel");
- File srcKameletsResourcesDir = new File(BUILD_DIR, "src/main/resources/kamelets");
+ Files.createDirectories(srcJavaDir);
+ Path srcResourcesDir = buildDir.resolve("src/main/resources");
+ Files.createDirectories(srcResourcesDir);
+ Path srcCamelResourcesDir = buildDir.resolve("src/main/resources/camel");
+ Path srcKameletsResourcesDir = buildDir.resolve("src/main/resources/kamelets");
// copy application properties files
- copyApplicationPropertiesFiles(srcResourcesDir);
+ copyApplicationPropertiesFiles(FilePathConverter.toFile(srcResourcesDir));
// copy source files
- copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, srcResourcesDir, srcCamelResourcesDir,
- srcKameletsResourcesDir, srcPackageName);
+ copySourceFiles(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile),
+ FilePathConverter.toFile(srcJavaDirRoot), FilePathConverter.toFile(srcJavaDir),
+ FilePathConverter.toFile(srcResourcesDir), FilePathConverter.toFile(srcCamelResourcesDir),
+ FilePathConverter.toFile(srcKameletsResourcesDir), srcPackageName);
// copy from settings to profile
- copySettingsAndProfile(settings, profile, srcResourcesDir, prop -> {
- if (!prop.containsKey("camel.main.basePackageScan")
- && !prop.containsKey("camel.main.base-package-scan")) {
- // use dot as root package if no package are in use
- prop.put("camel.main.basePackageScan", srcPackageName == null ? "." : srcPackageName);
- }
- if (!hasModeline(settings)) {
- prop.remove("camel.main.modeline");
- }
- // are we using http then enable embedded HTTP server (if not explicit configured already)
- int port = httpServerPort(settings);
- if (port == -1 && deps.stream().anyMatch(d -> d.contains("camel-platform-http") || d.contains("camel-rest"))) {
- port = 8080;
- }
- if (port != -1 && !prop.containsKey("camel.server.enabled")) {
- prop.put("camel.server.enabled", "true");
- if (port != 8080 && !prop.containsKey("camel.server.port")) {
- prop.put("camel.server.port", port);
- }
- if (!prop.containsKey("camel.server.health-check-enabled")) {
- prop.put("camel.server.health-check-enabled", "true");
- }
- }
- return prop;
- });
+ copySettingsAndProfile(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile),
+ FilePathConverter.toFile(srcResourcesDir), prop -> {
+ if (!prop.containsKey("camel.main.basePackageScan")
+ && !prop.containsKey("camel.main.base-package-scan")) {
+ // use dot as root package if no package are in use
+ prop.put("camel.main.basePackageScan", srcPackageName == null ? "." : srcPackageName);
+ }
+ if (!hasModeline(FilePathConverter.toFile(settings))) {
+ prop.remove("camel.main.modeline");
+ }
+ // are we using http then enable embedded HTTP server (if not explicit configured already)
+ int port = httpServerPort(FilePathConverter.toFile(settings));
+ if (port == -1
+ && deps.stream().anyMatch(d -> d.contains("camel-platform-http") || d.contains("camel-rest"))) {
+ port = 8080;
+ }
+ if (port != -1 && !prop.containsKey("camel.server.enabled")) {
+ prop.put("camel.server.enabled", "true");
+ if (port != 8080 && !prop.containsKey("camel.server.port")) {
+ prop.put("camel.server.port", port);
+ }
+ if (!prop.containsKey("camel.server.health-check-enabled")) {
+ prop.put("camel.server.health-check-enabled", "true");
+ }
+ }
+ return prop;
+ });
// create main class
- createMainClassSource(srcJavaDir, srcPackageName, mainClassname);
+ createMainClassSource(FilePathConverter.toFile(srcJavaDir), srcPackageName, mainClassname);
// copy local lib JARs
copyLocalLibDependencies(deps);
// copy agent JARs and remove as dependency
copyAgentDependencies(deps);
deps.removeIf(d -> d.startsWith("agent:"));
if ("maven".equals(buildTool)) {
- createMavenPom(settings, profile, new File(BUILD_DIR, "pom.xml"), deps, srcPackageName);
+ createMavenPom(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile),
+ FilePathConverter.toFile(buildDir.resolve("pom.xml")), deps, srcPackageName);
if (mavenWrapper) {
copyMavenWrapper();
}
@@ -150,8 +157,8 @@ class ExportCamelMain extends Export {
CommandHelper.cleanExportDir(exportDir);
}
// copy to export dir and remove work dir
- FileUtils.copyDirectory(new File(BUILD_DIR), new File(exportDir));
- FileUtil.removeDir(new File(BUILD_DIR));
+ FileUtils.copyDirectory(FilePathConverter.toFile(buildDir), new File(exportDir));
+ FileUtil.removeDir(FilePathConverter.toFile(buildDir));
return 0;
}
@@ -241,7 +248,7 @@ class ExportCamelMain extends Export {
// include docker/kubernetes with jib/jkube
context = enrichMavenPomJib(context, settings, profile);
- IOHelper.writeText(context, new FileOutputStream(pom, false));
+ Files.writeString(FilePathConverter.toPath(pom), context);
}
protected String enrichMavenPomJib(String context, File settings, File profile) throws Exception {
@@ -280,7 +287,8 @@ class ExportCamelMain extends Export {
String context2 = IOHelper.loadText(is);
IOHelper.close(is);
- context2 = context2.replaceFirst("\\{\\{ \\.JibMavenPluginVersion }}", jibMavenPluginVersion(settings, prop));
+ context2 = context2.replaceFirst("\\{\\{ \\.JibMavenPluginVersion }}",
+ jibMavenPluginVersion(settings, prop));
// image from/to auth
String auth = "";
@@ -371,7 +379,8 @@ class ExportCamelMain extends Export {
context = context.replaceFirst("\\{\\{ \\.PackageName }}", "");
}
context = context.replaceAll("\\{\\{ \\.MainClassname }}", mainClassname);
- IOHelper.writeText(context, new FileOutputStream(srcJavaDir + "/" + mainClassname + ".java", false));
+ Path outputFile = FilePathConverter.toPath(srcJavaDir).resolve(mainClassname + ".java");
+ Files.writeString(outputFile, context);
}
@Override
@@ -385,11 +394,12 @@ class ExportCamelMain extends Export {
// log4j configuration
InputStream is = ExportCamelMain.class.getResourceAsStream("/log4j2-main.properties");
- safeCopy(is, new File(srcResourcesDir, "log4j2.properties"));
+ safeCopy(is, FilePathConverter.toFile(FilePathConverter.toPath(srcResourcesDir).resolve("log4j2.properties")));
is = ExportCamelMain.class.getResourceAsStream("/log4j2.component.properties");
- safeCopy(is, new File(srcResourcesDir, "log4j2.component.properties"));
+ safeCopy(is,
+ FilePathConverter.toFile(FilePathConverter.toPath(srcResourcesDir).resolve("log4j2.component.properties")));
// assembly for runner jar
is = ExportCamelMain.class.getResourceAsStream("/assembly/runner.xml");
- safeCopy(is, new File(srcResourcesDir, "assembly/runner.xml"));
+ safeCopy(is, FilePathConverter.toFile(FilePathConverter.toPath(srcResourcesDir).resolve("assembly/runner.xml")));
}
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
index 995276a589f..030567340bc 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
@@ -19,6 +19,8 @@ package org.apache.camel.dsl.jbang.core.commands;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -32,6 +34,7 @@ import java.util.stream.Collectors;
import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.dsl.jbang.core.common.CatalogLoader;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.FilePathConverter;
import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
import org.apache.camel.dsl.jbang.core.common.VersionHelper;
import org.apache.camel.tooling.maven.MavenGav;
@@ -61,11 +64,11 @@ class ExportQuarkus extends Export {
return 1;
}
- File profile = new File("application.properties");
+ Path profile = Path.of("application.properties");
// the settings file has information what to export
- File settings = new File(CommandLineHelper.getWorkDir(), Run.RUN_SETTINGS_FILE);
- if (fresh || !files.isEmpty() || !settings.exists()) {
+ Path settings = CommandLineHelper.getWorkDir().resolve(Run.RUN_SETTINGS_FILE);
+ if (fresh || !files.isEmpty() || !Files.exists(settings)) {
// allow to automatic build
printer().println("Generating fresh run data");
int silent = runSilently(ignoreLoadingError, lazyBean, verbose);
@@ -97,11 +100,12 @@ class ExportQuarkus extends Export {
File srcCamelResourcesDir = new File(BUILD_DIR, "src/main/resources/camel");
File srcKameletsResourcesDir = new File(BUILD_DIR, "src/main/resources/kamelets");
// copy source files
- copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, srcResourcesDir, srcCamelResourcesDir,
+ copySourceFiles(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile), srcJavaDirRoot, srcJavaDir,
+ srcResourcesDir, srcCamelResourcesDir,
srcKameletsResourcesDir, srcPackageName);
// copy from settings to profile
- copySettingsAndProfile(settings, profile, srcResourcesDir, prop -> {
- if (!hasModeline(settings)) {
+ copySettingsAndProfile(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile), srcResourcesDir, prop -> {
+ if (!hasModeline(FilePathConverter.toFile(settings))) {
prop.remove("camel.main.modeline");
}
return prop;
@@ -111,18 +115,20 @@ class ExportQuarkus extends Export {
String appJar = "target" + File.separator + "quarkus-app" + File.separator + "quarkus-run.jar";
copyReadme(BUILD_DIR, appJar);
// gather dependencies
- Set<String> deps = resolveDependencies(settings, profile);
+ Set<String> deps = resolveDependencies(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile));
// copy local lib JARs
copyLocalLibDependencies(deps);
if ("maven".equals(buildTool)) {
- createMavenPom(settings, new File(BUILD_DIR, "pom.xml"), deps);
+ createMavenPom(FilePathConverter.toFile(settings), FilePathConverter.toFile(Path.of(BUILD_DIR).resolve("pom.xml")),
+ deps);
if (mavenWrapper) {
copyMavenWrapper();
}
} else if ("gradle".equals(buildTool)) {
- createGradleProperties(new File(BUILD_DIR, "gradle.properties"));
- createSettingsGradle(new File(BUILD_DIR, "settings.gradle"));
- createBuildGradle(settings, new File(BUILD_DIR, "build.gradle"), deps);
+ createGradleProperties(FilePathConverter.toFile(Path.of(BUILD_DIR).resolve("gradle.properties")));
+ createSettingsGradle(FilePathConverter.toFile(Path.of(BUILD_DIR).resolve("settings.gradle")));
+ createBuildGradle(FilePathConverter.toFile(settings),
+ FilePathConverter.toFile(Path.of(BUILD_DIR).resolve("build.gradle")), deps);
if (gradleWrapper) {
copyGradleWrapper();
}
@@ -494,7 +500,7 @@ class ExportQuarkus extends Export {
}
context = context.replaceFirst("\\{\\{ \\.CamelDependencies }}", sb.toString());
- IOHelper.writeText(context, new FileOutputStream(pom, false));
+ Files.writeString(pom.toPath(), context);
}
@Override
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
index 5b83a73ab47..dc63148d295 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
@@ -21,6 +21,8 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -30,6 +32,7 @@ import java.util.Set;
import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.dsl.jbang.core.common.CatalogLoader;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.FilePathConverter;
import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
import org.apache.camel.dsl.jbang.core.common.VersionHelper;
import org.apache.camel.tooling.maven.MavenGav;
@@ -59,11 +62,11 @@ class ExportSpringBoot extends Export {
return 1;
}
- File profile = new File("application.properties");
+ Path profile = Path.of("application.properties");
// the settings file has information what to export
- File settings = new File(CommandLineHelper.getWorkDir(), Run.RUN_SETTINGS_FILE);
- if (fresh || !files.isEmpty() || !settings.exists()) {
+ Path settings = CommandLineHelper.getWorkDir().resolve(Run.RUN_SETTINGS_FILE);
+ if (fresh || !files.isEmpty() || !Files.exists(settings)) {
// allow to automatic build
printer().println("Generating fresh run data");
int silent = runSilently(ignoreLoadingError, lazyBean, verbose);
@@ -98,18 +101,19 @@ class ExportSpringBoot extends Export {
copyApplicationPropertiesFiles(srcResourcesDir);
// copy source files
- copySourceFiles(settings, profile, srcJavaDirRoot, srcJavaDir, srcResourcesDir, srcCamelResourcesDir,
+ copySourceFiles(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile), srcJavaDirRoot, srcJavaDir,
+ srcResourcesDir, srcCamelResourcesDir,
srcKameletsResourcesDir, srcPackageName);
// create main class
createMainClassSource(srcJavaDir, srcPackageName, mainClassname);
// gather dependencies
- final Set<String> deps = resolveDependencies(settings, profile);
+ final Set<String> deps = resolveDependencies(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile));
// copy local lib JARs
copyLocalLibDependencies(deps);
// copy from settings to profile
- copySettingsAndProfile(settings, profile, srcResourcesDir, prop -> {
- if (!hasModeline(settings)) {
+ copySettingsAndProfile(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile), srcResourcesDir, prop -> {
+ if (!hasModeline(FilePathConverter.toFile(settings))) {
prop.remove("camel.main.modeline");
}
// ensure spring-boot keeps running if no HTTP server included
@@ -120,13 +124,14 @@ class ExportSpringBoot extends Export {
return prop;
});
if ("maven".equals(buildTool)) {
- createMavenPom(settings, profile, new File(BUILD_DIR, "pom.xml"), deps);
+ createMavenPom(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile),
+ new File(BUILD_DIR, "pom.xml"), deps);
if (mavenWrapper) {
copyMavenWrapper();
}
} else if ("gradle".equals(buildTool)) {
createSettingsGradle(new File(BUILD_DIR, "settings.gradle"));
- createBuildGradle(settings, new File(BUILD_DIR, "build.gradle"), deps);
+ createBuildGradle(FilePathConverter.toFile(settings), new File(BUILD_DIR, "build.gradle"), deps);
if (gradleWrapper) {
copyGradleWrapper();
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
index 581b499a5a8..b1af7e0db91 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
@@ -20,6 +20,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Stack;
import java.util.StringJoiner;
@@ -212,9 +213,13 @@ public class Init extends CamelCommand {
}
private void createWorkingDirectoryIfAbsent() {
- File work = CommandLineHelper.getWorkDir();
- if (!work.exists()) {
- work.mkdirs();
+ Path work = CommandLineHelper.getWorkDir();
+ if (!Files.exists(work)) {
+ try {
+ Files.createDirectories(work);
+ } catch (IOException e) {
+ // ignore
+ }
}
}
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 2abdde240bc..c4410aca0d1 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
@@ -43,6 +43,7 @@ import org.apache.camel.FailedToCreateRouteException;
import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.FilePathConverter;
import org.apache.camel.dsl.jbang.core.common.LoggingLevelCompletionCandidates;
import org.apache.camel.dsl.jbang.core.common.Printer;
import org.apache.camel.dsl.jbang.core.common.RuntimeCompletionCandidates;
@@ -474,10 +475,14 @@ public class Run extends CamelCommand {
}
}
- File work = CommandLineHelper.getWorkDir();
- removeDir(work);
- if (!work.exists() && !work.mkdirs()) {
- printer().println("WARN: Failed to create working directory: " + work.getAbsolutePath());
+ Path work = CommandLineHelper.getWorkDir();
+ removeDir(FilePathConverter.toFile(work));
+ if (!Files.exists(work)) {
+ try {
+ Files.createDirectories(work);
+ } catch (IOException e) {
+ printer().println("WARN: Failed to create working directory: " + work.toAbsolutePath());
+ }
}
Properties profileProperties = !empty ? loadProfileProperties() : null;
@@ -1368,7 +1373,8 @@ public class Run extends CamelCommand {
File log = null;
if (backgroundWait) {
// store background output in a log file to capture any error on startup
- log = getRunBackgroundLogFile("" + new Random().nextLong());
+ Path logPath = getRunBackgroundLogFile("" + new Random().nextLong());
+ log = FilePathConverter.toFile(logPath);
log.deleteOnExit();
pb.redirectErrorStream(true);
pb.redirectOutput(log);
@@ -1689,9 +1695,9 @@ public class Run extends CamelCommand {
if (!scriptRun) {
// remember log file
String name = RuntimeUtil.getPid() + ".log";
- final File logDir = CommandLineHelper.getCamelDir();
- logDir.mkdirs(); //make sure the parent dir exists
- logFile = new File(logDir, name);
+ final Path logDir = CommandLineHelper.getCamelDir();
+ Files.createDirectories(logDir); //make sure the parent dir exists
+ logFile = FilePathConverter.resolveToFile(logDir, name);
logFile.deleteOnExit();
}
} else {
@@ -1862,7 +1868,8 @@ public class Run extends CamelCommand {
StringWriter sw = new StringWriter();
prop.store(sw, null);
- File runSettings = new File(CommandLineHelper.getWorkDir(), RUN_SETTINGS_FILE);
+ Path runSettingsPath = CommandLineHelper.getWorkDir().resolve(RUN_SETTINGS_FILE);
+ File runSettings = FilePathConverter.toFile(runSettingsPath);
fos = new FileOutputStream(runSettings, true);
String[] lines = sw.toString().split(System.lineSeparator());
@@ -2026,7 +2033,8 @@ public class Run extends CamelCommand {
private JsonObject loadStatus(long pid) {
try {
- File f = getStatusFile(Long.toString(pid));
+ Path p = getStatusFile(Long.toString(pid));
+ File f = FilePathConverter.toFile(p);
if (f != null) {
FileInputStream fis = new FileInputStream(f);
String text = IOHelper.loadText(fis);
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/ActionBaseCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/ActionBaseCommand.java
index 36632f43ea4..f68d3cd1977 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/ActionBaseCommand.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/ActionBaseCommand.java
@@ -17,16 +17,18 @@
package org.apache.camel.dsl.jbang.core.commands.action;
import java.io.File;
-import java.io.FileInputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelCommand;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.FilePathConverter;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
import org.apache.camel.support.PatternHelper;
import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.json.JsonObject;
import org.apache.camel.util.json.Jsoner;
@@ -37,17 +39,15 @@ abstract class ActionBaseCommand extends CamelCommand {
super(main);
}
- protected static JsonObject getJsonObject(File outputFile) {
+ protected static JsonObject getJsonObject(Path outputFile) {
StopWatch watch = new StopWatch();
while (watch.taken() < 5000) {
try {
// give time for response to be ready
Thread.sleep(100);
- if (outputFile.exists()) {
- FileInputStream fis = new FileInputStream(outputFile);
- String text = IOHelper.loadText(fis);
- IOHelper.close(fis);
+ if (Files.exists(outputFile)) {
+ String text = Files.readString(outputFile);
return (JsonObject) Jsoner.deserialize(text);
}
} catch (InterruptedException e) {
@@ -59,6 +59,10 @@ abstract class ActionBaseCommand extends CamelCommand {
return null;
}
+ protected static JsonObject getJsonObject(File outputFile) {
+ return getJsonObject(FilePathConverter.toPath(outputFile));
+ }
+
List<Long> findPids(String name) {
List<Long> pids = new ArrayList<>();
@@ -117,11 +121,9 @@ abstract class ActionBaseCommand extends CamelCommand {
JsonObject loadStatus(long pid) {
try {
- File f = getStatusFile(Long.toString(pid));
- if (f != null) {
- FileInputStream fis = new FileInputStream(f);
- String text = IOHelper.loadText(fis);
- IOHelper.close(fis);
+ Path f = getStatusFile(Long.toString(pid));
+ if (f != null && Files.exists(f)) {
+ String text = Files.readString(f);
return (JsonObject) Jsoner.deserialize(text);
}
} catch (Exception e) {
@@ -129,4 +131,31 @@ abstract class ActionBaseCommand extends CamelCommand {
}
return null;
}
+
+ /**
+ * Prepares and writes an action to the action file.
+ *
+ * @param pid the process ID
+ * @param action the action name
+ * @param configureAction a function to configure the action JSON object
+ * @return the output file path
+ */
+ protected Path prepareAction(String pid, String action, java.util.function.Consumer<JsonObject> configureAction) {
+ // ensure output file is deleted before executing action
+ Path outputFile = getOutputFile(pid);
+ PathUtils.deleteFile(outputFile);
+
+ JsonObject root = new JsonObject();
+ root.put("action", action);
+
+ // Allow caller to configure the action
+ if (configureAction != null) {
+ configureAction.accept(root);
+ }
+
+ Path file = getActionFile(pid);
+ PathUtils.writeTextSafely(root.toJson(), file);
+
+ return outputFile;
+ }
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBeanDump.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBeanDump.java
index 417031da491..684de4fce8c 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBeanDump.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBeanDump.java
@@ -16,7 +16,7 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -27,8 +27,7 @@ import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
import picocli.CommandLine;
@@ -99,8 +98,8 @@ public class CamelBeanDump extends ActionBaseCommand {
this.pid = pids.get(0);
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "bean");
@@ -110,12 +109,8 @@ public class CamelBeanDump extends ActionBaseCommand {
root.put("properties", properties);
root.put("nulls", nulls);
root.put("internal", internal);
- File f = getActionFile(Long.toString(pid));
- try {
- IOHelper.writeText(root.toJson(), f);
- } catch (Exception e) {
- // ignore
- }
+ Path f = getActionFile(Long.toString(pid));
+ PathUtils.writeTextSafely(root.toJson(), f);
JsonObject jo = waitForOutputFile(outputFile);
@@ -174,7 +169,7 @@ public class CamelBeanDump extends ActionBaseCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
@@ -222,8 +217,8 @@ public class CamelBeanDump extends ActionBaseCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
- return getJsonObject(outputFile);
+ protected JsonObject waitForOutputFile(Path outputFile) {
+ return getJsonObject((Path) outputFile);
}
private static class Row {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBrowseAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBrowseAction.java
index 9366d70305e..112f29f48b5 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBrowseAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBrowseAction.java
@@ -16,7 +16,7 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -28,9 +28,8 @@ import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.TimeUtils;
import org.apache.camel.util.URISupport;
import org.apache.camel.util.json.JsonArray;
@@ -136,8 +135,8 @@ public class CamelBrowseAction extends ActionBaseCommand {
this.pid = pids.get(0);
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "browse");
@@ -151,15 +150,15 @@ public class CamelBrowseAction extends ActionBaseCommand {
root.put("bodyMaxChars", bodyMaxChars);
}
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), f);
+ PathUtils.writeTextSafely(root.toJson(), f);
} catch (Exception e) {
// ignore
}
List<Row> rows = new ArrayList<>();
- JsonObject jo = getJsonObject(outputFile);
+ JsonObject jo = getJsonObject((Path) outputFile);
if (jo != null) {
root = loadStatus(this.pid);
if (root != null) {
@@ -207,7 +206,7 @@ public class CamelBrowseAction extends ActionBaseCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelGCAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelGCAction.java
index 40fb7a43c3a..70c1c2b2c28 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelGCAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelGCAction.java
@@ -16,11 +16,11 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Path;
import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
-import org.apache.camel.util.IOHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.util.json.JsonObject;
import picocli.CommandLine;
@@ -41,8 +41,8 @@ public class CamelGCAction extends ActionBaseCommand {
for (long pid : pids) {
JsonObject root = new JsonObject();
root.put("action", "gc");
- File f = getActionFile(Long.toString(pid));
- IOHelper.writeText(root.toJson(), f);
+ Path f = getActionFile(Long.toString(pid));
+ PathUtils.writeTextSafely(root.toJson(), f);
}
return 0;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
index a88e4b5c47a..fa47ac06558 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
@@ -16,10 +16,10 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
-import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
@@ -41,6 +41,7 @@ import org.apache.camel.catalog.impl.TimePatternConverter;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.commands.CommandHelper;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.StringHelper;
@@ -255,12 +256,12 @@ public class CamelLogAction extends ActionBaseCommand {
for (Row row : rows.values()) {
if (row.reader == null) {
- File file = logFile(row.pid);
- if (file.exists()) {
- row.reader = new LineNumberReader(new FileReader(file));
+ Path file = logFile(row.pid);
+ if (Files.exists(file)) {
+ row.reader = new LineNumberReader(Files.newBufferedReader(file));
if (tail == 0) {
// only read new lines so forward to end of reader
- long size = file.length();
+ long size = Files.size(file);
row.reader.skip(size);
}
}
@@ -429,17 +430,17 @@ public class CamelLogAction extends ActionBaseCommand {
}
}
- private static File logFile(String pid) {
+ private static Path logFile(String pid) {
String name = pid + ".log";
- return new File(CommandLineHelper.getCamelDir(), name);
+ return PathUtils.createPath(CommandLineHelper.getCamelDir(), name);
}
private void tailStartupLogFiles(Map<Long, Row> rows) throws Exception {
for (Row row : rows.values()) {
- File log = logFile(row.pid);
- if (log.exists()) {
+ Path log = logFile(row.pid);
+ if (Files.exists(log)) {
row.fifo = new ArrayDeque<>();
- row.reader = new LineNumberReader(new FileReader(log));
+ row.reader = new LineNumberReader(Files.newBufferedReader(log));
String line;
do {
line = row.reader.readLine();
@@ -458,9 +459,9 @@ public class CamelLogAction extends ActionBaseCommand {
private void tailLogFiles(Map<Long, Row> rows, int tail, Date limit) throws Exception {
for (Row row : rows.values()) {
- File log = logFile(row.pid);
- if (log.exists()) {
- row.reader = new LineNumberReader(new FileReader(log));
+ Path log = logFile(row.pid);
+ if (Files.exists(log)) {
+ row.reader = new LineNumberReader(Files.newBufferedReader(log));
String line;
if (tail <= 0) {
row.fifo = new ArrayDeque<>();
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelReceiveAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelReceiveAction.java
index 7efcbaa9536..311f4ac8aad 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelReceiveAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelReceiveAction.java
@@ -16,10 +16,10 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
-import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
@@ -43,10 +43,9 @@ import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.catalog.impl.TimePatternConverter;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.commands.CommandHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.dsl.jbang.core.common.PidNameAgeCompletionCandidates;
import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.TimeUtils;
@@ -201,14 +200,14 @@ public class CamelReceiveAction extends ActionBaseCommand {
List<Long> pids = findPids(name);
for (long pid : pids) {
if ("clear".equals(action)) {
- File f = getReceiveFile("" + pid);
- if (f.exists()) {
- IOHelper.writeText("{}", f);
+ Path f = getReceiveFile("" + pid);
+ if (Files.exists(f)) {
+ Files.writeString(f, "{}");
}
} else {
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "receive");
@@ -222,8 +221,8 @@ public class CamelReceiveAction extends ActionBaseCommand {
} else if ("stop".equals(action)) {
root.put("enabled", "false");
}
- File f = getActionFile(Long.toString(pid));
- IOHelper.writeText(root.toJson(), f);
+ Path f = getActionFile(Long.toString(pid));
+ Files.writeString(f, root.toJson());
JsonObject jo = waitForOutputFile(outputFile);
if (jo != null) {
@@ -258,7 +257,7 @@ public class CamelReceiveAction extends ActionBaseCommand {
return 0;
}
- protected JsonObject waitForOutputFile(File outputFile) {
+ protected JsonObject waitForOutputFile(Path outputFile) {
return getJsonObject(outputFile);
}
@@ -451,9 +450,9 @@ public class CamelReceiveAction extends ActionBaseCommand {
private void tailReceiveFiles(Map<Long, Pid> pids, int tail) throws Exception {
for (Pid pid : pids.values()) {
- File file = getReceiveFile(pid.pid);
- if (file.exists() && file.length() > 0) {
- pid.reader = new LineNumberReader(new FileReader(file));
+ Path file = getReceiveFile(pid.pid);
+ if (Files.exists(file) && Files.size(file) > 0) {
+ pid.reader = new LineNumberReader(Files.newBufferedReader(file));
String line;
if (tail <= 0) {
pid.fifo = new ArrayDeque<>();
@@ -522,12 +521,12 @@ public class CamelReceiveAction extends ActionBaseCommand {
for (Pid pid : pids.values()) {
if (pid.reader == null) {
- File file = getReceiveFile(pid.pid);
- if (file.exists()) {
- pid.reader = new LineNumberReader(new FileReader(file));
+ Path file = getReceiveFile(pid.pid);
+ if (Files.exists(file)) {
+ pid.reader = new LineNumberReader(Files.newBufferedReader(file));
if (tail == 0) {
// only read new lines so forward to end of reader
- long size = file.length();
+ long size = Files.size(file);
pid.reader.skip(size);
}
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelReloadAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelReloadAction.java
index 3a1860864b4..ff57343793b 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelReloadAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelReloadAction.java
@@ -16,11 +16,11 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.json.JsonObject;
import picocli.CommandLine;
@@ -41,8 +41,8 @@ public class CamelReloadAction extends ActionBaseCommand {
for (long pid : pids) {
JsonObject root = new JsonObject();
root.put("action", "reload");
- File f = getActionFile(Long.toString(pid));
- IOHelper.writeText(root.toJson(), f);
+ Path f = getActionFile(Long.toString(pid));
+ Files.writeString(f, root.toJson());
}
return 0;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelResetStatsAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelResetStatsAction.java
index ea6a86ae9fa..b879f6d052f 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelResetStatsAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelResetStatsAction.java
@@ -16,11 +16,11 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.json.JsonObject;
import picocli.CommandLine;
@@ -41,8 +41,8 @@ public class CamelResetStatsAction extends ActionBaseCommand {
for (long pid : pids) {
JsonObject root = new JsonObject();
root.put("action", "reset-stats");
- File f = getActionFile(Long.toString(pid));
- IOHelper.writeText(root.toJson(), f);
+ Path f = getActionFile(Long.toString(pid));
+ Files.writeString(f, root.toJson());
}
return 0;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelRouteAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelRouteAction.java
index 850449baa0e..a4122151298 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelRouteAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelRouteAction.java
@@ -16,11 +16,11 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Path;
import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
-import org.apache.camel.util.IOHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.util.json.JsonObject;
import picocli.CommandLine;
@@ -45,9 +45,9 @@ public abstract class CamelRouteAction extends ActionBaseCommand {
JsonObject root = new JsonObject();
root.put("action", "route");
root.put("id", id);
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
onAction(root);
- IOHelper.writeText(root.toJson(), f);
+ PathUtils.writeTextSafely(root.toJson(), f);
}
return 0;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelRouteDumpAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelRouteDumpAction.java
index b204fc57406..6cfe45cd612 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelRouteDumpAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelRouteDumpAction.java
@@ -16,16 +16,17 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.support.PatternHelper;
import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
import org.apache.camel.util.json.Jsoner;
@@ -95,17 +96,17 @@ public class CamelRouteDumpAction extends ActionBaseCommand {
this.pid = pids.get(0);
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "route-dump");
root.put("filter", "*");
root.put("format", format);
root.put("uriAsParameters", uriAsParameters);
- File file = getActionFile(Long.toString(pid));
+ Path file = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), file);
+ Files.writeString(file, root.toJson());
} catch (Exception e) {
// ignore
}
@@ -165,7 +166,7 @@ public class CamelRouteDumpAction extends ActionBaseCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
@@ -207,7 +208,7 @@ public class CamelRouteDumpAction extends ActionBaseCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
+ protected JsonObject waitForOutputFile(Path outputFile) {
return getJsonObject(outputFile);
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSendAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSendAction.java
index 4aacfebade5..6dc59edd529 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSendAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSendAction.java
@@ -16,15 +16,15 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
-import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.TimeUtils;
@@ -120,8 +120,8 @@ public class CamelSendAction extends ActionBaseCommand {
this.pid = pids.get(0);
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "send");
@@ -151,9 +151,9 @@ public class CamelSendAction extends ActionBaseCommand {
}
root.put("headers", arr);
}
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), f);
+ PathUtils.writeText(root.toJson(), f);
} catch (Exception e) {
// ignore
}
@@ -166,12 +166,16 @@ public class CamelSendAction extends ActionBaseCommand {
JsonObject cause = jo.getMap("exception");
if (message != null || cause != null) {
if (replyFile != null) {
- File target = new File(replyFile);
+ Path target = Path.of(replyFile);
String json = jo.toJson();
if (pretty) {
json = Jsoner.prettyPrint(json, 2);
}
- IOHelper.writeText(json, target);
+ try {
+ Files.writeString(target, json);
+ } catch (IOException e) {
+ // ignore
+ }
}
if (!showExchangeProperties && message != null) {
message.remove("exchangeProperties");
@@ -201,7 +205,7 @@ public class CamelSendAction extends ActionBaseCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
@@ -285,7 +289,7 @@ public class CamelSendAction extends ActionBaseCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
+ protected JsonObject waitForOutputFile(Path outputFile) {
StopWatch watch = new StopWatch();
long wait = timeout + 10000; // wait longer than timeout
while (watch.taken() < wait) {
@@ -293,10 +297,8 @@ public class CamelSendAction extends ActionBaseCommand {
// give time for response to be ready
Thread.sleep(20);
- if (outputFile.exists()) {
- FileInputStream fis = new FileInputStream(outputFile);
- String text = IOHelper.loadText(fis);
- IOHelper.close(fis);
+ if (Files.exists(outputFile)) {
+ String text = Files.readString(outputFile);
return (JsonObject) Jsoner.deserialize(text);
}
} catch (InterruptedException e) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceAction.java
index 8c3bc282810..b18e390e172 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceAction.java
@@ -16,14 +16,15 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.support.PatternHelper;
import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
import org.apache.camel.util.json.Jsoner;
@@ -67,19 +68,9 @@ public class CamelSourceAction extends ActionBaseCommand {
this.pid = pids.get(0);
- // ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
-
- JsonObject root = new JsonObject();
- root.put("action", "source");
- root.put("filter", "*");
- File file = getActionFile(Long.toString(pid));
- try {
- IOHelper.writeText(root.toJson(), file);
- } catch (Exception e) {
- // ignore
- }
+ Path outputFile = prepareAction(Long.toString(pid), "source", root -> {
+ root.put("filter", "*");
+ });
JsonObject jo = waitForOutputFile(outputFile);
if (jo != null) {
@@ -136,7 +127,11 @@ public class CamelSourceAction extends ActionBaseCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ try {
+ Files.deleteIfExists(outputFile);
+ } catch (IOException e) {
+ // ignore
+ }
return 0;
}
@@ -170,8 +165,8 @@ public class CamelSourceAction extends ActionBaseCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
- return getJsonObject(outputFile);
+ protected JsonObject waitForOutputFile(Path outputFile) {
+ return getJsonObject((Path) outputFile);
}
public static String extractSourceName(String loc) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceTop.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceTop.java
index 0fe5dfc3cf1..056abeba550 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceTop.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSourceTop.java
@@ -16,14 +16,14 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
import org.apache.camel.util.json.Jsoner;
@@ -67,14 +67,14 @@ public class CamelSourceTop extends ActionWatchCommand {
this.pid = pids.get(0);
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "top-processors");
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), f);
+ Files.writeString(f, root.toJson());
} catch (Exception e) {
// ignore
}
@@ -147,7 +147,7 @@ public class CamelSourceTop extends ActionWatchCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
@@ -167,7 +167,7 @@ public class CamelSourceTop extends ActionWatchCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
+ protected JsonObject waitForOutputFile(Path outputFile) {
return getJsonObject(outputFile);
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelStartupRecorderAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelStartupRecorderAction.java
index a85421d9949..47253bb0f9c 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelStartupRecorderAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelStartupRecorderAction.java
@@ -16,7 +16,8 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -27,8 +28,7 @@ import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
@@ -67,14 +67,14 @@ public class CamelStartupRecorderAction extends ActionWatchCommand {
this.pid = pids.get(0);
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "startup-recorder");
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), f);
+ Files.writeString(f, root.toJson());
} catch (Exception e) {
// ignore
}
@@ -109,7 +109,7 @@ public class CamelStartupRecorderAction extends ActionWatchCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
@@ -147,7 +147,7 @@ public class CamelStartupRecorderAction extends ActionWatchCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
+ protected JsonObject waitForOutputFile(Path outputFile) {
return getJsonObject(outputFile);
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelStubAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelStubAction.java
index f194883bcde..b9b63b692c4 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelStubAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelStubAction.java
@@ -16,7 +16,8 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -30,9 +31,8 @@ import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.support.PatternHelper;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.URISupport;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
@@ -149,8 +149,8 @@ public class CamelStubAction extends ActionWatchCommand {
}
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "stub");
@@ -158,9 +158,9 @@ public class CamelStubAction extends ActionWatchCommand {
root.put("browse", browse);
root.put("filter", "*");
root.put("limit", limit);
- File file = getActionFile(Long.toString(pid));
+ Path file = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), file);
+ Files.writeString(file, root.toJson());
} catch (Exception e) {
// ignore
}
@@ -231,7 +231,7 @@ public class CamelStubAction extends ActionWatchCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
@@ -341,7 +341,7 @@ public class CamelStubAction extends ActionWatchCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
+ protected JsonObject waitForOutputFile(Path outputFile) {
return getJsonObject(outputFile);
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelThreadDump.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelThreadDump.java
index a2ee0e7901d..f996bee5f82 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelThreadDump.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelThreadDump.java
@@ -16,7 +16,8 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -27,9 +28,8 @@ import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.support.PatternHelper;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
import picocli.CommandLine;
@@ -97,14 +97,14 @@ public class CamelThreadDump extends ActionWatchCommand {
this.pid = pids.get(0);
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "thread-dump");
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), f);
+ Files.writeString(f, root.toJson());
} catch (Exception e) {
// ignore
}
@@ -159,7 +159,7 @@ public class CamelThreadDump extends ActionWatchCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
@@ -210,7 +210,7 @@ public class CamelThreadDump extends ActionWatchCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
+ protected JsonObject waitForOutputFile(Path outputFile) {
return getJsonObject(outputFile);
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
index c50c2b8f9f9..97b1d51c21e 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
@@ -16,10 +16,10 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
-import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
@@ -207,9 +207,9 @@ public class CamelTraceAction extends ActionBaseCommand {
List<Long> pids = findPids(name);
for (long pid : pids) {
if ("clear".equals(action)) {
- File f = getTraceFile("" + pid);
- if (f.exists()) {
- IOHelper.writeText("{}", f);
+ Path f = getTraceFile("" + pid);
+ if (Files.exists(f)) {
+ Files.writeString(f, "{}");
}
} else {
JsonObject root = new JsonObject();
@@ -219,8 +219,8 @@ public class CamelTraceAction extends ActionBaseCommand {
} else if ("stop".equals(action)) {
root.put("enabled", "false");
}
- File f = getActionFile(Long.toString(pid));
- IOHelper.writeText(root.toJson(), f);
+ Path f = getActionFile(Long.toString(pid));
+ Files.writeString(f, root.toJson());
}
}
@@ -420,10 +420,10 @@ public class CamelTraceAction extends ActionBaseCommand {
private void positionTraceLatest(Map<Long, Pid> pids) throws Exception {
for (Pid pid : pids.values()) {
- File file = getTraceFile(pid.pid);
+ Path file = getTraceFile(pid.pid);
long position = -1;
- if (file.exists()) {
- pid.reader = new LineNumberReader(new FileReader(file));
+ if (Files.exists(file)) {
+ pid.reader = new LineNumberReader(Files.newBufferedReader(file));
String line;
long counter = 0;
do {
@@ -443,7 +443,7 @@ public class CamelTraceAction extends ActionBaseCommand {
if (position != -1) {
IOHelper.close(pid.reader);
// re-create reader and forward to position
- pid.reader = new LineNumberReader(new FileReader(file));
+ pid.reader = new LineNumberReader(Files.newBufferedReader(file));
while (--position > 0) {
pid.reader.readLine();
}
@@ -453,9 +453,9 @@ public class CamelTraceAction extends ActionBaseCommand {
private void tailTraceFiles(Map<Long, Pid> pids, int tail) throws Exception {
for (Pid pid : pids.values()) {
- File file = getTraceFile(pid.pid);
- if (file.exists()) {
- pid.reader = new LineNumberReader(new FileReader(file));
+ Path file = getTraceFile(pid.pid);
+ if (Files.exists(file)) {
+ pid.reader = new LineNumberReader(Files.newBufferedReader(file));
String line;
if (tail <= 0) {
pid.fifo = new ArrayDeque<>();
@@ -524,12 +524,12 @@ public class CamelTraceAction extends ActionBaseCommand {
for (Pid pid : pids.values()) {
if (pid.reader == null) {
- File file = getTraceFile(pid.pid);
- if (file.exists()) {
- pid.reader = new LineNumberReader(new FileReader(file));
+ Path file = getTraceFile(pid.pid);
+ if (Files.exists(file)) {
+ pid.reader = new LineNumberReader(Files.newBufferedReader(file));
if (tail == 0) {
// only read new lines so forward to end of reader
- long size = file.length();
+ long size = Files.size(file);
pid.reader.skip(size);
}
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/LoggerAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/LoggerAction.java
index 8918740af8c..21b03bc596c 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/LoggerAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/LoggerAction.java
@@ -16,7 +16,8 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -30,7 +31,6 @@ import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.LoggingLevelCompletionCandidates;
import org.apache.camel.dsl.jbang.core.common.PidNameAgeCompletionCandidates;
import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.TimeUtils;
import org.apache.camel.util.json.JsonObject;
import picocli.CommandLine;
@@ -75,11 +75,11 @@ public class LoggerAction extends ActionBaseCommand {
for (long pid : pids) {
JsonObject root = new JsonObject();
root.put("action", "logger");
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
root.put("command", "set-logging-level");
root.put("logger-name", logger);
root.put("logging-level", loggingLevel);
- IOHelper.writeText(root.toJson(), f);
+ Files.writeString(f, root.toJson());
}
return 0;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/RouteControllerAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/RouteControllerAction.java
index 831404e9af9..c6a78058895 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/RouteControllerAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/RouteControllerAction.java
@@ -16,7 +16,8 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -28,8 +29,7 @@ import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.TimeUtils;
import org.apache.camel.util.json.JsonArray;
@@ -99,15 +99,15 @@ public class RouteControllerAction extends ActionWatchCommand {
this.pid = pids.get(0);
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "route-controller");
root.put("stacktrace", trace ? "true" : "false");
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), f);
+ Files.writeString(f, root.toJson());
} catch (Exception e) {
// ignore
}
@@ -191,7 +191,7 @@ public class RouteControllerAction extends ActionWatchCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
@@ -249,7 +249,7 @@ public class RouteControllerAction extends ActionWatchCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
+ protected JsonObject waitForOutputFile(Path outputFile) {
return getJsonObject(outputFile);
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/TransformMessageAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/TransformMessageAction.java
index 39e59b11030..bd20c799cd4 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/TransformMessageAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/TransformMessageAction.java
@@ -16,8 +16,8 @@
*/
package org.apache.camel.dsl.jbang.core.commands.action;
-import java.io.File;
-import java.io.FileInputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@@ -25,9 +25,8 @@ import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.commands.Run;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.dsl.jbang.core.common.VersionHelper;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.TimeUtils;
@@ -146,15 +145,15 @@ public class TransformMessageAction extends ActionWatchCommand {
if (source != null && source.startsWith("file:")) {
String s = source.substring(5);
s = StringHelper.beforeLast(s, ":", s); // remove line number
- File f = new File(s);
- if (!f.exists()) {
+ Path f = Path.of(s);
+ if (!Files.exists(f)) {
printer().printErr("Source file does not exist: " + f);
return -1;
}
}
if (template != null && template.startsWith("file:")) {
- File f = new File(template.substring(5));
- if (!f.exists()) {
+ Path f = Path.of(template.substring(5));
+ if (!Files.exists(f)) {
printer().printErr("Template file does not exist: " + f);
return -1;
}
@@ -177,12 +176,12 @@ public class TransformMessageAction extends ActionWatchCommand {
} finally {
if (pid > 0) {
// cleanup output file
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
// stop running camel as we are done
- File pidFile = new File(CommandLineHelper.getCamelDir(), Long.toString(pid));
- if (pidFile.exists()) {
- FileUtil.deleteFile(pidFile);
+ Path pidFile = PathUtils.resolve(CommandLineHelper.getCamelDir(), Long.toString(pid));
+ if (Files.exists(pidFile)) {
+ PathUtils.deleteFile(pidFile);
}
}
}
@@ -193,8 +192,8 @@ public class TransformMessageAction extends ActionWatchCommand {
@Override
protected Integer doWatchCall() throws Exception {
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(pid));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(pid));
+ PathUtils.deleteFile(outputFile);
JsonObject root = new JsonObject();
root.put("action", "transform");
@@ -242,9 +241,9 @@ public class TransformMessageAction extends ActionWatchCommand {
}
root.put("options", arr);
}
- File f = getActionFile(Long.toString(pid));
+ Path f = getActionFile(Long.toString(pid));
try {
- IOHelper.writeText(root.toJson(), f);
+ PathUtils.writeTextSafely(root.toJson(), f);
} catch (Exception e) {
// ignore
}
@@ -257,12 +256,12 @@ public class TransformMessageAction extends ActionWatchCommand {
JsonObject cause = jo.getMap("exception");
if (message != null || cause != null) {
if (output != null) {
- File target = new File(output);
+ Path target = Path.of(output);
String json = jo.toJson();
if (pretty) {
json = Jsoner.prettyPrint(json, 2);
}
- IOHelper.writeText(json, target);
+ Files.writeString(target, json);
}
if (!showExchangeProperties && message != null) {
message.remove("exchangeProperties");
@@ -291,7 +290,7 @@ public class TransformMessageAction extends ActionWatchCommand {
}
// delete output file after use
- FileUtil.deleteFile(outputFile);
+ PathUtils.deleteFile(outputFile);
return 0;
}
@@ -344,17 +343,15 @@ public class TransformMessageAction extends ActionWatchCommand {
}
}
- protected JsonObject waitForOutputFile(File outputFile) {
+ protected JsonObject waitForOutputFile(Path outputFile) {
StopWatch watch = new StopWatch();
while (watch.taken() < timeout) {
try {
// give time for response to be ready
Thread.sleep(20);
- if (outputFile.exists()) {
- FileInputStream fis = new FileInputStream(outputFile);
- String text = IOHelper.loadText(fis);
- IOHelper.close(fis);
+ if (Files.exists(outputFile)) {
+ String text = Files.readString(outputFile);
return (JsonObject) Jsoner.deserialize(text);
}
} catch (InterruptedException e) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraGet.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraGet.java
index bf93843c226..3b2c0136a94 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraGet.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraGet.java
@@ -16,9 +16,11 @@
*/
package org.apache.camel.dsl.jbang.core.commands.infra;
-import java.io.File;
+import java.io.IOException;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.List;
+import java.util.stream.Collectors;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
@@ -39,11 +41,20 @@ public class InfraGet extends InfraBaseCommand {
public Integer doCall() throws Exception {
String serviceToGet = serviceName.get(0);
boolean found = false;
- for (File jsonFile : CommandLineHelper.getCamelDir().listFiles(
- (dir, name) -> name.startsWith("infra-" + serviceToGet + "-") && name.endsWith(".json"))) {
- printer().println(Files.readString(jsonFile.toPath()));
- found = true;
- break;
+ try {
+ List<Path> jsonFiles = Files.list(CommandLineHelper.getCamelDir())
+ .filter(p -> {
+ String name = p.getFileName().toString();
+ return name.startsWith("infra-" + serviceToGet + "-") && name.endsWith(".json");
+ })
+ .collect(java.util.stream.Collectors.toList());
+
+ if (!jsonFiles.isEmpty()) {
+ printer().println(Files.readString(jsonFiles.get(0)));
+ found = true;
+ }
+ } catch (IOException e) {
+ // ignore
}
if (!found) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraLog.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraLog.java
index c0a2d52c696..97a7e6a6a9f 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraLog.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraLog.java
@@ -18,12 +18,14 @@ package org.apache.camel.dsl.jbang.core.commands.infra;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
+import java.util.stream.Collectors;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
@@ -52,12 +54,20 @@ public class InfraLog extends InfraBaseCommand {
List<Future<?>> futures = new ArrayList<>();
if (serviceName == null || serviceName.isEmpty()) {
// Log everything
- for (File logFile : CommandLineHelper.getCamelDir().listFiles(
- (dir, name) -> name.startsWith("infra-") && name.endsWith(".log"))) {
-
- String alias = logFile.getName().split("-")[1];
-
- createTailer(logFile, alias, futures);
+ try {
+ List<Path> logFiles = Files.list(CommandLineHelper.getCamelDir())
+ .filter(p -> {
+ String name = p.getFileName().toString();
+ return name.startsWith("infra-") && name.endsWith(".log");
+ })
+ .collect(java.util.stream.Collectors.toList());
+
+ for (Path logFile : logFiles) {
+ String alias = logFile.getFileName().toString().split("-")[1];
+ createTailer(logFile.toFile(), alias, futures);
+ }
+ } catch (IOException e) {
+ // ignore
}
if (futures.isEmpty()) {
@@ -68,10 +78,18 @@ public class InfraLog extends InfraBaseCommand {
} else {
String alias = serviceName.get(0);
- File logFile = Arrays.stream(CommandLineHelper.getCamelDir().listFiles(
- (dir, name) -> name.startsWith("infra-" + alias + "-") && name.endsWith(".log")))
- .findFirst()
- .orElse(null);
+ Path logFile = null;
+ try {
+ logFile = Files.list(CommandLineHelper.getCamelDir())
+ .filter(p -> {
+ String name = p.getFileName().toString();
+ return name.startsWith("infra-" + alias + "-") && name.endsWith(".log");
+ })
+ .findFirst()
+ .orElse(null);
+ } catch (IOException e) {
+ // ignore
+ }
if (logFile == null) {
printer().println("Log not found for service " + alias);
@@ -79,7 +97,7 @@ public class InfraLog extends InfraBaseCommand {
return -1;
}
- createTailer(logFile, alias, futures);
+ createTailer(logFile.toFile(), alias, futures);
}
for (Future<?> future : futures) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraPs.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraPs.java
index 973d3e08599..6b30df016d9 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraPs.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraPs.java
@@ -16,9 +16,13 @@
*/
package org.apache.camel.dsl.jbang.core.commands.infra;
-import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
+import java.util.stream.Collectors;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
@@ -36,10 +40,17 @@ public class InfraPs extends InfraBaseCommand {
public Integer doCall() throws Exception {
// retrieve running services to filter output
Set<String> runningAliases = new HashSet<>();
- for (File pidFile : CommandLineHelper.getCamelDir().listFiles(
- (dir, name) -> name.startsWith("infra-"))) {
- String runningServiceName = pidFile.getName().split("-")[1];
- runningAliases.add(runningServiceName);
+ try {
+ List<Path> pidFiles = Files.list(CommandLineHelper.getCamelDir())
+ .filter(p -> p.getFileName().toString().startsWith("infra-"))
+ .collect(java.util.stream.Collectors.toList());
+
+ for (Path pidFile : pidFiles) {
+ String runningServiceName = pidFile.getFileName().toString().split("-")[1];
+ runningAliases.add(runningServiceName);
+ }
+ } catch (IOException e) {
+ // ignore
}
return listServices(rows -> {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java
index 8b80fb4370d..ee22b4f6a2b 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java
@@ -16,7 +16,6 @@
*/
package org.apache.camel.dsl.jbang.core.commands.infra;
-import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
@@ -153,17 +152,17 @@ public class InfraRun extends InfraBaseCommand {
printer().println(jsonProperties);
String name = getLogFileName(testService, RuntimeUtil.getPid());
- File logFile = createFile(name);
+ Path logFile = createFile(name);
String jsonName = getJsonFileName(testService, RuntimeUtil.getPid());
- File jsonFile = createFile(jsonName);
- Files.write(jsonFile.toPath(), jsonProperties.getBytes());
+ Path jsonFile = createFile(jsonName);
+ Files.write(jsonFile, jsonProperties.getBytes());
if (Arrays.stream(actualService.getClass().getInterfaces()).filter(
c -> c.getName().contains("ContainerService")).count()
> 0) {
Object containerLogConsumer = cl.loadClass("org.apache.camel.test.infra.common.CamelLogConsumer")
- .getConstructor(Path.class, boolean.class).newInstance(logFile.toPath(), logToStdout);
+ .getConstructor(Path.class, boolean.class).newInstance(logFile, logToStdout);
actualService.getClass()
.getMethod("followLog", cl.loadClass("org.testcontainers.containers.output.BaseConsumer"))
@@ -182,10 +181,9 @@ public class InfraRun extends InfraBaseCommand {
sc.close();
}
- private static File createFile(String name) throws IOException {
- File logFile = new File(CommandLineHelper.getCamelDir(), name);
- logFile.createNewFile();
- logFile.deleteOnExit();
+ private static Path createFile(String name) throws IOException {
+ Path logFile = CommandLineHelper.getCamelDir().resolve(name);
+ Files.createFile(logFile);
return logFile;
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraStop.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraStop.java
index 001b212d40e..174a7833b7f 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraStop.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraStop.java
@@ -16,12 +16,14 @@
*/
package org.apache.camel.dsl.jbang.core.commands.infra;
-import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.List;
+import java.util.stream.Collectors;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
-import org.apache.camel.util.FileUtil;
import picocli.CommandLine;
@CommandLine.Command(name = "stop",
@@ -41,15 +43,21 @@ public class InfraStop extends InfraBaseCommand {
boolean serviceStopped = false;
String pid = null;
- for (File pidFile : CommandLineHelper.getCamelDir().listFiles(
- (dir, name) -> name.startsWith("infra-" + serviceToStop + "-"))) {
+ try {
+ List<Path> pidFiles = Files.list(CommandLineHelper.getCamelDir())
+ .filter(p -> p.getFileName().toString().startsWith("infra-" + serviceToStop + "-"))
+ .collect(java.util.stream.Collectors.toList());
- String name = pidFile.getName();
- pid = name.substring(name.lastIndexOf("-") + 1, name.lastIndexOf('.'));
+ for (Path pidFile : pidFiles) {
+ String name = pidFile.getFileName().toString();
+ pid = name.substring(name.lastIndexOf("-") + 1, name.lastIndexOf('.'));
- FileUtil.deleteFile(pidFile);
-
- serviceStopped = true;
+ Files.deleteIfExists(pidFile);
+ serviceStopped = true;
+ break;
+ }
+ } catch (IOException e) {
+ // ignore
}
if (!serviceStopped) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListKafka.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListKafka.java
index edd2ff1f272..7171ff326d7 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListKafka.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListKafka.java
@@ -16,8 +16,8 @@
*/
package org.apache.camel.dsl.jbang.core.commands.process;
-import java.io.File;
-import java.io.FileInputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -28,10 +28,9 @@ import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import org.apache.camel.dsl.jbang.core.common.PidNameAgeCompletionCandidates;
import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
-import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.TimeUtils;
@@ -102,14 +101,14 @@ public class ListKafka extends ProcessWatchCommand {
if (committed) {
// we ask for committed so need to do an action on-demand to get data
// ensure output file is deleted before executing action
- File outputFile = getOutputFile(Long.toString(ph.pid()));
- FileUtil.deleteFile(outputFile);
+ Path outputFile = getOutputFile(Long.toString(ph.pid()));
+ PathUtils.deleteFile(outputFile);
JsonObject root2 = new JsonObject();
root2.put("action", "kafka");
- File file = getActionFile(Long.toString(ph.pid()));
+ Path file = getActionFile(Long.toString(ph.pid()));
try {
- IOHelper.writeText(root2.toJson(), file);
+ PathUtils.writeTextSafely(root2.toJson(), file);
} catch (Exception e) {
// ignore
}
@@ -296,7 +295,7 @@ public class ListKafka extends ProcessWatchCommand {
}
}
- private JsonObject waitForOutputFile(File outputFile) {
+ private JsonObject waitForOutputFile(Path outputFile) {
JsonObject answer = null;
StopWatch watch = new StopWatch();
@@ -305,10 +304,8 @@ public class ListKafka extends ProcessWatchCommand {
// give time for response to be ready
Thread.sleep(100);
- if (outputFile.exists()) {
- FileInputStream fis = new FileInputStream(outputFile);
- String text = IOHelper.loadText(fis);
- IOHelper.close(fis);
+ if (Files.exists(outputFile)) {
+ String text = Files.readString(outputFile);
answer = (JsonObject) Jsoner.deserialize(text);
}
} catch (Exception e) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ProcessBaseCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ProcessBaseCommand.java
index 1274c448d33..845d5c25d46 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ProcessBaseCommand.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ProcessBaseCommand.java
@@ -16,8 +16,8 @@
*/
package org.apache.camel.dsl.jbang.core.commands.process;
-import java.io.File;
-import java.io.FileInputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@@ -26,7 +26,6 @@ import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
import org.apache.camel.support.PatternHelper;
import org.apache.camel.util.FileUtil;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.json.JsonObject;
import org.apache.camel.util.json.Jsoner;
@@ -95,11 +94,9 @@ abstract class ProcessBaseCommand extends CamelCommand {
JsonObject loadStatus(long pid) {
try {
- File f = getStatusFile(Long.toString(pid));
- if (f != null && f.exists()) {
- FileInputStream fis = new FileInputStream(f);
- String text = IOHelper.loadText(fis);
- IOHelper.close(fis);
+ Path f = getStatusFile(Long.toString(pid));
+ if (f != null && Files.exists(f)) {
+ String text = Files.readString(f);
return (JsonObject) Jsoner.deserialize(text);
}
} catch (Exception e) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
index 1d0b9256f53..6a5b66bdaa9 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
@@ -16,12 +16,13 @@
*/
package org.apache.camel.dsl.jbang.core.commands.process;
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.List;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
-import org.apache.camel.util.FileUtil;
+import org.apache.camel.dsl.jbang.core.common.PathUtils;
import picocli.CommandLine;
import picocli.CommandLine.Command;
@@ -46,10 +47,10 @@ public class StopProcess extends ProcessBaseCommand {
// stop by deleting the pid file
for (Long pid : pids) {
- File pidFile = new File(CommandLineHelper.getCamelDir(), Long.toString(pid));
- if (pidFile.exists()) {
+ Path pidFile = CommandLineHelper.getCamelDir().resolve(Long.toString(pid));
+ if (Files.exists(pidFile)) {
printer().println("Shutting down Camel integration (PID: " + pid + ")");
- FileUtil.deleteFile(pidFile);
+ PathUtils.deleteFile(pidFile);
}
}
for (Long pid : pids) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java
index 129bfc2f71d..31736eaa4d0 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java
@@ -16,17 +16,17 @@
*/
package org.apache.camel.dsl.jbang.core.common;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FileReader;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Properties;
import java.util.function.Consumer;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.OrderedProperties;
import picocli.CommandLine;
@@ -35,7 +35,7 @@ import picocli.CommandLine;
*/
public final class CommandLineHelper {
- private static volatile String homeDir = System.getProperty("user.home");
+ private static volatile Path homeDir = Paths.get(System.getProperty("user.home"));
public static final String USER_CONFIG = ".camel-jbang-user.properties";
public static final String LOCAL_USER_CONFIG = "camel-jbang-user.properties";
public static final String CAMEL_DIR = ".camel";
@@ -46,11 +46,11 @@ public final class CommandLineHelper {
}
public static void augmentWithUserConfiguration(CommandLine commandLine) {
- File file = getUserConfigurationFile();
- if (file.isFile() && file.exists()) {
+ Path file = getUserConfigurationFile();
+ if (Files.isRegularFile(file) && Files.exists(file)) {
Properties properties = new Properties();
- try {
- properties.load(new FileReader(file));
+ try (Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
+ properties.load(reader);
} catch (IOException e) {
commandLine.setDefaultValueProvider(new CamelUserConfigDefaultValueProvider(file));
}
@@ -59,10 +59,10 @@ public final class CommandLineHelper {
}
}
- private static File getUserConfigurationFile() {
- File file;
- if (Files.exists(Path.of(LOCAL_USER_CONFIG))) {
- file = new File(LOCAL_USER_CONFIG);
+ private static Path getUserConfigurationFile() {
+ Path file;
+ if (Files.exists(Paths.get(LOCAL_USER_CONFIG))) {
+ file = Paths.get(LOCAL_USER_CONFIG);
} else {
file = getUserPropertyFile(false);
}
@@ -70,9 +70,9 @@ public final class CommandLineHelper {
}
public static void createPropertyFile(boolean local) throws IOException {
- File file = getUserPropertyFile(local);
- if (!file.exists()) {
- file.createNewFile();
+ Path file = getUserPropertyFile(local);
+ if (!Files.exists(file)) {
+ Files.createFile(file);
}
}
@@ -81,32 +81,28 @@ public final class CommandLineHelper {
}
public static void loadProperties(Consumer<Properties> consumer, boolean local) {
- File file = getUserPropertyFile(local);
- if (file.isFile() && file.exists()) {
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(file);
+ Path file = getUserPropertyFile(local);
+ if (Files.isRegularFile(file) && Files.exists(file)) {
+ try (InputStream is = Files.newInputStream(file)) {
Properties prop = new OrderedProperties();
- prop.load(fis);
+ prop.load(is);
consumer.accept(prop);
} catch (Exception e) {
throw new RuntimeException("Cannot load user configuration: " + file);
- } finally {
- IOHelper.close(fis);
}
}
}
public static void storeProperties(Properties properties, Printer printer, boolean local) {
- File file = getUserPropertyFile(local);
- if (file.isFile() && file.exists()) {
- try (FileOutputStream fos = new FileOutputStream(file)) {
- properties.store(fos, null);
+ Path file = getUserPropertyFile(local);
+ if (Files.isRegularFile(file) && Files.exists(file)) {
+ try (OutputStream os = Files.newOutputStream(file)) {
+ properties.store(os, null);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
} else {
- printer.println(file.getName() + " does not exist");
+ printer.println(file.getFileName() + " does not exist");
}
}
@@ -116,11 +112,11 @@ public final class CommandLineHelper {
* @param local
* @return
*/
- private static File getUserPropertyFile(boolean local) {
+ private static Path getUserPropertyFile(boolean local) {
if (local) {
- return new File(LOCAL_USER_CONFIG);
+ return Paths.get(LOCAL_USER_CONFIG);
} else {
- return new File(homeDir, USER_CONFIG);
+ return homeDir.resolve(USER_CONFIG);
}
}
@@ -129,7 +125,7 @@ public final class CommandLineHelper {
*
* @return the user home directory.
*/
- public static String getHomeDir() {
+ public static Path getHomeDir() {
return homeDir;
}
@@ -140,7 +136,7 @@ public final class CommandLineHelper {
* @param homeDir the home directory.
*/
public static void useHomeDir(String homeDir) {
- CommandLineHelper.homeDir = homeDir;
+ CommandLineHelper.homeDir = Paths.get(homeDir);
}
/**
@@ -148,8 +144,8 @@ public final class CommandLineHelper {
*
* @return file pointing to the camel directory.
*/
- public static File getCamelDir() {
- return new File(homeDir, CAMEL_DIR);
+ public static Path getCamelDir() {
+ return homeDir.resolve(CAMEL_DIR);
}
/**
@@ -157,14 +153,14 @@ public final class CommandLineHelper {
*
* @return file pointing to the working directory.
*/
- public static File getWorkDir() {
- return new File(CAMEL_JBANG_WORK_DIR);
+ public static Path getWorkDir() {
+ return Paths.get(CAMEL_JBANG_WORK_DIR);
}
private static class CamelUserConfigDefaultValueProvider extends CommandLine.PropertiesDefaultProvider {
- public CamelUserConfigDefaultValueProvider(File file) {
- super(file);
+ public CamelUserConfigDefaultValueProvider(Path file) {
+ super(file.toFile());
}
public CamelUserConfigDefaultValueProvider(Properties properties) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/FilePathConverter.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/FilePathConverter.java
new file mode 100644
index 00000000000..78ddbf26c89
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/FilePathConverter.java
@@ -0,0 +1,93 @@
+/*
+ * 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.common;
+
+import java.io.File;
+import java.nio.file.Path;
+
+/**
+ * Utility class for converting between File and Path objects. This is a temporary class to help with the transition
+ * from File to Path.
+ */
+public final class FilePathConverter {
+
+ private FilePathConverter() {
+ // Utility class
+ }
+
+ /**
+ * Convert a Path to a File.
+ *
+ * @param path the path to convert
+ * @return the file
+ */
+ public static File toFile(Path path) {
+ return path.toFile();
+ }
+
+ /**
+ * Convert a File to a Path.
+ *
+ * @param file the file to convert
+ * @return the path
+ */
+ public static Path toPath(File file) {
+ return file.toPath();
+ }
+
+ /**
+ * Create a new Path by resolving a child path against a parent path.
+ *
+ * @param parent the parent path
+ * @param child the child path
+ * @return the resolved path
+ */
+ public static Path resolve(Path parent, String child) {
+ return parent.resolve(child);
+ }
+
+ /**
+ * Create a new File by resolving a child path against a parent path.
+ *
+ * @param parent the parent path
+ * @param child the child path
+ * @return the resolved file
+ */
+ public static File resolveToFile(Path parent, String child) {
+ return parent.resolve(child).toFile();
+ }
+
+ /**
+ * Convert a Path to a File, or return null if the Path is null.
+ *
+ * @param path the path to convert
+ * @return the file, or null if path is null
+ */
+ public static File toFileOrNull(Path path) {
+ return path != null ? path.toFile() : null;
+ }
+
+ /**
+ * Convert a File to a Path, or return null if the File is null.
+ *
+ * @param file the file to convert
+ * @return the path, or null if file is null
+ */
+ public static Path toPathOrNull(File file) {
+ return file != null ? file.toPath() : null;
+ }
+}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PathUtils.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PathUtils.java
new file mode 100644
index 00000000000..e1e0dd98f41
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PathUtils.java
@@ -0,0 +1,167 @@
+/*
+ * 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.common;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Utility methods for working with Path and File objects.
+ */
+public final class PathUtils {
+
+ private PathUtils() {
+ // Utility class
+ }
+
+ /**
+ * Convert a Path to a File. This is a helper method for legacy code that still requires File objects.
+ *
+ * @param path the path to convert
+ * @return the file
+ */
+ public static File toFile(Path path) {
+ return path.toFile();
+ }
+
+ /**
+ * Convert a File to a Path.
+ *
+ * @param file the file to convert
+ * @return the path
+ */
+ public static Path toPath(File file) {
+ return file.toPath();
+ }
+
+ /**
+ * Create a new Path by resolving a child path against a parent path.
+ *
+ * @param parent the parent path
+ * @param child the child path
+ * @return the resolved path
+ */
+ public static Path resolve(Path parent, String child) {
+ return parent.resolve(child);
+ }
+
+ /**
+ * List files in a directory that match a predicate.
+ *
+ * @param dir the directory to list
+ * @param filter the filter to apply
+ * @return the list of matching files
+ */
+ public static List<Path> listFiles(Path dir, Predicate<Path> filter) {
+ try (Stream<Path> stream = Files.list(dir)) {
+ return stream.filter(filter).collect(Collectors.toList());
+ } catch (IOException e) {
+ return List.of();
+ }
+ }
+
+ /**
+ * Delete a file if it exists.
+ *
+ * @param path the path to delete
+ * @return true if the file was deleted, false otherwise
+ */
+ public static boolean deleteFile(Path path) {
+ try {
+ return Files.deleteIfExists(path);
+ } catch (IOException e) {
+ return false;
+ }
+ }
+
+ /**
+ * Write text to a file.
+ *
+ * @param text the text to write
+ * @param path the path to write to
+ * @throws IOException if an I/O error occurs
+ */
+ public static void writeText(String text, Path path) throws IOException {
+ Files.writeString(path, text);
+ }
+
+ /**
+ * Safely write text to a file, ignoring any exceptions.
+ *
+ * @param text the text to write
+ * @param path the path to write to
+ * @return true if the write was successful, false otherwise
+ */
+ public static boolean writeTextSafely(String text, Path path) {
+ try {
+ Files.writeString(path, text);
+ return true;
+ } catch (IOException e) {
+ return false;
+ }
+ }
+
+ /**
+ * Create a Path from a parent Path and a child name. This is a helper method for code that previously used new
+ * File(parentFile, childName).
+ *
+ * @param parent the parent path
+ * @param child the child name
+ * @return the resolved path
+ */
+ public static Path createPath(Path parent, String child) {
+ return parent.resolve(child);
+ }
+
+ /**
+ * Create a File from a parent Path and a child name. This is a helper method for code that previously used new
+ * File(parentFile, childName).
+ *
+ * @param parent the parent path
+ * @param child the child name
+ * @return the resolved file
+ */
+ public static File resolveToFile(Path parent, String child) {
+ return parent.resolve(child).toFile();
+ }
+
+ /**
+ * Convert a Path to a File, or return null if the Path is null.
+ *
+ * @param path the path to convert
+ * @return the file, or null if path is null
+ */
+ public static File toFileOrNull(Path path) {
+ return path != null ? path.toFile() : null;
+ }
+
+ /**
+ * Convert a File to a Path, or return null if the File is null.
+ *
+ * @param file the file to convert
+ * @return the path, or null if file is null
+ */
+ public static Path toPathOrNull(File file) {
+ return file != null ? file.toPath() : null;
+ }
+}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
index fe9e504aa15..7ef9b8cada3 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/PluginHelper.java
@@ -16,11 +16,10 @@
*/
package org.apache.camel.dsl.jbang.core.common;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
import java.util.Properties;
@@ -174,12 +173,10 @@ public final class PluginHelper {
private static JsonObject getPluginConfig() {
try {
- File f = new File(CommandLineHelper.getHomeDir(), PLUGIN_CONFIG);
- if (f.exists()) {
- try (FileInputStream fis = new FileInputStream(f)) {
- String text = IOHelper.loadText(fis);
- return (JsonObject) Jsoner.deserialize(text);
- }
+ Path f = CommandLineHelper.getHomeDir().resolve(PLUGIN_CONFIG);
+ if (Files.exists(f)) {
+ String text = Files.readString(f);
+ return (JsonObject) Jsoner.deserialize(text);
}
} catch (Exception e) {
// ignore
@@ -189,10 +186,10 @@ public final class PluginHelper {
}
public static JsonObject createPluginConfig() {
- File f = new File(CommandLineHelper.getHomeDir(), PLUGIN_CONFIG);
+ Path f = CommandLineHelper.getHomeDir().resolve(PLUGIN_CONFIG);
JsonObject config = Jsoner.deserialize("{ \"plugins\": {} }", new JsonObject());
try {
- Files.writeString(f.toPath(), config.toJson(),
+ Files.writeString(f, config.toJson(),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
@@ -204,9 +201,9 @@ public final class PluginHelper {
}
public static void savePluginConfig(JsonObject plugins) {
- File f = new File(CommandLineHelper.getHomeDir(), PLUGIN_CONFIG);
+ Path f = CommandLineHelper.getHomeDir().resolve(PLUGIN_CONFIG);
try {
- Files.writeString(f.toPath(), plugins.toJson(),
+ Files.writeString(f, plugins.toJson(),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/VersionHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/VersionHelper.java
index 19c0e0086d2..692336fd3fe 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/VersionHelper.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/VersionHelper.java
@@ -16,10 +16,10 @@
*/
package org.apache.camel.dsl.jbang.core.common;
-import java.io.File;
-import java.io.FileInputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
-import org.apache.camel.util.IOHelper;
import org.apache.camel.util.StringHelper;
public final class VersionHelper {
@@ -35,13 +35,11 @@ public final class VersionHelper {
if (homeDir == null || homeDir.isBlank()) {
// fallback to .jbang cache that has a list of latest version
path = ".jbang/cache/";
- homeDir = CommandLineHelper.getHomeDir();
+ homeDir = CommandLineHelper.getHomeDir().toString();
}
- File file = new File(homeDir, path + "version.txt");
- if (file.exists() && file.isFile()) {
- FileInputStream fis = new FileInputStream(file);
- String text = IOHelper.loadText(fis);
- IOHelper.close(fis);
+ Path file = Paths.get(homeDir).resolve(path + "version.txt");
+ if (Files.exists(file) && Files.isRegularFile(file)) {
+ String text = Files.readString(file);
text = text.trim();
return text;
}
diff --git a/dsl/camel-jbang/camel-jbang-main/dist/classes/CamelJBang.java b/dsl/camel-jbang/camel-jbang-main/dist/classes/CamelJBang.java
new file mode 100644
index 00000000000..2c5ad8e388b
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-main/dist/classes/CamelJBang.java
@@ -0,0 +1,39 @@
+///usr/bin/env jbang "$0" "$@" ; exit $?
+
+/*
+ * 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.
+ */
+
+//JAVA 17+
+//REPOS central=https://repo1.maven.org/maven2,apache-snapshot=https://repository.apache.org/content/groups/snapshots/
+//DEPS org.apache.camel:camel-bom:${camel.jbang.version:4.11.0}@pom
+//DEPS org.apache.camel:camel-jbang-core:${camel.jbang.version:4.11.0}
+//DEPS org.apache.camel.kamelets:camel-kamelets:${camel-kamelets.version:4.11.0}
+
+package main;
+
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+
+/**
+ * Main to run CamelJBang
+ */
+public class CamelJBang {
+
+ public static void main(String... args) {
+ CamelJBangMain.run(args);
+ }
+
+}
diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExport.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExport.java
index 1ff2cba6658..b391555cc64 100644
--- a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExport.java
+++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/KubernetesExport.java
@@ -334,7 +334,7 @@ public class KubernetesExport extends Export {
addDependencies("org.apache.camel:camel-health", "org.apache.camel:camel-platform-http-main");
}
- File settings = new File(CommandLineHelper.getWorkDir(), Run.RUN_SETTINGS_FILE);
+ File settings = new File(CommandLineHelper.getWorkDir().toString(), Run.RUN_SETTINGS_FILE);
var jkubeVersion = jkubeMavenPluginVersion(settings, mapBuildProperties());
buildProperties.add("jkube.version=%s".formatted(jkubeVersion));
diff --git a/dsl/camel-jbang/fix-all-files.sh b/dsl/camel-jbang/fix-all-files.sh
new file mode 100755
index 00000000000..a2be19a6e37
--- /dev/null
+++ b/dsl/camel-jbang/fix-all-files.sh
@@ -0,0 +1,62 @@
+#!/bin/bash
+#
+# 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.
+#
+
+
+# Fix all the remaining files with Path/File conversion issues
+find camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action -name "*.java" | while read file; do
+ echo "Fixing $file"
+ # Add imports
+ sed -i '' 's/import java.io.File;/import java.nio.file.Files;\nimport java.nio.file.Path;\nimport org.apache.camel.dsl.jbang.core.common.FilePathConverter;\nimport org.apache.camel.dsl.jbang.core.common.PathUtils;/g' "$file"
+ # Fix File to Path conversions
+ sed -i '' 's/File f = getActionFile/Path f = getActionFile/g' "$file"
+ sed -i '' 's/File f = getTraceFile/Path f = getTraceFile/g' "$file"
+ sed -i '' 's/File f = getReceiveFile/Path f = getReceiveFile/g' "$file"
+ sed -i '' 's/File f = getOutputFile/Path f = getOutputFile/g' "$file"
+ sed -i '' 's/File outputFile = getOutputFile/Path outputFile = getOutputFile/g' "$file"
+ sed -i '' 's/File file = getTraceFile/Path file = getTraceFile/g' "$file"
+ sed -i '' 's/File file = getReceiveFile/Path file = getReceiveFile/g' "$file"
+ # Fix file.exists() to Files.exists(file)
+ sed -i '' 's/file.exists()/Files.exists(file)/g' "$file"
+ sed -i '' 's/f.exists()/Files.exists(f)/g' "$file"
+ sed -i '' 's/outputFile.exists()/Files.exists(outputFile)/g' "$file"
+ # Fix file.length() to Files.size(file)
+ sed -i '' 's/file.length()/Files.size(file)/g' "$file"
+ # Fix IOHelper.writeText to Files.writeString
+ sed -i '' 's/IOHelper.writeText(root.toJson(), f);/Files.writeString(f, root.toJson());/g' "$file"
+ sed -i '' 's/IOHelper.writeText("{}", f);/Files.writeString(f, "{}");/g' "$file"
+ # Fix FileReader to Files.newBufferedReader
+ sed -i '' 's/new FileReader(file)/Files.newBufferedReader(file)/g' "$file"
+ sed -i '' 's/new FileReader(f)/Files.newBufferedReader(f)/g' "$file"
+ # Fix FileUtil.deleteFile to PathUtils.deleteFile
+ sed -i '' 's/FileUtil.deleteFile(outputFile);/PathUtils.deleteFile(outputFile);/g' "$file"
+ sed -i '' 's/FileUtil.deleteFile(file);/PathUtils.deleteFile(file);/g' "$file"
+ sed -i '' 's/FileUtil.deleteFile(f);/PathUtils.deleteFile(f);/g' "$file"
+done
+
+# Fix ExportCamelMain.java
+echo "Fixing ExportCamelMain.java"
+sed -i '' 's/resolveDependencies(settings, profile)/resolveDependencies(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile))/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+sed -i '' 's/copyApplicationPropertiesFiles(srcResourcesDir)/copyApplicationPropertiesFiles(FilePathConverter.toFile(srcResourcesDir))/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+sed -i '' 's/copyDockerFiles(BUILD_DIR)/copyDockerFiles(BUILD_DIR)/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+sed -i '' 's/copyReadme(BUILD_DIR, appJar)/copyReadme(BUILD_DIR, appJar)/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+
+# Fix ExportQuarkus.java
+echo "Fixing ExportQuarkus.java"
+sed -i '' 's/resolveDependencies(settings, profile)/resolveDependencies(FilePathConverter.toFile(settings), FilePathConverter.toFile(profile))/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
+sed -i '' 's/copyDockerFiles(BUILD_DIR)/copyDockerFiles(BUILD_DIR)/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
+sed -i '' 's/copyReadme(BUILD_DIR, appJar)/copyReadme(BUILD_DIR, appJar)/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
diff --git a/dsl/camel-jbang/fix-file-imports.sh b/dsl/camel-jbang/fix-file-imports.sh
new file mode 100755
index 00000000000..a7b108e64f6
--- /dev/null
+++ b/dsl/camel-jbang/fix-file-imports.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+#
+# 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.
+#
+
+
+# Add java.io.File import to all action files
+find camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action -name "*.java" | while read file; do
+ echo "Fixing imports in $file"
+ # Add java.io.File import if it doesn't exist
+ if ! grep -q "import java.io.File;" "$file"; then
+ sed -i '' 's/import java.nio.file.Files;/import java.io.File;\nimport java.nio.file.Files;/g' "$file"
+ fi
+done
diff --git a/dsl/camel-jbang/fix-file-to-path.sh b/dsl/camel-jbang/fix-file-to-path.sh
new file mode 100755
index 00000000000..128ec1bb5ed
--- /dev/null
+++ b/dsl/camel-jbang/fix-file-to-path.sh
@@ -0,0 +1,75 @@
+#!/bin/bash
+#
+# 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.
+#
+
+
+# This script helps fix java.io.File to java.nio.Path conversion issues
+
+# Add FilePathConverter import to a file
+add_import() {
+ file=$1
+ if ! grep -q "import org.apache.camel.dsl.jbang.core.common.FilePathConverter;" "$file"; then
+ sed -i '' '/^import/a\\
+import org.apache.camel.dsl.jbang.core.common.FilePathConverter;
+' "$file"
+ fi
+}
+
+# Fix Path to File conversions
+fix_path_to_file() {
+ file=$1
+ # Replace Path variables used as File
+ sed -i '' 's/\([a-zA-Z0-9_]*\): Path.*cannot be converted to.*File/FilePathConverter.toFile(\1)/g' "$file"
+}
+
+# Fix File to Path conversions
+fix_file_to_path() {
+ file=$1
+ # Replace File variables used as Path
+ sed -i '' 's/\([a-zA-Z0-9_]*\): File.*cannot be converted to.*Path/FilePathConverter.toPath(\1)/g' "$file"
+}
+
+# Fix File constructor with Path parent
+fix_file_constructor() {
+ file=$1
+ # Replace new File(Path, String) with FilePathConverter.resolveToFile(Path, String)
+ sed -i '' 's/new File(\([a-zA-Z0-9_]*\): Path, \([^)]*\))/FilePathConverter.resolveToFile(\1, \2)/g' "$file"
+}
+
+# Process a file
+process_file() {
+ file=$1
+ echo "Processing $file"
+ add_import "$file"
+ fix_path_to_file "$file"
+ fix_file_to_path "$file"
+ fix_file_constructor "$file"
+}
+
+# Main script
+if [ $# -eq 0 ]; then
+ echo "Usage: $0 <file1> [file2] ..."
+ exit 1
+fi
+
+for file in "$@"; do
+ if [ -f "$file" ]; then
+ process_file "$file"
+ else
+ echo "File not found: $file"
+ fi
+done
diff --git a/dsl/camel-jbang/fix-remaining-issues.sh b/dsl/camel-jbang/fix-remaining-issues.sh
new file mode 100755
index 00000000000..e698f665978
--- /dev/null
+++ b/dsl/camel-jbang/fix-remaining-issues.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+#
+# 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.
+#
+
+
+# Fix the ExportCamelMain.java file
+sed -i '' 's/hasModeline(settings)/hasModeline(FilePathConverter.toFile(settings))/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+sed -i '' 's/httpServerPort(settings)/httpServerPort(FilePathConverter.toFile(settings))/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+sed -i '' 's/jibMavenPluginVersion(settings, prop)/jibMavenPluginVersion(FilePathConverter.toFile(settings), prop)/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+
+# Fix the ExportQuarkus.java file
+sed -i '' 's/createMavenPom(FilePathConverter.toFile(settings), new File(BUILD_DIR, "pom.xml"), deps);/createMavenPom(FilePathConverter.toFile(settings), FilePathConverter.toFile(Path.of(BUILD_DIR).resolve("pom.xml")), deps);/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
+
+# Add missing imports
+echo "import java.nio.file.Files;" >> missing-imports.txt
+find camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action -name "*.java" | while read file; do
+ if ! grep -q "import java.nio.file.Files;" "$file"; then
+ sed -i '' 's/import java.nio.file.Path;/import java.nio.file.Files;\nimport java.nio.file.Path;/g' "$file"
+ fi
+done
diff --git a/dsl/camel-jbang/fix-remaining-issues2.sh b/dsl/camel-jbang/fix-remaining-issues2.sh
new file mode 100755
index 00000000000..bf93c9d10a3
--- /dev/null
+++ b/dsl/camel-jbang/fix-remaining-issues2.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+#
+# 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.
+#
+
+
+# Fix the ExportCamelMain.java file
+sed -i '' 's/jkubeMavenPluginVersion(settings, prop)/jkubeMavenPluginVersion(FilePathConverter.toFile(settings), prop)/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+
+# Fix the ExportQuarkus.java file
+sed -i '' 's/createBuildGradle(settings, FilePathConverter.toFile(Path.of(BUILD_DIR).resolve("build.gradle")), deps)/createBuildGradle(FilePathConverter.toFile(settings), FilePathConverter.toFile(Path.of(BUILD_DIR).resolve("build.gradle")), deps)/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
diff --git a/dsl/camel-jbang/fix-remaining-issues3.sh b/dsl/camel-jbang/fix-remaining-issues3.sh
new file mode 100755
index 00000000000..ea29b6c8b29
--- /dev/null
+++ b/dsl/camel-jbang/fix-remaining-issues3.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+#
+# 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.
+#
+
+
+# Fix the ExportCamelMain.java file
+sed -i '' 's/jibMavenPluginVersion(FilePathConverter.toFile(settings), prop)/jibMavenPluginVersion(settings, prop)/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+sed -i '' 's/jkubeMavenPluginVersion(FilePathConverter.toFile(settings), prop)/jkubeMavenPluginVersion(settings, prop)/g' camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
diff --git a/dsl/camel-jbang/missing-imports.txt b/dsl/camel-jbang/missing-imports.txt
new file mode 100644
index 00000000000..0d2aba19915
--- /dev/null
+++ b/dsl/camel-jbang/missing-imports.txt
@@ -0,0 +1 @@
+import java.nio.file.Files;