Search in sources :

Example 1 with ClassLoaderProviderImpl

use of org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl in project kie-wb-common by kiegroup.

the class ClassLoaderProviderTest method getClassloaderFromAllDependenciesTestComplex.

@Test
public void getClassloaderFromAllDependenciesTestComplex() {
    AFClassLoaderProvider kieClazzLoaderProvider = new ClassLoaderProviderImpl();
    Optional<ClassLoader> classloaderOptional = kieClazzLoaderProvider.getClassloaderFromAllDependencies("src/test/projects/dummy_deps_complex", mavenRepo.toAbsolutePath().toString());
    assertTrue(classloaderOptional.isPresent());
    ClassLoader classloader = classloaderOptional.get();
    URLClassLoader urlsc = (URLClassLoader) classloader;
    assertTrue(urlsc.getURLs().length == 7);
}
Also used : ClassLoaderProviderImpl(org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) AFClassLoaderProvider(org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider) Test(org.junit.Test)

Example 2 with ClassLoaderProviderImpl

use of org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl in project kie-wb-common by kiegroup.

the class ClassLoaderProviderTest method loadProjectClassloaderFromStringTest.

@Test
public void loadProjectClassloaderFromStringTest() throws Exception {
    // we use NIO for this part of the test because Uberfire lack the implementation to copy a tree
    Path tmpRoot = Files.createTempDirectory("repo");
    Path tmp = Files.createDirectories(Paths.get(tmpRoot.toString(), "dummy"));
    TestUtil.copyTree(Paths.get("src/test/projects/dummy_kie_multimodule_classloader"), tmp);
    AFCompiler compiler = MavenCompilerFactory.getCompiler(Decorator.NONE);
    Path uberfireTmp = Paths.get(tmp.toAbsolutePath().toString());
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(uberfireTmp);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info, new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE, MavenCLIArgs.INSTALL }, new HashMap<>(), Boolean.FALSE);
    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(), "ClassLoaderProviderTest.loadProjectClassloaderFromStringTest");
    }
    assertTrue(res.isSuccessful());
    AFClassLoaderProvider kieClazzLoaderProvider = new ClassLoaderProviderImpl();
    Optional<ClassLoader> clazzLoader = kieClazzLoaderProvider.loadDependenciesClassloaderFromProject(uberfireTmp.toAbsolutePath().toString(), mavenRepo.toAbsolutePath().toString());
    assertNotNull(clazzLoader);
    assertTrue(clazzLoader.isPresent());
    ClassLoader prjClassloader = clazzLoader.get();
    // we try to load the only dep in the prj with a simple call method to see if is loaded or not
    Class clazz;
    try {
        clazz = prjClassloader.loadClass("org.slf4j.LoggerFactory");
        assertFalse(clazz.isInterface());
        Method m = clazz.getMethod("getLogger", String.class);
        Logger logger = (Logger) m.invoke(clazz, "Dummy");
        assertTrue(logger.getName().equals("Dummy"));
        logger.info("dependency loaded from the prj classpath");
    } catch (ClassNotFoundException e) {
        fail();
    }
    TestUtil.rm(tmpRoot.toFile());
}
Also used : Path(org.uberfire.java.nio.file.Path) CompilationResponse(org.kie.workbench.common.services.backend.compiler.CompilationResponse) Method(java.lang.reflect.Method) Logger(org.slf4j.Logger) ClassLoaderProviderImpl(org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl) URLClassLoader(java.net.URLClassLoader) AFClassLoaderProvider(org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) Test(org.junit.Test)

Example 3 with ClassLoaderProviderImpl

use of org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl in project kie-wb-common by kiegroup.

the class ClassLoaderProviderTest method loadProjectClassloaderTest.

