com.diboot.core.holder.AnnotationRestApiHolder Maven / Gradle / Ivy
/*
* Copyright (c) 2015-2021, www.dibo.ltd ([email protected]).
*
* 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
*
* https://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.diboot.core.holder;
import com.diboot.core.cache.StaticMemoryCacheManager;
import com.diboot.core.config.Cons;
import com.diboot.core.holder.api.CollectThisApi;
import com.diboot.core.holder.api.RestApi;
import com.diboot.core.holder.api.RestApiWrapper;
import com.diboot.core.util.AnnotationUtils;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.ContextHolder;
import com.diboot.core.util.V;
import com.diboot.core.vo.ApiUri;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 注解 RestApi 信息缓存
* @author JerryMa
* @version v2.2.1
* @date 2021/4/23
* Copyright © diboot.com
*/
@Slf4j
public class AnnotationRestApiHolder {
/**
* 实体相关定义缓存管理器
*/
private static StaticMemoryCacheManager cacheManager;
/**
* 类-ApiWrapper缓存key
*/
private static final String CACHE_NAME_CLASS_TO_WRAPPER = "CLASS_TO_WRAPPER";
/**
* 类别-Api接口集合 缓存key
*/
private static final String CACHE_NAME_CATEGORY_TO_APILIST = "CATEGORY_TO_API_LIST";
private synchronized static StaticMemoryCacheManager getCacheManager(){
if(cacheManager == null){
cacheManager = new StaticMemoryCacheManager(
CACHE_NAME_CLASS_TO_WRAPPER,
CACHE_NAME_CATEGORY_TO_APILIST);
}
return cacheManager;
}
/**
* 获取默认分类的 RestApi 接口
* @return
*/
public static List getDefaultCategoryRestApiList(){
return getRestApiList("default");
}
/**
* 获取 RestApi 接口
* @param category
* @return
*/
public synchronized static List getRestApiList(String category){
List apiList = getCacheManager()
.getCacheObj(CACHE_NAME_CATEGORY_TO_APILIST, category, List.class);
if(apiList == null && getCacheManager().isUninitializedCache(CACHE_NAME_CATEGORY_TO_APILIST)){
initRestApiCache();
apiList = getCacheManager()
.getCacheObj(CACHE_NAME_CATEGORY_TO_APILIST, category, List.class);
}
return apiList;
}
/**
* 获取 RestApiWrapper 接口
* @param className
* @return
*/
public synchronized static RestApiWrapper getRestApiWrapper(String className){
RestApiWrapper apiWrapper = getCacheManager()
.getCacheObj(CACHE_NAME_CLASS_TO_WRAPPER, className, RestApiWrapper.class);
if(apiWrapper == null && getCacheManager().isUninitializedCache(CACHE_NAME_CLASS_TO_WRAPPER)){
initRestApiCache();
apiWrapper = getCacheManager()
.getCacheObj(CACHE_NAME_CLASS_TO_WRAPPER, className, RestApiWrapper.class);
}
return apiWrapper;
}
/**
* 初始化
*/
private synchronized static void initRestApiCache() {
List