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

com.zlyx.easyapi.ApiManager Maven / Gradle / Ivy

package com.zlyx.easyapi;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.zlyx.easyapi.annotations.ApiDesc;
import com.zlyx.easyapi.register.ApiRegister;
import com.zlyx.easyapi.utils.MapBeanUtils;
import com.zlyx.easycore.annotations.ApplicationBean;
import com.zlyx.easycore.event.IHandlerOnRefreshed;
import com.zlyx.easycore.list.Lists;
import com.zlyx.easycore.map.Maps;
import com.zlyx.easycore.tool.Ops;
import com.zlyx.easycore.utils.SpringUtils;
import com.zlyx.easylog.config.LogProperties;

import io.swagger.models.Swagger;

/**
 * @Auth 赵光
 * @Describle
 * @2019年7月30日 上午9:21:29
 */
@ApplicationBean(todo = { "" })
public class ApiManager implements IHandlerOnRefreshed {

	private static Map swaggers = Maps.newHashMap();

	private static Map, Object>> refs = Maps.newHashMap();

	private static Map, Object> interfaceMapRef = new ConcurrentHashMap, Object>();

	private static LogProperties logProperties;

	@Autowired
	public ApiManager(LogProperties logProperties) {
		ApiManager.logProperties = logProperties;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void doOnRefreshed(ApplicationContext context) {
		Map beans = Maps.newHashMap();
		beans.putAll(SpringUtils.getBeanswithAnnotation(ApiDesc.class));
		beans.putAll(SpringUtils.getBeansOfType(ApiRegister.class));
		Map, Object> beanMap = null;
		ApiDesc apiDesc = null;
		for (String name : beans.keySet()) {
			beanMap = Maps.newHashMap();
			if (beans.get(name) != null && beans.get(name) instanceof ApiRegister) {
				beanMap.putAll(((ApiRegister) beans.get(name)).register(context));
			}
			apiDesc = beans.get(name).getClass().getAnnotation(ApiDesc.class);
			if (apiDesc != null) {
				Class[] clses = apiDesc.clses();
				if (clses.length > 0) {
					for (Class cls : clses) {
						if (cls.isAnnotation()) {
							beanMap.putAll(MapBeanUtils
									.mapBeans(SpringUtils.getBeanswithAnnotation((Class) cls)));
						} else {
							beanMap.putAll(MapBeanUtils.mapBeans(SpringUtils.getBeansOfType(cls)));
						}
					}
				}
				if (Ops.isNotEmpty(apiDesc.name())) {
					name = apiDesc.name();
				}
			}
			if (!beanMap.isEmpty()) {
				refs.put(name, beanMap);
				swaggers.put(name, null);
				interfaceMapRef.putAll(beanMap);
			}
		}
	}

	public static Map, Object> interfaceMapRef(String groupName) {
		return refs.get(groupName);
	}

	/**
	 * 根据类名获取
	 * 
	 * @param interfaceClass
	 * @return
	 */
	public static Entry, Object> getRef(String interfaceClass) {
		Set, Object>> entrySet = interfaceMapRef.entrySet();
		for (Entry, Object> entry : entrySet) {
			if (entry.getKey().getName().equals(interfaceClass)) {
				return entry;
			}
		}
		return null;
	}

	public static Swagger getSwagger(String groupName) {
		return swaggers.get(groupName);
	}

	public static Map> getGroupNames() {
		return Maps.newHashMap(logProperties.getHost(), Lists.newArrayList(swaggers.keySet()));
	}

	public static void registerSwagger(String groupName, Swagger swagger) {
		swaggers.put(groupName, swagger);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy