fix 添加扩展点设计

master
陈景阳 4 years ago
parent cacce79dca
commit fefa3469fd
  1. 5
      pom.xml
  2. 7
      src/main/java/org/anyin/gitee/shiro/config/AppConfig.java
  3. 63
      src/main/java/org/anyin/gitee/shiro/extension/DefaultExtensionHandlerFactoryImpl.java
  4. 16
      src/main/java/org/anyin/gitee/shiro/extension/ExtensionCacheKey.java
  5. 6
      src/main/java/org/anyin/gitee/shiro/extension/IExtensionHandler.java
  6. 8
      src/main/java/org/anyin/gitee/shiro/extension/IExtensionHandlerFactory.java

@ -46,6 +46,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<dependencyManagement>

@ -2,6 +2,8 @@ package org.anyin.gitee.shiro.config;
import org.anyin.gitee.shiro.advisor.ApiMessageAdvisor;
import org.anyin.gitee.shiro.base.MdcExecutor;
import org.anyin.gitee.shiro.extension.DefaultExtensionHandlerFactoryImpl;
import org.anyin.gitee.shiro.extension.IExtensionHandlerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@ -17,6 +19,11 @@ public class AppConfig {
return new ApiMessageAdvisor();
}
@Bean
public IExtensionHandlerFactory extensionHandlerFactory(){
return new DefaultExtensionHandlerFactoryImpl();
}
@Bean
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

@ -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…
Cancel
Save