<commit_before>/*
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.wso2.carbon.identity.user.store.count.util;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.user.store.count.UserStoreCountRetriever;
import org.wso2.carbon.identity.user.store.count.dto.PairDTO;
import org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException;
import org.wso2.carbon.identity.user.store.count.internal.UserStoreCountDataHolder;
import org.wso2.carbon.identity.user.store.count.jdbc.internal.InternalCountRetriever;
import org.wso2.carbon.identity.user.store.count.jdbc.internal.InternalStoreCountConstants;
import org.wso2.carbon.user.api.RealmConfiguration;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.UserCoreConstants;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Util class for user store counting functionality for users, roles and by claims
 */
public class UserStoreCountUtils {
    public static final String countRetrieverClass = "CountRetrieverClass";
    private static Log log = LogFactory.getLog(UserStoreCountUtils.class);


    /**
     * Get the available list of user store domains
     *
     * @return
     * @throws UserStoreCounterException
     */
    public static Map<String, RealmConfiguration> getUserStoreList() throws UserStoreCounterException {
        String domain;
        RealmConfiguration realmConfiguration;
        Map<String, RealmConfiguration> userStoreList = new HashMap<>();

        try {
            realmConfiguration = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration();
            domain = IdentityUtil.getPrimaryDomainName();
            userStoreList.put(domain, realmConfiguration);

            while (realmConfiguration != null) {
                realmConfiguration = realmConfiguration.getSecondaryRealmConfig();
                if (realmConfiguration != null) {
                    domain = realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
                    userStoreList.put(domain, realmConfiguration);
                } else {
                    break;
                }
            }

        } catch (UserStoreException e) {
            throw new UserStoreCounterException("Error while listing user stores for count functionality", e);
        }

        return userStoreList;
    }

    /**
     * Get the domain names of user stores which has count functionality enabled
     *
     * @return
     */
    public static Set<String> getCountEnabledUserStores() throws UserStoreCounterException {
        RealmConfiguration realmConfiguration;
        Set<String> userStoreList = new HashSet<>();

        try {
            realmConfiguration = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration();

            while (realmConfiguration != null) {
                if (realmConfiguration.getUserStoreProperty(countRetrieverClass) != null) {
                    userStoreList.add(realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME));
                }
                realmConfiguration = realmConfiguration.getSecondaryRealmConfig();
            }
        } catch (UserStoreException e) {
            throw new UserStoreCounterException("Error while getting the count enabled user stores", e);
        }

