@Test public void extractGFMetrics() throws IOException { String scriptContent = load("glassfish_methodstatistic.js"); String metric = load("methodstatistic.json"); assertNotNull(scriptContent); Function<String, JsonObject> extractor = this.cut.extractor(scriptContent); JsonObject jsonMetric = extractor.apply(metric); assertNotNull(jsonMetric); System.out.println("jsonMetric = " + jsonMetric); int value = jsonMetric.getJsonNumber("value").intValue(); assertThat(value, is(-1)); }
Function<String, JsonObject> extractor(String scriptContent) { try { this.scriptEngine.eval(scriptContent); } catch (ScriptException ex) { throw new ScriptEvaluationException("Script evaluation failed", ex); } Invocable invocable = (Invocable) this.scriptEngine; Function<String, String> nashorn = invocable.getInterface(Function.class); Function<String, JsonObject> withToJson = nashorn.andThen(this::fromString); return (input) -> { try { return withToJson.apply(input); } catch (Throwable t) { throw new ScriptEvaluationException("Script execution failed", scriptContent, input, t); } }; }
Extractor { Function<String, JsonObject> extractor(String scriptContent) { try { this.scriptEngine.eval(scriptContent); } catch (ScriptException ex) { throw new ScriptEvaluationException("Script evaluation failed", ex); } Invocable invocable = (Invocable) this.scriptEngine; Function<String, String> nashorn = invocable.getInterface(Function.class); Function<String, JsonObject> withToJson = nashorn.andThen(this::fromString); return (input) -> { try { return withToJson.apply(input); } catch (Throwable t) { throw new ScriptEvaluationException("Script execution failed", scriptContent, input, t); } }; } }
Extractor { Function<String, JsonObject> extractor(String scriptContent) { try { this.scriptEngine.eval(scriptContent); } catch (ScriptException ex) { throw new ScriptEvaluationException("Script evaluation failed", ex); } Invocable invocable = (Invocable) this.scriptEngine; Function<String, String> nashorn = invocable.getInterface(Function.class); Function<String, JsonObject> withToJson = nashorn.andThen(this::fromString); return (input) -> { try { return withToJson.apply(input); } catch (Throwable t) { throw new ScriptEvaluationException("Script execution failed", scriptContent, input, t); } }; }  }
Extractor { Function<String, JsonObject> extractor(String scriptContent) { try { this.scriptEngine.eval(scriptContent); } catch (ScriptException ex) { throw new ScriptEvaluationException("Script evaluation failed", ex); } Invocable invocable = (Invocable) this.scriptEngine; Function<String, String> nashorn = invocable.getInterface(Function.class); Function<String, JsonObject> withToJson = nashorn.andThen(this::fromString); return (input) -> { try { return withToJson.apply(input); } catch (Throwable t) { throw new ScriptEvaluationException("Script execution failed", scriptContent, input, t); } }; }  @PostConstruct void initEngine(); Optional<Function<String, JsonObject>> getExtractor(Optional<JsonObject> optionalConfiguration); }
Extractor { Function<String, JsonObject> extractor(String scriptContent) { try { this.scriptEngine.eval(scriptContent); } catch (ScriptException ex) { throw new ScriptEvaluationException("Script evaluation failed", ex); } Invocable invocable = (Invocable) this.scriptEngine; Function<String, String> nashorn = invocable.getInterface(Function.class); Function<String, JsonObject> withToJson = nashorn.andThen(this::fromString); return (input) -> { try { return withToJson.apply(input); } catch (Throwable t) { throw new ScriptEvaluationException("Script execution failed", scriptContent, input, t); } }; }  @PostConstruct void initEngine(); Optional<Function<String, JsonObject>> getExtractor(Optional<JsonObject> optionalConfiguration); final static String EXTRACTOR_KEY; }