■ ■ ■ ■ ■ ■
main/src/main/java/com/google/tsunami/main/cli/LanguageServerOptions.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; |
| 17 | + | |
| 18 | + | import com.beust.jcommander.Parameter; |
| 19 | + | import com.beust.jcommander.ParameterException; |
| 20 | + | import com.beust.jcommander.Parameters; |
| 21 | + | import com.google.tsunami.common.cli.CliOption; |
| 22 | + | import com.google.tsunami.common.data.NetworkEndpointUtils; |
| 23 | + | import java.nio.file.Files; |
| 24 | + | import java.nio.file.Paths; |
| 25 | + | import java.util.List; |
| 26 | + | |
| 27 | + | /** Command line arguments for Tsunami language servers. */ |
| 28 | + | @Parameters(separators = "=") |
| 29 | + | public final class LanguageServerOptions implements CliOption { |
| 30 | + | |
| 31 | + | @Parameter( |
| 32 | + | names = "--plugin-server-paths", |
| 33 | + | description = "The filename of the language server to run language-spceific plugins.") |
| 34 | + | public List<String> pluginServerFilenames; |
| 35 | + | |
| 36 | + | @Parameter( |
| 37 | + | names = "--plugin-server-ports", |
| 38 | + | description = |
| 39 | + | "The port of the plugin server to open connection with. If not enough ports were" |
| 40 | + | + " specified for the number of language servers specified, an open port will be" |
| 41 | + | + " chosen.") |
| 42 | + | public List<String> pluginServerPorts; |
| 43 | + | |
| 44 | + | @Override |
| 45 | + | public void validate() { |
| 46 | + | if (pluginServerFilenames != null || pluginServerPorts != null) { |
| 47 | + | if (pluginServerFilenames != null && !pluginServerFilenames.isEmpty()) { |
| 48 | + | for (String pluginServerFilename : pluginServerFilenames) { |
| 49 | + | if (!Files.exists(Paths.get(pluginServerFilename))) { |
| 50 | + | throw new ParameterException( |
| 51 | + | String.format("Language server path %s does not exist", pluginServerFilename)); |
| 52 | + | } |
| 53 | + | } |
| 54 | + | } |
| 55 | + | |
| 56 | + | if (pluginServerPorts != null && !pluginServerPorts.isEmpty()) { |
| 57 | + | for (String pluginServerPort : pluginServerPorts) { |
| 58 | + | try { |
| 59 | + | var port = Integer.parseInt(pluginServerPort); |
| 60 | + | if (!(port <= NetworkEndpointUtils.MAX_PORT_NUMBER && port > 0)) { |
| 61 | + | throw new ParameterException( |
| 62 | + | String.format( |
| 63 | + | "Port out of range. Expected [0, %s], actual %s.", |
| 64 | + | NetworkEndpointUtils.MAX_PORT_NUMBER, pluginServerPort)); |
| 65 | + | } |
| 66 | + | } catch (NumberFormatException e) { |
| 67 | + | throw new ParameterException( |
| 68 | + | String.format("Port number must be an integer. Got %s instead.", pluginServerPort), |
| 69 | + | e); |
| 70 | + | } |
| 71 | + | } |
| 72 | + | } |
| 73 | + | |
| 74 | + | if ((pluginServerFilenames != null && pluginServerPorts == null) |
| 75 | + | || (pluginServerFilenames == null && pluginServerPorts != null) |
| 76 | + | || (pluginServerFilenames.size() != pluginServerPorts.size())) { |
| 77 | + | throw new ParameterException( |
| 78 | + | String.format( |
| 79 | + | "Number of plugin server paths must be equal to number of plugin server ports." |
| 80 | + | + " Paths: %s. Ports: %s.", |
| 81 | + | pluginServerFilenames.size(), pluginServerPorts.size())); |
| 82 | + | } |
| 83 | + | } |
| 84 | + | } |
| 85 | + | } |
| 86 | + | |