Search in sources :

Example 1 with JexlScript

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;
}
Also used : FloatWritable(org.apache.hadoop.io.FloatWritable) JexlContext(org.apache.commons.jexl3.JexlContext) Writable(org.apache.hadoop.io.Writable) FloatWritable(org.apache.hadoop.io.FloatWritable) IntWritable(org.apache.hadoop.io.IntWritable) Text(org.apache.hadoop.io.Text) MapContext(org.apache.commons.jexl3.MapContext) HashMap(java.util.HashMap) Map(java.util.Map) IntWritable(org.apache.hadoop.io.IntWritable) ProtocolStatus(org.apache.nutch.protocol.ProtocolStatus) IOException(java.io.IOException) VersionMismatchException(org.apache.hadoop.io.VersionMismatchException)

Example 2 with JexlScript

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;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JexlContext(org.apache.commons.jexl3.JexlContext) JexlScript(org.apache.commons.jexl3.JexlScript) MapContext(org.apache.commons.jexl3.MapContext)

Example 3 with JexlScript

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;
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) Matcher(java.util.regex.Matcher) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) Date(java.util.Date)

Example 4 with JexlScript

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);
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) JexlContext(org.apache.commons.jexl3.JexlContext) JexlScript(org.apache.commons.jexl3.JexlScript) MapContext(org.apache.commons.jexl3.MapContext)

Aggregations

JexlContext (org.apache.commons.jexl3.JexlContext)3 MapContext (org.apache.commons.jexl3.MapContext)3 JexlScript (org.apache.commons.jexl3.JexlScript)2 IOException (java.io.IOException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)1 JexlEngine (org.apache.commons.jexl3.JexlEngine)1 ProcessedClaimCollection (org.apache.cxf.sts.claims.ProcessedClaimCollection)1 FloatWritable (org.apache.hadoop.io.FloatWritable)1 IntWritable (org.apache.hadoop.io.IntWritable)1 Text (org.apache.hadoop.io.Text)1 VersionMismatchException (org.apache.hadoop.io.VersionMismatchException)1 Writable (org.apache.hadoop.io.Writable)1 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)1 JMeterContext (org.apache.jmeter.threads.JMeterContext)1 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)1 ProtocolStatus (org.apache.nutch.protocol.ProtocolStatus)1