Search in sources :

Example 11 with Expression

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;
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) ThreadSharedProperties(com.revolsys.collection.map.ThreadSharedProperties) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertyBatchUpdateException(org.springframework.beans.PropertyBatchUpdateException) PropertyAccessException(org.springframework.beans.PropertyAccessException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) IOException(java.io.IOException) MethodInvocationException(org.springframework.beans.MethodInvocationException) MissingOptionException(org.apache.commons.cli.MissingOptionException) ParseException(org.apache.commons.cli.ParseException) Expression(org.apache.commons.jexl.Expression) HashMapContext(org.apache.commons.jexl.context.HashMapContext) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) MissingOptionException(org.apache.commons.cli.MissingOptionException)

Aggregations

Expression (org.apache.commons.jexl.Expression)11 HashMapContext (org.apache.commons.jexl.context.HashMapContext)4 ThreadInterruptedException (com.revolsys.parallel.ThreadInterruptedException)2 ClosedException (com.revolsys.parallel.channel.ClosedException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 JexlContext (org.apache.commons.jexl.JexlContext)2 ThreadSharedProperties (com.revolsys.collection.map.ThreadSharedProperties)1 ScriptExecutorRunnable (com.revolsys.parallel.tools.ScriptExecutorRunnable)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Future (java.util.concurrent.Future)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1