<commit_before>/*
 * #%L
 * ACS AEM Samples
 * %%
 * Copyright (C) 2015 Adobe
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

package com.adobe.acs.samples.events.impl;

import org.apache.commons.collections.map.HashedMap;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.observation.JackrabbitEvent;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;


@Component(
        label = "ACS AEM Samples - JCR Event Listener",
        description = "Sample implementation of a low-level JCR Event Listener.",

        // One of the few cases where immediate = true; this is so the Event Listener starts listening immediately
        immediate = true
)
@Service

/**
 * NOTE: YOU ALMOST NEVER WANT TO USE JCR EVENT LISTENERS; USE SLING EVENT HANDLERS INSTEAD!
 */

public class SampleJcrEventListener implements EventListener {
    private static final Logger log = LoggerFactory.getLogger(SampleJcrEventListener.class);

    @Reference
    private JobManager jobManager;

    /*
     * A combination of one or more event type constants encoded as a bitmask
     *
     * Available JCR Events:
     *
     * Event.NODE_ADDED
     * Event.NODE_MOVED
     * Event.NODE_REMOVED
     * Event.PERSIST
     * Event.PROPERTY_ADDED
     * Event.PROPERTY_REMOVED
     * Event.PROPERTY_CHANGED
    */
    private final int events = Event.PROPERTY_ADDED | Event.NODE_ADDED;

    // Only events whose associated node is at absPath (or within its subtree, if isDeep is true) will be received.
    // It is permissible to register a listener for a path where no node currently exists.
    private final String absPath = "/content/samples";
    private final boolean isDeep = true;

    // If noLocal is true, events generated by the session through which the listener was registered are ignored.
    // Otherwise, they are not ignored.
    private final boolean noLocal = false;

    private final String[] uuids = null;

    // Only events whose associated node has one of the node types (or a subtype of one of the node types) in this list will be received. If his parameter is null then no node type-related restriction is placed on events received.
    private final String[] nodeTypes = new String[]{"nt:unstructured", "nt:folder"};

    // This is one of the VERY FEW times that a JCR Session is appropriate in an OSGi Services instance var space
    // This must be left open for the life of the Event Listener as this is the security context that scopes what
    // events this listener can see.
    private Session observationSession = null;


    @Reference
    private SlingRepository repository;

    @Reference
    private EventAdmin eventAdmin;

    @Override
    public void onEvent(final EventIterator events) {
        // Handle events<commit_msg>Sample OSGi Event Emitter<commit_after>/*
 * #%L
 * ACS AEM Samples
 * %%
 * Copyright (C) 2015 Adobe
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

package com.adobe.acs.samples.events.impl;

import org.apache.commons.collections.map.HashedMap;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.observation.JackrabbitEvent;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;


@Component(
        label = "ACS AEM Samples - JCR Event Listener",
        description = "Sample implementation of a low-level JCR Event Listener.",

        // One of the few cases where immediate = true; this is so the Event Listener starts listening immediately
        immediate = true
)
@Service

/**
 * NOTE: YOU ALMOST NEVER WANT TO USE JCR EVENT LISTENERS; USE SLING EVENT HANDLERS INSTEAD!
 */

public class SampleJcrEventListener implements EventListener {
    private static final Logger log = LoggerFactory.getLogger(SampleJcrEventListener.class);

    @Reference
    private JobManager jobManager;

    /*
     * A combination of one or more event type constants encoded as a bitmask
     *
     * Available JCR Events:
     *
     * Event.NODE_ADDED
     * Event.NODE_MOVED
     * Event.NODE_REMOVED
     * Event.PERSIST
     * Event.PROPERTY_ADDED
     * Event.PROPERTY_REMOVED
     * Event.PROPERTY_CHANGED
    */
    private final int events = Event.PROPERTY_ADDED | Event.NODE_ADDED;

    // Only events whose associated node is at absPath (or within its subtree, if isDeep is true) will be received.
    // It is permissible to register a listener for a path where no node currently exists.
    private final String absPath = "/content/samples";
    private final boolean isDeep = true;

    // If noLocal is true, events generated by the session through which the listener was registered are ignored.
    // Otherwise, they are not ignored.
    private final boolean noLocal = false;

    private final String[] uuids = null;

    // Only events whose associated node has one of the node types (or a subtype of one of the node types) in this list will be received. If his parameter is null then no node type-related restriction is placed on events received.
    private final String[] nodeTypes = new String[]{"nt:unstructured", "nt:folder"};

    // This is one of the VERY FEW times that a JCR Session is appropriate in an OSGi Services instance var space
    // This must be left open for the life of the Event Listener as this is the security context that scopes what
    // events this listener can see.
    private Session observationSession = null;


    @Reference
    private SlingRepository repository;

    @Reference
    private EventAdmin eventAdmin;

    public void onEvent(final EventIterator events) {
        // Handle events
{'fraction_non_alphanumeric': 0.07525669922364137, 'fraction_numerical': 0.002504382669671926, 'mean_word_length': 3.592869465209891, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6038147', 'n_tokens_mistral': 2216, 'n_tokens_neox': 2147, 'n_words': 843}