Skip to content

Commit

Permalink
[KOGITO-9354] Adding search path configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jun 22, 2023
1 parent b10c23a commit a00ca5d
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,6 +38,25 @@ public static <T> T convert(Object value, Class<T> clazz) {
return convert(value, clazz, Object::toString);
}

/**
* Converts a string into a list of objects
*
* @param <T>
* @param value object to be converted into list
* @param clazz the item target class
* @return a collection
*/
public static <T> Collection<T> convertToCollection(Object value, Class<T> clazz) {
return convertToCollection(value, clazz, ",");
}

public static <T> Collection<T> convertToCollection(Object value, Class<T> clazz, String separator) {
if (value == null) {
return Collections.emptyList();
}
return Arrays.stream(value.toString().split(separator)).map(v -> ConversionUtils.convert(v, clazz)).collect(Collectors.toList());
}

/**
* Converts an object to an instance of the provided class
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package org.kie.kogito.internal.utils;

import java.util.Arrays;
import java.util.Objects;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.kie.kogito.internal.utils.ConversionUtils.concatPaths;
import static org.kie.kogito.internal.utils.ConversionUtils.convert;
import static org.kie.kogito.internal.utils.ConversionUtils.convertToCollection;
import static org.kie.kogito.internal.utils.ConversionUtils.toCamelCase;

class ConversionUtilsTest {
Expand Down Expand Up @@ -127,6 +129,10 @@ public void testConcatPaths() {
assertThat(concatPaths("http:localhost:8080/pepe", "pepa/pepi")).isEqualTo(expected);
assertThat(concatPaths("http:localhost:8080/pepe/", "pepa/pepi")).isEqualTo(expected);
assertThat(concatPaths("http:localhost:8080/pepe", "/pepa/pepi")).isEqualTo(expected);
}

@Test
public void testConvertToCollection() {
assertThat(convertToCollection("1,2,3", Integer.class)).isEqualTo(Arrays.asList(1, 2, 3));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ void testPythonService() {
assertThat(application.execute(workflow, Map.of("x", 5)).getWorkflowdata().get("result").asInt()).isEqualTo(120);
}
}

@Test
void testNotStandardPythonService() {
try (StaticWorkflowApplication application = StaticWorkflowApplication.create(Map.of("sonata.python.searchPath", "./src/test/resources/"))) {
Workflow workflow = workflow("Factorial").start(operation().action(call(python("factorial", "custom", "factorial"), new TextNode(".x")).outputFilter(".result")))
.end().build();
assertThat(application.execute(workflow, Map.of("x", 5)).getWorkflowdata().get("result").asInt()).isEqualTo(120);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def factorial(x):
result = 1
if x > 1:
for i in range(2,x+1):
result = result*i
return result
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package org.kie.kogito.serverless.workflow.python;

import java.util.Collection;

import org.kie.kogito.serverless.workflow.utils.ConfigResolverHolder;

import jep.Interpreter;
import jep.SharedInterpreter;

Expand All @@ -23,13 +27,21 @@ public class PythonWorkItemHandlerUtils {
private PythonWorkItemHandlerUtils() {
}

private static ThreadLocal<Interpreter> interpreter = new ThreadLocal<>();
private static final String PYTHON_SYS_PATH = "sys.path.append('%s')\n";

private static final ThreadLocal<Interpreter> interpreter = new ThreadLocal<>();

protected static Interpreter interpreter() {
Interpreter py = interpreter.get();
if (py == null) {
py = new SharedInterpreter();
interpreter.set(py);
Collection<String> searchPath = ConfigResolverHolder.getConfigResolver().getIndexedConfigProperty("sonata.python.searchPath", String.class);
if (!searchPath.isEmpty()) {
StringBuilder sb = new StringBuilder("import sys\n");
searchPath.forEach(path -> sb.append(String.format(PYTHON_SYS_PATH, path)));
py.exec(sb.toString());
}
}
return py;
}
Expand All @@ -45,5 +57,4 @@ protected static void closeInterpreter() {
protected static Object getValue(String key) {
return interpreter().getValue(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.kie.kogito.serverless.workflow.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -25,6 +26,8 @@ public interface ConfigResolver {

Iterable<String> getPropertyNames();

<T> Collection<T> getIndexedConfigProperty(String name, Class<T> clazz);

default Map<String, Object> asMap() {
Map<String, Object> map = new HashMap<>();
for (String name : getPropertyNames()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package org.kie.kogito.serverless.workflow.utils;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import org.kie.kogito.internal.utils.ConversionUtils;

public class MapConfigResolver implements ConfigResolver {

private final Map<String, Object> map;
Expand All @@ -45,4 +48,9 @@ public Iterable<String> getPropertyNames() {
public Map<String, Object> asMap() {
return map;
}

@Override
public <T> Collection<T> getIndexedConfigProperty(String name, Class<T> clazz) {
return ConversionUtils.convertToCollection(map.get(name), clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package org.kie.kogito.serverless.workflow.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

import org.kie.kogito.internal.utils.ConversionUtils;

public class MultiSourceConfigResolver implements ConfigResolver {

private final Iterable<ConfigResolver> configResolvers;
Expand Down Expand Up @@ -72,4 +75,17 @@ public Map<String, Object> asMap() {
public Iterable<String> getPropertyNames() {
return asMap().keySet();
}

@Override
public <T> Collection<T> getIndexedConfigProperty(String name, Class<T> clazz) {
Map<String, Object> collect = map.get();
if (collect != null) {
return ConversionUtils.convertToCollection(collect.get(name), clazz);
}
Collection<T> result = new ArrayList<>();
for (ConfigResolver resolver : configResolvers) {
result.addAll(resolver.getIndexedConfigProperty(name, clazz));
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package org.kie.kogito.serverless.workflow.utils;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import org.kie.kogito.internal.utils.ConversionUtils;

public class SystemPropertiesConfigResolver implements ConfigResolver {

@Override
Expand All @@ -41,4 +44,8 @@ public Map asMap() {
return System.getProperties();
}

@Override
public <T> Collection<T> getIndexedConfigProperty(String name, Class<T> clazz) {
return ConversionUtils.convertToCollection(System.getProperty(name), clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.kie.kogito.secret;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -47,4 +49,14 @@ public <T> Optional<T> getConfigProperty(String key, Class<T> clazz) {
public Iterable<String> getPropertyNames() {
return config.getPropertyNames();
}

@Override
public <T> Collection<T> getIndexedConfigProperty(String key, Class<T> clazz) {
try {
return config.getOptionalValues(key, clazz).orElse(Collections.emptyList());
} catch (SecurityException ex) {
// see https://smallrye.io/docs/smallrye-config/config/secret-keys.html
return SecretKeys.doUnlocked(() -> config.getOptionalValues(key, clazz)).orElse(Collections.emptyList());
}
}
}

0 comments on commit a00ca5d

Please sign in to comment.