JavaExecutor implements Executor { Object execute(String className, String methodName, JavaExecutionParametersProvider parametersProvider) { ClassLoader origCL = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(classLoader); Class actionClass = getActionClass(className); Method executionMethod = getMethodByName(actionClass, methodName); Object[] executionParameters = parametersProvider.getExecutionParameters(executionMethod); Object[] transformedExecutionParameters = transformExecutionParameters(executionParameters, executionMethod); return executionMethod.invoke(actionClass.newInstance(), transformedExecutionParameters); } catch (Exception e) { throw new RuntimeException( "Method [" + methodName + "] invocation of class [" + className + "] failed: " + e.getMessage(), e); } finally { Thread.currentThread().setContextClassLoader(origCL); } } JavaExecutor(Set<String> filePaths); static ClassLoader getParentClassLoader(); @Override void allocate(); @Override void release(); @Override void close();  }
@Test public void testJavaExecutorDifferentClassloaders() { File one1 = new File(getClass().getClassLoader().getResource("one1.zip").getFile()); File one2 = new File(getClass().getClassLoader().getResource("one2.zip").getFile()); File one3 = new File(getClass().getClassLoader().getResource("one3.zip").getFile()); File another1 = new File(getClass().getClassLoader().getResource("another1.zip").getFile()); File another2 = new File(getClass().getClassLoader().getResource("another2.zip").getFile()); File another3 = new File(getClass().getClassLoader().getResource("another3.zip").getFile()); JavaExecutor javaExecutor1 = new JavaExecutor(new HashSet<>(Arrays.asList(one1.getAbsolutePath(), another2.getAbsolutePath()))); JavaExecutor javaExecutor2 = new JavaExecutor(new HashSet<>(Arrays.asList(one2.getAbsolutePath(), another3.getAbsolutePath()))); JavaExecutor javaExecutor3 = new JavaExecutor(new HashSet<>(Arrays.asList(one3.getAbsolutePath(), another1.getAbsolutePath()))); assertEquals("The version is One 1 and [The version is Another 2]", javaExecutor1.execute(CLASS_NAME, METHOD_NAME, PARAM_PROVIDER).toString()); assertEquals("The version is One 2 and [The version is Another 3]", javaExecutor2.execute(CLASS_NAME, METHOD_NAME, PARAM_PROVIDER).toString()); assertEquals("The version is One 3 and [The version is Another 1]", javaExecutor3.execute(CLASS_NAME, METHOD_NAME, PARAM_PROVIDER).toString()); assertEquals("The version is One 1 and [The version is Another 2]", javaExecutor1.execute(CLASS_NAME, METHOD_NAME, PARAM_PROVIDER).toString()); assertEquals("The version is One 2 and [The version is Another 3]", javaExecutor2.execute(CLASS_NAME, METHOD_NAME, PARAM_PROVIDER).toString()); assertEquals("The version is One 3 and [The version is Another 1]", javaExecutor3.execute(CLASS_NAME, METHOD_NAME, PARAM_PROVIDER).toString()); }
@Test public void testJavaExecutorMissingDependency() { expectedEx.expect(RuntimeException.class); expectedEx.expectMessage("Method [getVersion] invocation of class [group.artifact.OneClass] failed: null"); File one = new File(getClass().getClassLoader().getResource("one1.zip").getFile()); JavaExecutor javaExecutor = new JavaExecutor(new HashSet<>(Arrays.asList(one.getAbsolutePath()))); javaExecutor.execute(CLASS_NAME, METHOD_NAME, PARAM_PROVIDER).toString(); }