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
The following commit(s) were added to refs/heads/main by this push:
new 55effd53ff1c CAMEL-22935: Generate know core simple functions and
validate that adding new custom function does not clash with core names
55effd53ff1c is described below
commit 55effd53ff1c285906e506b5bf1f6c6c9a9ea4ca
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Feb 1 22:07:38 2026 +0100
CAMEL-22935: Generate know core simple functions and validate that adding
new custom function does not clash with core names
---
catalog/camel-catalog/pom.xml | 2 +
.../apache/camel/spi/SimpleFunctionRegistry.java | 22 ++-
.../impl/engine/DefaultSimpleFunctionRegistry.java | 26 +++-
.../camel/impl/console/SimpleLanguageConsole.java | 22 ++-
.../impl/console/SimpleLanguageDevConsoleTest.java | 5 +-
.../camel/language/simple/ast/ChainExpression.java | 2 +-
.../simple/ast/SimpleFunctionExpression.java | 2 +-
.../language/simple/SimpleCustomFunctionTest.java | 4 +-
.../mbean/ManagedSimpleFunctionRegistryMBean.java | 14 +-
.../mbean/ManagedSimpleFunctionRepository.java | 18 ++-
.../ManagedSimpleFunctionRegistryTest.java | 12 +-
.../java/org/apache/camel/util/SimpleUtils.java | 151 +++++++++++++++++++++
.../camel/maven/packaging/UpdateSimpleHelper.java | 142 +++++++++++++++++++
13 files changed, 377 insertions(+), 45 deletions(-)
diff --git a/catalog/camel-catalog/pom.xml b/catalog/camel-catalog/pom.xml
index 42082b5bf00b..2aa71fda2e35 100644
--- a/catalog/camel-catalog/pom.xml
+++ b/catalog/camel-catalog/pom.xml
@@ -163,6 +163,8 @@
<goal>update-important-header-helper</goal>
<!-- update names in camel-main -->
<goal>update-main-helper</goal>
+ <!-- update simple fuinctions in
camel-core-languages -->
+ <goal>update-simple-helper</goal>
<!-- update test-infra metadata -->
<goal>update-test-infra-metadata</goal>
</goals>
diff --git
a/core/camel-api/src/main/java/org/apache/camel/spi/SimpleFunctionRegistry.java
b/core/camel-api/src/main/java/org/apache/camel/spi/SimpleFunctionRegistry.java
index ea594014d5f5..bd5538e017fc 100644
---
a/core/camel-api/src/main/java/org/apache/camel/spi/SimpleFunctionRegistry.java
+++
b/core/camel-api/src/main/java/org/apache/camel/spi/SimpleFunctionRegistry.java
@@ -27,15 +27,15 @@ import org.apache.camel.StaticService;
public interface SimpleFunctionRegistry extends StaticService {
/**
- * Add a function
+ * Add a custom simple function
*
- * @param name name of function
+ * @param name name of custom simple function
* @param expression the expression to use as the function
*/
void addFunction(String name, Expression expression);
/**
- * Remove a function
+ * Remove a custom simple function
*
* @param name name of function
*/
@@ -50,13 +50,23 @@ public interface SimpleFunctionRegistry extends
StaticService {
Expression getFunction(String name);
/**
- * Returns a set with all the function names
+ * Returns a set with all the custom function names
*/
- Set<String> getFunctionNames();
+ Set<String> getCustomFunctionNames();
+
+ /**
+ * Returns a set with all the core/built-in function names
+ */
+ Set<String> getCoreFunctionNames();
/**
* Number of custom functions
*/
- int size();
+ int customSize();
+
+ /**
+ * Number of core functions
+ */
+ int coreSize();
}
diff --git
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultSimpleFunctionRegistry.java
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultSimpleFunctionRegistry.java
index d7d414081325..f874c19fa389 100644
---
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultSimpleFunctionRegistry.java
+++
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultSimpleFunctionRegistry.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.impl.engine;
+import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -25,6 +26,7 @@ import org.apache.camel.Expression;
import org.apache.camel.StaticService;
import org.apache.camel.spi.SimpleFunctionRegistry;
import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.SimpleUtils;
/**
* Default {@link SimpleFunctionRegistry}.
@@ -40,6 +42,10 @@ public class DefaultSimpleFunctionRegistry extends
ServiceSupport implements Sim
@Override
public void addFunction(String name, Expression expression) {
+ String lower = name.toLowerCase(Locale.ENGLISH);
+ if (SimpleUtils.getFunctions().contains(lower)) {
+ throw new IllegalArgumentException("Simple already have built-in
function with name: " + name);
+ }
expression.init(camelContext);
functions.put(name, expression);
}
@@ -55,18 +61,28 @@ public class DefaultSimpleFunctionRegistry extends
ServiceSupport implements Sim
}
@Override
- public Set<String> getFunctionNames() {
+ public Set<String> getCustomFunctionNames() {
return functions.keySet();
}
@Override
- protected void doStop() throws Exception {
- super.doShutdown();
- functions.clear();
+ public Set<String> getCoreFunctionNames() {
+ return SimpleUtils.getFunctions();
}
@Override
- public int size() {
+ public int customSize() {
return functions.size();
}
+
+ @Override
+ public int coreSize() {
+ return getCoreFunctionNames().size();
+ }
+
+ @Override
+ protected void doStop() throws Exception {
+ super.doShutdown();
+ functions.clear();
+ }
}
diff --git
a/core/camel-console/src/main/java/org/apache/camel/impl/console/SimpleLanguageConsole.java
b/core/camel-console/src/main/java/org/apache/camel/impl/console/SimpleLanguageConsole.java
index d32421c14ce2..39dcfcfce097 100644
---
a/core/camel-console/src/main/java/org/apache/camel/impl/console/SimpleLanguageConsole.java
+++
b/core/camel-console/src/main/java/org/apache/camel/impl/console/SimpleLanguageConsole.java
@@ -37,11 +37,14 @@ public class SimpleLanguageConsole extends
AbstractDevConsole {
StringBuilder sb = new StringBuilder();
SimpleFunctionRegistry reg =
PluginHelper.getSimpleFunctionRegistry(getCamelContext());
- sb.append(String.format("%n Custom Functions: %d", reg.size()));
- for (String name : reg.getFunctionNames()) {
+ sb.append(String.format("%n Core Functions: %d", reg.coreSize()));
+ for (String name : reg.getCoreFunctionNames()) {
+ sb.append(String.format("%n %s", name));
+ }
+ sb.append(String.format("%n Custom Functions: %d",
reg.customSize()));
+ for (String name : reg.getCustomFunctionNames()) {
sb.append(String.format("%n %s", name));
}
-
return sb.toString();
}
@@ -50,13 +53,18 @@ public class SimpleLanguageConsole extends
AbstractDevConsole {
SimpleFunctionRegistry reg =
PluginHelper.getSimpleFunctionRegistry(getCamelContext());
JsonObject root = new JsonObject();
- root.put("size", reg.size());
+ root.put("coreSize", reg.coreSize());
+ root.put("customSize", reg.customSize());
JsonArray arr = new JsonArray();
- arr.addAll(reg.getFunctionNames());
+ arr.addAll(reg.getCoreFunctionNames());
if (!arr.isEmpty()) {
- root.put("functions", arr);
+ root.put("coreFunctions", arr);
+ }
+ arr = new JsonArray();
+ arr.addAll(reg.getCustomFunctionNames());
+ if (!arr.isEmpty()) {
+ root.put("customFunctions", arr);
}
-
return root;
}
}
diff --git
a/core/camel-console/src/test/java/org/apache/camel/impl/console/SimpleLanguageDevConsoleTest.java
b/core/camel-console/src/test/java/org/apache/camel/impl/console/SimpleLanguageDevConsoleTest.java
index ae79db9056ce..2778d12c0338 100644
---
a/core/camel-console/src/test/java/org/apache/camel/impl/console/SimpleLanguageDevConsoleTest.java
+++
b/core/camel-console/src/test/java/org/apache/camel/impl/console/SimpleLanguageDevConsoleTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.impl.console;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.console.DevConsole;
import org.apache.camel.support.PluginHelper;
+import org.apache.camel.util.SimpleUtils;
import org.apache.camel.util.json.JsonObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -34,6 +35,7 @@ public class SimpleLanguageDevConsoleTest extends
ContextTestSupport {
String out = (String) con.call(DevConsole.MediaType.TEXT);
Assertions.assertNotNull(out);
+ Assertions.assertTrue(out.contains("Core Functions: " +
SimpleUtils.getFunctions().size()));
Assertions.assertTrue(out.contains("Custom Functions: 0"));
}
@@ -45,7 +47,8 @@ public class SimpleLanguageDevConsoleTest extends
ContextTestSupport {
Assertions.assertEquals("simple-language", con.getId());
JsonObject out = (JsonObject) con.call(DevConsole.MediaType.JSON);
- Assertions.assertEquals(0, out.getInteger("size"));
+ Assertions.assertEquals(0, out.getInteger("customSize"));
+ Assertions.assertEquals(SimpleUtils.getFunctions().size(),
out.getInteger("coreSize"));
Assertions.assertNotNull(out);
}
diff --git
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/ChainExpression.java
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/ChainExpression.java
index 01e752c063a5..560da1fb26c5 100644
---
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/ChainExpression.java
+++
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/ChainExpression.java
@@ -132,7 +132,7 @@ public class ChainExpression extends BaseSimpleNode {
// this may be a function
String key = text.substring(1);
key = StringHelper.before(key, "(", key);
- if
(PluginHelper.getSimpleFunctionRegistry(camelContext).getFunctionNames().contains(key))
{
+ if
(PluginHelper.getSimpleFunctionRegistry(camelContext).getCustomFunctionNames().contains(key))
{
String changed = text.replace("$" + key + "()",
"function(" + key + ")");
if (changed.equals(text)) {
changed = text.replace("$" + key + "(", "function(" +
key);
diff --git
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index c5b624b4ba88..d265cc90a7cc 100644
---
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -373,7 +373,7 @@ public class SimpleFunctionExpression extends
LiteralExpression {
// it may be a custom function
String name = StringHelper.before(function, "(", function);
- if
(PluginHelper.getSimpleFunctionRegistry(camelContext).getFunctionNames().contains(name))
{
+ if
(PluginHelper.getSimpleFunctionRegistry(camelContext).getCustomFunctionNames().contains(name))
{
String after = StringHelper.after(function, "(");
if (after == null || after.equals(")")) {
function = "function(" + name + ")";
diff --git
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleCustomFunctionTest.java
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleCustomFunctionTest.java
index 917ab106a9e8..25dfd64850e7 100644
---
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleCustomFunctionTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleCustomFunctionTest.java
@@ -31,7 +31,7 @@ public class SimpleCustomFunctionTest extends
ContextTestSupport {
@Test
public void testCustomFunctionWithBody() throws Exception {
SimpleFunctionRegistry reg =
PluginHelper.getSimpleFunctionRegistry(context);
- Assertions.assertEquals(1, reg.size());
+ Assertions.assertEquals(1, reg.customSize());
getMockEndpoint("mock:result").expectedBodiesReceived("Hello I was
here World");
template.sendBody("direct:start", "World");
@@ -41,7 +41,7 @@ public class SimpleCustomFunctionTest extends
ContextTestSupport {
@Test
public void testCustomFunctionWithExp() throws Exception {
SimpleFunctionRegistry reg =
PluginHelper.getSimpleFunctionRegistry(context);
- Assertions.assertEquals(1, reg.size());
+ Assertions.assertEquals(1, reg.customSize());
getMockEndpoint("mock:result").expectedBodiesReceived("Bye I was here
Moon");
template.sendBody("direct:start2", "World");
diff --git
a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedSimpleFunctionRegistryMBean.java
b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedSimpleFunctionRegistryMBean.java
index f1c687932fcd..67024f9c6b80 100644
---
a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedSimpleFunctionRegistryMBean.java
+++
b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedSimpleFunctionRegistryMBean.java
@@ -19,17 +19,19 @@ package org.apache.camel.api.management.mbean;
import java.util.Set;
import org.apache.camel.api.management.ManagedAttribute;
-import org.apache.camel.api.management.ManagedOperation;
public interface ManagedSimpleFunctionRegistryMBean extends
ManagedServiceMBean {
+ @ManagedAttribute(description = "Number of core functions")
+ int getCoreSize();
+
@ManagedAttribute(description = "Number of custom functions")
- int getSize();
+ int getCustomSize();
- @ManagedAttribute(description = "The names of the custom functions")
- Set<String> getFunctionNames();
+ @ManagedAttribute(description = "The names of the core functions")
+ Set<String> getCoreFunctionNames();
- @ManagedOperation(description = "Is there a custom function with the given
name")
- boolean hasFunction(String name);
+ @ManagedAttribute(description = "The names of the custom functions")
+ Set<String> getCustomFunctionNames();
}
diff --git
a/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedSimpleFunctionRepository.java
b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedSimpleFunctionRepository.java
index d3137d506e02..9b0460050ae3 100644
---
a/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedSimpleFunctionRepository.java
+++
b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedSimpleFunctionRepository.java
@@ -34,17 +34,23 @@ public class ManagedSimpleFunctionRepository extends
ManagedService implements M
}
@Override
- public int getSize() {
- return registry.size();
+ public int getCoreSize() {
+ return registry.coreSize();
}
@Override
- public Set<String> getFunctionNames() {
- return registry.getFunctionNames();
+ public int getCustomSize() {
+ return registry.customSize();
}
@Override
- public boolean hasFunction(String name) {
- return registry.getFunction(name) != null;
+ public Set<String> getCoreFunctionNames() {
+ return registry.getCoreFunctionNames();
}
+
+ @Override
+ public Set<String> getCustomFunctionNames() {
+ return registry.getCustomFunctionNames();
+ }
+
}
diff --git
a/core/camel-management/src/test/java/org/apache/camel/management/ManagedSimpleFunctionRegistryTest.java
b/core/camel-management/src/test/java/org/apache/camel/management/ManagedSimpleFunctionRegistryTest.java
index 93ac38180fe4..561576983c08 100644
---
a/core/camel-management/src/test/java/org/apache/camel/management/ManagedSimpleFunctionRegistryTest.java
+++
b/core/camel-management/src/test/java/org/apache/camel/management/ManagedSimpleFunctionRegistryTest.java
@@ -45,20 +45,12 @@ public class ManagedSimpleFunctionRegistryTest extends
ManagementTestSupport {
MBeanServer mbeanServer = getMBeanServer();
ObjectName name = getCamelObjectName(TYPE_SERVICE,
"DefaultSimpleFunctionRegistry");
- Integer size = (Integer) mbeanServer.getAttribute(name, "Size");
+ Integer size = (Integer) mbeanServer.getAttribute(name, "CustomSize");
assertEquals(1, size);
- Set<String> names = (Set) mbeanServer.getAttribute(name,
"FunctionNames");
+ Set<String> names = (Set) mbeanServer.getAttribute(name,
"CustomFunctionNames");
assertEquals(1, names.size());
assertEquals("hi", names.iterator().next());
-
- Boolean bool
- = (Boolean) mbeanServer.invoke(name, "hasFunction", new
Object[] { "hi" }, new String[] { "java.lang.String" });
- assertEquals(Boolean.TRUE, bool);
-
- bool = (Boolean) mbeanServer.invoke(name, "hasFunction", new Object[]
{ "goodbye" },
- new String[] { "java.lang.String" });
- assertEquals(Boolean.FALSE, bool);
}
@Override
diff --git
a/core/camel-util/src/main/java/org/apache/camel/util/SimpleUtils.java
b/core/camel-util/src/main/java/org/apache/camel/util/SimpleUtils.java
new file mode 100644
index 000000000000..4d87e2a0173a
--- /dev/null
+++ b/core/camel-util/src/main/java/org/apache/camel/util/SimpleUtils.java
@@ -0,0 +1,151 @@
+/*
+ * 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.util;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+public class SimpleUtils {
+
+ private static final Set<String> SIMPLE_FUNCTIONS =
Collections.unmodifiableSet(new HashSet<>(
+ Arrays.asList(
+ // Generated by camel build tools - do NOT edit this list!
+ // SIMPLE-FUNCTIONS: START
+ "abs",
+ "assert",
+ "attachment",
+ "attachments",
+ "attachmentscontent",
+ "attachmentscontentas",
+ "attachmentscontentastext",
+ "attachmentsheader",
+ "attachmentsheaderas",
+ "attachmentskeys",
+ "attachmentssize",
+ "average",
+ "base64decode",
+ "base64encode",
+ "bean",
+ "body",
+ "bodyas",
+ "bodyoneline",
+ "bodytype",
+ "camelcontext",
+ "camelid",
+ "capitalize",
+ "ceil",
+ "clearattachments",
+ "collate",
+ "concat",
+ "convertto",
+ "date",
+ "date-with-timezone",
+ "distinct",
+ "empty",
+ "env",
+ "exception",
+ "exchange",
+ "exchangeid",
+ "exchangeproperty",
+ "floor",
+ "foreach",
+ "fromrouteid",
+ "function",
+ "hash",
+ "header",
+ "headeras",
+ "headers",
+ "hostname",
+ "id",
+ "iif",
+ "isalpha",
+ "isalphanumeric",
+ "isempty",
+ "isnumeric",
+ "join",
+ "jq",
+ "jsonpath",
+ "kindoftype",
+ "length",
+ "list",
+ "lowercase",
+ "mandatorybodyas",
+ "map",
+ "max",
+ "messageas",
+ "messagehistory",
+ "messagetimestamp",
+ "min",
+ "newempty",
+ "normalizewhitespace",
+ "not",
+ "null",
+ "originalbody",
+ "pad",
+ "pretty",
+ "prettybody",
+ "properties",
+ "propertiesexist",
+ "quote",
+ "random",
+ "ref",
+ "replace",
+ "reverse",
+ "routegroup",
+ "routeid",
+ "safequote",
+ "setheader",
+ "setvariable",
+ "shuffle",
+ "size",
+ "skip",
+ "split",
+ "stepid",
+ "substring",
+ "substringafter",
+ "substringbefore",
+ "substringbetween",
+ "sum",
+ "sys",
+ "threadid",
+ "threadname",
+ "throwexception",
+ "trim",
+ "type",
+ "unquote",
+ "uppercase",
+ "uuid",
+ "variable",
+ "variableas",
+ "variables",
+ "xpath"
+ // SIMPLE-FUNCTIONS: END
+ )));
+
+ private SimpleUtils() {
+ }
+
+ /**
+ * All the built-in simple language function names (in lower case)
+ */
+ public static Set<String> getFunctions() {
+ return SIMPLE_FUNCTIONS;
+ }
+
+}
diff --git
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateSimpleHelper.java
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateSimpleHelper.java
new file mode 100644
index 000000000000..5661602c41a4
--- /dev/null
+++
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/UpdateSimpleHelper.java
@@ -0,0 +1,142 @@
+/*
+ * 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.maven.packaging;
+
+import java.io.File;
+import java.util.Locale;
+import java.util.Set;
+import java.util.StringJoiner;
+import java.util.TreeSet;
+
+import javax.inject.Inject;
+
+import org.apache.camel.tooling.model.JsonMapper;
+import org.apache.camel.tooling.model.LanguageModel;
+import org.apache.camel.tooling.util.PackageHelper;
+import org.apache.camel.tooling.util.Strings;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.MavenProjectHelper;
+import org.codehaus.plexus.build.BuildContext;
+
+import static org.apache.camel.tooling.util.PackageHelper.findCamelDirectory;
+
+/**
+ * Updates the SimpleHelper.java with the built-in simple functions
+ */
+@Mojo(name = "update-simple-helper", threadSafe = true)
+public class UpdateSimpleHelper extends AbstractGeneratorMojo {
+
+ private static final String FUNCTIONS_START_TOKEN = "// SIMPLE-FUNCTIONS:
START";
+ private static final String FUNCTIONS_END_TOKEN = "// SIMPLE-FUNCTIONS:
END";
+
+ @Parameter(defaultValue =
"${project.basedir}/src/generated/resources/org/apache/camel/catalog/languages/simple.json")
+ protected File simpleFile;
+
+ @Parameter(defaultValue = "${project.basedir}/")
+ protected File baseDir;
+
+ @Inject
+ public UpdateSimpleHelper(MavenProjectHelper projectHelper, BuildContext
buildContext) {
+ super(projectHelper, buildContext);
+ }
+
+ /**
+ * Execute goal.
+ *
+ * @throws MojoExecutionException execution of the main class or one of
the threads it generated failed.
+ */
+ @Override
+ public void execute() throws MojoExecutionException {
+ File camelDir = findCamelDirectory(baseDir, "core/camel-util");
+ if (camelDir == null) {
+ getLog().debug("No core/camel-util folder found, skipping
execution");
+ return;
+ }
+ Set<String> functions = new TreeSet<>();
+
+ try {
+ String json = PackageHelper.loadText(simpleFile);
+ LanguageModel model = JsonMapper.generateLanguageModel(json);
+ model.getFunctions().forEach(f -> {
+ String name = f.getName();
+ if (name.contains("(")) {
+ name = name.substring(0, name.indexOf("("));
+ }
+ if (name.contains(":")) {
+ name = name.substring(0, name.indexOf(":"));
+ }
+ if (name.contains(".")) {
+ name = name.substring(0, name.indexOf("."));
+ }
+ name = name.toLowerCase(Locale.ENGLISH);
+ functions.add(name);
+ });
+ } catch (Exception e) {
+ throw new MojoExecutionException("Error loading json: " +
simpleFile, e);
+ }
+
+ getLog().info("There are " + functions.size() + " simple language
functions");
+ try {
+ boolean updated = updateSimpleHelperFunctions(camelDir, functions);
+ if (updated) {
+ getLog().info(
+ "Updated
camel-util/src/main/java/org/apache/camel/util/SimpleUtils.java file");
+ } else {
+ getLog().debug(
+ "No changes to
camel-util/src/main/java/org/apache/camel/util/SimpleUtils.java file");
+ }
+ } catch (Exception e) {
+ throw new MojoExecutionException("Error updating
SimpleUtils.java", e);
+ }
+ }
+
+ private boolean updateSimpleHelperFunctions(File camelDir, Set<String>
secrets) throws Exception {
+ // load source code and update
+ File java = new File(camelDir,
"src/main/java/org/apache/camel/util/SimpleUtils.java");
+ String text = PackageHelper.loadText(java);
+ String spaces20 = " ";
+ String spaces12 = " ";
+
+ StringJoiner sb = new StringJoiner(",\n");
+ for (String name : secrets) {
+ sb.add(spaces20 + "\"" + name + "\"");
+ }
+ String changed = sb.toString();
+
+ String existing = Strings.between(text, FUNCTIONS_START_TOKEN,
FUNCTIONS_END_TOKEN);
+ if (existing != null) {
+ // remove leading line breaks etc
+ existing = existing.trim();
+ changed = changed.trim();
+ if (existing.equals(changed)) {
+ return false;
+ } else {
+ String before = Strings.before(text, FUNCTIONS_START_TOKEN);
+ String after = Strings.after(text, FUNCTIONS_END_TOKEN);
+ text = before + FUNCTIONS_START_TOKEN + "\n" + spaces20 +
changed + "\n" + spaces12 + FUNCTIONS_END_TOKEN
+ + after;
+ PackageHelper.writeText(java, text);
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+}