use of org.apache.nifi.processors.groovyx.flow.GroovyProcessSessionWrap in project nifi by apache.
the class ExecuteGroovyScript method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession _session) throws ProcessException {
boolean toFailureOnError = VALID_FAIL_STRATEGY[1].equals(context.getProperty(FAIL_STRATEGY).getValue());
// create wrapped session to control list of newly created and files got from this session.
// so transfer original input to failure will be possible
GroovyProcessSessionWrap session = new GroovyProcessSessionWrap(_session, toFailureOnError);
HashMap CTL = new AccessMap("CTL");
HashMap SQL = new AccessMap("SQL");
try {
// compilation must be moved to validation
Script script = getGroovyScript();
Map bindings = script.getBinding().getVariables();
bindings.clear();
// Find the user-added properties and bind them for the script
for (Map.Entry<PropertyDescriptor, String> property : context.getProperties().entrySet()) {
if (property.getKey().isDynamic()) {
if (property.getKey().getName().startsWith("CTL.")) {
// get controller service
ControllerService ctl = context.getProperty(property.getKey()).asControllerService(ControllerService.class);
CTL.put(property.getKey().getName().substring(4), ctl);
} else if (property.getKey().getName().startsWith("SQL.")) {
DBCPService dbcp = context.getProperty(property.getKey()).asControllerService(DBCPService.class);
SQL.put(property.getKey().getName().substring(4), dbcp);
} else {
// 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()));
}
}
}
}
onInitSQL(SQL);
bindings.put("session", session);
bindings.put("context", context);
bindings.put("log", getLogger());
bindings.put("REL_SUCCESS", REL_SUCCESS);
bindings.put("REL_FAILURE", REL_FAILURE);
bindings.put("CTL", CTL);
bindings.put("SQL", SQL);
script.run();
bindings.clear();
onCommitSQL(SQL);
session.commit();
} catch (Throwable t) {
getLogger().error(t.toString(), t);
onFailSQL(SQL);
if (toFailureOnError) {
// transfer all received to failure with two new attributes: ERROR_MESSAGE and ERROR_STACKTRACE.
session.revertReceivedTo(REL_FAILURE, StackTraceUtils.deepSanitize(t));
} else {
session.rollback(true);
}
} finally {
onFinitSQL(SQL);
}
}
Aggregations