Search in sources :

Example 26 with JexlContext

use of org.apache.commons.jexl2.JexlContext 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 27 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project dbeaver by dbeaver.

the class DashboardUpdater method fetchDashboardMapData.

private void fetchDashboardMapData(DBRProgressMonitor monitor, DashboardContainer dashboard) {
    MapQueryInfo mqi = getMapQueryData(dashboard);
    if (mqi == null) {
        return;
    }
    Map<String, Object> mapValue = mqi.mapValue;
    if (mapValue != null) {
        String[] mapKeys = dashboard.getMapKeys();
        String[] mapLabels = dashboard.getMapLabels();
        if (!ArrayUtils.isEmpty(mapKeys)) {
            if (ArrayUtils.isEmpty(mapLabels)) {
                mapLabels = mapKeys;
            }
            DashboardDataset dataset = new DashboardDataset(mapLabels);
            Object[] mapValues = new Object[mapKeys.length];
            for (int i = 0; i < mapKeys.length; i++) {
                Object value = mapValue.get(mapKeys[i]);
                Number numValue;
                if (value instanceof Number) {
                    numValue = (Number) value;
                } else {
                    numValue = CommonUtils.toDouble(value);
                }
                mapValues[i] = numValue;
            }
            Date timestamp = mqi.timestamp;
            if (timestamp == null) {
                timestamp = new Date();
            }
            dataset.addRow(new DashboardDatasetRow(timestamp, mapValues));
            dashboard.updateDashboardData(dataset);
        } else if (dashboard.getMapFormula() != null) {
            Map<String, Object> ciMap = new HashMap<>(mapValue.size());
            for (Map.Entry<String, Object> me : mapValue.entrySet()) {
                ciMap.put(me.getKey().toLowerCase(Locale.ENGLISH), me.getValue());
            }
            JexlContext context = new JexlContext() {

                @Override
                public Object get(String name) {
                    if (name.equals("map")) {
                        return ciMap;
                    } else if (name.equals("dashboard")) {
                        return dashboard;
                    }
                    return null;
                }

                @Override
                public void set(String name, Object value) {
                    log.warn("Set is not implemented in DBX model");
                }

                @Override
                public boolean has(String name) {
                    return name.equals("object") || name.equals("dashboard");
                }
            };
            Object result = dashboard.getMapFormula().evaluate(context);
            if (result instanceof Number) {
                String columnName = dashboard.getDashboardTitle();
                if (!ArrayUtils.isEmpty(mapLabels)) {
                    columnName = mapLabels[0];
                }
                DashboardDataset dataset = new DashboardDataset(new String[] { columnName });
                dataset.addRow(new DashboardDatasetRow(new Date(), new Object[] { result }));
                dashboard.updateDashboardData(dataset);
            } else {
                log.debug("Wrong expression result: " + result);
            }
        }
    }
}
Also used : DashboardDatasetRow(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow) DashboardDataset(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset) JexlContext(org.apache.commons.jexl3.JexlContext)

Example 28 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project vorto by eclipse.

the class DataMapperJxpath method matchesCondition.

private boolean matchesCondition(FunctionblockModel fbModel, JXPathContext context) {
    Optional<Stereotype> conditionStereotype = fbModel.getStereotype("condition");
    if (conditionStereotype.isPresent() && conditionStereotype.get().hasAttribute("value")) {
        Expression e = jexlEngine.createExpression(normalizeCondition(conditionStereotype.get().getAttributes().get("value")));
        JexlContext jc = new ObjectContext<Object>(jexlEngine, context.getContextBean());
        jc.set("this", context.getContextBean());
        jc.set("obj", context.getContextBean());
        return (boolean) e.evaluate(jc);
    } else {
        return true;
    }
}
Also used : Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) Stereotype(org.eclipse.vorto.model.Stereotype) ObjectContext(org.apache.commons.jexl2.ObjectContext)

Example 29 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project opennms by OpenNMS.

the class JexlIndexStorageStrategy method getResourceNameFromIndex.

/**
 * {@inheritDoc}
 */
