private MahoutDriver() {
}
public static void main(String[] args) throws Throwable {
ProgramDriver programDriver = new ProgramDriver();
Properties mainClasses = loadProperties("driver.classes.props");
if (mainClasses == null) {
mainClasses = loadProperties("driver.classes.default.props");
}
if (mainClasses == null) {
throw new IOException("Can't load any properties file?");
}
boolean foundShortName = false;
for (Object key : mainClasses.keySet()) {
String keyString = (String) key;
if (args.length > 0 && shortName(mainClasses.getProperty(keyString)).equals(args[0])) {
foundShortName = true;
}
addClass(programDriver, keyString, mainClasses.getProperty(keyString));
}
if (args.length < 1 || args[0] == null || "-h".equals(args[0]) || "--help".equals(args[0])) {
programDriver.driver(args);
}
String progName = args[0];
if (!foundShortName) {
addClass(programDriver, progName, progName);
}
shift(args);
Properties mainProps = loadProperties(progName + ".props");
if (mainProps == null) {
log.warn("No " + progName + ".props found on classpath, will use command-line arguments only");
mainProps = new Properties();
}
Map<String,String[]> argMap = new HashMap<String,String[]>();
int i = 0;
while (i < args.length && args[i] != null) {
List<String> argValues = new ArrayList<String>();
String arg = args[i];
i++;
if (arg.startsWith("-D")) { // '-Dkey=value' or '-Dkey=value1,value2,etc' case
String[] argSplit = arg.split("=");
arg = argSplit[0];
if (argSplit.length == 2) {
argValues.add(argSplit[1]);
}
} else { // '-key [values]' or '--key [values]' case.
while (i < args.length && args[i] != null) {
if (args[i].startsWith("-")) {
break;
}
argValues.add(args[i]);
i++;
}
}
argMap.put(arg, argValues.toArray(new String[argValues.size()]));
}
// Add properties from the .props file that are not overridden on the command line
for (String key : mainProps.stringPropertyNames()) {
String[] argNamePair = key.split("\\|");
String shortArg = '-' + argNamePair[0].trim();
String longArg = argNamePair.length < 2 ? null : "--" + argNamePair[1].trim();
if (!argMap.containsKey(shortArg) && (longArg == null || !argMap.containsKey(longArg))) {
argMap.put(longArg, new String[] {mainProps.getProperty(key)});
}
}
// Now add command-line args
List<String> argsList = new ArrayList<String>();
argsList.add(progName);
for (Map.Entry<String,String[]> entry : argMap.entrySet()) {
String arg = entry.getKey();
if (arg.startsWith("-D")) { // arg is -Dkey - if value for this !isEmpty(), then arg -> -Dkey + "=" + value
String[] argValues = entry.getValue();
if (argValues.length > 0 && !argValues[0].trim().isEmpty()) {
arg += '=' + argValues[0].trim();
}
argsList.add(1, arg);
} else {
argsList.add(arg);
for (String argValue : Arrays.asList(argMap.get(arg))) {
if (argValue.length() > 0) {
argsList.add(argValue);
}
}
}
}
long start = System.currentTimeMillis();
programDriver.driver(argsList.toArray(new String[argsList.size()]));
if (log.isInfoEnabled()) {
log.info("Program took {} ms", System.currentTimeMillis() - start);
}
}