use of org.apache.commons.jexl3.JexlScript in project nutch by apache.
the class CrawlDatum method execute.
public boolean execute(JexlScript expr, String url) {
if (expr != null && url != null) {
// Create a context and add data
JexlContext jcontext = new MapContext();
// https://issues.apache.org/jira/browse/NUTCH-2229
jcontext.set("url", url);
jcontext.set("status", getStatusName(getStatus()));
jcontext.set("fetchTime", (long) (getFetchTime()));
jcontext.set("modifiedTime", (long) (getModifiedTime()));
jcontext.set("retries", getRetriesSinceFetch());
jcontext.set("interval", Integer.valueOf(getFetchInterval()));
jcontext.set("score", getScore());
jcontext.set("signature", StringUtil.toHexString(getSignature()));
// Set metadata variables
for (Map.Entry<Writable, Writable> entry : getMetaData().entrySet()) {
Object value = entry.getValue();
Text tkey = (Text) entry.getKey();
if (value instanceof FloatWritable) {
FloatWritable fvalue = (FloatWritable) value;
jcontext.set(tkey.toString(), fvalue.get());
}
if (value instanceof IntWritable) {
IntWritable ivalue = (IntWritable) value;
jcontext.set(tkey.toString(), ivalue.get());
}
if (value instanceof Text) {
Text tvalue = (Text) value;
jcontext.set(tkey.toString().replace("-", "_"), tvalue.toString());
}
if (value instanceof ProtocolStatus) {
ProtocolStatus pvalue = (ProtocolStatus) value;
jcontext.set(tkey.toString().replace("-", "_"), pvalue.toString());
}
}
try {
if (Boolean.TRUE.equals(expr.execute(jcontext))) {
return true;
}
} catch (Exception e) {
//
}
}
return false;
}
use of org.apache.commons.jexl3.JexlScript 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.jexl3.JexlScript in project nutch by apache.
the class JexlUtil method parseExpression.
/**
* Parses the given expression to a JEXL expression. This supports
* date parsing.
*
* @param expr string JEXL expression
* @return parsed JEXL expression or null in case of parse error
*/
public static JexlScript parseExpression(String expr) {
if (expr == null)
return null;
try {
// Translate any date object into a long. Dates must be in the DATE_PATTERN
// format. For example: 2016-03-20T00:00:00Z
Matcher matcher = DATE_PATTERN.matcher(expr);
if (matcher.find()) {
String date = matcher.group();
// parse the matched substring and get the epoch
Date parsedDate = DateUtils.parseDateStrictly(date, new String[] { "yyyy-MM-dd'T'HH:mm:ss'Z'" });
long time = parsedDate.getTime();
// replace the original string date with the numeric value
expr = expr.replace(date, Long.toString(time));
}
JexlEngine jexl = new JexlBuilder().silent(true).strict(true).create();
return jexl.createScript(expr);
} catch (Exception e) {
LOG.error(e.getMessage());
}
return null;
}
use of org.apache.commons.jexl3.JexlScript 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