diff options
Diffstat (limited to 'module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java')
-rw-r--r-- | module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java index b8ee03a..d3d309a 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java @@ -14,6 +14,8 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; import org.drools.core.common.DefaultFactHandle; import org.drools.core.factmodel.GeneratedFact; import org.kie.api.KieBase; +import org.kie.api.definition.type.FactType; +import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.rule.FactHandle; import java.io.File; @@ -29,14 +31,14 @@ public class DbIo { private static final List<String> prioritizedKeys = List.of("key", "name", "fqdn"); - public DbIo(KieBase kieBase) { + public DbIo(KieContainer container, KieBase kieBase) { var factory = new YAMLFactory(); factory.enable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID); factory.enable(YAMLGenerator.Feature.USE_NATIVE_OBJECT_ID); mapper = new ObjectMapper(factory); mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); var typeFactory = TypeFactory.defaultInstance() - .withClassLoader(new AcmeClassLoader(kieBase)); + .withClassLoader(new DbClassLoader(container, kieBase)); mapper.setTypeFactory(typeFactory); mapper.findAndRegisterModules(); @@ -50,7 +52,7 @@ public class DbIo { for (String name : prioritizedKeys) { try { final String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); - var f = klass.getMethod(getter); + klass.getMethod(getter); return new ObjectIdInfo(PropertyName.construct(name), null, ObjectIdGenerators.PropertyGenerator.class, null); } catch (NoSuchMethodException ignore) { } @@ -64,8 +66,8 @@ public class DbIo { }); } - public List<Object> load(String file) throws IOException { - var parser = mapper.getFactory().createParser(new File(file)); + public List<Object> load(File file) throws IOException { + var parser = mapper.getFactory().createParser(file); var objects = mapper.readValues(parser, DbObject.class).readAll(new ArrayList<>()); @@ -79,6 +81,8 @@ public class DbIo { } items.add(x); } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + System.out.println("e.getClass() = " + e.getClass().getName()); + System.out.println("e.getMessage() = " + e.getMessage()); // ignore } } @@ -86,8 +90,8 @@ public class DbIo { return items; } - public void dump(String s, Collection<FactHandle> factHandles) throws IOException { - dump(s, factHandles, (o) -> true); + public void dump(File file, Collection<FactHandle> factHandles) throws IOException { + dump(file, factHandles, (o) -> true); } // This should just sort by all getters instead. @@ -221,10 +225,8 @@ public class DbIo { static record DbObject2(String type, Object data) { } - public void dump(String s, Collection<FactHandle> factHandles, Function<Object, Boolean> filter) throws IOException { - var yamlFile = new File("out", s + ".yaml"); - - FileUtil.createMissingParentDirectories(yamlFile); + public void dump(File file, Collection<FactHandle> factHandles, Function<Object, Boolean> filter) throws IOException { + FileUtil.createMissingParentDirectories(file); var facts = new TreeMap<Class<?>, FactCollection<Object>>(Comparator.comparing(Class::getName)); for (var handle : factHandles) { @@ -260,16 +262,18 @@ public class DbIo { objects.sort(new DbObjectComparator()); var factory = mapper.getFactory(); - try (var writer = new FileWriter(yamlFile); + try (var writer = new FileWriter(file); var g = factory.createGenerator(writer)) { g.writeObject(objects); } } - private static class AcmeClassLoader extends ClassLoader { + private static class DbClassLoader extends ClassLoader { private final KieBase kieBase; + private final KieContainer container; - public AcmeClassLoader(KieBase kieBase) { + public DbClassLoader(KieContainer container, KieBase kieBase) { + this.container = container; this.kieBase = kieBase; } @@ -287,7 +291,18 @@ public class DbIo { pkg = name.substring(0, i); klass = name.substring(i + 1); } - var clazz = kieBase.getFactType(pkg, klass); + + try { + return container.getClassLoader().loadClass(name); + } catch (ClassNotFoundException ignore) { + } + + FactType clazz = null; + try { + clazz = kieBase.getFactType(pkg, klass); + } catch (UnsupportedOperationException ignore) { + System.out.println("AcmeClassLoader.loadClass: " + name); + } if (clazz == null) { throw e; } |