<commit_before>package org.fusesource.fabric.features;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.karaf.features.Feature;
import org.apache.karaf.features.FeaturesService;
import org.apache.karaf.features.Repository;
import org.apache.karaf.features.internal.FeatureValidationUtil;
import org.apache.karaf.features.internal.FeaturesServiceImpl;
import org.apache.karaf.features.internal.RepositoryImpl;
import org.apache.zookeeper.KeeperException;
import org.fusesource.fabric.api.Container;
import org.fusesource.fabric.api.FabricService;
import org.fusesource.fabric.api.Profile;
import org.fusesource.fabric.api.Version;
import org.fusesource.fabric.zookeeper.IZKClient;
import org.fusesource.fabric.zookeeper.ZkPath;
import org.linkedin.zookeeper.client.LifecycleListener;
import org.linkedin.zookeeper.tracker.NodeEvent;
import org.linkedin.zookeeper.tracker.NodeEventsListener;
import org.linkedin.zookeeper.tracker.ZKStringDataReader;
import org.linkedin.zookeeper.tracker.ZooKeeperTreeTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.fusesource.fabric.utils.features.FeatureUtils.search;

/**
 * A FeaturesService implementation for Fabric managed containers.
 */
public class FabricFeaturesServiceImpl extends FeaturesServiceImpl implements FeaturesService, NodeEventsListener<String>, LifecycleListener {

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

    private FabricService fabricService;
    private IZKClient zooKeeper;

    private ZooKeeperTreeTracker<String> profilesTracker;


    private final Set<Repository> repositories = new HashSet<Repository>();
    private final Set<Feature> allfeatures = new HashSet<Feature>();
    private final Set<Feature> installed = new HashSet<Feature>();

    public void init() throws Exception {
    }

    public void destroy() throws Exception {
        profilesTracker.destroy();
    }

    @Override
    public void onEvents(Collection<NodeEvent<String>> nodeEvents) {
        try {
            repositories.clear();
            listRepositories();
            allfeatures.clear();
            listFeatures();
            installed.clear();
            listInstalledFeatures();
        } catch (Exception e) {
            LOGGER.error("Error while updating FeaturesService information from Fabric Registry.", e);
        }

    }

    @Override
    public void onConnected() {
        profilesTracker = new ZooKeeperTreeTracker<String>(zooKeeper, new ZKStringDataReader(), ZkPath.CONFIG_VERSIONS.getPath());
        try {
            profilesTracker.track(this);
        } catch (InterruptedException e) {
            LOGGER.error("Error while setting tracker for Fabric Features Service.", e);
        } catch (KeeperException e) {
            LOGGER.error("Error while setting tracker for Fabric Features Service.", e);
        }
        onEvents(null);
    }

    @Override
    public void onDisconnected() {
    }

    @Override
    public void validateRepository(URI uri) throws Exception {<commit_msg>Lazily load repos and features to avoid exceptions when the zookeeper ensemble is recreated<commit_after>package org.fusesource.fabric.features;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.karaf.features.Feature;
import org.apache.karaf.features.FeaturesService;
import org.apache.karaf.features.Repository;
import org.apache.karaf.features.internal.FeatureValidationUtil;
import org.apache.karaf.features.internal.FeaturesServiceImpl;
import org.apache.karaf.features.internal.RepositoryImpl;
import org.apache.zookeeper.KeeperException;
import org.fusesource.fabric.api.Container;
import org.fusesource.fabric.api.FabricService;
import org.fusesource.fabric.api.Profile;
import org.fusesource.fabric.api.Version;
import org.fusesource.fabric.zookeeper.IZKClient;
import org.fusesource.fabric.zookeeper.ZkPath;
import org.linkedin.zookeeper.client.LifecycleListener;
import org.linkedin.zookeeper.tracker.NodeEvent;
import org.linkedin.zookeeper.tracker.NodeEventsListener;
import org.linkedin.zookeeper.tracker.ZKStringDataReader;
import org.linkedin.zookeeper.tracker.ZooKeeperTreeTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.fusesource.fabric.utils.features.FeatureUtils.search;

/**
 * A FeaturesService implementation for Fabric managed containers.
 */
public class FabricFeaturesServiceImpl extends FeaturesServiceImpl implements FeaturesService, NodeEventsListener<String>, LifecycleListener {

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

    private FabricService fabricService;
    private IZKClient zooKeeper;

    private ZooKeeperTreeTracker<String> profilesTracker;


    private final Set<Repository> repositories = new HashSet<Repository>();
    private final Set<Feature> allfeatures = new HashSet<Feature>();
    private final Set<Feature> installed = new HashSet<Feature>();

    public void init() throws Exception {
    }

    public void destroy() throws Exception {
        profilesTracker.destroy();
    }

    @Override
    public void onEvents(Collection<NodeEvent<String>> nodeEvents) {
        repositories.clear();
        allfeatures.clear();
        installed.clear();
    }

    @Override
    public void onConnected() {
        profilesTracker = new ZooKeeperTreeTracker<String>(zooKeeper, new ZKStringDataReader(), ZkPath.CONFIG_VERSIONS.getPath());
        try {
            profilesTracker.track(this);
        } catch (InterruptedException e) {
            LOGGER.error("Error while setting tracker for Fabric Features Service.", e);
        } catch (KeeperException e) {
            LOGGER.error("Error while setting tracker for Fabric Features Service.", e);
        }
        onEvents(null);
    }

    @Override
    public void onDisconnected() {
    }

    @Override
    public void validateRepository(URI uri) throws Exception {
{'fraction_non_alphanumeric': 0.09249183895538629, 'fraction_numerical': 0.000621793875330328, 'mean_word_length': 4.243683781581092, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5837347', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1778, 'n_words': 395}