@Test
public void loadProjectClassloaderTest() throws Exception {
    // we use NIO for this part of the test because Uberfire lack the implementation to copy a tree
    Path tmpRoot = Files.createTempDirectory("repo");
    Path tmp = Files.createDirectories(Paths.get(tmpRoot.toString(), "dummy"));
    TestUtil.copyTree(Paths.get("src/test/projects/dummy_kie_multimodule_classloader"), tmp);
    AFCompiler compiler = MavenCompilerFactory.getCompiler(Decorator.NONE);
    Path uberfireTmp = Paths.get(tmp.toAbsolutePath().toString());
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(uberfireTmp);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info, new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE, MavenCLIArgs.INSTALL }, new HashMap<>(), Boolean.FALSE);
    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(), "ClassLoaderProviderTest.loadProjectClassloaderTest");
    }
    assertTrue(res.isSuccessful());
    AFClassLoaderProvider kieClazzLoaderProvider = new ClassLoaderProviderImpl();
    List<String> pomList = new ArrayList<>();
    MavenUtils.searchPoms(Paths.get("src/test/projects/dummy_kie_multimodule_classloader/"), pomList);
    Optional<ClassLoader> clazzLoader = kieClazzLoaderProvider.loadDependenciesClassloaderFromProject(pomList, mavenRepo.toAbsolutePath().toString());
    assertNotNull(clazzLoader);
    assertTrue(clazzLoader.isPresent());
    ClassLoader prjClassloader = clazzLoader.get();
    // we try to load the only dep in the prj with a simple call method to see if is loaded or not
    Class clazz;
    try {
        clazz = prjClassloader.loadClass("org.slf4j.LoggerFactory");
        assertFalse(clazz.isInterface());
        Method m = clazz.getMethod("getLogger", String.class);
        Logger logger = (Logger) m.invoke(clazz, "Dummy");
        assertTrue(logger.getName().equals("Dummy"));
        logger.info("dependency loaded from the prj classpath");
    } catch (ClassNotFoundException e) {
        fail();
    }
    TestUtil.rm(tmpRoot.toFile());
}
Also used : Path(org.uberfire.java.nio.file.Path) ArrayList(java.util.ArrayList) CompilationResponse(org.kie.workbench.common.services.backend.compiler.CompilationResponse) Method(java.lang.reflect.Method) Logger(org.slf4j.Logger) ClassLoaderProviderImpl(org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl) URLClassLoader(java.net.URLClassLoader) AFClassLoaderProvider(org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) Test(org.junit.Test)

Example 4 with ClassLoaderProviderImpl

use of org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl in project kie-wb-common by kiegroup.

the class ClassLoaderProviderTest method loadTargetFolderClassloaderTest.

@Test
public void loadTargetFolderClassloaderTest() throws Exception {
    // we use NIO for this part of the test because Uberfire lack the implementation to copy a tree
    Path tmpRoot = Files.createTempDirectory("repo");
    Path tmp = Files.createDirectories(Paths.get(tmpRoot.toString(), "dummy"));
    TestUtil.copyTree(Paths.get("src/test/projects/dummy_kie_multimodule_classloader"), tmp);
    AFCompiler compiler = MavenCompilerFactory.getCompiler(Decorator.NONE);
    Path uberfireTmp = Paths.get(tmp.toAbsolutePath().toString());
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(uberfireTmp);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info, new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE }, new HashMap<>(), Boolean.FALSE);
    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(), "ClassLoaderProviderTest.loadTargetFolderClassloaderTest");
    }
    assertTrue(res.isSuccessful());
    AFClassLoaderProvider kieClazzLoaderProvider = new ClassLoaderProviderImpl();
    List<String> pomList = new ArrayList<>();
    MavenUtils.searchPoms(uberfireTmp, pomList);
    Optional<ClassLoader> clazzLoader = kieClazzLoaderProvider.getClassloaderFromProjectTargets(pomList, Boolean.FALSE);
    assertNotNull(clazzLoader);
    assertTrue(clazzLoader.isPresent());
    ClassLoader prjClassloader = clazzLoader.get();
    // we try to load the only dep in the prj with a simple call method to see if is loaded or not
    Class clazz;
    try {
        clazz = prjClassloader.loadClass("dummy.DummyB");
        assertFalse(clazz.isInterface());
        Object obj = clazz.newInstance();
        assertTrue(obj.toString().startsWith("dummy.DummyB"));
        Method m = clazz.getMethod("greetings", new Class[] {});
        Object greeting = m.invoke(obj, new Object[] {});
        assertTrue(greeting.toString().equals("Hello World !"));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        fail();
    }
    TestUtil.rm(tmpRoot.toFile());
}
Also used : Path(org.uberfire.java.nio.file.Path) ArrayList(java.util.ArrayList) CompilationResponse(org.kie.workbench.common.services.backend.compiler.CompilationResponse) Method(java.lang.reflect.Method) ClassLoaderProviderImpl(org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl) URLClassLoader(java.net.URLClassLoader) AFClassLoaderProvider(org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) Test(org.junit.Test)

