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

net.happyonroad.component.container.support.DefaultServiceRegistry Maven / Gradle / Ivy

Go to download

The spring component framework is used to setup a plugin based, micro-kernel, standalone application (today, we will support webapp in later releases) which is based on SpringFramework. It can help you decouple your application into several components clearly with zero invasion and keep your application consistent between develop time and runtime.

The newest version!
/**
 * @author XiongJie, Date: 13-10-29
 */
package net.happyonroad.component.container.support;

import net.happyonroad.component.container.MutableServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/** 缺省的服务注册表 */
@SuppressWarnings("unchecked")
public class DefaultServiceRegistry implements MutableServiceRegistry {
    protected Logger                          logger   = LoggerFactory.getLogger(getClass());
    private   Map> services = new HashMap>();

    ////////////////////////////////////////////////////////////////////////
    // Mutable Service Registry的实现
    ////////////////////////////////////////////////////////////////////////

    @Override
    public  void register(Class interfaceClass, T service) {
        register(interfaceClass, service, DEFAULT_HINT);
    }

    @Override
    public  void register(Class interfaceClass, T service, String hint) {
        logger.info("Register {} with hint = {}", interfaceClass.getName(), hint);
        Map instances = services.get(interfaceClass);
        if (instances == null) {
            instances = new HashMap(2);
            services.put(interfaceClass, instances);
        }
        instances.put(hint, service);
    }

    @Override
    public  T unRegister(Class interfaceClass, String hint) {
        List removes = new ArrayList();
        Set keys = services.keySet();
        for (Class key : keys) {
            if (interfaceClass.isAssignableFrom(key)) {
                Map theMap = services.get(key);
                if (theMap != null) {
                    T removed = (T) theMap.remove(hint);
                    if (removed != null) removes.add(removed);
                }
            }
        }
        if (removes.size() == 1) {
            return removes.get(0);
        } else if (removes.isEmpty()) {
            return null;
        } else {
            logger.warn("I have unRegister multiple service with hint {}", hint);
            return removes.get(0);
        }
    }

    @Override
    public  Map unRegister(Class interfaceClass) {
        Set keys = services.keySet();
        Map removes = new HashMap();
        for (Class key : keys) {
            if (interfaceClass.isAssignableFrom(key)) {
                Map theMap = (Map) services.remove(key);
                if (theMap != null) {
                    removes.putAll(theMap);
                }
            }
        }
        return removes;
    }

    ////////////////////////////////////////////////////////////////////////
    // Service Registry的实现
    ////////////////////////////////////////////////////////////////////////

    @Override
    public  T getService(Class requiredType) {
        return getService(requiredType, ANY_HINT);
    }

    @Override
    public  T getService(Class requiredType, String hint) {
        Map map = services.get(requiredType);
        if (map == null) {
            //如果精确查找找不到,则进行模糊匹配,找到第一个算数
            map = digMaps(requiredType);
        }
        if(map.isEmpty()) return null;
        if(ANY_HINT.equals(hint)){
            return (T) map.values().iterator().next();
        }
        return (T) map.get(hint);
    }

    @Override
    public  Map getServices(Class requiredType) {
        Map map = digMaps(requiredType);
        return (Map) map;
    }

    /**
     * 找到符合相应类型的服务map
     *
     * @param requiredType 服务类型
     * @param           服务类型
     * @return 第一个找到的map
     */
    private  Map digMaps(Class requiredType) {
        Map all = new HashMap();
        Set keys = services.keySet();
        for (Class serviceClass : keys) {
            if (requiredType.isAssignableFrom(serviceClass)) {
                //如果出现子类型中的服务与已有存在的冲突了怎么办?
                Map map = services.get(serviceClass);
                for (String key : map.keySet()) {
                    Object service = map.get(key);
                    Object exist = all.get(key);
                    if (exist != null) {
                        logger.warn("A service instance: {} override another: {} with hint = {}", service, exist, key);
                    }
                    all.put(key, service);
                }
            }
        }
        return all;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy