1
|
package eu.dnetlib.dhp.common.java;
|
2
|
|
3
|
import java.util.HashMap;
|
4
|
import java.util.Map;
|
5
|
import java.util.Map.Entry;
|
6
|
import java.util.Properties;
|
7
|
import java.util.Set;
|
8
|
|
9
|
import org.apache.commons.cli.CommandLine;
|
10
|
import org.apache.hadoop.fs.Path;
|
11
|
|
12
|
/**
|
13
|
* Handles parsing parameters passed to the {@link Process}
|
14
|
* @author Mateusz Kobos
|
15
|
*
|
16
|
*/
|
17
|
public class CmdLineParserForProcessRunParameters {
|
18
|
/** Parse the command line arguments.
|
19
|
*
|
20
|
* @param cmdLine command line arguments
|
21
|
* @param ports names of ports that ought to be extracted from command line
|
22
|
*/
|
23
|
public ProcessParameters run(CommandLine cmdLine, Ports ports) {
|
24
|
|
25
|
Properties inputProperties = cmdLine.getOptionProperties(
|
26
|
CmdLineParser.inputPrefix);
|
27
|
assumePortNamesMatch(CmdLineParser.inputPrefix, inputProperties,
|
28
|
ports.getInput().keySet());
|
29
|
Map<String, Path> inputBindings = getBindings(
|
30
|
inputProperties, ports.getInput().keySet());
|
31
|
|
32
|
Properties outputProperties = cmdLine.getOptionProperties(
|
33
|
CmdLineParser.outputPrefix);
|
34
|
assumePortNamesMatch(CmdLineParser.outputPrefix, outputProperties,
|
35
|
ports.getOutput().keySet());
|
36
|
Map<String, Path> outputBindings = getBindings(
|
37
|
outputProperties, ports.getOutput().keySet());
|
38
|
|
39
|
PortBindings bindings = new PortBindings(inputBindings, outputBindings);
|
40
|
|
41
|
Properties specialProperties = cmdLine.getOptionProperties(
|
42
|
CmdLineParser.specialParametersPrefix);
|
43
|
assumeContainAllMandatoryParameters(
|
44
|
specialProperties, CmdLineParser.mandatorySpecialParameters);
|
45
|
|
46
|
Properties rawProperties = cmdLine.getOptionProperties(
|
47
|
CmdLineParser.processParametersPrefix);
|
48
|
Map<String, String> processParameters = new HashMap<String, String>();
|
49
|
for(Entry<Object, Object> entry: rawProperties.entrySet()){
|
50
|
processParameters.put(
|
51
|
(String)entry.getKey(), (String)entry.getValue());
|
52
|
}
|
53
|
|
54
|
return new ProcessParameters(bindings, processParameters);
|
55
|
}
|
56
|
|
57
|
private static void assumeContainAllMandatoryParameters(
|
58
|
Properties properties, String[] mandatoryParameters){
|
59
|
for(String otherParameter: mandatoryParameters){
|
60
|
if(!properties.containsKey(otherParameter)){
|
61
|
throw new CmdLineParserException(String.format(
|
62
|
"Not all mandatory properties are set using the \"%s\" "
|
63
|
+ "option are given, e.g. \"-%s\" parameter is missing",
|
64
|
CmdLineParser.specialParametersPrefix, otherParameter));
|
65
|
}
|
66
|
}
|
67
|
}
|
68
|
|
69
|
private static void assumePortNamesMatch(String cmdLineParamPrefix,
|
70
|
Properties cmdLineProperties, Set<String> portNames) {
|
71
|
for (String name : portNames) {
|
72
|
if (!cmdLineProperties.containsKey(name)) {
|
73
|
throw new CmdLineParserException(String.format(
|
74
|
"The port with name \"%s\" is not specified in "
|
75
|
+ "command line (command line option \"-%s\" is missing)",
|
76
|
name, cmdLineParamPrefix + name));
|
77
|
}
|
78
|
}
|
79
|
for (Object cmdLineKeyObject : cmdLineProperties.keySet()) {
|
80
|
String name = (String) cmdLineKeyObject;
|
81
|
if (!portNames.contains(name)) {
|
82
|
throw new CmdLineParserException(String.format(
|
83
|
"A port name \"%s\" which is not specified is given "
|
84
|
+ "in the command line "
|
85
|
+ "(command line option \"%s\" is excess)",
|
86
|
name, cmdLineParamPrefix + name));
|
87
|
}
|
88
|
}
|
89
|
}
|
90
|
|
91
|
private static Map<String, Path> getBindings(
|
92
|
Properties cmdLineProperties, Set<String> portNames) {
|
93
|
Map<String, Path> bindings = new HashMap<String, Path>();
|
94
|
for (String name : portNames) {
|
95
|
Path path = new Path((String) cmdLineProperties.get(name));
|
96
|
bindings.put(name, path);
|
97
|
}
|
98
|
return bindings;
|
99
|
}
|
100
|
}
|