<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.fusesource.fabric.zookeeper.curator;

import com.google.common.base.Strings;
import com.google.common.io.Closeables;
import org.apache.curator.RetryPolicy;
import org.apache.curator.ensemble.EnsembleProvider;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.ACLProvider;
import org.apache.curator.framework.imps.CuratorFrameworkImpl;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.curator.framework.state.ConnectionStateManager;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.fusesource.fabric.zookeeper.curator.Constants.ACL_PROVIDER;
import static org.fusesource.fabric.zookeeper.curator.Constants.CONNECTION_TIMEOUT;
import static org.fusesource.fabric.zookeeper.curator.Constants.DEFAULT_CONNECTION_TIMEOUT_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.DEFAULT_MAX_SLEEP_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.DEFAULT_SESSION_TIMEOUT_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.ENSEMBLE_ID;
import static org.fusesource.fabric.zookeeper.curator.Constants.MAX_RETRIES_LIMIT;
import static org.fusesource.fabric.zookeeper.curator.Constants.RETRY_POLICY_BASE_SLEEP_TIME_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.RETRY_POLICY_MAX_RETRIES;
import static org.fusesource.fabric.zookeeper.curator.Constants.RETRY_POLICY_MAX_SLEEP_TIME_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.SESSION_TIMEOUT;
import static org.fusesource.fabric.zookeeper.curator.Constants.ZOOKEEPER_PASSWORD;
import static org.fusesource.fabric.zookeeper.curator.Constants.ZOOKEEPER_URL;
import static org.fusesource.fabric.zookeeper.curator.TrackerUtils.closeQuietly;


public class ManagedCuratorFramework implements ManagedService, Closeable {

    private static final Logger LOGGER = LoggerFactory.getLogger(ManagedCuratorFramework.class);

    private final BundleContext bundleContext;
    private final Map<String, ACLProvider> aclProviders = new HashMap<String, ACLProvider>();
    private final DynamicEnsembleProvider ensembleProvider = new DynamicEnsembleProvider();
    private final ExecutorService executor = Executors.newSingleThreadExecutor();

    private CuratorFramework curatorFramework;
    private ServiceRegistration registration;

    private ServiceTracker connectionStateListenerTracker;
    private ServiceTracker aclProviderTracker;
    private Dictionary oldProperties;

    public ManagedCuratorFramework(BundleContext bundleContext) {
        this.bundleContext = bundleContext;
        registerAclProviderTracker();
    }

    @Override
    public void updated(Dictionary properties) throws ConfigurationException {
        if (isRestartRequired(oldProperties, properties)) {
            updateService(buildCuratorFramework(properties));
        } else {
            updateEnsemble(properties);
            if (!propertyEquals(oldProperties, properties, ZOOKEEPER_URL)) {
                try {
                    reset((CuratorFrameworkImpl) curatorFramework);
                } catch (Exception e) {
                    LOGGER.error("Failed to update the ensemble url.", e);
                }<commit_msg>Add a thread pool for curator state change listeners.<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.fusesource.fabric.zookeeper.curator;

import com.google.common.base.Strings;
import com.google.common.io.Closeables;
import org.apache.curator.RetryPolicy;
import org.apache.curator.ensemble.EnsembleProvider;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.ACLProvider;
import org.apache.curator.framework.imps.CuratorFrameworkImpl;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.curator.framework.state.ConnectionStateManager;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.fusesource.fabric.zookeeper.curator.Constants.ACL_PROVIDER;
import static org.fusesource.fabric.zookeeper.curator.Constants.CONNECTION_TIMEOUT;
import static org.fusesource.fabric.zookeeper.curator.Constants.DEFAULT_CONNECTION_TIMEOUT_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.DEFAULT_MAX_SLEEP_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.DEFAULT_SESSION_TIMEOUT_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.MAX_RETRIES_LIMIT;
import static org.fusesource.fabric.zookeeper.curator.Constants.RETRY_POLICY_BASE_SLEEP_TIME_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.RETRY_POLICY_MAX_RETRIES;
import static org.fusesource.fabric.zookeeper.curator.Constants.RETRY_POLICY_MAX_SLEEP_TIME_MS;
import static org.fusesource.fabric.zookeeper.curator.Constants.SESSION_TIMEOUT;
import static org.fusesource.fabric.zookeeper.curator.Constants.ZOOKEEPER_PASSWORD;
import static org.fusesource.fabric.zookeeper.curator.Constants.ZOOKEEPER_URL;
import static org.fusesource.fabric.zookeeper.curator.TrackerUtils.closeQuietly;


public class ManagedCuratorFramework implements ManagedService, Closeable {

    private static final Logger LOGGER = LoggerFactory.getLogger(ManagedCuratorFramework.class);

    private final BundleContext bundleContext;
    private final Map<String, ACLProvider> aclProviders = new HashMap<String, ACLProvider>();
    private final DynamicEnsembleProvider ensembleProvider = new DynamicEnsembleProvider();
    private final ExecutorService executor = Executors.newFixedThreadPool(5);

    private CuratorFramework curatorFramework;
    private ServiceRegistration registration;

    private ServiceTracker connectionStateListenerTracker;
    private ServiceTracker aclProviderTracker;
    private Dictionary oldProperties;

    public ManagedCuratorFramework(BundleContext bundleContext) {
        this.bundleContext = bundleContext;
        registerAclProviderTracker();
    }

    @Override
    public void updated(Dictionary properties) throws ConfigurationException {
        if (isRestartRequired(oldProperties, properties)) {
            updateService(buildCuratorFramework(properties));
        } else {
            updateEnsemble(properties);
            if (!propertyEquals(oldProperties, properties, ZOOKEEPER_URL)) {
                try {
                    reset((CuratorFrameworkImpl) curatorFramework);
                } catch (Exception e) {
                    LOGGER.error("Failed to update the ensemble url.", e);
                }
{'fraction_non_alphanumeric': 0.07699609745807404, 'fraction_numerical': 0.0013711633793903597, 'mean_word_length': 5.03564608529599, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6005509', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2586, 'n_words': 603}