Search in sources :

Example 1 with LSPIOException

use of org.graalvm.tools.lsp.exceptions.LSPIOException in project graal by oracle.

the class LSPInstrument method launchServer.

private TruffleAdapter launchServer(PrintWriter info, PrintWriter err) {
    assert options != null;
    assert options.hasSetOptions();
    TruffleAdapter truffleAdapter = new TruffleAdapter(environment, options.get(DeveloperMode));
    Context.Builder builder = Context.newBuilder();
    builder.allowAllAccess(true);
    builder.engine(Engine.create());
    builder.fileSystem(LSPFileSystem.newReadOnlyFileSystem(truffleAdapter));
    ContextAwareExecutor executorWrapper = new ContextAwareExecutorImpl(builder);
    setWaitForClose();
    executorWrapper.executeWithDefaultContext(() -> {
        HostAndPort hostAndPort = options.get(Lsp);
        try {
            Context context = builder.build();
            context.enter();
            Instrument instrument = context.getEngine().getInstruments().get(ID);
            EnvironmentProvider envProvider = instrument.lookup(EnvironmentProvider.class);
            truffleAdapter.register(envProvider.getEnvironment(), executorWrapper);
            InetSocketAddress socketAddress = hostAndPort.createSocket();
            int port = socketAddress.getPort();
            Integer backlog = options.get(SocketBacklogSize);
            InetAddress address = socketAddress.getAddress();
            ServerSocket serverSocket = new ServerSocket(port, backlog, address);
            List<Pair<String, SocketAddress>> delegates = createDelegateSockets(options.get(Delegates));
            LanguageServerImpl.create(truffleAdapter, info, err).start(serverSocket, delegates).thenRun(() -> {
                try {
                    executorWrapper.executeWithDefaultContext(() -> {
                        context.leave();
                        return null;
                    }).get();
                } catch (ExecutionException | InterruptedException ex) {
                }
                executorWrapper.shutdown();
                notifyClose();
            }).exceptionally((throwable) -> {
                throwable.printStackTrace(err);
                notifyClose();
                return null;
            });
        } catch (ThreadDeath td) {
            throw td;
        } catch (Throwable e) {
            String message = String.format("[Graal LSP] Starting server on %s failed: %s", hostAndPort.getHostPort(), e.getLocalizedMessage());
            new LSPIOException(message, e).printStackTrace(err);
        }
        return null;
    });
    return truffleAdapter;
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) Context(org.graalvm.polyglot.Context) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) TruffleAdapter(org.graalvm.tools.lsp.server.TruffleAdapter) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) LSPIOException(org.graalvm.tools.lsp.exceptions.LSPIOException) InetAddress(java.net.InetAddress) ContextAwareExecutor(org.graalvm.tools.lsp.server.ContextAwareExecutor) Pair(org.graalvm.collections.Pair)

Aggregations

EventContext (com.oracle.truffle.api.instrumentation.EventContext)1 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Pair (org.graalvm.collections.Pair)1 Context (org.graalvm.polyglot.Context)1 Instrument (org.graalvm.polyglot.Instrument)1 LSPIOException (org.graalvm.tools.lsp.exceptions.LSPIOException)1 ContextAwareExecutor (org.graalvm.tools.lsp.server.ContextAwareExecutor)1 TruffleAdapter (org.graalvm.tools.lsp.server.TruffleAdapter)1