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

org.nervousync.cache.provider.ProviderManager Maven / Gradle / Ivy

package org.nervousync.cache.provider;

import org.nervousync.cache.annotation.CacheProvider;
import org.nervousync.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.ServiceLoader;

public final class ProviderManager {

	/**
	 * Logger instance
	 * 日志实例
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(ProviderManager.class);

	/**
	 * Registered cache provider map
	 * 注册的缓存实现类与名称对应关系
	 */
	private static final Hashtable> REGISTERED_PROVIDERS = new Hashtable<>();

	static {
		//  Register all cache providers by Java SPI
		ServiceLoader.load(Provider.class).forEach(provider -> {
			if (provider.getClass().isAnnotationPresent(CacheProvider.class)) {
				registerProvider(provider.getClass());
			}
		});
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("Registered cache provider number: {}", REGISTERED_PROVIDERS.size());
		}
	}

	/**
	 * 

Retrieve registered provider name list

*

读取已注册的缓存适配器名称列表

* * @return Registered provider name list * 已注册的缓存适配器名称列表 */ public static List registeredProviderNames() { return new ArrayList<>(REGISTERED_PROVIDERS.keySet()); } /** *

Check register status of given provider name

*

检查给定的缓存适配器名称是否已经注册

* * @param providerName Cache provider name * 缓存适配器名称 * @return Register status * 注册状态 */ public static boolean registeredProvider(final String providerName) { if (StringUtils.isEmpty(providerName)) { return Boolean.FALSE; } return REGISTERED_PROVIDERS.containsKey(providerName); } /** *

Retrieve provider class by given provider name

*

根据指定的适配器名称获取注册的适配器类

* * @param providerName Cache provider name * 缓存适配器名称 * @return Register provider class * 注册适配器类 */ public static Class providerClass(final String providerName) { if (StringUtils.isEmpty(providerName)) { return null; } return REGISTERED_PROVIDERS.get(providerName); } /** *

Register cache provider implement class manual

*

注册缓存适配器

* * @param providerClass Cache provider implements class * 缓存适配器实现类 */ private static void registerProvider(final Class providerClass) { if (providerClass != null && providerClass.isAnnotationPresent(CacheProvider.class)) { CacheProvider cacheProvider = providerClass.getAnnotation(CacheProvider.class); if (REGISTERED_PROVIDERS.containsKey(cacheProvider.name())) { LOGGER.warn("Override cache provider name: {}, replace class: {}, new class: {}", cacheProvider.name(), REGISTERED_PROVIDERS.get(cacheProvider.name()).getName(), providerClass.getName()); } REGISTERED_PROVIDERS.put(cacheProvider.name(), providerClass); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy