Search in sources :

Example 1 with PhoenicisScriptEngine

use of org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine in project POL-POM-5 by PlayOnLinux.

the class EnginesManager method getEngine.

/**
 * Fetches the required engine
 *
 * @param engineId The engine ID (e.g. "wine")
 * @param doneCallback The callback which will be executed with the fetched engine
 * @param errorCallback The callback which will be executed if an error occurs
 */
public void getEngine(String engineId, Consumer<Engine> doneCallback, Consumer<Exception> errorCallback) {
    executorService.execute(() -> {
        final PhoenicisScriptEngine phoenicisScriptEngine = phoenicisScriptEngineFactory.createEngine();
        final String include = String.format("include(\"engines.%s.engine.implementation\");", engineId);
        final Value engineClass = (Value) phoenicisScriptEngine.evalAndReturn(include, errorCallback);
        final Engine engine = engineClass.newInstance().as(Engine.class);
        doneCallback.accept(engine);
    });
}
Also used : Value(org.graalvm.polyglot.Value) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine)

Example 2 with PhoenicisScriptEngine

use of org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine in project POL-POM-5 by PlayOnLinux.

the class IncludeInjector method injectInto.

@Override
public void injectInto(PhoenicisScriptEngine phoenicisScriptEngine) {
    // store scripts that are currently in progress
    final Stack<String> includeStack = new Stack<>();
    // store included scripts (include path -> JS object)
    final Map<String, Object> includedScripts = new HashMap<>();
    phoenicisScriptEngine.put("include", (Function<String, Object>) argument -> {
        if (includeStack.contains(argument)) {
            throw new CircularIncludeException(argument, includeStack);
        }
        includeStack.push(argument);
        if (!includedScripts.containsKey(argument)) {
            final String script = scriptFetcher.getScript(argument);
            if (script == null) {
                throw new ScriptNotFoundException(argument);
            }
            try {
                String extendedString = String.format("(module) => { %s }", script);
                Value includeFunction = (Value) phoenicisScriptEngine.evalAndReturn(extendedString, this::throwException);
                Value module = (Value) phoenicisScriptEngine.evalAndReturn("({})", this::throwException);
                includeFunction.execute(module);
                if (module.hasMember("default")) {
                    includedScripts.put(argument, module.getMember("default"));
                } else {
                    includedScripts.put(argument, module);
                }
            } catch (ScriptException se) {
                throw new IncludeException(argument, se);
            }
        }
        includeStack.pop();
        return includedScripts.get(argument);
    }, this::throwException);
}
Also used : IncludeException(org.phoenicis.scripts.exceptions.IncludeException) ScriptException(org.phoenicis.scripts.exceptions.ScriptException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) Value(org.graalvm.polyglot.Value) Map(java.util.Map) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine) ScriptNotFoundException(org.phoenicis.scripts.exceptions.ScriptNotFoundException) ScriptFetcher(org.phoenicis.scripts.interpreter.ScriptFetcher) HashMap(java.util.HashMap) Function(java.util.function.Function) Stack(java.util.Stack) ScriptException(org.phoenicis.scripts.exceptions.ScriptException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) HashMap(java.util.HashMap) Value(org.graalvm.polyglot.Value) IncludeException(org.phoenicis.scripts.exceptions.IncludeException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) ScriptNotFoundException(org.phoenicis.scripts.exceptions.ScriptNotFoundException) Stack(java.util.Stack)

Example 3 with PhoenicisScriptEngine

use of org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine in project POL-POM-5 by PlayOnLinux.

the class PhoenicisScriptEngineFactory method createEngine.

public PhoenicisScriptEngine createEngine() {
    final PhoenicisScriptEngine phoenicisScriptEngine = type.createScriptEngine();
    engineInjectors.forEach(engineInjector -> engineInjector.injectInto(phoenicisScriptEngine));
    return phoenicisScriptEngine;
}
Also used : PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine)

Example 4 with PhoenicisScriptEngine

use of org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine in project POL-POM-5 by PlayOnLinux.

the class EngineSettingsManager method fetchAvailableEngineSettings.

/**
 * Fetches the available engine settings
 *
 * @param repositoryDTO The repository containing the engine settings
 * @param callback The callback which recieves the found engine settings
 * @param errorCallback The callback which will be executed if an error occurs
 */
