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);
}
}
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());
}
}
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/"));
}
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);
}
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);
}
Aggregations