private MahoutDriver() {
}
public static void main(String[] args) throws Throwable {
ProgramDriver programDriver = new ProgramDriver();
Properties mainClasses = new Properties();
InputStream propsStream = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("driver.classes.props");
try {
mainClasses.load(propsStream);
} catch (Throwable e) {
//try getting the default one
propsStream = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("driver.classes.default.props");
mainClasses.load(propsStream);
}
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 || args[0].equals("-h") || args[0].equals("--help")) {
programDriver.driver(args);
}
String progName = args[0];
if (!foundShortName) {
addClass(programDriver, progName, progName);
}
shift(args);
InputStream defaultsStream = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream(progName + ".props");
Properties mainProps = new Properties();
if (defaultsStream != null) { // can't find props file, use empty props.
mainProps.load(defaultsStream);
} else {
log.warn("No " + progName + ".props found on classpath, will use command-line arguments only");
}
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.length() > 2 && arg.charAt(1) == '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].length() > 0 && args[i].charAt(0) != '-') {
argValues.add(args[i]);
i++;
} else {
break;
}
}
}
argMap.put(arg, argValues.toArray(new String[argValues.size()]));
}
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)});
}
}
List<String> argsList = new ArrayList<String>();
argsList.add(progName);
for (String arg : argMap.keySet()) {
if (arg.startsWith("-D")) { // arg is -Dkey - if value for this !isEmpty(), then arg -> -Dkey + "=" + value
if (argMap.get(arg).length > 0 && !argMap.get(arg)[0].trim().isEmpty()) {
arg += '=' + argMap.get(arg)[0].trim();
}
}
argsList.add(arg);
if (!arg.startsWith("-D")) {
argsList.addAll(Arrays.asList(argMap.get(arg)));
}
}
long start = System.currentTimeMillis();
programDriver.driver(argsList.toArray(new String[argsList.size()]));
long finish = System.currentTimeMillis();
if (log.isInfoEnabled()) {
log.info("Program took " + (finish - start) + " ms");
}
}