Apache Commons JEXL Overview ? Apache Commons JEXL (original) (raw)

Java Expression Language (JEXL)

JEXL is a library intended to facilitate the implementation of dynamic and scripting features in applications and frameworks written in Java.

JEXL implements an Expression Language based on some extensions to the JSTL Expression Language supporting most of the constructs seen in shell-script or ECMAScript.
Its goal is to expose scripting features usable by technical operatives or consultants working with enterprise platforms. In many use cases, JEXL allows end-users of an application to code their own scripts or expressions and ensure their execution within controlled functional constraints.

The library exposes a small footprint API - the core features fit in 3 classes and 10 methods - that can be used in various conditions:

JEXL name stands for Java EXpression Language, a simple expression language originally inspired by Apache Velocity and the Expression Language defined in the JavaServer Pages Standard Tag Library version 1.1 (JSTL) and JavaServer Pages version 2.0 (JSP). JEXL 2.0 added features inspired byUnified EL. The syntax is now close to a mix of ECMAScript and "shell-script" making it easy to master by technical operatives or consultants. The objects exposed and their behavior obviously need to be documented though...

The API and the expression language exploit Java-beans naming patterns through introspection to expose property getters and setters. It also considers public class fields as properties and allows to invoke any accessible method.

A Detailed Example

To create expressions and scripts, aJexlEngine is required. To instantiate one, a JexlBuilder is needed to describe the allowed JexlPermissions and JexlFeatures that will determine which classes and methods scripts can access and call and which syntactic elements scripts are allowed to use. Do not overlook this configuration aspect, especially the permissions since security of your application might depend on it. Once built, the JEXL engine should be stored, shared and reused. It is thread-safe ; so are the scripts during evaluation.

When evaluating expressions, JEXL merges anJexlExpression or aJexlScript with aJexlContext. In its simplest form, a script is created usingJexlEngine#createExpression(), passing a String containing valid JEXL syntax. A simple JexlContext can be created by instantiating aMapContext; a map of variables that will be internally wrapped can be optionally provided through its constructor.

JEXL's intention is a tight integration with its hosting platform; the scripting syntax is very close to JScript but leverages (potentially) any public class or method that Java exposes. How tight and how rich this integration is up to you; deriving JEXL API classes - most notably JexlPermissions, JexlContext, JexlArithmetic - are the means to that end.

The following example illustrate these aspects. It uses a specific set of permissions to allow using URI class and a tailored context to expose streams in a convenient manner.

/**

}

Extensions to JSTL Expression Language

JEXL attempts to bring some of the lessons learned by the Velocity community about expression languages in templating to a wider audience.Commons Jelly needed Velocity-ish method access, it just had to have it.

It must be noted that JEXL is not a compatible implementation of EL as defined in JSTL 1.1 (JSR-052) or JSP 2.0 (JSR-152). For a compatible implementation of these specifications, see the Commons EL project.

While JEXL 3.3 is now closer to JScript (without prototypes), its roots are the expression language defined in JSTL and its has improved upon its syntax in a few areas:

JEXL is not a product of the Java Community Process (JCP), but it provides a similar expression syntax. For more information about JSP 2.0 EL and JSTL 1.1 EL:

Velocity

Apache Velocity implements a similar expression language.

In particular the References section of the User Guide has some good information on properties and method which correlate directly to JEXL.

Projects Using Commons JEXL