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;
}
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);
}
}
}
}
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;
}
}
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;
}
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");
}
}
Aggregations