<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.jackrabbit.jcr2spi.observation;

import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeRegistry;
import org.apache.jackrabbit.jcr2spi.WorkspaceManager;
import org.apache.jackrabbit.name.MalformedPathException;
import org.apache.jackrabbit.name.NameException;
import org.apache.jackrabbit.name.NameFormat;
import org.apache.jackrabbit.name.NamespaceResolver;
import org.apache.jackrabbit.name.Path;
import org.apache.jackrabbit.name.PathFormat;
import org.apache.jackrabbit.name.QName;
import org.apache.jackrabbit.spi.EventBundle;
import org.apache.jackrabbit.spi.EventFilter;
import org.apache.jackrabbit.util.IteratorHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.EventListenerIterator;
import javax.jcr.observation.ObservationManager;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

/**
 * <code>ObservationManagerImpl</code>...
 */
public class ObservationManagerImpl implements ObservationManager, InternalEventListener {

    /**
     * The logger instance for this class.
     */
    private static final Logger log = LoggerFactory.getLogger(ObservationManagerImpl.class);

    /**
     * The workspace manager.
     */
    private final WorkspaceManager wspManager;

    /**
     * The session this observation manager belongs to.
     */
    private final NamespaceResolver nsResolver;

    /**
     * The <code>NodeTypeRegistry</code> of the session.
     */
    private final NodeTypeRegistry ntRegistry;

    /**
     * Live mapping of <code>EventListener</code> to <code>EventFilter</code>.
     */
    private final Map subscriptions = new HashMap();

    /**
     * A read only mapping of <code>EventListener</code> to <code>EventFilter</code>.
     */
    private Map readOnlySubscriptions;

    /**
     * Creates a new observation manager for <code>session</code>.
     * @param wspManager the WorkspaceManager.
     * @param nsResolver NamespaceResolver to be used by this observation manager
     * is based on.
     * @param ntRegistry The <code>NodeTypeRegistry</code> of the session.
     */
    public ObservationManagerImpl(WorkspaceManager wspManager, NamespaceResolver nsResolver, NodeTypeRegistry ntRegistry) {
        this.wspManager = wspManager;
        this.nsResolver = nsResolver;
        this.ntRegistry = ntRegistry;
        this.wspManager.addEventListener(this);
    }

    /**
     * @inheritDoc
     */
    public void addEventListener(EventListener listener,
                                 int eventTypes,
                                 String absPath,
                                 boolean isDeep,
                                 String[] uuids,
                                 String[] nodeTypeNames,
                                 boolean noLocal) throws RepositoryException {
        Path path;
        try {
            path = PathFormat.parse(absPath, nsResolver).getCanonicalPath();
        } catch (MalformedPathException e) {
            throw new RepositoryException("Malformed path: " + absPath);
        }

        // create NodeType instances from names
        QName[] nodeTypeQNames;
        if (nodeTypeNames == null) {
            nodeTypeQNames = null;
        } else {
            try {
                nodeTypeQNames = new QName[nodeTypeNames.length];
                for (int i = 0; i < nodeTypeNames.length; i++) {
                    QName ntName = NameFormat.parse(nodeTypeNames[i], nsResolver);
                    if (!ntRegistry.isRegistered(ntName)) {
                        throw new RepositoryException("unknown node type: " + nodeTypeNames[i]);
                    }
                    nodeTypeQNames[i] = ntName;
                }<commit_msg>- Only register as internal event listener when external listeners are added.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.jackrabbit.jcr2spi.observation;

import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeRegistry;
import org.apache.jackrabbit.jcr2spi.WorkspaceManager;
import org.apache.jackrabbit.name.MalformedPathException;
import org.apache.jackrabbit.name.NameException;
import org.apache.jackrabbit.name.NameFormat;
import org.apache.jackrabbit.name.NamespaceResolver;
import org.apache.jackrabbit.name.Path;
import org.apache.jackrabbit.name.PathFormat;
import org.apache.jackrabbit.name.QName;
import org.apache.jackrabbit.spi.EventBundle;
import org.apache.jackrabbit.spi.EventFilter;
import org.apache.jackrabbit.util.IteratorHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.EventListenerIterator;
import javax.jcr.observation.ObservationManager;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

/**
 * <code>ObservationManagerImpl</code>...
 */
public class ObservationManagerImpl implements ObservationManager, InternalEventListener {

    /**
     * The logger instance for this class.
     */
    private static final Logger log = LoggerFactory.getLogger(ObservationManagerImpl.class);

    /**
     * The workspace manager.
     */
    private final WorkspaceManager wspManager;

    /**
     * The session this observation manager belongs to.
     */
    private final NamespaceResolver nsResolver;

    /**
     * The <code>NodeTypeRegistry</code> of the session.
     */
    private final NodeTypeRegistry ntRegistry;

    /**
     * Live mapping of <code>EventListener</code> to <code>EventFilter</code>.
     */
    private final Map subscriptions = new HashMap();

    /**
     * A read only mapping of <code>EventListener</code> to <code>EventFilter</code>.
     */
    private Map readOnlySubscriptions;

    /**
     * Indicates if this observation manager is registered as an internal event
     * listener on the workspace manager.
     */
    private boolean isRegistered = false;

    /**
     * Creates a new observation manager for <code>session</code>.
     * @param wspManager the WorkspaceManager.
     * @param nsResolver NamespaceResolver to be used by this observation manager
     * is based on.
     * @param ntRegistry The <code>NodeTypeRegistry</code> of the session.
     */
    public ObservationManagerImpl(WorkspaceManager wspManager, NamespaceResolver nsResolver, NodeTypeRegistry ntRegistry) {
        this.wspManager = wspManager;
        this.nsResolver = nsResolver;
        this.ntRegistry = ntRegistry;
    }

    /**
     * @inheritDoc
     */
    public void addEventListener(EventListener listener,
                                 int eventTypes,
                                 String absPath,
                                 boolean isDeep,
                                 String[] uuids,
                                 String[] nodeTypeNames,
                                 boolean noLocal) throws RepositoryException {
        if (!isRegistered) {
            wspManager.addEventListener(this);
            isRegistered = true;
        }
        Path path;
        try {
            path = PathFormat.parse(absPath, nsResolver).getCanonicalPath();
        } catch (MalformedPathException e) {
            throw new RepositoryException("Malformed path: " + absPath);
        }

        // create NodeType instances from names
        QName[] nodeTypeQNames;
        if (nodeTypeNames == null) {
            nodeTypeQNames = null;
        } else {
            try {
                nodeTypeQNames = new QName[nodeTypeNames.length];
                for (int i = 0; i < nodeTypeNames.length; i++) {
                    QName ntName = NameFormat.parse(nodeTypeNames[i], nsResolver);
                    if (!ntRegistry.isRegistered(ntName)) {
                        throw new RepositoryException("unknown node type: " + nodeTypeNames[i]);
                    }
                    nodeTypeQNames[i] = ntName;
                }
{'fraction_non_alphanumeric': 0.08434478502365768, 'fraction_numerical': 0.002057189878625797, 'mean_word_length': 2.6635267520723436, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3962456', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2437, 'n_words': 731}