use of org.apache.commons.jexl2.JexlContext in project newts by OpenNMS.
the class ResultDescriptor method expression.
public ResultDescriptor expression(String label, String expression) {
final JexlExpression expr = JEXL_ENGINE.createExpression(expression);
final String[] labels = getLabels().toArray(new String[0]);
CalculationFunction evaluate = new CalculationFunction() {
private static final long serialVersionUID = -3328049421398096252L;
@Override
public double apply(double... ds) {
JexlContext jc = new MapContext();
for (int i = 0; i < labels.length; i++) {
jc.set(labels[i], ds[i]);
}
return ((Number) expr.evaluate(jc)).doubleValue();
}
};
return calculate(label, evaluate, labels);
}
use of org.apache.commons.jexl2.JexlContext in project jmeter by apache.
the class Jexl3Function method execute.
/**
* {@inheritDoc}
*/
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
// $NON-NLS-1$
String str = "";
CompoundVariable var = (CompoundVariable) values[0];
String exp = var.execute();
// $NON-NLS-1$
String varName = "";
if (values.length > 1) {
varName = ((CompoundVariable) values[1]).execute().trim();
}
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
try {
JexlContext jc = new MapContext();
// $NON-NLS-1$
jc.set("log", log);
// $NON-NLS-1$
jc.set("ctx", jmctx);
// $NON-NLS-1$
jc.set("vars", vars);
// $NON-NLS-1$
jc.set("props", JMeterUtils.getJMeterProperties());
// Previously mis-spelt as theadName
// $NON-NLS-1$
jc.set("threadName", Thread.currentThread().getName());
// $NON-NLS-1$ (may be null)
jc.set("sampler", currentSampler);
// $NON-NLS-1$ (may be null)
jc.set("sampleResult", previousResult);
// $NON-NLS-1$
jc.set("OUT", System.out);
// Now evaluate the script, getting the result
JexlScript e = threadLocalJexl.get().createScript(exp);
Object o = e.execute(jc);
if (o != null) {
str = o.toString();
}
if (vars != null && varName.length() > 0) {
// vars will be null on TestPlan
vars.put(varName, str);
}
} catch (Exception e) {
log.error("An error occurred while evaluating the expression \"{}\"\n", exp, e);
}
return str;
}
use of org.apache.commons.jexl2.JexlContext in project jmeter by apache.
the class Jexl2Function method execute.
/**
* {@inheritDoc}
*/
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
// $NON-NLS-1$
String str = "";
CompoundVariable var = (CompoundVariable) values[0];
String exp = var.execute();
// $NON-NLS-1$
String varName = "";
if (values.length > 1) {
varName = ((CompoundVariable) values[1]).execute().trim();
}
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
try {
JexlContext jc = new MapContext();
// $NON-NLS-1$
jc.set("log", log);
// $NON-NLS-1$
jc.set("ctx", jmctx);
// $NON-NLS-1$
jc.set("vars", vars);
// $NON-NLS-1$
jc.set("props", JMeterUtils.getJMeterProperties());
// Previously mis-spelt as theadName
// $NON-NLS-1$
jc.set("threadName", Thread.currentThread().getName());
// $NON-NLS-1$ (may be null)
jc.set("sampler", currentSampler);
// $NON-NLS-1$ (may be null)
jc.set("sampleResult", previousResult);
// $NON-NLS-1$
jc.set("OUT", System.out);
// Now evaluate the script, getting the result
Script e = getJexlEngine().createScript(exp);
Object o = e.execute(jc);
if (o != null) {
str = o.toString();
}
if (vars != null && varName.length() > 0) {
// vars will be null on TestPlan
vars.put(varName, str);
}
} catch (Exception e) {
log.error("An error occurred while evaluating the expression \"" + exp + "\"\n", e);
}
return str;
}
use of org.apache.commons.jexl2.JexlContext in project nutch by apache.
the class JexlIndexingFilter method metadataToContext.
private JexlContext metadataToContext(Metadata metadata) {
JexlContext context = new MapContext();
for (String name : metadata.names()) {
String[] values = metadata.getValues(name);
context.set(name, values.length > 1 ? values : values[0]);
}
return context;
}
use of org.apache.commons.jexl2.JexlContext in project cxf by apache.
the class JexlClaimsMapper method mapClaims.
public ProcessedClaimCollection mapClaims(String sourceRealm, ProcessedClaimCollection sourceClaims, String targetRealm, ClaimsParameters parameters) {
JexlContext context = new MapContext();
context.set("sourceClaims", sourceClaims);
context.set("targetClaims", new ProcessedClaimCollection());
context.set("sourceRealm", sourceRealm);
context.set("targetRealm", targetRealm);
context.set("claimsParameters", parameters);
JexlScript s = getScript();
if (s == null) {
LOG.warning("No claim mapping script defined");
// TODO Check if null or an exception would be more
return new ProcessedClaimCollection();
// appropriate
}
return (ProcessedClaimCollection) s.execute(context);
}
Aggregations