Search in sources :

Example 31 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project syncope by apache.

the class JexlUtils method evaluate.

public static String evaluate(final String expression, final JexlContext jexlContext) {
    String result = StringUtils.EMPTY;
    if (StringUtils.isNotBlank(expression) && jexlContext != null) {
        try {
            JexlExpression jexlExpression = getEngine().createExpression(expression);
            Object evaluated = jexlExpression.evaluate(jexlContext);
            if (evaluated != null) {
                result = evaluated.toString();
            }
        } catch (Exception e) {
            LOG.error("Error while evaluating JEXL expression: " + expression, e);
        }
    } else {
        LOG.debug("Expression not provided or invalid context");
    }
    return result;
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression) JexlException(org.apache.commons.jexl3.JexlException) IntrospectionException(java.beans.IntrospectionException)

Example 32 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression 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 JexlExpression expression = JexlUtil.newExpression(logFileName);
                        final MapContext context = new MapContext(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) MapContext(org.apache.commons.jexl3.MapContext) 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) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) JexlExpression(org.apache.commons.jexl3.JexlExpression) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) MissingOptionException(org.apache.commons.cli.MissingOptionException)

Example 33 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project syncope by apache.

the class JexlUtils method evaluate.

public static String evaluate(final String expression, final JexlContext jexlContext) {
    String result = StringUtils.EMPTY;
    if (StringUtils.isNotBlank(expression) && jexlContext != null) {
        try {
            JexlExpression jexlExpression = getEngine().createExpression(expression);
            Object evaluated = jexlExpression.evaluate(jexlContext);
            if (evaluated != null) {
                result = evaluated.toString();
            }
        } catch (Exception e) {
            LOG.error("Error while evaluating JEXL expression: " + expression, e);
        }
    } else {
        LOG.debug("Expression not provided or invalid context");
    }
    return result;
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression) JexlException(org.apache.commons.jexl3.JexlException) IntrospectionException(java.beans.IntrospectionException)

Example 34 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project com.revolsys.open by revolsys.

the class JavaComponent method includeComponent.

@Override
public void includeComponent(final PageContext context) throws ServletException, IOException {
    Object instance;
    try {
        instance = this.componentClass.newInstance();
    } catch (final Exception e) {
        throw new ServletException("Unable to create component instance", e);
    }
    try {
        final Iterator propertyNames = this.properties.keySet().iterator();
        while (propertyNames.hasNext()) {
            final String propertyName = (String) propertyNames.next();
            Object value = this.properties.get(propertyName);
            final WebUiContext niceContext = WebUiContext.get();
            try {
                final JexlExpression expression = JexlUtil.newExpression(value.toString());
                if (expression != null) {
                    value = niceContext.evaluateExpression(expression);
                }
            } catch (final Exception e) {
                throw new ServletException(e.getMessage(), e);
            }
            this.setPropertyMethod.invoke(instance, new Object[] { propertyName, value });
        }
    } catch (final IllegalAccessException e) {
        log.error("Unable to set component properties", e.getCause());
        throw new ServletException("Unable to set component properties", e);
    } catch (final InvocationTargetException e) {
        log.error("Unable to set component properties", e.getCause());
        throw new ServletException("Unable to set component properties", e.getCause());
    }
    try {
        final Writer out = context.getOut();
        this.serializeMethod.invoke(instance, new Object[] { out });
    } catch (final IllegalAccessException e) {
        throw new ServletException("Unable to serialize component", e);
    } catch (final InvocationTargetException e) {
        final Throwable cause = e.getCause();
        log.error(cause.getMessage(), cause);
        if (cause instanceof IOException) {
            throw (IOException) cause;
        }
        throw new ServletException("Unable to serialize component", cause);
    }
}
Also used : ServletException(javax.servlet.ServletException) Iterator(java.util.Iterator) JexlExpression(org.apache.commons.jexl3.JexlExpression) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Writer(java.io.Writer)

Example 35 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project com.revolsys.open by revolsys.

the class MenuItem method setTitle.

public void setTitle(final String title) {
    if (title != null) {
        JexlExpression titleExpression = null;
        try {
            titleExpression = JexlUtil.newExpression(title);
        } catch (final Exception e) {
            log.error(e.getMessage(), e);
        }
        if (titleExpression == null) {
            this.title = title;
            this.titleExpression = null;
        } else {
            this.title = null;
            this.titleExpression = titleExpression;
        }
    } else {
        this.title = null;
        this.titleExpression = null;
    }
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression)

Aggregations

JexlExpression (org.apache.commons.jexl3.JexlExpression)43 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)18 MapContext (org.apache.commons.jexl3.MapContext)18 JexlEngine (org.apache.commons.jexl3.JexlEngine)17 JexlContext (org.apache.commons.jexl3.JexlContext)13 Test (org.junit.jupiter.api.Test)10 Test (org.junit.Test)9 JexlException (org.apache.commons.jexl3.JexlException)8 HashMap (java.util.HashMap)4 DBException (org.jkiss.dbeaver.DBException)4 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Engine (org.apache.commons.jexl3.internal.Engine)2 IOUtilities.readAsString (org.finos.waltz.common.IOUtilities.readAsString)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)2 ThreadSharedProperties (com.revolsys.collection.map.ThreadSharedProperties)1 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)1