Search in sources :

Example 16 with ProjectClassLoader

use of org.drools.wiring.api.classloader.ProjectClassLoader in project drools by kiegroup.

the class KieContainerImpl method reinitModifiedClasses.

private Collection<Class<?>> reinitModifiedClasses(InternalKieModule newKM, List<String> modifiedClasses, ClassLoader classLoader, boolean modifyingUsedClass) {
    if (modifiedClasses.isEmpty() || !(classLoader instanceof ProjectClassLoader)) {
        return Collections.emptyList();
    }
    Set<String> reloadedClasses = new HashSet<>(modifiedClasses);
    ProjectClassLoader projectClassLoader = (ProjectClassLoader) classLoader;
    projectClassLoader.clearStore();
    if (modifyingUsedClass) {
        reloadedClasses.addAll(projectClassLoader.reinitTypes().stream().map(ClassUtils::convertClassToResourcePath).collect(toList()));
    }
    List<Class<?>> classes = new ArrayList<Class<?>>();
    for (String resourceName : reloadedClasses) {
        String className = convertResourceToClassName(resourceName);
        byte[] bytes = newKM.getBytes(resourceName);
        if (bytes != null) {
            Class<?> clazz = projectClassLoader.defineClass(className, resourceName, bytes);
            classes.add(clazz);
        }
    }
    return classes;
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ArrayList(java.util.ArrayList) ClassUtils(org.drools.core.util.ClassUtils) HashSet(java.util.HashSet)

Example 17 with ProjectClassLoader

use of org.drools.wiring.api.classloader.ProjectClassLoader in project drools by kiegroup.

the class CanonicalKieModule method createKieBase.

@Override
public InternalKnowledgeBase createKieBase(KieBaseModelImpl kBaseModel, KieProject kieProject, BuildContext buildContext, KieBaseConfiguration conf) {
    this.moduleClassLoader = ((ProjectClassLoader) kieProject.getClassLoader());
    if (generatedClassNames == null) {
        generatedClassNames = findGeneratedClassNamesWithDependencies();
    }
    moduleClassLoader.setGeneratedClassNames(generatedClassNames);
    KieBaseConfiguration kBaseConf = getKieBaseConfiguration(kBaseModel, moduleClassLoader, conf);
    CanonicalKiePackages kpkgs = pkgsInKbase.computeIfAbsent(kBaseModel.getName(), k -> createKiePackages(kieProject, kBaseModel, buildContext, kBaseConf));
    checkStreamMode(kBaseModel, conf, kpkgs.getKiePackages());
    InternalKnowledgeBase kieBase = new KieBaseBuilder(kBaseModel, kBaseConf).createKieBase(kpkgs);
    registerNonNativeResources(kBaseModel, kieProject, kieBase, buildContext);
    return kieBase;
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 18 with ProjectClassLoader

use of org.drools.wiring.api.classloader.ProjectClassLoader in project drools by kiegroup.

the class ClassLoaderTest method testParallelClassLoading.

@Test(timeout = 20000)
public void testParallelClassLoading() {
    final Integer THREAD_COUNT = 100;
    final ClassLoader projectClassLoader = ProjectClassLoader.createProjectClassLoader();
    final ClassLoader internalTypesClassLoader = (ClassLoader) ((ProjectClassLoader) projectClassLoader).makeClassLoader();
    ((ProjectClassLoader) projectClassLoader).setInternalClassLoader((ProjectClassLoader.InternalTypesClassLoader) internalTypesClassLoader);
    final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);
    try {
        final List<Future> futures = new ArrayList<>();
        for (int i = 0; i < THREAD_COUNT; i++) {
            if (i % 2 == 0) {
                futures.add(executorService.submit(() -> {
                    try {
                        Class.forName("nonexistant", true, projectClassLoader);
                    } catch (ClassNotFoundException e) {
                    // 
                    }
                }));
            } else {
                futures.add(executorService.submit(() -> {
                    try {
                        Class.forName("nonexistant", true, internalTypesClassLoader);
                    } catch (ClassNotFoundException e) {
                    // 
                    }
                }));
            }
        }
        for (int i = 1; i <= THREAD_COUNT; i++) {
            try {
                futures.get(i - 1).get();
            } catch (final InterruptedException | ExecutionException e) {
            // Nothing
            }
        }
    } finally {
        executorService.shutdownNow();
    }
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 19 with ProjectClassLoader

use of org.drools.wiring.api.classloader.ProjectClassLoader in project drools by kiegroup.

the class HasKnowledgeBuilderImplTest method getClassLoader.

@Test
public void getClassLoader() {
    ClassLoader retrieved = hasKnowledgeBuilder.getClassLoader();
    assertNotNull(retrieved);
    assertEquals(knowledgeBuilder.getRootClassLoader(), retrieved);
    assertTrue(retrieved instanceof ProjectClassLoader);
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) Test(org.junit.Test)

Example 20 with ProjectClassLoader

use of org.drools.wiring.api.classloader.ProjectClassLoader in project drools by kiegroup.

the class StandaloneTest method init.

@Before
public void init() {
    ProjectClassLoader loader = ProjectClassLoader.createProjectClassLoader();
    factory = createStandaloneTraitFactory();
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) Before(org.junit.Before)

Aggregations

ProjectClassLoader (org.drools.wiring.api.classloader.ProjectClassLoader)21 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)4 JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)3 Dialect (org.drools.compiler.compiler.Dialect)2 KieContainerImpl (org.drools.compiler.kie.builder.impl.KieContainerImpl)2 KieProject (org.drools.compiler.kie.builder.impl.KieProject)2 KieServices (org.kie.api.KieServices)2 KieFileSystem (org.kie.api.builder.KieFileSystem)2 ByteString (com.google.protobuf.ByteString)1 JCodeModel (com.sun.codemodel.JCodeModel)1 BadCommandLineException (com.sun.tools.xjc.BadCommandLineException)1 ErrorReceiver (com.sun.tools.xjc.ErrorReceiver)1 Model (com.sun.tools.xjc.model.Model)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1