Search in sources :

Example 1 with EnvironmentProvider

use of org.graalvm.tools.lsp.instrument.EnvironmentProvider in project graal by oracle.

the class TruffleLSPTest method setup.

@Before
public void setup() {
    engine = Engine.newBuilder().allowExperimentalOptions(true).build();
    Instrument instrument = engine.getInstruments().get("lsp");
    EnvironmentProvider envProvider = instrument.lookup(EnvironmentProvider.class);
    truffleAdapter = new TruffleAdapter(envProvider.getEnvironment(), true);
    Builder contextBuilder = Context.newBuilder();
    contextBuilder.allowAllAccess(true);
    contextBuilder.fileSystem(LSPFileSystem.newReadOnlyFileSystem(truffleAdapter));
    contextBuilder.engine(engine);
    context = contextBuilder.build();
    context.enter();
    ContextAwareExecutor executorWrapper = new ContextAwareExecutor() {

        @Override
        public <T> Future<T> executeWithDefaultContext(Callable<T> taskWithResult) {
            try {
                return CompletableFuture.completedFuture(taskWithResult.call());
            } catch (Exception e) {
                CompletableFuture<T> cf = new CompletableFuture<>();
                cf.completeExceptionally(e);
                return cf;
            }
        }

        @Override
        public <T> Future<T> executeWithNestedContext(Callable<T> taskWithResult, boolean cached) {
            try (Context newContext = contextBuilder.build()) {
                newContext.enter();
                newContext.initialize("sl");
                try {
                    return CompletableFuture.completedFuture(taskWithResult.call());
                } catch (Exception e) {
                    CompletableFuture<T> cf = new CompletableFuture<>();
                    cf.completeExceptionally(e);
                    return cf;
                } finally {
                    newContext.leave();
                }
            }
        }

        @Override
        public <T> Future<T> executeWithNestedContext(Callable<T> taskWithResult, int timeoutMillis, Callable<T> onTimeoutTask) {
            if (timeoutMillis <= 0) {
                return executeWithNestedContext(taskWithResult, false);
            } else {
                CompletableFuture<Future<T>> future = CompletableFuture.supplyAsync(() -> executeWithNestedContext(taskWithResult, false));
                try {
                    return future.get(timeoutMillis, TimeUnit.MILLISECONDS);
                } catch (TimeoutException e) {
                    future.cancel(true);
                    try {
                        return CompletableFuture.completedFuture(onTimeoutTask.call());
                    } catch (Exception timeoutTaskException) {
                        CompletableFuture<T> cf = new CompletableFuture<>();
                        cf.completeExceptionally(timeoutTaskException);
                        return cf;
                    }
                } catch (InterruptedException | ExecutionException e) {
                    CompletableFuture<T> cf = new CompletableFuture<>();
                    cf.completeExceptionally(e);
                    return cf;
                }
            }
        }

        @Override
        public void shutdown() {
        }

        @Override
        public void resetContextCache() {
        }
    };
    truffleAdapter.register(envProvider.getEnvironment(), executorWrapper);
}
Also used : Context(org.graalvm.polyglot.Context) Builder(org.graalvm.polyglot.Context.Builder) Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) EnvironmentProvider(org.graalvm.tools.lsp.instrument.EnvironmentProvider) CompletableFuture(java.util.concurrent.CompletableFuture) TruffleAdapter(org.graalvm.tools.lsp.server.TruffleAdapter) Instrument(org.graalvm.polyglot.Instrument) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) ContextAwareExecutor(org.graalvm.tools.lsp.server.ContextAwareExecutor) TimeoutException(java.util.concurrent.TimeoutException) Before(org.junit.Before)

Aggregations

IOException (java.io.IOException)1 Callable (java.util.concurrent.Callable)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 Context (org.graalvm.polyglot.Context)1 Builder (org.graalvm.polyglot.Context.Builder)1 Instrument (org.graalvm.polyglot.Instrument)1 EnvironmentProvider (org.graalvm.tools.lsp.instrument.EnvironmentProvider)1 ContextAwareExecutor (org.graalvm.tools.lsp.server.ContextAwareExecutor)1 TruffleAdapter (org.graalvm.tools.lsp.server.TruffleAdapter)1 Before (org.junit.Before)1