use of javax.script.SimpleBindings in project nifi by apache.
the class ScriptedReportingTask method onTrigger.
@Override
public void onTrigger(final ReportingContext context) {
synchronized (scriptingComponentHelper.isInitialized) {
if (!scriptingComponentHelper.isInitialized.get()) {
scriptingComponentHelper.createResources();
}
}
ScriptEngine scriptEngine = scriptingComponentHelper.engineQ.poll();
ComponentLog log = getLogger();
if (scriptEngine == null) {
// No engine available so nothing more to do here
return;
}
try {
try {
Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
if (bindings == null) {
bindings = new SimpleBindings();
}
bindings.put("context", context);
bindings.put("log", log);
bindings.put("vmMetrics", vmMetrics);
// Find the user-added properties and set them on the script
for (Map.Entry<PropertyDescriptor, String> property : context.getProperties().entrySet()) {
if (property.getKey().isDynamic()) {
// Add the dynamic property bound to its full PropertyValue to the script engine
if (property.getValue() != null) {
bindings.put(property.getKey().getName(), context.getProperty(property.getKey()));
}
}
}
scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
// Execute any engine-specific configuration before the script is evaluated
ScriptEngineConfigurator configurator = scriptingComponentHelper.scriptEngineConfiguratorMap.get(scriptingComponentHelper.getScriptEngineName().toLowerCase());
// Evaluate the script with the configurator (if it exists) or the engine
if (configurator != null) {
configurator.eval(scriptEngine, scriptToRun, scriptingComponentHelper.getModules());
} else {
scriptEngine.eval(scriptToRun);
}
} catch (ScriptException e) {
throw new ProcessException(e);
}
} catch (final Throwable t) {
// Mimic AbstractProcessor behavior here
getLogger().error("{} failed to process due to {}; rolling back session", new Object[] { this, t });
throw t;
} finally {
scriptingComponentHelper.engineQ.offer(scriptEngine);
}
}
use of javax.script.SimpleBindings in project janusgraph by JanusGraph.
the class JanusGraphManager method getAsBindings.
@Override
public Bindings getAsBindings() {
final Bindings bindings = new SimpleBindings();
graphs.forEach(bindings::put);
return bindings;
}
use of javax.script.SimpleBindings in project opennms by OpenNMS.
the class GraphMLScriptVertexStatusProvider method getStatusForVertices.
@Override
public Map<? extends VertexRef, ? extends Status> getStatusForVertices(final VertexProvider vertexProvider, final Collection<VertexRef> vertices, final Criteria[] criteria) {
// All vertices for the current vertexProvider
final List<GraphMLVertex> graphMLVertices = vertices.stream().filter(eachVertex -> contributesTo(eachVertex.getNamespace()) && eachVertex instanceof GraphMLVertex).map(eachVertex -> (GraphMLVertex) eachVertex).collect(Collectors.toList());
// Alarm summary for each node id
final Map<Integer, AlarmSummary> nodeIdToAlarmSummaryMap = alarmSummaryWrapper.getAlarmSummaries(Lists.transform(graphMLVertices, AbstractVertex::getNodeID)).stream().collect(Collectors.toMap(AlarmSummary::getNodeId, Function.identity()));
// Calculate status via scripts
return serviceAccessor.getTransactionOperations().execute(t -> this.scripting.compute(graphMLVertices.stream(), (vertex) -> {
final SimpleBindings bindings = new SimpleBindings();
bindings.put("vertex", vertex);
if (vertex.getNodeID() != null) {
bindings.put("node", serviceAccessor.getNodeDao().get(vertex.getNodeID()));
bindings.put("alarmSummary", nodeIdToAlarmSummaryMap.get(vertex.getNodeID()));
}
bindings.put("measurements", new MeasurementsWrapper(serviceAccessor.getMeasurementsService()));
bindings.put("nodeDao", serviceAccessor.getNodeDao());
bindings.put("snmpInterfaceDao", serviceAccessor.getSnmpInterfaceDao());
return bindings;
}));
}
use of javax.script.SimpleBindings in project opennms by OpenNMS.
the class ScriptedCollectionSetBuilder method build.
/**
* Builds a collection set from the given message.
*
* WARNING: This method is not necessarily thread safe. This depends on the
* script, and the script engine that is being used.
*
* @param agent
* the agent associated with the collection set
* @param message
* the messaged passed to script containing the metrics
* @return a collection set
* @throws ScriptException
*/
public CollectionSet build(CollectionAgent agent, Object message) throws ScriptException {
final CollectionSetBuilder builder = new CollectionSetBuilder(agent);
final SimpleBindings globals = new SimpleBindings();
globals.put("agent", agent);
globals.put("builder", builder);
globals.put("msg", message);
compiledScript.eval(globals);
return builder.build();
}
use of javax.script.SimpleBindings in project vcell by virtualcell.
the class ScriptingPanel method run.
private void run() {
// create scripting engine
ScriptEngineManager manager = new ScriptEngineManager();
// ScriptEngine engine = manager.getEngineByName("JavaScript");
ScriptEngine engine = manager.getEngineByName("jython");
// create environment for the script to run in.
// 1) capture stdout and send to the resultsTextField
// 2) predefine variables for windowManager, bioModel, model, and current selections
//
ScriptContext scriptContext = new SimpleScriptContext();
scriptContext.setBindings(new SimpleBindings(), ScriptContext.GLOBAL_SCOPE);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
scriptContext.setWriter(printWriter);
Bindings globals = scriptContext.getBindings(ScriptContext.GLOBAL_SCOPE);
// }
if (getBioModel() != null) {
globals.put("biomodel", getBioModel());
globals.put("model", getBioModel().getModel());
}
engine.setContext(scriptContext);
//
try {
String fullScript = getPredefinedJythonFunctions();
fullScript += scriptTextArea.getText();
System.out.println(fullScript);
engine.eval(fullScript);
try {
Bindings engineBindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
printWriter.println("\n\n\nengine variable bindings: ");
for (Map.Entry<String, Object> entry : engineBindings.entrySet()) {
if (entry.getKey().equals("__builtins__")) {
continue;
}
printWriter.println(" " + entry.getKey() + " : " + entry.getValue());
}
Bindings globalBindings = scriptContext.getBindings(ScriptContext.GLOBAL_SCOPE);
printWriter.println("\nglobal variable bindings: ");
for (Map.Entry<String, Object> entry : globalBindings.entrySet()) {
printWriter.println(" " + entry.getKey() + " : " + entry.getValue());
}
printWriter.flush();
resultsTextArea.append(stringWriter.getBuffer().toString());
// scroll to bottom.
resultsTextArea.scrollRectToVisible(new Rectangle(0, resultsTextArea.getHeight() - 2, 1, 1));
} catch (RuntimeException e1) {
e1.printStackTrace();
}
} catch (ScriptException e1) {
e1.printStackTrace();
}
}
Aggregations