Search in sources :

Example 1 with StringJavaSource

use of org.finos.legend.engine.shared.javaCompiler.StringJavaSource in project legend-engine by finos.

the class ExecutionNodeJavaPlatformHelper method getClassToExecute.

public static Class<?> getClassToExecute(ExecutionNode node, String _class, ExecutionState executionState, MutableList<CommonProfile> pm) {
    if (executionState.isJavaCompilationForbidden()) {
        try {
            return Thread.currentThread().getContextClassLoader().loadClass(_class);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    JavaPlatformImplementation j = (JavaPlatformImplementation) node.implementation;
    List<JavaClass> javaClasses = FastList.newList();
    if (j.code != null) {
        JavaClass executeClass = JavaHelper.newJavaClass(JavaHelper.getExecutionClassFullName(j));
        executeClass.source = j.code;
        javaClasses.add(executeClass);
    }
    javaClasses.addAll(getLocalImplementationSupportClasses(node));
    if (javaClasses.isEmpty()) {
        ClassLoader classLoader = executionState.hasJavaCompiler() ? executionState.getJavaCompiler().getClassLoader() : Thread.currentThread().getContextClassLoader();
        try {
            return classLoader.loadClass(_class);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    try {
        // TODO Confirm we can delete this
        MutableMap<String, String> compiledClassesWithByteCode = getCompiledClasses(node);
        List<StringJavaSource> classesToCompile = javaClasses.stream().filter(c -> !compiledClassesWithByteCode.containsKey(JavaHelper.getJavaClassFullName(c))).map(JavaHelper::buildStringJavaSource).collect(Collectors.toList());
        EngineJavaCompiler compiler = new EngineJavaCompiler(executionState.getJavaCompiler());
        long start = System.currentTimeMillis();
        try {
            if (!compiledClassesWithByteCode.isEmpty()) {
                compiler.load(compiledClassesWithByteCode);
            }
            if (!classesToCompile.isEmpty()) {
                LOGGER.info(new LogInfo(pm, LoggingEventType.JAVA_COMPILATION_START, "Node: " + node.getClass().getName()).toString());
                compiler.compile(classesToCompile);
                LOGGER.info(new LogInfo(pm, LoggingEventType.JAVA_COMPILATION_STOP, (double) System.currentTimeMillis() - start).toString());
            }
        } catch (Exception jce) {
            LOGGER.info(new LogInfo(pm, LoggingEventType.JAVA_COMPILATION_ERROR, new ErrorResult(1, jce).getMessage()).toString());
            throw jce;
        }
        return compiler.getClassLoader().loadClass(_class);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) EngineJavaCompiler(org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler) ErrorResult(org.finos.legend.engine.plan.execution.result.ErrorResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) JavaClass(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaClass) JavaPlatformImplementation(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation) StringJavaSource(org.finos.legend.engine.shared.javaCompiler.StringJavaSource)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ErrorResult (org.finos.legend.engine.plan.execution.result.ErrorResult)1 JavaClass (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaClass)1 JavaPlatformImplementation (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.JavaPlatformImplementation)1 LogInfo (org.finos.legend.engine.shared.core.operational.logs.LogInfo)1 EngineJavaCompiler (org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler)1 StringJavaSource (org.finos.legend.engine.shared.javaCompiler.StringJavaSource)1