You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/11/02 13:06:42 UTC
[camel] branch master updated: CAMEL-15781: camel-core - Optimize
FilePathResolver to use simpler parser than regexp
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 13a2ced CAMEL-15781: camel-core - Optimize FilePathResolver to use simpler parser than regexp
13a2ced is described below
commit 13a2ced1629d8dcd17fec49137f5c71a46423db1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Nov 2 14:06:02 2020 +0100
CAMEL-15781: camel-core - Optimize FilePathResolver to use simpler parser than regexp
---
.../apache/camel/util/FilePathResolverTest.java | 7 +++
.../org/apache/camel/util/FilePathResolver.java | 56 +++++++++++-----------
2 files changed, 34 insertions(+), 29 deletions(-)
diff --git a/core/camel-core/src/test/java/org/apache/camel/util/FilePathResolverTest.java b/core/camel-core/src/test/java/org/apache/camel/util/FilePathResolverTest.java
index aa602d7..0672e0a 100644
--- a/core/camel-core/src/test/java/org/apache/camel/util/FilePathResolverTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/util/FilePathResolverTest.java
@@ -26,10 +26,17 @@ public class FilePathResolverTest {
public void testFilePathResolver() throws Exception {
assertEquals("/foo/bar", FilePathResolver.resolvePath("/foo/bar"));
+ assertEquals("/foo/myserver/bar", FilePathResolver.resolvePath("/foo/${env.FOO_SERVICE_HOST}/bar"));
+
String tmp = System.getProperty("java.io.tmpdir");
assertEquals(tmp + "foo", FilePathResolver.resolvePath("${java.io.tmpdir}foo"));
System.setProperty("beer", "Carlsberg");
assertEquals(tmp + "foo/Carlsberg", FilePathResolver.resolvePath("${java.io.tmpdir}foo/${beer}"));
+
+ assertEquals("/myprefix/" + tmp + "bar/Carlsberg",
+ FilePathResolver.resolvePath("/myprefix/${java.io.tmpdir}bar/${beer}"));
+
+ assertEquals("/foo/myserver/bar/Carlsberg", FilePathResolver.resolvePath("/foo/${env.FOO_SERVICE_HOST}/bar/${beer}"));
}
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/FilePathResolver.java b/core/camel-util/src/main/java/org/apache/camel/util/FilePathResolver.java
index 836424d..adb14f5 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/FilePathResolver.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/FilePathResolver.java
@@ -16,18 +16,11 @@
*/
package org.apache.camel.util;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
/**
* A resolver for file paths that supports resolving with system and environment properties.
*/
public final class FilePathResolver {
- // must be non greedy patterns
- private static final Pattern ENV_PATTERN = Pattern.compile("\\$\\{env:(.*?)\\}", Pattern.DOTALL);
- private static final Pattern SYS_PATTERN = Pattern.compile("\\$\\{(.*?)\\}", Pattern.DOTALL);
-
private FilePathResolver() {
}
@@ -46,32 +39,37 @@ public final class FilePathResolver {
* @throws IllegalArgumentException is thrown if system property / environment not found
*/
public static String resolvePath(String path) throws IllegalArgumentException {
- Matcher matcher = ENV_PATTERN.matcher(path);
- while (matcher.find()) {
- String key = matcher.group(1);
- String value = System.getenv(key);
- if (ObjectHelper.isEmpty(value)) {
- throw new IllegalArgumentException("Cannot find system environment with key: " + key);
+ int count = StringHelper.countChar(path, '}') + 1;
+ if (count <= 1) {
+ return path;
+ }
+
+ String[] functions = StringHelper.splitOnCharacter(path, "}", count);
+ for (String fun : functions) {
+ int pos = fun.indexOf("${env.");
+ if (pos != -1) {
+ String key = fun.substring(pos + 6);
+ String value = System.getenv(key);
+ if (value != null) {
+ path = path.replace("${env." + key + "}", value);
+ }
}
- // must quote the replacement to have it work as literal replacement
- value = Matcher.quoteReplacement(value);
- path = matcher.replaceFirst(value);
- // must match again as location is changed
- matcher = ENV_PATTERN.matcher(path);
}
- matcher = SYS_PATTERN.matcher(path);
- while (matcher.find()) {
- String key = matcher.group(1);
- String value = System.getProperty(key);
- if (ObjectHelper.isEmpty(value)) {
- throw new IllegalArgumentException("Cannot find JVM system property with key: " + key);
+ count = StringHelper.countChar(path, '}') + 1;
+ if (count <= 1) {
+ return path;
+ }
+ functions = StringHelper.splitOnCharacter(path, "}", count);
+ for (String fun : functions) {
+ int pos = fun.indexOf("${");
+ if (pos != -1) {
+ String key = fun.substring(pos + 2);
+ String value = System.getProperty(key);
+ if (value != null) {
+ path = path.replace("${" + key + "}", value);
+ }
}
- // must quote the replacement to have it work as literal replacement
- value = Matcher.quoteReplacement(value);
- path = matcher.replaceFirst(value);
- // must match again as location is changed
- matcher = SYS_PATTERN.matcher(path);
}
return path;