Example 5 with ClassLoaderProviderImpl

use of org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl in project kie-wb-common by kiegroup.

the class KieClassLoaderProviderTest method loadTargetFolderClassloaderTest.

@Test
public void loadTargetFolderClassloaderTest() throws Exception {
    // we use NIO for this part of the test because Uberfire lack the implementation to copy a tree
    Path tmpRoot = Files.createTempDirectory("repo");
    Path tmp = Files.createDirectories(Paths.get(tmpRoot.toString(), "dummy"));
    TestUtil.copyTree(Paths.get("src/test/projects/dummy_kie_multimodule_classloader"), tmp);
    AFCompiler compiler = KieMavenCompilerFactory.getCompiler(KieDecorator.NONE);
    Path uberfireTmp = Paths.get(tmp.toAbsolutePath().toString());
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(uberfireTmp);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info, new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE }, new HashMap<>(), Boolean.FALSE);
    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(), "KieClassLoaderProviderTest.loadTargetFolderClassloaderTest");
    }
    assertTrue(res.isSuccessful());
    AFClassLoaderProvider kieClazzLoaderProvider = new ClassLoaderProviderImpl();
    List<String> pomList = new ArrayList<>();
    MavenUtils.searchPoms(uberfireTmp, pomList);
    Optional<ClassLoader> clazzLoader = kieClazzLoaderProvider.getClassloaderFromProjectTargets(pomList, Boolean.FALSE);
    assertNotNull(clazzLoader);
    assertTrue(clazzLoader.isPresent());
    ClassLoader prjClassloader = clazzLoader.get();
    // we try to load the only dep in the prj with a simple call method to see if is loaded or not
    Class clazz;
    try {
        clazz = prjClassloader.loadClass("dummy.DummyB");
        assertFalse(clazz.isInterface());
        Object obj = clazz.newInstance();
        assertTrue(obj.toString().startsWith("dummy.DummyB"));
        Method m = clazz.getMethod("greetings", new Class[] {});
        Object greeting = m.invoke(obj, new Object[] {});
        assertTrue(greeting.toString().equals("Hello World !"));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        fail();
    }
    TestUtil.rm(tmpRoot.toFile());
}
Also used : Path(org.uberfire.java.nio.file.Path) ArrayList(java.util.ArrayList) CompilationResponse(org.kie.workbench.common.services.backend.compiler.CompilationResponse) Method(java.lang.reflect.Method) ClassLoaderProviderImpl(org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl) WorkspaceCompilationInfo(org.kie.workbench.common.services.backend.compiler.nio.WorkspaceCompilationInfo) URLClassLoader(java.net.URLClassLoader) AFClassLoaderProvider(org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) AFCompiler(org.kie.workbench.common.services.backend.compiler.nio.AFCompiler) CompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.CompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) Test(org.junit.Test)

Aggregations

AFClassLoaderProvider (org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider)11 ClassLoaderProviderImpl (org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl)11 URLClassLoader (java.net.URLClassLoader)10 Test (org.junit.Test)10 Method (java.lang.reflect.Method)6 CompilationResponse (org.kie.workbench.common.services.backend.compiler.CompilationResponse)6 DefaultCompilationRequest (org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest)6 Path (org.uberfire.java.nio.file.Path)6 ArrayList (java.util.ArrayList)4 Logger (org.slf4j.Logger)4 AFCompiler (org.kie.workbench.common.services.backend.compiler.nio.AFCompiler)3 CompilationRequest (org.kie.workbench.common.services.backend.compiler.nio.CompilationRequest)3 WorkspaceCompilationInfo (org.kie.workbench.common.services.backend.compiler.nio.WorkspaceCompilationInfo)3 URI (java.net.URI)1 List (java.util.List)1 DefaultKieCompilationResponse (org.kie.workbench.common.services.backend.compiler.impl.DefaultKieCompilationResponse)1