[camel] 02/02: CAMEL-17073: Fixed simple language caching bug.
This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.7.x in repository https://gitbox.apache.org/repos/asf/camel.git commit aa54cad66a636e207452e503526feaba2d8d8ac6 Author: Claus Ibsen AuthorDate: Wed Oct 13 14:33:45 2021 +0200 CAMEL-17073: Fixed simple language caching bug. --- .../modules/languages/pages/simple-language.adoc | 8 +--- .../camel/language/simple/SimpleLanguage.java | 43 +- .../camel/language/simple/SimpleTokenizer.java | 14 +++ .../language/simple/SimpleCacheExpressionTest.java | 5 +-- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc index 6e98a0b..e28a7fb 100644 --- a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc +++ b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc @@ -471,19 +471,13 @@ function, otherwise parsed as literal. |-- |To decrement a number by one. The left hand side must be a function, otherwise parsed as literal. -|\ |To escape a value, eg \$, to indicate a $ sign. -Special: Use \n for new line, \t for tab, and \r for carriage return. -*Notice:* Escaping is *not* supported using the -xref:file-language.adoc[File Language]. *Notice:* The escape character is not supported, use the -following three special escaping instead. - |\n |To use newline character. |\t |To use tab character. |\r |To use carriage return character. -|\} |To use the } character as text +|\} |To use the } character as text. This may be needed when building a JSon structure with the simple language. |=== And the following logical operators can be used to group expressions: diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java index 4e67a11..6f910d0 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java @@ -166,13 +166,18 @@ public class SimpleLanguage extends LanguageSupport implements StaticService { public Expression createExpression(String expression) { ObjectHelper.notNull(expression, "expression"); -Expression answer = cacheExpression != null ? cacheExpression.get(expression) : null; -if (answer == null) { +Expression answer = null; + +// only lookup in cache if there are functions or special escape tokens +boolean function = hasSimpleFunction(expression) || hasEscapeToken(expression); +if (function && cacheExpression != null) { +answer = cacheExpression.get(expression); +} +if (answer == null) { if (isDynamicResource(expression)) { // we need to load the resource dynamic based on evaluating the expression via the exchange -// so create an embedded expression as result -// need to lazy eval as its a dynamic resource +// so create an embedded expression as result need to lazy eval due to dynamic resource final String text = expression; return new Expression() { @Override @@ -191,18 +196,28 @@ public class SimpleLanguage extends LanguageSupport implements StaticService { } if (isStaticResource(expression)) { +// load static resource and re-eval if there are functions expression = loadResource(expression); +function = hasSimpleFunction(expression) || hasEscapeToken(expression); } -SimpleExpressionParser parser -= new SimpleExpressionParser(getCamelContext(), expression, allowEscape, cacheExpression); -answer = parser.parseExpression(); +// only parse if there are simple functions +if (function) { +SimpleExpressionParser parser += new SimpleExpressionParser(getCamelContext(), expression, allowEscape, cacheExpression); +answer = parser.parseExpression(); -if (cacheExpression != null && answer != null) { -cacheExpression.put(expression, answer); +if (cacheExpression != null && answer != null) { +cacheExpression.put(expression, answer); +} } } +if (answer == null) { +// it has no functions so its static text +answer = ExpressionBuilder.constantExpression(expression); +} + return answer; } @@ -269,4 +284,14 @@ public class
[camel] 02/02: CAMEL-17073: Fixed simple language caching bug.
This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.11.x in repository https://gitbox.apache.org/repos/asf/camel.git commit a153e6f570f88ace20542159f4feffc28b75d867 Author: Claus Ibsen AuthorDate: Wed Oct 13 14:33:45 2021 +0200 CAMEL-17073: Fixed simple language caching bug. --- .../modules/languages/pages/simple-language.adoc | 8 +--- .../camel/language/simple/SimpleLanguage.java | 43 +- .../camel/language/simple/SimpleTokenizer.java | 14 +++ .../language/simple/SimpleCacheExpressionTest.java | 5 +-- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc index d811592..d17eb2c 100644 --- a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc +++ b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc @@ -486,19 +486,13 @@ function, otherwise parsed as literal. |-- |To decrement a number by one. The left hand side must be a function, otherwise parsed as literal. -|\ |To escape a value, eg \$, to indicate a $ sign. -Special: Use \n for new line, \t for tab, and \r for carriage return. -*Notice:* Escaping is *not* supported using the -xref:file-language.adoc[File Language]. *Notice:* The escape character is not supported, use the -following three special escaping instead. - |\n |To use newline character. |\t |To use tab character. |\r |To use carriage return character. -|\} |To use the } character as text +|\} |To use the } character as text. This may be needed when building a JSon structure with the simple language. |=== And the following logical operators can be used to group expressions: diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java index 4e67a11..6f910d0 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java @@ -166,13 +166,18 @@ public class SimpleLanguage extends LanguageSupport implements StaticService { public Expression createExpression(String expression) { ObjectHelper.notNull(expression, "expression"); -Expression answer = cacheExpression != null ? cacheExpression.get(expression) : null; -if (answer == null) { +Expression answer = null; + +// only lookup in cache if there are functions or special escape tokens +boolean function = hasSimpleFunction(expression) || hasEscapeToken(expression); +if (function && cacheExpression != null) { +answer = cacheExpression.get(expression); +} +if (answer == null) { if (isDynamicResource(expression)) { // we need to load the resource dynamic based on evaluating the expression via the exchange -// so create an embedded expression as result -// need to lazy eval as its a dynamic resource +// so create an embedded expression as result need to lazy eval due to dynamic resource final String text = expression; return new Expression() { @Override @@ -191,18 +196,28 @@ public class SimpleLanguage extends LanguageSupport implements StaticService { } if (isStaticResource(expression)) { +// load static resource and re-eval if there are functions expression = loadResource(expression); +function = hasSimpleFunction(expression) || hasEscapeToken(expression); } -SimpleExpressionParser parser -= new SimpleExpressionParser(getCamelContext(), expression, allowEscape, cacheExpression); -answer = parser.parseExpression(); +// only parse if there are simple functions +if (function) { +SimpleExpressionParser parser += new SimpleExpressionParser(getCamelContext(), expression, allowEscape, cacheExpression); +answer = parser.parseExpression(); -if (cacheExpression != null && answer != null) { -cacheExpression.put(expression, answer); +if (cacheExpression != null && answer != null) { +cacheExpression.put(expression, answer); +} } } +if (answer == null) { +// it has no functions so its static text +answer = ExpressionBuilder.constantExpression(expression); +} + return answer; } @@ -269,4 +284,14 @@ public class
[camel] 02/02: CAMEL-17073: Fixed simple language caching bug.
This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git commit e25112d1293a9ff66a5f1ffee10490eaffdaad3c Author: Claus Ibsen AuthorDate: Wed Oct 13 14:33:45 2021 +0200 CAMEL-17073: Fixed simple language caching bug. --- .../modules/languages/pages/simple-language.adoc | 8 +--- .../camel/language/simple/SimpleLanguage.java | 43 +- .../camel/language/simple/SimpleTokenizer.java | 14 +++ .../language/simple/SimpleCacheExpressionTest.java | 5 +-- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc index b71b6fb..62bd070 100644 --- a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc +++ b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc @@ -444,19 +444,13 @@ function, otherwise parsed as literal. |-- |To decrement a number by one. The left hand side must be a function, otherwise parsed as literal. -|\ |To escape a value, eg \$, to indicate a $ sign. -Special: Use \n for new line, \t for tab, and \r for carriage return. -*Notice:* Escaping is *not* supported using the -xref:file-language.adoc[File Language]. *Notice:* The escape character is not supported, use the -following three special escaping instead. - |\n |To use newline character. |\t |To use tab character. |\r |To use carriage return character. -|\} |To use the } character as text +|\} |To use the } character as text. This may be needed when building a JSon structure with the simple language. |=== And the following logical operators can be used to group expressions: diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java index 4e67a11..6f910d0 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java @@ -166,13 +166,18 @@ public class SimpleLanguage extends LanguageSupport implements StaticService { public Expression createExpression(String expression) { ObjectHelper.notNull(expression, "expression"); -Expression answer = cacheExpression != null ? cacheExpression.get(expression) : null; -if (answer == null) { +Expression answer = null; + +// only lookup in cache if there are functions or special escape tokens +boolean function = hasSimpleFunction(expression) || hasEscapeToken(expression); +if (function && cacheExpression != null) { +answer = cacheExpression.get(expression); +} +if (answer == null) { if (isDynamicResource(expression)) { // we need to load the resource dynamic based on evaluating the expression via the exchange -// so create an embedded expression as result -// need to lazy eval as its a dynamic resource +// so create an embedded expression as result need to lazy eval due to dynamic resource final String text = expression; return new Expression() { @Override @@ -191,18 +196,28 @@ public class SimpleLanguage extends LanguageSupport implements StaticService { } if (isStaticResource(expression)) { +// load static resource and re-eval if there are functions expression = loadResource(expression); +function = hasSimpleFunction(expression) || hasEscapeToken(expression); } -SimpleExpressionParser parser -= new SimpleExpressionParser(getCamelContext(), expression, allowEscape, cacheExpression); -answer = parser.parseExpression(); +// only parse if there are simple functions +if (function) { +SimpleExpressionParser parser += new SimpleExpressionParser(getCamelContext(), expression, allowEscape, cacheExpression); +answer = parser.parseExpression(); -if (cacheExpression != null && answer != null) { -cacheExpression.put(expression, answer); +if (cacheExpression != null && answer != null) { +cacheExpression.put(expression, answer); +} } } +if (answer == null) { +// it has no functions so its static text +answer = ExpressionBuilder.constantExpression(expression); +} + return answer; } @@ -269,4 +284,14 @@ public class