public void fetchAvailableEngineSettings(RepositoryDTO repositoryDTO, Consumer<Map<String, List<EngineSetting>>> callback, Consumer<Exception> errorCallback) {
    executorService.execute(() -> {
        final List<SettingConfig> configurations = fetchSettingConfigurations(repositoryDTO);
        // the script engine needs to be created inside the correct thread otherwise GraalJS throws an error
        final PhoenicisScriptEngine phoenicisScriptEngine = phoenicisScriptEngineFactory.createEngine();
        final Map<String, List<EngineSetting>> result = configurations.stream().collect(Collectors.groupingBy(configuration -> configuration.engineId, Collectors.mapping(configuration -> {
            final String include = String.format("include(\"engines.%s.settings.%s\");", configuration.engineId, configuration.settingId);
            final Value settingClass = (Value) phoenicisScriptEngine.evalAndReturn(include, errorCallback);
            return settingClass.newInstance().as(EngineSetting.class);
        }, Collectors.toList())));
        callback.accept(result);
    });
}
Also used : Consumer(java.util.function.Consumer) CategoryDTO(org.phoenicis.repository.dto.CategoryDTO) List(java.util.List) RepositoryDTO(org.phoenicis.repository.dto.RepositoryDTO) Value(org.graalvm.polyglot.Value) Map(java.util.Map) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine) PhoenicisScriptEngineFactory(org.phoenicis.scripts.engine.PhoenicisScriptEngineFactory) Collectors(java.util.stream.Collectors) ExecutorService(java.util.concurrent.ExecutorService) ApplicationDTO(org.phoenicis.repository.dto.ApplicationDTO) Value(org.graalvm.polyglot.Value) List(java.util.List) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine)

Example 5 with PhoenicisScriptEngine

use of org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine in project POL-POM-5 by PlayOnLinux.

the class EnginesManager method fetchAvailableEngines.

/**
 * Fetches the available engines
 *
 * @param repositoryDTO The repository containing the engines
 * @param callback The callback which receives the fetched engines
 * @param errorCallback The callback which is executed if an error occurs
 */
public void fetchAvailableEngines(RepositoryDTO repositoryDTO, Consumer<Map<String, Engine>> callback, Consumer<Exception> errorCallback) {
    final List<String> engineIds = repositoryDTO.getTypes().stream().filter(type -> type.getId().equals("engines")).flatMap(type -> type.getCategories().stream()).map(engine -> engine.getId().replaceAll("^.*\\.", "")).collect(Collectors.toList());
    executorService.execute(() -> {
        final PhoenicisScriptEngine phoenicisScriptEngine = phoenicisScriptEngineFactory.createEngine();
        Map<String, Engine> result = engineIds.stream().collect(Collectors.toMap(Function.identity(), engineId -> {
            final String include = String.format("include(\"engines.%s.engine.implementation\");", engineId);
            final Value engineClass = (Value) phoenicisScriptEngine.evalAndReturn(include, errorCallback);
            return engineClass.newInstance().as(Engine.class);
        }));
        callback.accept(result);
    });
}
Also used : CategoryDTO(org.phoenicis.repository.dto.CategoryDTO) RepositoryDTO(org.phoenicis.repository.dto.RepositoryDTO) Logger(org.slf4j.Logger) Value(org.graalvm.polyglot.Value) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) PhoenicisScriptEngineFactory(org.phoenicis.scripts.engine.PhoenicisScriptEngineFactory) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) EngineCategoryDTO(org.phoenicis.engines.dto.EngineCategoryDTO) Safe(org.phoenicis.configuration.security.Safe) Map(java.util.Map) TypeReference(com.fasterxml.jackson.core.type.TypeReference) EngineSubCategoryDTO(org.phoenicis.engines.dto.EngineSubCategoryDTO) Collections(java.util.Collections) ExecutorService(java.util.concurrent.ExecutorService) Value(org.graalvm.polyglot.Value) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine)

Aggregations

PhoenicisScriptEngine (org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine)10 Value (org.graalvm.polyglot.Value)8 Map (java.util.Map)6 List (java.util.List)4 ExecutorService (java.util.concurrent.ExecutorService)4 Consumer (java.util.function.Consumer)4 Function (java.util.function.Function)4 Collectors (java.util.stream.Collectors)4 CategoryDTO (org.phoenicis.repository.dto.CategoryDTO)4 RepositoryDTO (org.phoenicis.repository.dto.RepositoryDTO)4 PhoenicisScriptEngineFactory (org.phoenicis.scripts.engine.PhoenicisScriptEngineFactory)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Stack (java.util.Stack)2 Safe (org.phoenicis.configuration.security.Safe)2 EngineCategoryDTO (org.phoenicis.engines.dto.EngineCategoryDTO)2