        return userStoreList;
    }


    /**
     * Get the available user store domains
     *
     * @return
     * @throws UserStoreCounterException
     */
    public static Set<String> getUserStoreDomains() throws UserStoreCounterException {
        return getUserStoreList().keySet();
    }

    /**
     * Create an instance of the given count retriever class
     *
     * @param domain
     * @return
     * @throws UserStoreCounterException
     */
    public static UserStoreCountRetriever getCounterInstanceForDomain(String domain) throws UserStoreCounterException {
        if (StringUtils.isEmpty(domain)) {
            domain = IdentityUtil.getPrimaryDomainName();
        }

        RealmConfiguration realmConfiguration = getUserStoreList().get(domain);
        if (realmConfiguration != null && realmConfiguration.getUserStoreProperty(countRetrieverClass) != null) {
            String retriever = realmConfiguration.getUserStoreProperty(countRetrieverClass);
            UserStoreCountRetriever userStoreCountRetriever = UserStoreCountDataHolder.getInstance().
                    getUserStoreCountRetrievers().get(retriever);
            if (userStoreCountRetriever == null) {
                throw new UserStoreCounterException("Could not create an instance of class: " + retriever + " for " +
                        "the domain: " + domain);
            }
            userStoreCountRetriever.init(realmConfiguration);
            return userStoreCountRetriever;
        } else {
            throw new UserStoreCounterException("Realm Configuration is not ready for count the domain: " + domain);
        }
    }

    /**
     * Create an instance of the given count retriever for internal domain
     *
     * @return
     * @throws UserStoreCounterException
     */
    public static UserStoreCountRetriever getInternalCounterInstance() throws UserStoreCounterException {
        return UserStoreCountDataHolder.getInstance().getUserStoreCountRetrievers().get(
                InternalCountRetriever.class.getName());
<commit_msg>fixing test failure. Cannot stop the flow here.<commit_after>/*
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.wso2.carbon.identity.user.store.count.util;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.user.store.count.UserStoreCountRetriever;
import org.wso2.carbon.identity.user.store.count.dto.PairDTO;
import org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException;
import org.wso2.carbon.identity.user.store.count.internal.UserStoreCountDataHolder;
import org.wso2.carbon.identity.user.store.count.jdbc.internal.InternalCountRetriever;
import org.wso2.carbon.identity.user.store.count.jdbc.internal.InternalStoreCountConstants;
import org.wso2.carbon.user.api.RealmConfiguration;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.UserCoreConstants;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Util class for user store counting functionality for users, roles and by claims
 */
public class UserStoreCountUtils {
    public static final String countRetrieverClass = "CountRetrieverClass";
    private static Log log = LogFactory.getLog(UserStoreCountUtils.class);


    /**
     * Get the available list of user store domains
     *
     * @return
     * @throws UserStoreCounterException
     */
    public static Map<String, RealmConfiguration> getUserStoreList() throws UserStoreCounterException {
        String domain;
        RealmConfiguration realmConfiguration;
        Map<String, RealmConfiguration> userStoreList = new HashMap<>();

        try {
            realmConfiguration = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration();
            domain = IdentityUtil.getPrimaryDomainName();
            userStoreList.put(domain, realmConfiguration);

            while (realmConfiguration != null) {
                realmConfiguration = realmConfiguration.getSecondaryRealmConfig();
                if (realmConfiguration != null) {
                    domain = realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
                    userStoreList.put(domain, realmConfiguration);
                } else {
                    break;
                }
            }

        } catch (UserStoreException e) {
            throw new UserStoreCounterException("Error while listing user stores for count functionality", e);
        }

        return userStoreList;
    }

    /**
     * Get the domain names of user stores which has count functionality enabled
     *
     * @return
     */
    public static Set<String> getCountEnabledUserStores() throws UserStoreCounterException {
        RealmConfiguration realmConfiguration;
        Set<String> userStoreList = new HashSet<>();

        try {
            realmConfiguration = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration();

            while (realmConfiguration != null) {
                if (realmConfiguration.getUserStoreProperty(countRetrieverClass) != null) {
                    userStoreList.add(realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME));
                }
                realmConfiguration = realmConfiguration.getSecondaryRealmConfig();
            }
        } catch (UserStoreException e) {
            throw new UserStoreCounterException("Error while getting the count enabled user stores", e);
        }

        return userStoreList;
    }


    /**
     * Get the available user store domains
     *
     * @return
     * @throws UserStoreCounterException
     */
    public static Set<String> getUserStoreDomains() throws UserStoreCounterException {
        return getUserStoreList().keySet();
    }

    /**
     * Create an instance of the given count retriever class
     *
     * @param domain
     * @return
     * @throws UserStoreCounterException
     */
    public static UserStoreCountRetriever getCounterInstanceForDomain(String domain) throws UserStoreCounterException {
        if (StringUtils.isEmpty(domain)) {
            domain = IdentityUtil.getPrimaryDomainName();
        }

        RealmConfiguration realmConfiguration = getUserStoreList().get(domain);
        if (realmConfiguration != null && realmConfiguration.getUserStoreProperty(countRetrieverClass) != null) {
            String retriever = realmConfiguration.getUserStoreProperty(countRetrieverClass);
            UserStoreCountRetriever userStoreCountRetriever = UserStoreCountDataHolder.getInstance().
                    getUserStoreCountRetrievers().get(retriever);
            if (userStoreCountRetriever == null) {
                throw new UserStoreCounterException("Could not create an instance of class: " + retriever + " for " +
                        "the domain: " + domain);
            }
            userStoreCountRetriever.init(realmConfiguration);
            return userStoreCountRetriever;
        } else {
            return null;
        }
    }

    /**
     * Create an instance of the given count retriever for internal domain
     *
     * @return
     * @throws UserStoreCounterException
     */
    public static UserStoreCountRetriever getInternalCounterInstance() throws UserStoreCounterException {
        return UserStoreCountDataHolder.getInstance().getUserStoreCountRetrievers().get(
                InternalCountRetriever.class.getName());

{'fraction_non_alphanumeric': 0.08243670886075949, 'fraction_numerical': 0.003639240506329114, 'mean_word_length': 3.056803594351733, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '7494108', 'n_tokens_mistral': 3256, 'n_tokens_neox': 3160, 'n_words': 780}