forked from eclipse-glsp/glsp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eclipse-glsp#95 Cleanup GLSPServerLauncher & add CLI parsing (eclipse…
…-glsp#69) * eclipse-glsp#95 Cleanup GLSPServerLauncher & add CLI parsing - Cleanup GLSPServerLauncher (refactor methods & add support to inject additional modules) - Add a CLIParser helper class and configure default options for configuring -- Server port -- consoleLog flag -- logging directory -- logLevel -- help Fixes eclipse-glsp/glsp/issues/95
- Loading branch information
Showing
10 changed files
with
331 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
plugins/org.eclipse.glsp.server/src/org/eclipse/glsp/server/launch/CLIParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2020 EclipseSource and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
package org.eclipse.glsp.server.launch; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.DefaultParser; | ||
import org.apache.commons.cli.Options; | ||
import org.apache.commons.cli.ParseException; | ||
import org.apache.log4j.Logger; | ||
import org.eclipse.glsp.server.utils.LaunchUtil; | ||
|
||
public abstract class CLIParser { | ||
private static final Logger LOG = Logger.getLogger(CLIParser.class); | ||
protected static final String INVALID_ARGUMENT_MESSAGE = "%s' is not a valid argument for option '--%s'! The default value '%s' is used."; | ||
|
||
protected final CommandLine cmd; | ||
protected final Options options; | ||
protected final String processName; | ||
|
||
public CLIParser(final String[] args, final Options options, final String processName) throws ParseException { | ||
this.cmd = new DefaultParser().parse(options, args); | ||
this.options = options; | ||
this.processName = processName; | ||
} | ||
|
||
public boolean hasOption(final String optionName) { | ||
return cmd.hasOption(optionName); | ||
} | ||
|
||
public String parseOption(final String optionName, final String defaultValue) { | ||
return parseOption(optionName, defaultValue, null); | ||
} | ||
|
||
public String parseOption(final String optionName, final String defaultValue, final Predicate<String> validator) { | ||
String arg = cmd.getOptionValue(optionName); | ||
if (arg != null) { | ||
if (validator == null || validator.test(arg)) { | ||
return arg; | ||
} | ||
LOG.warn(String.format(INVALID_ARGUMENT_MESSAGE, | ||
arg, optionName, defaultValue)); | ||
} | ||
return defaultValue; | ||
} | ||
|
||
public int parseIntOption(final String optionName, final int defaultValue) { | ||
return parseIntOption(optionName, defaultValue, null); | ||
} | ||
|
||
public int parseIntOption(final String optionName, final int defaultValue, final Predicate<Integer> validator) { | ||
String intArg = cmd.getOptionValue(optionName); | ||
int value = defaultValue; | ||
if (intArg != null) { | ||
try { | ||
value = Integer.parseInt(intArg); | ||
if (validator != null && !validator.test(value)) { | ||
throw new NumberFormatException(); | ||
} | ||
} catch (NumberFormatException e) { | ||
LOG.warn(String.format(INVALID_ARGUMENT_MESSAGE, | ||
intArg, optionName, defaultValue)); | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
public boolean parseBoolOption(final String optionName, final boolean defaultValue) { | ||
String arg = cmd.getOptionValue(optionName); | ||
return arg != null ? Boolean.parseBoolean(arg) : defaultValue; | ||
} | ||
|
||
public void printHelp() { | ||
LaunchUtil.printHelp(this.processName, options); | ||
} | ||
|
||
public CommandLine getCmd() { return cmd; } | ||
} |
85 changes: 85 additions & 0 deletions
85
plugins/org.eclipse.glsp.server/src/org/eclipse/glsp/server/launch/DefaultCLIParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2020 EclipseSource and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
package org.eclipse.glsp.server.launch; | ||
|
||
import java.io.File; | ||
import java.util.function.Predicate; | ||
|
||
import org.apache.commons.cli.Options; | ||
import org.apache.commons.cli.ParseException; | ||
import org.apache.log4j.Level; | ||
import org.eclipse.glsp.server.utils.LaunchUtil; | ||
import org.eclipse.glsp.server.utils.LaunchUtil.DefaultOptions; | ||
|
||
public class DefaultCLIParser extends CLIParser { | ||
public static final String OPTION_HELP = "help"; | ||
public static final String OPTION_PORT = "port"; | ||
public static final String OPTION_CONSOLE_LOG = "consoleLog"; | ||
public static final String OPTION_FILE_LOG = "fileLog"; | ||
public static final String OPTION_LOG_LEVEL = "logLevel"; | ||
public static final String OPTION_LOG_DIR = "logDir"; | ||
|
||
public DefaultCLIParser(final String[] args, final String processName) throws ParseException { | ||
this(args, getDefaultOptions(), processName); | ||
} | ||
|
||
public DefaultCLIParser(final String[] args, final Options options, final String processName) throws ParseException { | ||
super(args, options, processName); | ||
} | ||
|
||
public int parsePort() { | ||
Predicate<Integer> validator = (port) -> LaunchUtil.isValidPort(port); | ||
return parseIntOption(OPTION_PORT, DefaultOptions.SERVER_PORT, validator); | ||
} | ||
|
||
public String parseLogDir() { | ||
Predicate<String> validator = (logDirArg) -> { | ||
File file = new File(logDirArg); | ||
return file.exists() && file.isDirectory(); | ||
}; | ||
String logDir = parseOption(OPTION_LOG_DIR, DefaultOptions.LOG_DIR, validator); | ||
return new File(logDir).getAbsolutePath(); | ||
} | ||
|
||
public Level parseLogLevel() { | ||
String levelArg = parseOption(OPTION_LOG_LEVEL, DefaultOptions.LOG_LEVEL.toString()); | ||
return Level.toLevel(levelArg, DefaultOptions.LOG_LEVEL); | ||
} | ||
|
||
public boolean isConsoleLog() { return parseBoolOption(OPTION_CONSOLE_LOG, DefaultOptions.CONSOLE_LOG_ENABLED); } | ||
|
||
public boolean isFileLog() { return parseBoolOption(OPTION_FILE_LOG, DefaultOptions.FILE_LOG_ENABLED); } | ||
|
||
public boolean isHelp() { return hasOption(OPTION_HELP); } | ||
|
||
public static Options getDefaultOptions() { | ||
Options options = new Options(); | ||
options.addOption(null, OPTION_HELP, false, "Display usage information about GLSPServerLauncher"); | ||
options.addOption(null, OPTION_PORT, true, | ||
String.format("Set server port. [default='%s']", DefaultOptions.SERVER_PORT)); | ||
options.addOption(null, OPTION_CONSOLE_LOG, true, | ||
String.format("Enable/Disable console logging. [default='%s']", DefaultOptions.CONSOLE_LOG_ENABLED)); | ||
options.addOption(null, OPTION_FILE_LOG, true, | ||
String.format("Enable/Disable file logging. [default='%s']", DefaultOptions.FILE_LOG_ENABLED)); | ||
options.addOption(null, OPTION_LOG_DIR, true, | ||
String.format("Set the directory for log files (File logging has to be enabled). [default='%s']", | ||
DefaultOptions.LOG_DIR)); | ||
options.addOption(null, OPTION_LOG_LEVEL, true, | ||
String.format("Set the log level. [default='%s']", DefaultOptions.LOG_LEVEL)); | ||
return options; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.