Search in sources :

Example 1 with EventSubscribeXmlRenderingCallback

use of org.apache.nifi.processors.windows.event.log.jna.EventSubscribeXmlRenderingCallback in project nifi by apache.

the class ConsumeWindowsEventLog method subscribe.

/**
 * Register subscriber via native call
 *
 * @param context the process context
 */
private String subscribe(ProcessContext context) throws URISyntaxException {
    String channel = context.getProperty(CHANNEL).getValue();
    String query = context.getProperty(QUERY).getValue();
    renderedXMLs = new LinkedBlockingQueue<>(context.getProperty(MAX_EVENT_QUEUE_SIZE).asInteger());
    provenanceUri = new URI("winlog", name, "/" + channel, query, null).toASCIIString();
    evtSubscribeCallback = new EventSubscribeXmlRenderingCallback(getLogger(), s -> {
        try {
            renderedXMLs.put(s);
        } catch (InterruptedException e) {
            throw new IllegalStateException("Got interrupted while waiting to add to queue.", e);
        }
    }, context.getProperty(MAX_BUFFER_SIZE).asInteger(), wEvtApi, kernel32, errorLookup);
    subscriptionHandle = wEvtApi.EvtSubscribe(null, null, channel, query, null, null, evtSubscribeCallback, WEvtApi.EvtSubscribeFlags.SUBSCRIBE_TO_FUTURE | WEvtApi.EvtSubscribeFlags.EVT_SUBSCRIBE_STRICT);
    if (!isSubscribed()) {
        return UNABLE_TO_SUBSCRIBE + errorLookup.getLastError();
    }
    return null;
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) Arrays(java.util.Arrays) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) ValidationContext(org.apache.nifi.components.ValidationContext) URISyntaxException(java.net.URISyntaxException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) WEvtApi(org.apache.nifi.processors.windows.event.log.jna.WEvtApi) ArrayList(java.util.ArrayList) WinNT(com.sun.jna.platform.win32.WinNT) HashSet(java.util.HashSet) Kernel32Util(com.sun.jna.platform.win32.Kernel32Util) ErrorLookup(org.apache.nifi.processors.windows.event.log.jna.ErrorLookup) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Relationship(org.apache.nifi.processor.Relationship) AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) URI(java.net.URI) TriggerSerially(org.apache.nifi.annotation.behavior.TriggerSerially) ValidationResult(org.apache.nifi.components.ValidationResult) Charsets(org.apache.commons.io.Charsets) FlowFile(org.apache.nifi.flowfile.FlowFile) Collection(java.util.Collection) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) ProcessSession(org.apache.nifi.processor.ProcessSession) BlockingQueue(java.util.concurrent.BlockingQueue) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) List(java.util.List) Kernel32(com.sun.jna.platform.win32.Kernel32) EventSubscribeXmlRenderingCallback(org.apache.nifi.processors.windows.event.log.jna.EventSubscribeXmlRenderingCallback) Tags(org.apache.nifi.annotation.documentation.Tags) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) Collections(java.util.Collections) EventSubscribeXmlRenderingCallback(org.apache.nifi.processors.windows.event.log.jna.EventSubscribeXmlRenderingCallback) URI(java.net.URI)

Example 2 with EventSubscribeXmlRenderingCallback

use of org.apache.nifi.processors.windows.event.log.jna.EventSubscribeXmlRenderingCallback in project nifi by apache.

the class ConsumeWindowsEventLogTest method testProcessesBlockedEvents.

