Search in sources :

Example 1 with KieServicesImpl

use of org.drools.compiler.kie.builder.impl.KieServicesImpl in project drools by kiegroup.

the class KieProjectCDITest method createMultpleJarAndFileResources.

@Test
public void createMultpleJarAndFileResources() throws IOException, ClassNotFoundException, InterruptedException {
    createKieModule("jar1", true);
    createKieModule("jar2", true);
    createKieModule("jar3", true);
    createKieModule("fol4", false);
    ClassLoader origCl = Thread.currentThread().getContextClassLoader();
    try {
        java.io.File file1 = fileManager.newFile("jar1-1.0-SNAPSHOT.jar");
        java.io.File file2 = fileManager.newFile("jar2-1.0-SNAPSHOT.jar");
        java.io.File file3 = fileManager.newFile("jar3-1.0-SNAPSHOT.jar");
        java.io.File fol4 = fileManager.newFile("fol4-1.0-SNAPSHOT");
        URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { file1.toURI().toURL(), file2.toURI().toURL(), file3.toURI().toURL(), fol4.toURI().toURL() });
        Thread.currentThread().setContextClassLoader(urlClassLoader);
        Enumeration<URL> e = urlClassLoader.getResources(KieModuleModelImpl.KMODULE_JAR_PATH);
        while (e.hasMoreElements()) {
            URL url = e.nextElement();
            System.out.println(url);
        }
        Class cls = Thread.currentThread().getContextClassLoader().loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar1");
        assertNotNull(cls);
        cls = Thread.currentThread().getContextClassLoader().loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar2");
        assertNotNull(cls);
        cls = Thread.currentThread().getContextClassLoader().loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar3");
        assertNotNull(cls);
        Weld weld = CDITestRunner.createWeld(KProjectTestClass.class.getName(), KPTestLiteral.class.getName(), "org.drools.compiler.cdi.test.KProjectTestClassjar1", "org.drools.compiler.cdi.test.KProjectTestClassjar2", "org.drools.compiler.cdi.test.KProjectTestClassjar3", "org.drools.compiler.cdi.test.KProjectTestClassfol4");
        ((KieServicesImpl) KieServices.Factory.get()).nullKieClasspathContainer();
        WeldContainer container = weld.initialize();
        Set<Bean<?>> beans = container.getBeanManager().getBeans(KProjectTestClass.class, new KPTestLiteral("jar1"));
        Bean bean = (Bean) beans.toArray()[0];
        KProjectTestClass o1 = (KProjectTestClass) bean.create(container.getBeanManager().createCreationalContext(null));
        assertNotNull(o1);
        testEntry(o1, "jar1");
        beans = container.getBeanManager().getBeans(KProjectTestClass.class, new KPTestLiteral("jar2"));
        bean = (Bean) beans.toArray()[0];
        KProjectTestClass o2 = (KProjectTestClass) bean.create(container.getBeanManager().createCreationalContext(null));
        assertNotNull(o2);
        testEntry(o2, "jar2");
        beans = container.getBeanManager().getBeans(KProjectTestClass.class, new KPTestLiteral("jar3"));
        bean = (Bean) beans.toArray()[0];
        KProjectTestClass o3 = (KProjectTestClass) bean.create(container.getBeanManager().createCreationalContext(null));
        assertNotNull(o3);
        testEntry(o3, "jar3");
        beans = container.getBeanManager().getBeans(KProjectTestClass.class, new KPTestLiteral("fol4"));
        bean = (Bean) beans.toArray()[0];
        KProjectTestClass o4 = (KProjectTestClass) bean.create(container.getBeanManager().createCreationalContext(null));
        assertNotNull(o4);
        testEntry(o4, "fol4");
        weld.shutdown();
    } finally {
        // FIXME Java 7+
        // on Windows, the URLClassLoader will not release all resources,
        // so the attempt to delete the temporary files will fail.
        // an explicit dispose call is needed, but it has not been introduced until Java7+
        // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4950148
        /*
            ((URLClassLoader) Thread.currentThread().getContextClassLoader()).close();
            */
        Thread.currentThread().setContextClassLoader(origCl);
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) URL(java.net.URL) Weld(org.jboss.weld.environment.se.Weld) Bean(javax.enterprise.inject.spi.Bean) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) Test(org.junit.Test)

