parent
cacce79dca
commit
fefa3469fd
6 changed files with 105 additions and 0 deletions
@ -0,0 +1,63 @@ |
||||
package org.anyin.gitee.shiro.extension; |
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.beans.BeansException; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.ApplicationContextAware; |
||||
import org.springframework.util.Assert; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
@Slf4j |
||||
public class DefaultExtensionHandlerFactoryImpl implements IExtensionHandlerFactory, ApplicationContextAware { |
||||
|
||||
private final Map<Enum, List<IExtensionHandler>> serviceListCache = new ConcurrentHashMap<>(); |
||||
private final Map<ExtensionCacheKey, IExtensionHandler> serviceCache = new ConcurrentHashMap<>(); |
||||
|
||||
@Override |
||||
public <Y extends Enum<Y>> void addExtensionHandler(IExtensionHandler<Y> extensionHandler) { |
||||
Assert.notNull(extensionHandler.extension(), "add extension handler failed, bean class " + extensionHandler.getClass().getName() + " extension is null"); |
||||
serviceListCache.putIfAbsent(extensionHandler.extension(), new LinkedList<>()); |
||||
serviceListCache.get(extensionHandler.extension()).add(extensionHandler); |
||||
} |
||||
|
||||
@Override |
||||
public <T extends IExtensionHandler<Y>, Y extends Enum<Y>> T getExtensionHandler(Y extension, Class<T> type) { |
||||
ExtensionCacheKey<Y> cacheKey = new ExtensionCacheKey(extension, type); |
||||
IExtensionHandler result = this.serviceCache.get(cacheKey); |
||||
if (result == null) { |
||||
List<IExtensionHandler> payPlatformAbles = serviceListCache.getOrDefault(extension, Collections.synchronizedList(Collections.emptyList())); |
||||
for (IExtensionHandler payPlatformAble : payPlatformAbles) { |
||||
if (type.isAssignableFrom(payPlatformAble.getClass())) { |
||||
result = payPlatformAble; |
||||
serviceCache.put(cacheKey, result); |
||||
break; |
||||
} |
||||
} |
||||
if (result == null) { |
||||
log.warn("No IExtensionHandler found by CacheKey : " + cacheKey + " !"); |
||||
} |
||||
} |
||||
return (T) result; |
||||
} |
||||
|
||||
@Override |
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
||||
Map<String,IExtensionHandler> handlerMap = applicationContext.getBeansOfType(IExtensionHandler.class); |
||||
log.info("疯狂加载扩展ing ..."); |
||||
long start = System.currentTimeMillis(); |
||||
handlerMap.forEach((k, v) -> { |
||||
//排除组合类自己
|
||||
if (!this.getClass().isAssignableFrom(v.getClass())) { |
||||
addExtensionHandler(v); |
||||
} |
||||
}); |
||||
long end = System.currentTimeMillis(); |
||||
log.info("加载扩展点结束,耗时: {}, 扩展点个数: {}", end - start, handlerMap.size()); |
||||
} |
||||
} |
@ -0,0 +1,16 @@ |
||||
package org.anyin.gitee.shiro.extension; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
import lombok.ToString; |
||||
|
||||
@Data |
||||
@AllArgsConstructor |
||||
@ToString |
||||
@EqualsAndHashCode |
||||
public class ExtensionCacheKey<T extends Enum> { |
||||
|
||||
private T extension; |
||||
private Class<? extends IExtensionHandler<T>> extensionHandlerClass; |
||||
} |
@ -0,0 +1,6 @@ |
||||
package org.anyin.gitee.shiro.extension; |
||||
|
||||
public interface IExtensionHandler<Y extends Enum>{ |
||||
|
||||
Y extension(); |
||||
} |
@ -0,0 +1,8 @@ |
||||
package org.anyin.gitee.shiro.extension; |
||||
|
||||
public interface IExtensionHandlerFactory { |
||||
|
||||
<Y extends Enum<Y>>void addExtensionHandler(IExtensionHandler<Y> extensionHandler); |
||||
|
||||
<T extends IExtensionHandler<Y>,Y extends Enum<Y>> T getExtensionHandler(Y extension, Class<T> type); |
||||
} |
Loading…
Reference in new issue