Source:
private void init(@Nullable TransformContext context, FailureCollector collector) { ScriptEngineManager manager = new ScriptEngineManager(); engine = manager.getEngineByName("JavaScript"); try { engine.eval(ScriptConstants.HELPER_DEFINITION); } catch (ScriptException e) { collector.addFailure("Couldn't define helper functions", "Something went wrong"); throw collector.getOrThrowException(); } JavaTypeConverters js = ((Invocable) engine).getInterface( engine.get(ScriptConstants.HELPER_NAME), JavaTypeConverters.class); LookupConfig lookupConfig; try { lookupConfig = GSON.fromJson(config.lookup, LookupConfig.class); } catch (JsonSyntaxException e) { collector.addFailure("Invalid lookup config. Expected map of string to string", "Ensure the provided lookup config is valid JSON in the form of a string to string map") .withConfigProperty("lookup"); throw collector.getOrThrowException(); } Arguments arguments = context == null ? null : context.getArguments(); engine.put(CONTEXT_NAME, new ScriptContext(LOG, metrics, context, lookupConfig, js, arguments)); try { String script = String.format("function %s() { return transform(%s, %s, %s); }\n%s", FUNCTION_NAME, VARIABLE_NAME, EMITTER_NAME, CONTEXT_NAME, config.script); engine.eval(script); } catch (ScriptException e) { collector.addFailure( String.format("Invalid script: %s", e.getMessage()), "Ensure the input script is valid") .withConfigProperty("script"); } invocable = (Invocable) engine; if (config.schema != null) { try { schema = Schema.parseJson(config.schema); } catch (IOException e) { collector.addFailure(String.format("Unable to parse schema: %s", e.getMessage()), "Ensure the output schema is JSON parseable").withConfigProperty("schema"); } } collector.getOrThrowException(); }

Target:
private void init(@Nullable TransformContext context, FailureCollector collector) { ScriptEngineManager manager = new ScriptEngineManager(); engine = manager.getEngineByName("JavaScript"); try { engine.eval(ScriptConstants.HELPER_DEFINITION); } catch (ScriptException e) { collector.addFailure("Failed to define helper functions.", null); throw collector.getOrThrowException(); } JavaTypeConverters js = ((Invocable) engine).getInterface( engine.get(ScriptConstants.HELPER_NAME), JavaTypeConverters.class); LookupConfig lookupConfig; try { lookupConfig = GSON.fromJson(config.lookup, LookupConfig.class); } catch (JsonSyntaxException e) { collector.addFailure("Invalid lookup config.", "Expected JSON map of string to string.") .withConfigProperty(Config.LOOKUP); throw collector.getOrThrowException(); } Arguments arguments = context == null ? null : context.getArguments(); engine.put(CONTEXT_NAME, new ScriptContext(LOG, metrics, context, lookupConfig, js, arguments)); try { String script = String.format("function %s() { return transform(%s, %s, %s); }\n%s", FUNCTION_NAME, VARIABLE_NAME, EMITTER_NAME, CONTEXT_NAME, config.script); engine.eval(script); } catch (ScriptException e) { collector.addFailure(String.format("Invalid script: %s.", e.getMessage()), null) .withConfigProperty(Config.SCRIPT); } invocable = (Invocable) engine; if (config.schema != null) { try { schema = Schema.parseJson(config.schema); } catch (IOException e) { collector.addFailure(String.format("Invalid schema: %s.", e.getMessage()), "Output schema must be JSON parseable.") .withConfigProperty(Config.SCHEMA); } } collector.getOrThrowException(); }