本文整理汇总了Java中org.apache.commons.jexl3.JexlContext类的典型用法代码示例。如果您正苦于以下问题:Java JexlContext类的具体用法?Java JexlContext怎么用?Java JexlContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JexlContext类属于org.apache.commons.jexl3包,在下文中一共展示了JexlContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evaluate
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Override
public boolean evaluate(final VariableSource variableSource) {
if (expression.isPresent()) {
Set<String> vars = variableSource.getVariableSet();
JexlContext jc = new MapContext();
// load the values, if present, into the context
vars.forEach(variable -> variableSource.get(variable).ifPresent(value -> jc.set(variable, value)));
Object o = expression.get().evaluate(jc);
return (o instanceof Boolean) ? (Boolean) o : false;
}
else {
return true;
}
}
示例2: beforePropagation
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Override
public List<PlainAttrValue> beforePropagation(
final Item item,
final Entity entity,
final List<PlainAttrValue> values) {
if (StringUtils.isNotBlank(propagationJEXL) && values != null) {
values.forEach(value -> {
JexlContext jexlContext = new MapContext();
if (entity != null) {
JexlUtils.addFieldsToContext(entity, jexlContext);
if (entity instanceof Any) {
JexlUtils.addPlainAttrsToContext(((Any<?>) entity).getPlainAttrs(), jexlContext);
JexlUtils.addDerAttrsToContext(((Any<?>) entity), jexlContext);
}
}
jexlContext.set("value", value.getValueAsString());
value.setStringValue(JexlUtils.evaluate(propagationJEXL, jexlContext));
});
return values;
}
return values;
}
示例3: evaluateNAME
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
/**
* Build __NAME__ for propagation.
* First look if there is a defined connObjectLink for the given resource (and in
* this case evaluate as JEXL); otherwise, take given connObjectKey.
*
* @param any given any object
* @param provision external resource
* @param connObjectKey connector object key
* @return the value to be propagated as __NAME__
*/
public static Name evaluateNAME(final Any<?> any, final Provision provision, final String connObjectKey) {
if (StringUtils.isBlank(connObjectKey)) {
// LOG error but avoid to throw exception: leave it to the external resource
LOG.error("Missing ConnObjectKey for '{}': ", provision.getResource());
}
// Evaluate connObjectKey expression
String connObjectLink = provision == null || provision.getMapping() == null
? null
: provision.getMapping().getConnObjectLink();
String evalConnObjectLink = null;
if (StringUtils.isNotBlank(connObjectLink)) {
JexlContext jexlContext = new MapContext();
JexlUtils.addFieldsToContext(any, jexlContext);
JexlUtils.addPlainAttrsToContext(any.getPlainAttrs(), jexlContext);
JexlUtils.addDerAttrsToContext(any, jexlContext);
evalConnObjectLink = JexlUtils.evaluate(connObjectLink, jexlContext);
}
return getName(evalConnObjectLink, connObjectKey);
}
示例4: evaluate
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
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;
}
示例5: getRawValue
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Override
public String getRawValue(String key) {
if (data.containsKey(key)) {
return data.get(key);
} else if (expressions.containsKey(key)) {
try {
JexlContext jexlContext = new MapContext();
jexlContext.set("v", data);
Object result = expressions.get(key).evaluate(jexlContext);
if (result != null) {
return result.toString();
} else {
return null;
}
} catch (Throwable throwable) {
LOG.info("Error during mapping", throwable);
errorHandler.valueGenerateFailed(
key,
String.format("Could not execute expression '%s' for row with values: '%s.", expressions.get(key), data)
);
return null;
}
} else {
return null;
}
}
示例6: eval
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Override
public Object eval(final String script, final ScriptContext context) throws ScriptException {
// This is mandated by JSR-223 (see SCR.5.5.2 Methods)
if (script == null || context == null) {
throw new NullPointerException("script and context must be non-null");
}
// This is mandated by JSR-223 (end of section SCR.4.3.4.1.2 - JexlScript Execution)
context.setAttribute(CONTEXT_KEY, context, ScriptContext.ENGINE_SCOPE);
try {
JexlScript jexlScript = jexlEngine.createScript(script);
JexlContext ctxt = new JexlContextWrapper(context);
return jexlScript.execute(ctxt);
} catch (Exception e) {
throw new ScriptException(e.toString());
}
}
示例7: getProperty
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Override
public Object getProperty(JexlContext context, Object bean, String expr) {
if (context == null) {
context = EMPTY_CONTEXT;
}
// synthetize expr using register
String src = trimSource(expr);
src = "#0" + (src.charAt(0) == '[' ? "" : ".") + src;
try {
final Scope scope = new Scope(null, "#0");
final ASTJexlScript script = parse(null, PROPERTY_FEATURES, src, scope);
final JexlNode node = script.jjtGetChild(0);
final Scope.Frame frame = script.createFrame(bean);
final Interpreter interpreter = createInterpreter(context, frame);
return node.jjtAccept(interpreter, null);
} catch (JexlException xjexl) {
if (silent) {
logger.warn(xjexl.getMessage(), xjexl.getCause());
return null;
}
throw xjexl.clean();
}
}
示例8: setProperty
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Override
public void setProperty(JexlContext context, Object bean, String expr, Object value) {
if (context == null) {
context = EMPTY_CONTEXT;
}
// synthetize expr using register
String src = trimSource(expr);
src = "#0" + (src.charAt(0) == '[' ? "" : ".") + src + "=" + "#1";
try {
final Scope scope = new Scope(null, "#0", "#1");
final ASTJexlScript script = parse(null, PROPERTY_FEATURES, src, scope);
final JexlNode node = script.jjtGetChild(0);
final Scope.Frame frame = script.createFrame(bean, value);
final Interpreter interpreter = createInterpreter(context, frame);
node.jjtAccept(interpreter, null);
} catch (JexlException xjexl) {
if (silent) {
logger.warn(xjexl.getMessage(), xjexl.getCause());
return;
}
throw xjexl.clean();
}
}
示例9: testCantSeeMe
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Test
public void testCantSeeMe() throws Exception {
JexlContext jc = new MapContext();
String expr = "foo.doIt()";
JexlScript script;
Object result = null;
JexlSandbox sandbox = new JexlSandbox(false);
sandbox.white(Foo.class.getName());
JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).create();
jc.set("foo", new CantSeeMe());
script = sjexl.createScript(expr);
try {
result = script.execute(jc);
Assert.fail("should have failed, doIt()");
} catch (JexlException xany) {
//
}
jc.set("foo", new Foo("42"));
result = script.execute(jc);
Assert.assertEquals(42, ((Integer) result).intValue());
}
示例10: match
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Override
public MatchResult match(FlowPosition flowPosition) {
final JexlContext ctx = new MapContext();
ctx.set("serviceName", flowPosition.toString());
Object result = expression.evaluate(ctx);
verifyExpressionResult(sid, result);
if ((Boolean) result) {
return new MatchResult(true, sid);
}
return MatchResult.FALSE;
}
示例11: match
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
public MatchResult match(FlowPosition flowPosition) {
final JexlContext ctx = new MapContext();
ctx.set("serviceName", flowPosition.toString());
Object result = expression.evaluate(ctx);
verifyExpressionResult(sid, result);
if ((Boolean) result) {
return new MatchResult(true, sid);
}
return MatchResult.FALSE;
}
示例12: match
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Override
public MatchResult match(IData idata) {
JexlContext ctx = new IDataJexlContext(idata);
for (Entry<String, JexlExpression> expr : expressions.entrySet()) {
Object result = expr.getValue().evaluate(ctx);
verifyExpressionResult(expr.getKey(), result);
if ((Boolean) result)
return new MatchResult(true, expr.getKey());
}
return MatchResult.FALSE;
}
示例13: evaluateVisibility
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
private boolean evaluateVisibility(Object object,
PropertyDescriptor[] beanPropertyDescriptors, String visibleWhen)
{
Map<String, PropertyDescriptor> descriptorsMap =
new HashMap<String, PropertyDescriptor>();
for (PropertyDescriptor pd : beanPropertyDescriptors)
{
descriptorsMap.put(pd.getName(), pd);
}
JexlEngine jexl = new JexlBuilder().create();
JexlExpression expression = jexl.createExpression(visibleWhen);
JexlContext context = new MapContext();
Set<List<String>> vars = ((Script) expression).getVariables();
for (List<String> varList : vars)
{
for (String varName : varList)
{
PropertyDescriptor propertyDescriptor = descriptorsMap.get(varName);
if (propertyDescriptor != null)
{
try
{
Object value = propertyDescriptor.getReadMethod().invoke(object);
context.set(varName, value);
}
catch (Exception e)
{
Activator.logError(Status.ERROR,
"Could not retrieve value for property " + varName, e);
}
}
}
}
return (boolean) expression.evaluate(context);
}
示例14: getValues
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
private Map<DerSchema, String> getValues(final Any<?> any, final Set<DerSchema> schemas) {
Map<DerSchema, String> result = new HashMap<>(schemas.size());
for (DerSchema schema : schemas) {
JexlContext jexlContext = new MapContext();
JexlUtils.addPlainAttrsToContext(any.getPlainAttrs(), jexlContext);
JexlUtils.addFieldsToContext(any, jexlContext);
result.put(schema, JexlUtils.evaluate(schema.getExpression(), jexlContext));
}
return result;
}
示例15: beforePull
import org.apache.commons.jexl3.JexlContext; //导入依赖的package包/类
@Override
public List<Object> beforePull(
final Item item,
final EntityTO entityTO,
final List<Object> values) {
if (StringUtils.isNotBlank(pullJEXL) && values != null) {
List<Object> newValues = new ArrayList<>(values.size());
values.forEach(value -> {
JexlContext jexlContext = new MapContext();
jexlContext.set("value", value);
if (entityTO instanceof AnyTO) {
JexlUtils.addFieldsToContext((AnyTO) entityTO, jexlContext);
JexlUtils.addAttrTOsToContext(((AnyTO) entityTO).getPlainAttrs(), jexlContext);
JexlUtils.addAttrTOsToContext(((AnyTO) entityTO).getDerAttrs(), jexlContext);
JexlUtils.addAttrTOsToContext(((AnyTO) entityTO).getVirAttrs(), jexlContext);
} else if (entityTO instanceof RealmTO) {
JexlUtils.addFieldsToContext((RealmTO) entityTO, jexlContext);
newValues.add(JexlUtils.evaluate(pullJEXL, jexlContext));
}
newValues.add(JexlUtils.evaluate(pullJEXL, jexlContext));
});
return newValues;
}
return JEXLItemTransformer.super.beforePull(item, entityTO, values);
}