@Test(timeout = 10 * 1000)
public void testProcessesBlockedEvents() throws UnsupportedEncodingException {
    testRunner.setProperty(ConsumeWindowsEventLog.MAX_EVENT_QUEUE_SIZE, "1");
    testRunner.run(1, false, true);
    EventSubscribeXmlRenderingCallback renderingCallback = getRenderingCallback();
    List<String> eventXmls = Arrays.asList("one", "two", "three", "four", "five", "six");
    List<WinNT.HANDLE> eventHandles = mockEventHandles(wEvtApi, kernel32, eventXmls);
    AtomicBoolean done = new AtomicBoolean(false);
    new Thread(() -> {
        for (WinNT.HANDLE eventHandle : eventHandles) {
            renderingCallback.onEvent(WEvtApi.EvtSubscribeNotifyAction.DELIVER, null, eventHandle);
        }
        done.set(true);
    }).start();
    // Wait until the thread has really started
    while (testRunner.getFlowFilesForRelationship(ConsumeWindowsEventLog.REL_SUCCESS).size() == 0) {
        testRunner.run(1, false, false);
    }
    // Process rest of events
    while (!done.get()) {
        testRunner.run(1, false, false);
    }
    testRunner.run(1, true, false);
    List<MockFlowFile> flowFilesForRelationship = testRunner.getFlowFilesForRelationship(ConsumeWindowsEventLog.REL_SUCCESS);
    assertEquals(eventXmls.size(), flowFilesForRelationship.size());
    for (int i = 0; i < eventXmls.size(); i++) {
        flowFilesForRelationship.get(i).assertContentEquals(eventXmls.get(i));
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EventSubscribeXmlRenderingCallback(org.apache.nifi.processors.windows.event.log.jna.EventSubscribeXmlRenderingCallback) Test(org.junit.Test)

Example 3 with EventSubscribeXmlRenderingCallback

use of org.apache.nifi.processors.windows.event.log.jna.EventSubscribeXmlRenderingCallback in project nifi by apache.

the class ConsumeWindowsEventLogTest method testScheduleErrorThenTriggerSubscribe.

@Test
public void testScheduleErrorThenTriggerSubscribe() throws InvocationTargetException, IllegalAccessException {
    evtSubscribe = new ConsumeWindowsEventLog(wEvtApi, kernel32);
    when(subscriptionHandle.getPointer()).thenReturn(subscriptionPointer);
    when(wEvtApi.EvtSubscribe(isNull(WinNT.HANDLE.class), isNull(WinNT.HANDLE.class), eq(ConsumeWindowsEventLog.DEFAULT_CHANNEL), eq(ConsumeWindowsEventLog.DEFAULT_XPATH), isNull(WinNT.HANDLE.class), isNull(WinDef.PVOID.class), isA(EventSubscribeXmlRenderingCallback.class), eq(WEvtApi.EvtSubscribeFlags.SUBSCRIBE_TO_FUTURE | WEvtApi.EvtSubscribeFlags.EVT_SUBSCRIBE_STRICT))).thenReturn(null).thenReturn(subscriptionHandle);
    testRunner = TestRunners.newTestRunner(evtSubscribe);
    testRunner.run(1, false, true);
    WinNT.HANDLE handle = mockEventHandles(wEvtApi, kernel32, Arrays.asList("test")).get(0);
    List<EventSubscribeXmlRenderingCallback> renderingCallbacks = getRenderingCallbacks(2);
    EventSubscribeXmlRenderingCallback subscribeRenderingCallback = renderingCallbacks.get(0);
    EventSubscribeXmlRenderingCallback renderingCallback = renderingCallbacks.get(1);
    renderingCallback.onEvent(WEvtApi.EvtSubscribeNotifyAction.DELIVER, null, handle);
    testRunner.run(1, true, false);
    assertNotEquals(subscribeRenderingCallback, renderingCallback);
    verify(wEvtApi).EvtClose(subscriptionHandle);
}
Also used : EventSubscribeXmlRenderingCallback(org.apache.nifi.processors.windows.event.log.jna.EventSubscribeXmlRenderingCallback) WinNT(com.sun.jna.platform.win32.WinNT) WinDef(com.sun.jna.platform.win32.WinDef) Test(org.junit.Test)

Aggregations

EventSubscribeXmlRenderingCallback (org.apache.nifi.processors.windows.event.log.jna.EventSubscribeXmlRenderingCallback)3 WinNT (com.sun.jna.platform.win32.WinNT)2 Test (org.junit.Test)2 Kernel32 (com.sun.jna.platform.win32.Kernel32)1 Kernel32Util (com.sun.jna.platform.win32.Kernel32Util)1 WinDef (com.sun.jna.platform.win32.WinDef)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Charsets (org.apache.commons.io.Charsets)1 InputRequirement (org.apache.nifi.annotation.behavior.InputRequirement)1