@Override
public String getResourceNameFromIndex(CollectionResource resource) {
    String resourceName = null;
    try {
        UnifiedJEXL.Expression expr = EL.parse(m_parameters.get(PARAM_INDEX_FORMAT));
        JexlContext context = new MapContext();
        m_parameters.entrySet().forEach((entry) -> {
            context.set(entry.getKey(), entry.getValue());
        });
        updateContext(context, resource);
        resourceName = (String) expr.evaluate(new ReadonlyContext(context));
    } catch (JexlException e) {
        LOG.error("getResourceNameFromIndex(): error evaluating index-format [{}] as a Jexl Expression", m_parameters.get(PARAM_INDEX_FORMAT), e);
    } finally {
        if (resourceName == null) {
            resourceName = resource.getInstance();
        }
    }
    if ("true".equals(m_parameters.get(PARAM_CLEAN_OUTPUT)) && resourceName != null) {
        resourceName = resourceName.replaceAll("\\s+", "_").replaceAll(":", "_").replaceAll("\\\\", "_").replaceAll("[\\[\\]]", "_").replaceAll("[|/]", "_").replaceAll("=", "").replaceAll("[_]+$", "").replaceAll("___", "_");
    }
    LOG.debug("getResourceNameFromIndex(): {}", resourceName);
    return resourceName;
}
Also used : UnifiedJEXL(org.apache.commons.jexl2.UnifiedJEXL) JexlException(org.apache.commons.jexl2.JexlException) JexlContext(org.apache.commons.jexl2.JexlContext) ReadonlyContext(org.apache.commons.jexl2.ReadonlyContext) MapContext(org.apache.commons.jexl2.MapContext)

Example 30 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project syncope by apache.

the class LDAPMembershipPropagationActions method before.

@Transactional(readOnly = true)
@Override
public void before(final PropagationTask task, final ConnectorObject beforeObj) {
    Optional<? extends Provision> provision = task.getResource().getProvision(anyTypeDAO.findGroup());
    if (AnyTypeKind.USER == task.getAnyTypeKind() && provision.isPresent() && provision.get().getMapping() != null && StringUtils.isNotBlank(provision.get().getMapping().getConnObjectLink())) {
        User user = userDAO.find(task.getEntityKey());
        if (user != null) {
            List<String> groupConnObjectLinks = new ArrayList<>();
            userDAO.findAllGroupKeys(user).forEach(groupKey -> {
                Group group = groupDAO.find(groupKey);
                if (group != null && groupDAO.findAllResourceKeys(groupKey).contains(task.getResource().getKey())) {
                    LOG.debug("Evaluating connObjectLink for {}", group);
                    JexlContext jexlContext = new MapContext();
                    JexlUtils.addFieldsToContext(group, jexlContext);
                    JexlUtils.addPlainAttrsToContext(group.getPlainAttrs(), jexlContext);
                    JexlUtils.addDerAttrsToContext(group, jexlContext);
                    String groupConnObjectLinkLink = JexlUtils.evaluate(provision.get().getMapping().getConnObjectLink(), jexlContext);
                    LOG.debug("ConnObjectLink for {} is '{}'", group, groupConnObjectLinkLink);
                    if (StringUtils.isNotBlank(groupConnObjectLinkLink)) {
                        groupConnObjectLinks.add(groupConnObjectLinkLink);
                    }
                }
            });
            LOG.debug("Group connObjectLinks to propagate for membership: {}", groupConnObjectLinks);
            Set<Attribute> attributes = new HashSet<>(task.getAttributes());
            Set<String> groups = new HashSet<>(groupConnObjectLinks);
            Attribute ldapGroups = AttributeUtil.find(getGroupMembershipAttrName(), attributes);
            if (ldapGroups != null) {
                ldapGroups.getValue().forEach(obj -> {
                    groups.add(obj.toString());
                });
                attributes.remove(ldapGroups);
            }
            attributes.add(AttributeBuilder.build(getGroupMembershipAttrName(), groups));
            task.setAttributes(attributes);
        }
    } else {
        LOG.debug("Not about user, or group mapping missing for resource: not doing anything");
    }
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) User(org.apache.syncope.core.persistence.api.entity.user.User) Attribute(org.identityconnectors.framework.common.objects.Attribute) ArrayList(java.util.ArrayList) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

JexlContext (org.apache.commons.jexl2.JexlContext)31 Expression (org.apache.commons.jexl2.Expression)25 MapContext (org.apache.commons.jexl2.MapContext)23 JexlContext (org.apache.commons.jexl3.JexlContext)21 JexlEngine (org.apache.commons.jexl2.JexlEngine)20 MapContext (org.apache.commons.jexl3.MapContext)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)5 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 JexlException (org.apache.commons.jexl2.JexlException)3 ObjectContext (org.apache.commons.jexl2.ObjectContext)3 IOException (java.io.IOException)2 ReadonlyContext (org.apache.commons.jexl2.ReadonlyContext)2 JexlScript (org.apache.commons.jexl3.JexlScript)2 NumberUtils (org.apache.commons.lang.math.NumberUtils)2 FloatWritable (org.apache.hadoop.io.FloatWritable)2 IntWritable (org.apache.hadoop.io.IntWritable)2 Text (org.apache.hadoop.io.Text)2 VersionMismatchException (org.apache.hadoop.io.VersionMismatchException)2