🤬
  • Add RemoteServerLoader to load and run all language servers specified as well as add command helper class.

    PiperOrigin-RevId: 466727834
    Change-Id: Ieb3696d781e6a0a73a13168bfb03a5f5fe4ad8b9
  • Loading...
  • John Y. Kim committed with Copybara-Service 2 years ago
    56ced2d6
    1 parent afda7792
  • ■ ■ ■ ■ ■ ■
    main/build.gradle
    skipped 13 lines
    14 14   compile deps.flogger_google_ext
    15 15   compile deps.guava
    16 16   compile deps.jsoup
     17 + compile deps.autovalue
    17 18   runtime deps.jaxb_runtime
     19 + annotationProcessor deps.autovalue_annotation_processor
    18 20   
    19 21   testCompile deps.junit
    20 22   testCompile deps.mockito
    skipped 12 lines
  • ■ ■ ■ ■ ■ ■
    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 + 
  • ■ ■ ■ ■ ■ ■
    main/src/main/java/com/google/tsunami/main/cli/server/RemoteServerLoaderModule.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 com.google.common.collect.ImmutableList;
     19 +import com.google.inject.AbstractModule;
     20 +import com.google.inject.Provides;
     21 +import java.util.List;
     22 + 
     23 +/** Installs {@link RemoteServerLoaderModule}. */
     24 +public final class RemoteServerLoaderModule extends AbstractModule {
     25 + 
     26 + private final ImmutableList<ServerPortCommand> commands;
     27 + 
     28 + public RemoteServerLoaderModule(ImmutableList<ServerPortCommand> commands) {
     29 + this.commands = commands;
     30 + }
     31 + 
     32 + @Provides
     33 + @RemoteServerLoader.ServerPortCommands
     34 + List<ServerPortCommand> provideServerPortCommands() {
     35 + return commands;
     36 + }
     37 +}
     38 + 
  • ■ ■ ■ ■ ■ ■
    main/src/main/java/com/google/tsunami/main/cli/server/ServerPortCommand.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 com.google.auto.value.AutoValue;
     19 + 
     20 +/** Command to spawn a language server and associated port. */
     21 +@AutoValue
     22 +public abstract class ServerPortCommand {
     23 + public static ServerPortCommand create(String serverCommand, String port) {
     24 + return new AutoValue_ServerPortCommand(serverCommand, port);
     25 + }
     26 + 
     27 + public abstract String serverCommand();
     28 + 
     29 + public abstract String port();
     30 +}
     31 + 
  • ■ ■ ■ ■ ■ ■
    main/src/test/java/com/google/tsunami/main/cli/server/RemoteServerLoaderTest.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.truth.Truth.assertThat;
     19 + 
     20 +import com.google.common.collect.ImmutableList;
     21 +import com.google.inject.AbstractModule;
     22 +import com.google.inject.Guice;
     23 +import org.junit.Test;
     24 +import org.junit.runner.RunWith;
     25 +import org.junit.runners.JUnit4;
     26 + 
     27 +@RunWith(JUnit4.class)
     28 +public final class RemoteServerLoaderTest {
     29 + 
     30 + @Test
     31 + public void runServerProcess_whenPathExistsAndNormalPort_returnsValidProcessList() {
     32 + ImmutableList<ServerPortCommand> commands =
     33 + ImmutableList.of(ServerPortCommand.create("/bin/sh", "34567"));
     34 + RemoteServerLoader loader =
     35 + Guice.createInjector(
     36 + new AbstractModule() {
     37 + @Override
     38 + protected void configure() {
     39 + install(new RemoteServerLoaderModule(commands));
     40 + }
     41 + })
     42 + .getInstance(RemoteServerLoader.class);
     43 + var processList = loader.runServerProcesses();
     44 + assertThat(processList).hasSize(1);
     45 + assertThat(processList.get(0)).isNotNull();
     46 + }
     47 +}
     48 + 
Please wait...
Page is in error, reload to recover