-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[grid] Remove some duplication from Grid cli commands
- Loading branch information
Showing
14 changed files
with
529 additions
and
635 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
91 changes: 91 additions & 0 deletions
91
java/server/src/org/openqa/selenium/grid/TemplateGridCommand.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,91 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.grid; | ||
|
||
import com.beust.jcommander.JCommander; | ||
import com.beust.jcommander.ParameterException; | ||
import org.openqa.selenium.cli.CliCommand; | ||
import org.openqa.selenium.grid.config.AnnotatedConfig; | ||
import org.openqa.selenium.grid.config.CompoundConfig; | ||
import org.openqa.selenium.grid.config.ConcatenatingConfig; | ||
import org.openqa.selenium.grid.config.Config; | ||
import org.openqa.selenium.grid.config.ConfigFlags; | ||
import org.openqa.selenium.grid.config.EnvConfig; | ||
import org.openqa.selenium.grid.log.LoggingOptions; | ||
import org.openqa.selenium.grid.server.HelpFlags; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
public abstract class TemplateGridCommand implements CliCommand { | ||
|
||
@Override | ||
public final Executable configure(String... args) { | ||
Set<Object> allFlags = getFlagObjects(); | ||
|
||
HelpFlags helpFlags = new HelpFlags(); | ||
ConfigFlags configFlags = new ConfigFlags(); | ||
|
||
JCommander.Builder builder = JCommander.newBuilder() | ||
.programName(getName()) | ||
.addObject(configFlags) | ||
.addObject(helpFlags); | ||
allFlags.forEach(builder::addObject); | ||
JCommander commander = builder.build(); | ||
|
||
return () -> { | ||
try { | ||
commander.parse(args); | ||
} catch (ParameterException e) { | ||
System.err.println(e.getMessage()); | ||
commander.usage(); | ||
return; | ||
} | ||
|
||
if (helpFlags.displayHelp(commander, System.out)) { | ||
return; | ||
} | ||
|
||
Set<Config> allConfigs = new LinkedHashSet<>(); | ||
allConfigs.add(new EnvConfig()); | ||
allConfigs.add(new ConcatenatingConfig(getSystemPropertiesConfigPrefix(), '.', System.getProperties())); | ||
allFlags.forEach(flags -> allConfigs.add(new AnnotatedConfig(flags))); | ||
allConfigs.add(configFlags.readConfigFiles()); | ||
allConfigs.add(getDefaultConfig()); | ||
|
||
Config config = new CompoundConfig(allConfigs.toArray(Config[]::new)); | ||
|
||
if (helpFlags.dumpConfig(config, System.out)) { | ||
return; | ||
} | ||
|
||
LoggingOptions loggingOptions = new LoggingOptions(config); | ||
loggingOptions.configureLogging(); | ||
|
||
execute(config); | ||
}; | ||
} | ||
|
||
protected abstract Set<Object> getFlagObjects(); | ||
|
||
protected abstract String getSystemPropertiesConfigPrefix(); | ||
|
||
protected abstract Config getDefaultConfig(); | ||
|
||
protected abstract void execute(Config config) throws Exception; | ||
} |
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.