Example 2 with KieServicesImpl

use of org.drools.compiler.kie.builder.impl.KieServicesImpl in project drools by kiegroup.

the class CDITestRunner method setUp.

public static void setUp(File... files) {
    fileManager = new FileManager();
    fileManager.setUp();
    origCl = Thread.currentThread().getContextClassLoader();
    ((KieServicesImpl) KieServices.Factory.get()).nullKieClasspathContainer();
    URLClassLoader urlClassLoader;
    try {
        List<URL> urls = new ArrayList<>();
        if (files != null && files.length > 0) {
            for (File file : files) {
                urls.add(file.toURI().toURL());
            }
        }
        urlClassLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread.currentThread().getContextClassLoader());
        Thread.currentThread().setContextClassLoader(urlClassLoader);
    } catch (MalformedURLException e) {
        fail(e.getMessage());
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) FileManager(org.drools.core.util.FileManager) URL(java.net.URL)

Example 3 with KieServicesImpl

use of org.drools.compiler.kie.builder.impl.KieServicesImpl in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenNoDependencies.

@Test
public void testKieModuleFromMavenNoDependencies() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId releaseId = ks.newReleaseId("org.kie", "maven-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJar(ks, releaseId, true, "rule1", "rule2");
    String pomText = getPom(releaseId);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieBaseModel kbaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
    assertNotNull("Default kbase was not found", kbaseModel);
    String kbaseName = kbaseModel.getName();
    assertEquals("KBase1", kbaseName);
    // Check classloader
    assertUrlEnumerationContainsMatch(".*org/kie/maven\\-test/1.0\\-SNAPSHOT.*", kieContainer.getClassLoader().getResources(""));
    assertUrlEnumerationContainsMatch(".*org/kie/maven\\-test/1.0\\-SNAPSHOT.*", kieContainer.getClassLoader().getResources("KBase1/org/test"));
    assertUrlEnumerationContainsMatch(".*org/kie/maven\\-test/1.0\\-SNAPSHOT.*", kieContainer.getClassLoader().getResources("KBase1/org/test/"));
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 4 with KieServicesImpl

use of org.drools.compiler.kie.builder.impl.KieServicesImpl in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenWithDependenciesProperties.

@Test
public void testKieModuleFromMavenWithDependenciesProperties() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "${version.org.drools}");
    ReleaseId releaseId = ks.newReleaseId("org.kie.test", "maven-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJarWithProperties(ks, releaseId, true, "5.5.0.Final", new ReleaseId[] { dependency }, "rule1", "rule2");
    String pomText = generatePomXmlWithProperties(releaseId, "5.5.0.Final", dependency);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieBaseModel kbaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
    assertNotNull("Default kbase was not found", kbaseModel);
    String kbaseName = kbaseModel.getName();
    assertEquals("KBase1", kbaseName);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 5 with KieServicesImpl

use of org.drools.compiler.kie.builder.impl.KieServicesImpl in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenWithDependencies.

@Test
public void testKieModuleFromMavenWithDependencies() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "5.5.0.Final");
    ReleaseId releaseId = ks.newReleaseId("org.kie", "maven-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJar(ks, releaseId, true, "rule1", "rule2");
    String pomText = getPom(releaseId, dependency);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieBaseModel kbaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
    assertNotNull("Default kbase was not found", kbaseModel);
    String kbaseName = kbaseModel.getName();
    assertEquals("KBase1", kbaseName);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieServicesImpl (org.drools.compiler.kie.builder.impl.KieServicesImpl)7 File (java.io.File)6 Test (org.junit.Test)6 FileOutputStream (java.io.FileOutputStream)5 IOException (java.io.IOException)5 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)5 KieRepositoryImpl (org.drools.compiler.kie.builder.impl.KieRepositoryImpl)5 KieServices (org.kie.api.KieServices)5 ReleaseId (org.kie.api.builder.ReleaseId)5 KieContainer (org.kie.api.runtime.KieContainer)5 KieBaseModel (org.kie.api.builder.model.KieBaseModel)3 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Bean (javax.enterprise.inject.spi.Bean)1 FileManager (org.drools.core.util.FileManager)1 Weld (org.jboss.weld.environment.se.Weld)1 WeldContainer (org.jboss.weld.environment.se.WeldContainer)1