use of org.apache.commons.jexl.Expression in project com.revolsys.open by revolsys.
the class ScriptTool method processArguments.
public boolean processArguments(final String[] args) {
try {
loadProperties("script.properties");
final CommandLineParser parser = new PosixParser();
this.commandLine = parser.parse(this.options, args);
final Option[] options = this.commandLine.getOptions();
for (final Option option : options) {
final String shortOpt = option.getOpt();
if (shortOpt != null && shortOpt.equals("D")) {
final Properties properties = this.commandLine.getOptionProperties("D");
for (final Entry<Object, Object> property : properties.entrySet()) {
final String key = (String) property.getKey();
final String value = (String) property.getValue();
this.parameters.put(key, value);
System.setProperty(key, value);
ThreadSharedProperties.setProperty(key, value);
}
}
}
if (this.commandLine.hasOption(SCRIPT_OPTION)) {
if (!setScriptFileName(this.commandLine.getOptionValue(SCRIPT_OPTION))) {
return false;
}
}
if (this.commandLine.hasOption(PROPERTIES_OPTION)) {
this.propertiesName = this.commandLine.getOptionValue(PROPERTIES_OPTION);
try {
final File propertiesFile = new File(this.propertiesName);
if (propertiesFile.exists()) {
final InputStream in = new FileInputStream(propertiesFile);
loadProperties(this.propertiesName, in);
} else {
if (!loadProperties(this.propertiesName)) {
System.err.println("Properties file '" + this.propertiesName + "' does not exist");
return false;
}
}
} catch (final IOException e) {
System.err.println("Properties file '" + this.propertiesName + "' could not be read:" + e.getMessage());
return false;
}
}
if (this.commandLine.hasOption(LOG_FILE_OPTION)) {
this.logFile = new File(this.commandLine.getOptionValue(LOG_FILE_OPTION));
final File logDirectory = this.logFile.getParentFile();
if (!logDirectory.exists()) {
if (!logDirectory.mkdirs()) {
System.err.println("Unable to create Log directory '" + logDirectory.getAbsolutePath() + "'");
return false;
}
}
} else {
String logFileName = System.getProperty("logFile");
if (logFileName != null) {
try {
while (logFileName.contains("${")) {
final Expression expression = JexlUtil.newExpression(logFileName);
final HashMapContext context = new HashMapContext();
context.setVars(ThreadSharedProperties.getProperties());
logFileName = (String) JexlUtil.evaluateExpression(context, expression);
}
} catch (final Exception e) {
e.printStackTrace();
logFileName = null;
}
}
}
if (this.logFile != null) {
if (this.logFile.exists() && !this.logFile.isFile()) {
System.err.println("Log file '" + this.logFile.getAbsolutePath() + "' is not a file");
return false;
}
System.setProperty("logFile", this.logFile.getAbsolutePath());
}
if (this.commandLine.hasOption(VERSION_OPTION)) {
displayVersion();
return false;
}
if (this.scriptFileName == null) {
final String[] extraArgs = this.commandLine.getArgs();
if (extraArgs.length > 0) {
if (!setScriptFileName(extraArgs[0])) {
return false;
}
}
}
return true;
} catch (final MissingOptionException e) {
if (this.commandLine.hasOption(VERSION_OPTION)) {
displayVersion();
} else {
System.err.println("Missing " + e.getMessage() + " argument");
}
return false;
} catch (final ParseException e) {
System.err.println("Unable to process command line arguments: " + e.getMessage());
return false;
}
}
Aggregations