Search in sources :

Example 1 with ScriptException

use of org.phoenicis.scripts.interpreter.ScriptException in project POL-POM-5 by PlayOnLinux.

the class NashornEngineFactory method createEngine.

NashornEngine createEngine() {
    final Set<List<String>> includedScripts = new HashSet<>();
    final NashornEngine nashornEngine = new NashornEngine(new ScriptEngineManager().getEngineByName("nashorn"));
    nashornEngine.eval(new InputStreamReader(getClass().getResourceAsStream("utils.js")), this::throwException);
    nashornEngine.put("Bean", (Function<String, Object>) title -> applicationContext.getBean(title), this::throwException);
    nashornEngine.put("SetupWizard", (Function<String, UiSetupWizardImplementation>) (name) -> {
        final UiSetupWizardImplementation uiSetupWizardImplementation = uiSetupWizardFactory.create(name);
        nashornEngine.addErrorHandler(e -> uiSetupWizardImplementation.close());
        return uiSetupWizardImplementation;
    }, this::throwException);
    nashornEngine.put("EngineProgressUi", (Function<String, UiProgressWizardImplementation>) (name) -> {
        final UiProgressWizardImplementation uiProgressWizardImplementation = uiProgressWizardFactory.create(name);
        nashornEngine.addErrorHandler(e -> uiProgressWizardImplementation.close());
        return uiProgressWizardImplementation;
    }, this::throwException);
    nashornEngine.put("include", (Consumer<ScriptObjectMirror>) args -> {
        final String[] arguments = args.to(String[].class);
        final String script = scriptFetcher.getScript(arguments);
        if (script == null) {
            throwException(new ScriptException(Arrays.asList(arguments).toString() + " is not found"));
        }
        if (includedScripts.add(Arrays.asList(arguments))) {
            nashornEngine.eval("//# sourceURL=" + Arrays.asList(arguments).toString() + "\n" + script, this::throwException);
        }
    }, this::throwException);
    return nashornEngine;
}
Also used : Arrays(java.util.Arrays) UiProgressWizardFactory(org.phoenicis.scripts.wizard.UiProgressWizardFactory) ScriptException(org.phoenicis.scripts.interpreter.ScriptException) UiSetupWizardFactory(org.phoenicis.scripts.wizard.UiSetupWizardFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ScriptEngineManager(javax.script.ScriptEngineManager) Set(java.util.Set) ScriptFetcher(org.phoenicis.scripts.interpreter.ScriptFetcher) ApplicationContext(org.springframework.context.ApplicationContext) InputStreamReader(java.io.InputStreamReader) Function(java.util.function.Function) UiSetupWizardImplementation(org.phoenicis.scripts.wizard.UiSetupWizardImplementation) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) List(java.util.List) UiProgressWizardImplementation(org.phoenicis.scripts.wizard.UiProgressWizardImplementation) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InputStreamReader(java.io.InputStreamReader) UiSetupWizardImplementation(org.phoenicis.scripts.wizard.UiSetupWizardImplementation) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptException(org.phoenicis.scripts.interpreter.ScriptException) UiProgressWizardImplementation(org.phoenicis.scripts.wizard.UiProgressWizardImplementation) List(java.util.List) HashSet(java.util.HashSet)

Example 2 with ScriptException

use of org.phoenicis.scripts.interpreter.ScriptException in project POL-POM-5 by PlayOnLinux.

the class UiSetupWizardImplementation method licenceFile.

/**
     * Show the content of a licence file
     *
     * @param textToShow  a message above the licence
     * @param licenceFile the licence file to display (with 'from java.io import File')
     */
@Override
public Void licenceFile(String textToShow, File licenceFile) {
    try {
        try (final FileInputStream content = new FileInputStream(licenceFile)) {
            final StringWriter writer = new StringWriter();
            IOUtils.copy(content, writer, "UTF-8");
            return licence(textToShow, writer.toString());
        }
    } catch (IOException e) {
        throw new ScriptException("Cannot acces the licence file", e);
    }
}
Also used : ScriptException(org.phoenicis.scripts.interpreter.ScriptException) StringWriter(java.io.StringWriter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

ScriptException (org.phoenicis.scripts.interpreter.ScriptException)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1 ScriptFetcher (org.phoenicis.scripts.interpreter.ScriptFetcher)1 UiProgressWizardFactory (org.phoenicis.scripts.wizard.UiProgressWizardFactory)1 UiProgressWizardImplementation (org.phoenicis.scripts.wizard.UiProgressWizardImplementation)1 UiSetupWizardFactory (org.phoenicis.scripts.wizard.UiSetupWizardFactory)1 UiSetupWizardImplementation (org.phoenicis.scripts.wizard.UiSetupWizardImplementation)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ApplicationContext (org.springframework.context.ApplicationContext)1