Search in sources :

Example 1 with BeforeStep

use of org.springframework.batch.core.annotation.BeforeStep in project jvarkit by lindenb.

the class JdkFilterItemProcessor method beforeStep.

@BeforeStep
public void beforeStep(final StepExecution stepExecution) {
    try {
        InMemoryCompiler javac = new InMemoryCompiler();
        String javaCode = InMemoryCompiler.getTheSourceCode(getExpression(), getScriptFile());
        final Class<?> clazz;
        if (CODE2CLASS.containsKey(javaCode)) {
            if (LOG.isWarnEnabled())
                LOG.warn("Code already compiled");
            clazz = CODE2CLASS.get(javaCode);
        } else {
            final String className = "FunctionImpl" + rand.nextInt() + System.currentTimeMillis();
            if (LOG.isInfoEnabled())
                LOG.info("Compiling\n" + InMemoryCompiler.beautifyCode(javaCode));
            clazz = javac.compileClass(className, javaCode.replace("__CLASS__", className));
            CODE2CLASS.put(javaCode, clazz);
        }
        final Constructor<?> ctor = clazz.getConstructor();
        final Object instance = ctor.newInstance();
        if (!(instance instanceof Function)) {
            throw new JvarkitException.ScriptingError("Not an instance of Function");
        }
        this.transformer = (Function) instance;
    } catch (final Exception err) {
        throw new RuntimeException(err);
    }
}
Also used : Function(java.util.function.Function) InMemoryCompiler(com.github.lindenb.jvarkit.lang.InMemoryCompiler) JvarkitException(com.github.lindenb.jvarkit.lang.JvarkitException) BeforeStep(org.springframework.batch.core.annotation.BeforeStep)

Aggregations

InMemoryCompiler (com.github.lindenb.jvarkit.lang.InMemoryCompiler)1 JvarkitException (com.github.lindenb.jvarkit.lang.JvarkitException)1 Function (java.util.function.Function)1 BeforeStep (org.springframework.batch.core.annotation.BeforeStep)1