All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xingyuv.captcha.service.impl.CaptchaServiceFactory Maven / Gradle / Ivy

The newest version!
package com.xingyuv.captcha.service.impl;

import com.xingyuv.captcha.model.common.Const;
import com.xingyuv.captcha.service.CaptchaCacheService;
import com.xingyuv.captcha.service.CaptchaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;

/**
 * Created by raodeming on 2020/5/26.
 */
public class CaptchaServiceFactory {

    private static Logger logger = LoggerFactory.getLogger(CaptchaServiceFactory.class);

    public static CaptchaService getInstance(Properties config) {
        //先把所有CaptchaService初始化,通过init方法,实例字体等,add by [email protected]
        /*try{
            for(CaptchaService item: instances.values()){
                item.init(config);
            }
        }catch (Exception e){
            logger.warn("init captchaService fail:{}", e);
        }*/

        String captchaType = config.getProperty(Const.CAPTCHA_TYPE, "default");
        CaptchaService ret = instances.get(captchaType);
        if (ret == null) {
            throw new RuntimeException("unsupported-[captcha.type]=" + captchaType);
        }
        ret.init(config);
        return ret;
    }

    public static CaptchaCacheService getCache(String cacheType) {
        return cacheService.get(cacheType);
    }

    public volatile static Map instances = new HashMap<>();
    public volatile static Map cacheService = new HashMap<>();

    static {
        ServiceLoader cacheServices = ServiceLoader.load(CaptchaCacheService.class);
        for (CaptchaCacheService item : cacheServices) {
            cacheService.put(item.type(), item);
        }
        logger.debug("supported-captchaCache-service:{}", cacheService.keySet().toString());
        ServiceLoader services = ServiceLoader.load(CaptchaService.class);
        for (CaptchaService item : services) {
            instances.put(item.captchaType(), item);
        }
        logger.debug("supported-captchaTypes-service:{}", instances.keySet().toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy