■ ■ ■ ■ ■ ■
main/src/main/java/com/google/tsunami/main/cli/server/RemoteServerLoader.java
| 1 | + | /* |
| 2 | + | * Copyright 2022 Google LLC |
| 3 | + | * |
| 4 | + | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + | * you may not use this file except in compliance with the License. |
| 6 | + | * You may obtain a copy of the License at |
| 7 | + | * |
| 8 | + | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + | * |
| 10 | + | * Unless required by applicable law or agreed to in writing, software |
| 11 | + | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + | * See the License for the specific language governing permissions and |
| 14 | + | * limitations under the License. |
| 15 | + | */ |
| 16 | + | package com.google.tsunami.main.cli.server; |
| 17 | + | |
| 18 | + | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | + | import static com.google.common.collect.ImmutableList.toImmutableList; |
| 20 | + | import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 21 | + | |
| 22 | + | import com.google.common.collect.ImmutableList; |
| 23 | + | import com.google.common.flogger.GoogleLogger; |
| 24 | + | import com.google.tsunami.common.command.CommandExecutor; |
| 25 | + | import com.google.tsunami.common.command.CommandExecutorFactory; |
| 26 | + | import java.io.IOException; |
| 27 | + | import java.lang.annotation.Retention; |
| 28 | + | import java.util.List; |
| 29 | + | import java.util.Optional; |
| 30 | + | import java.util.concurrent.ExecutionException; |
| 31 | + | import javax.inject.Inject; |
| 32 | + | import javax.inject.Qualifier; |
| 33 | + | |
| 34 | + | /** Loader to run language servers. */ |
| 35 | + | public class RemoteServerLoader { |
| 36 | + | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
| 37 | + | |
| 38 | + | private final List<ServerPortCommand> commands; |
| 39 | + | |
| 40 | + | @Inject |
| 41 | + | RemoteServerLoader(@ServerPortCommands List<ServerPortCommand> commands) { |
| 42 | + | this.commands = checkNotNull(commands); |
| 43 | + | } |
| 44 | + | |
| 45 | + | public ImmutableList<Process> runServerProcesses() { |
| 46 | + | logger.atInfo().log("Starting language server processes..."); |
| 47 | + | return commands.stream() |
| 48 | + | .map( |
| 49 | + | command -> |
| 50 | + | runProcess( |
| 51 | + | CommandExecutorFactory.create( |
| 52 | + | command.serverCommand(), "--port=" + command.port()))) |
| 53 | + | .filter(Optional::isPresent) |
| 54 | + | .map(Optional::get) |
| 55 | + | .collect(toImmutableList()); |
| 56 | + | } |
| 57 | + | |
| 58 | + | private Optional<Process> runProcess(CommandExecutor executor) { |
| 59 | + | try { |
| 60 | + | return Optional.of(executor.executeWithNoStreamCollection()); |
| 61 | + | } catch (IOException | InterruptedException | ExecutionException e) { |
| 62 | + | logger.atWarning().withCause(e).log("Could not execute language server binary."); |
| 63 | + | } |
| 64 | + | return Optional.empty(); |
| 65 | + | } |
| 66 | + | |
| 67 | + | /** Guice interface for injecting {@link ServerPortCommand} object lists. */ |
| 68 | + | @Qualifier |
| 69 | + | @Retention(RUNTIME) |
| 70 | + | public @interface ServerPortCommands {} |
| 71 | + | } |
| 72 | + | |