<commit_before>/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * 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.
 */
package com.alibaba.nacos.naming.healthcheck.extend;

import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker;
import com.alibaba.nacos.naming.healthcheck.HealthCheckProcessor;
import com.alibaba.nacos.api.naming.pojo.healthcheck.HealthCheckType;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.SingletonBeanRegistry;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.Set;

/**
 * @author XCXCXCXCX
 */
@Component
public class HealthCheckExtendProvider implements BeanFactoryAware {

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

    private ServiceLoader<HealthCheckProcessor> processorLoader
        = ServiceLoader.load(HealthCheckProcessor.class);

    private ServiceLoader<AbstractHealthChecker> checkerLoader
        = ServiceLoader.load(AbstractHealthChecker.class);

    private SingletonBeanRegistry registry;

    public void init(){
        loadExtend();
    }

    private void loadExtend() {
        Iterator<HealthCheckProcessor> processorIt = processorLoader.iterator();
        Iterator<AbstractHealthChecker> healthCheckerIt = checkerLoader.iterator();

        Set<String> origin = new HashSet<>();
        for(HealthCheckType type : HealthCheckType.values()){
            origin.add(type.name());
        }
        Set<String> processorType = new HashSet<>();
        Set<String> healthCheckerType = new HashSet<>();
        processorType.addAll(origin);
        healthCheckerType.addAll(origin);

        while(processorIt.hasNext()){
            HealthCheckProcessor processor = processorIt.next();
            String type = processor.getType();
            if(processorType.contains(type)){
                throw new RuntimeException("More than one processor of the same type was found : [type=\"" + type + "\"]");
            }
            processorType.add(type);
            registry.registerSingleton(lowerFirstChar(processor.getClass().getSimpleName()), processor);
        }

        while(healthCheckerIt.hasNext()){
            AbstractHealthChecker checker = healthCheckerIt.next();
            String type = checker.getType();
            if(healthCheckerType.contains(type)){
                throw new RuntimeException("More than one healthChecker of the same type was found : [type=\"" + type + "\"]");
            }<commit_msg>hi.here 'addAll()' call can be replaced with parametrized constructor call,performance will be better<commit_after>/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * 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.
 */
package com.alibaba.nacos.naming.healthcheck.extend;

import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker;
import com.alibaba.nacos.naming.healthcheck.HealthCheckProcessor;
import com.alibaba.nacos.api.naming.pojo.healthcheck.HealthCheckType;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.SingletonBeanRegistry;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.Set;

/**
 * @author XCXCXCXCX
 */
@Component
public class HealthCheckExtendProvider implements BeanFactoryAware {

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

    private ServiceLoader<HealthCheckProcessor> processorLoader
        = ServiceLoader.load(HealthCheckProcessor.class);

    private ServiceLoader<AbstractHealthChecker> checkerLoader
        = ServiceLoader.load(AbstractHealthChecker.class);

    private SingletonBeanRegistry registry;

    public void init(){
        loadExtend();
    }

    private void loadExtend() {
        Iterator<HealthCheckProcessor> processorIt = processorLoader.iterator();
        Iterator<AbstractHealthChecker> healthCheckerIt = checkerLoader.iterator();

        Set<String> origin = new HashSet<>();
        for(HealthCheckType type : HealthCheckType.values()){
            origin.add(type.name());
        }
        Set<String> processorType = new HashSet<>(origin);
        Set<String> healthCheckerType = new HashSet<>(origin);

        while(processorIt.hasNext()){
            HealthCheckProcessor processor = processorIt.next();
            String type = processor.getType();
            if(processorType.contains(type)){
                throw new RuntimeException("More than one processor of the same type was found : [type=\"" + type + "\"]");
            }
            processorType.add(type);
            registry.registerSingleton(lowerFirstChar(processor.getClass().getSimpleName()), processor);
        }

        while(healthCheckerIt.hasNext()){
            AbstractHealthChecker checker = healthCheckerIt.next();
            String type = checker.getType();
            if(healthCheckerType.contains(type)){
                throw new RuntimeException("More than one healthChecker of the same type was found : [type=\"" + type + "\"]");
            }
{'fraction_non_alphanumeric': 0.09602153754113073, 'fraction_numerical': 0.004486987735566856, 'mean_word_length': 3.695926966292135, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6725523', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1714, 'n_words': 450}