001/* 002 * Copyright 1999-2018 Alibaba Group Holding Ltd. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.support.sentinel; 017 018import com.alibaba.csp.sentinel.util.StringUtil; 019 020import java.lang.reflect.Method; 021import java.util.Map; 022import java.util.concurrent.ConcurrentHashMap; 023 024/** 025 * Registry for resource configuration metadata (e.g. fallback method) 026 * 027 * @author Eric Zhao 028 */ 029final class ResourceMetadataRegistry { 030 031 private static final Map<String, MethodWrapper> FALLBACK_MAP = new ConcurrentHashMap<>(); 032 private static final Map<String, MethodWrapper> DEFAULT_FALLBACK_MAP = new ConcurrentHashMap<>(); 033 private static final Map<String, MethodWrapper> BLOCK_HANDLER_MAP = new ConcurrentHashMap<>(); 034 035 static MethodWrapper lookupFallback(Class<?> clazz, String name) { 036 return FALLBACK_MAP.get(getKey(clazz, name)); 037 } 038 039 static MethodWrapper lookupDefaultFallback(Class<?> clazz, String name) { 040 return DEFAULT_FALLBACK_MAP.get(getKey(clazz, name)); 041 } 042 043 static MethodWrapper lookupBlockHandler(Class<?> clazz, String name) { 044 return BLOCK_HANDLER_MAP.get(getKey(clazz, name)); 045 } 046 047 static void updateFallbackFor(Class<?> clazz, String name, Method method) { 048 if (clazz == null || StringUtil.isBlank(name)) { 049 throw new IllegalArgumentException("Bad argument"); 050 } 051 FALLBACK_MAP.put(getKey(clazz, name), MethodWrapper.wrap(method)); 052 } 053 054 static void updateDefaultFallbackFor(Class<?> clazz, String name, Method method) { 055 if (clazz == null || StringUtil.isBlank(name)) { 056 throw new IllegalArgumentException("Bad argument"); 057 } 058 DEFAULT_FALLBACK_MAP.put(getKey(clazz, name), MethodWrapper.wrap(method)); 059 } 060 061 static void updateBlockHandlerFor(Class<?> clazz, String name, Method method) { 062 if (clazz == null || StringUtil.isBlank(name)) { 063 throw new IllegalArgumentException("Bad argument"); 064 } 065 BLOCK_HANDLER_MAP.put(getKey(clazz, name), MethodWrapper.wrap(method)); 066 } 067 068 private static String getKey(Class<?> clazz, String name) { 069 return clazz.getCanonicalName() + ":" + name; 070 } 071 072 /** 073 * Only for internal test. 074 */ 075 static void clearFallbackMap() { 076 FALLBACK_MAP.clear(); 077 } 078 079 /** 080 * Only for internal test. 081 */ 082 static void clearBlockHandlerMap() { 083 BLOCK_HANDLER_MAP.clear(); 084 } 085}