use of org.apache.nifi.components.PropertyDescriptor in project nifi by apache.
the class ExecuteScript method onTrigger.
/**
* Evaluates the given script body (or file) using the current session, context, and flowfile. The script
* evaluation expects a FlowFile to be returned, in which case it will route the FlowFile to success. If a script
* error occurs, the original FlowFile will be routed to failure. If the script succeeds but does not return a
* FlowFile, the original FlowFile will be routed to no-flowfile
*
* @param context the current process context
* @param sessionFactory provides access to a {@link ProcessSessionFactory}, which
* can be used for accessing FlowFiles, etc.
* @throws ProcessException if the scripted processor's onTrigger() method throws an exception
*/
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
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;
}
ProcessSession session = sessionFactory.createSession();
try {
try {
Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
if (bindings == null) {
bindings = new SimpleBindings();
}
bindings.put("session", session);
bindings.put("context", context);
bindings.put("log", log);
bindings.put("REL_SUCCESS", REL_SUCCESS);
bindings.put("REL_FAILURE", REL_FAILURE);
// 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);
}
// Commit this session for the user. This plus the outermost catch statement mimics the behavior
// of AbstractProcessor. This class doesn't extend AbstractProcessor in order to share a base
// class with InvokeScriptedProcessor
session.commit();
} 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 });
session.rollback(true);
throw t;
} finally {
scriptingComponentHelper.engineQ.offer(scriptEngine);
}
}
use of org.apache.nifi.components.PropertyDescriptor in project nifi by apache.
the class InvokeScriptedProcessor method getSupportedPropertyDescriptors.
/**
* Returns a list of property descriptors supported by this processor. The
* list always includes properties such as script engine name, script file
* name, script body name, script arguments, and an external module path. If
* the scripted processor also defines supported properties, those are added
* to the list as well.
*
* @return a List of PropertyDescriptor objects supported by this processor
*/
@Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
synchronized (scriptingComponentHelper.isInitialized) {
if (!scriptingComponentHelper.isInitialized.get()) {
scriptingComponentHelper.createResources();
}
}
List<PropertyDescriptor> supportedPropertyDescriptors = new ArrayList<>();
supportedPropertyDescriptors.addAll(scriptingComponentHelper.getDescriptors());
final Processor instance = processor.get();
if (instance != null) {
try {
final List<PropertyDescriptor> instanceDescriptors = instance.getPropertyDescriptors();
if (instanceDescriptors != null) {
supportedPropertyDescriptors.addAll(instanceDescriptors);
}
} catch (final Throwable t) {
final ComponentLog logger = getLogger();
final String message = "Unable to get property descriptors from Processor: " + t;
logger.error(message);
if (logger.isDebugEnabled()) {
logger.error(message, t);
}
}
}
return Collections.unmodifiableList(supportedPropertyDescriptors);
}
use of org.apache.nifi.components.PropertyDescriptor in project nifi by apache.
the class TestInvokeGroovy method testScriptDefinedAttribute.
/**
* Tests a script that has a Groovy Processor that that reads the first line of text from the flowfiles content and
* stores the value in an attribute of the outgoing flowfile.
*
* @throws Exception Any error encountered while testing
*/
@Test
public void testScriptDefinedAttribute() throws Exception {
InvokeScriptedProcessor processor = new InvokeScriptedProcessor();
MockProcessContext context = new MockProcessContext(processor);
MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
processor.initialize(initContext);
context.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
context.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/groovy/test_reader.groovy");
context.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/groovy");
// State Manger is unused, and a null reference is specified
processor.customValidate(new MockValidationContext(context));
processor.setup(context);
List<PropertyDescriptor> descriptors = processor.getSupportedPropertyDescriptors();
assertNotNull(descriptors);
assertTrue(descriptors.size() > 0);
boolean found = false;
for (PropertyDescriptor descriptor : descriptors) {
if (descriptor.getName().equals("test-attribute")) {
found = true;
break;
}
}
assertTrue(found);
}
use of org.apache.nifi.components.PropertyDescriptor in project nifi by apache.
the class TestInvokeJavascript method testScriptDefinedAttribute.
/**
* Tests a scripted processor written in Javascript that reads the first line of text from the flowfiles content and
* stores the value in an attribute of the outgoing flowfile.
* Confirms that the scripted processor can return property descriptors defined in it.
*
* @throws Exception Any error encountered while testing
*/
@Test
public void testScriptDefinedAttribute() throws Exception {
InvokeScriptedProcessor processor = new InvokeScriptedProcessor();
MockProcessContext context = new MockProcessContext(processor);
MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
processor.initialize(initContext);
context.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "ECMAScript");
context.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/javascript/test_reader.js");
context.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/javascript");
// State Manger is unused, and a null reference is specified
processor.customValidate(new MockValidationContext(context));
processor.setup(context);
List<PropertyDescriptor> descriptors = processor.getSupportedPropertyDescriptors();
assertNotNull(descriptors);
assertTrue(descriptors.size() > 0);
boolean found = false;
for (PropertyDescriptor descriptor : descriptors) {
if (descriptor.getName().equals("test-attribute")) {
found = true;
break;
}
}
assertTrue(found);
}
use of org.apache.nifi.components.PropertyDescriptor in project nifi by apache.
the class TestSiteToSiteBulletinReportingTask method testWhenProvenanceMaxIdEqualToLastEventIdInStateManager.
@Test
public void testWhenProvenanceMaxIdEqualToLastEventIdInStateManager() throws IOException, InitializationException {
// creating the list of bulletins
final List<Bulletin> bulletins = new ArrayList<Bulletin>();
bulletins.add(BulletinFactory.createBulletin("category", "severity", "message"));
bulletins.add(BulletinFactory.createBulletin("category", "severity", "message"));
bulletins.add(BulletinFactory.createBulletin("category", "severity", "message"));
bulletins.add(BulletinFactory.createBulletin("category", "severity", "message"));
// mock the access to the list of bulletins
final ReportingContext context = Mockito.mock(ReportingContext.class);
final BulletinRepository repository = Mockito.mock(BulletinRepository.class);
Mockito.when(context.getBulletinRepository()).thenReturn(repository);
Mockito.when(repository.findBulletins(Mockito.any(BulletinQuery.class))).thenReturn(bulletins);
final long maxEventId = getMaxBulletinId(bulletins);
;
// create the mock reporting task and mock state manager
final MockSiteToSiteBulletinReportingTask task = new MockSiteToSiteBulletinReportingTask();
final MockStateManager stateManager = new MockStateManager(task);
// settings properties and mocking access to properties
final Map<PropertyDescriptor, String> properties = new HashMap<>();
for (final PropertyDescriptor descriptor : task.getSupportedPropertyDescriptors()) {
properties.put(descriptor, descriptor.getDefaultValue());
}
properties.put(SiteToSiteBulletinReportingTask.BATCH_SIZE, "1000");
properties.put(SiteToSiteBulletinReportingTask.PLATFORM, "nifi");
properties.put(SiteToSiteBulletinReportingTask.TRANSPORT_PROTOCOL, SiteToSiteTransportProtocol.HTTP.name());
properties.put(SiteToSiteBulletinReportingTask.HTTP_PROXY_HOSTNAME, "localhost");
properties.put(SiteToSiteBulletinReportingTask.HTTP_PROXY_PORT, "80");
properties.put(SiteToSiteBulletinReportingTask.HTTP_PROXY_USERNAME, "username");
properties.put(SiteToSiteBulletinReportingTask.HTTP_PROXY_PASSWORD, "password");
Mockito.doAnswer(new Answer<PropertyValue>() {
@Override
public PropertyValue answer(final InvocationOnMock invocation) throws Throwable {
final PropertyDescriptor descriptor = invocation.getArgumentAt(0, PropertyDescriptor.class);
return new MockPropertyValue(properties.get(descriptor));
}
}).when(context).getProperty(Mockito.any(PropertyDescriptor.class));
// create the state map and set the last id to the same value as maxEventId
final Map<String, String> state = new HashMap<>();
state.put(SiteToSiteProvenanceReportingTask.LAST_EVENT_ID_KEY, String.valueOf(maxEventId));
stateManager.setState(state, Scope.LOCAL);
// setup the mock reporting context to return the mock state manager
Mockito.when(context.getStateManager()).thenReturn(stateManager);
// setup the mock initialization context
final ComponentLog logger = Mockito.mock(ComponentLog.class);
final ReportingInitializationContext initContext = Mockito.mock(ReportingInitializationContext.class);
Mockito.when(initContext.getIdentifier()).thenReturn(UUID.randomUUID().toString());
Mockito.when(initContext.getLogger()).thenReturn(logger);
task.initialize(initContext);
// execute the reporting task and should not produce any data b/c max id same as previous id
task.onTrigger(context);
assertEquals(0, task.dataSent.size());
}
Aggregations