com.github.dennisit.vplus.data.dsource.shard.ShardFactory Maven / Gradle / Ivy
package com.github.dennisit.vplus.data.dsource.shard;
import com.github.dennisit.vplus.data.dsource.shard.strategy.ShardStrategy;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class ShardFactory implements FactoryBean, ApplicationContextAware, InitializingBean {
private ShardContext shardContext;
private List shardStrategies = Lists.newArrayList();
public ShardFactory() {
}
public ShardContext getObject() throws Exception {
return this.shardContext;
}
public Class> getObjectType() {
return this.shardContext == null ? ShardContext.class : this.shardContext.getClass();
}
public boolean isSingleton() {
return true;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map shardStrategyBeans = applicationContext.getBeansOfType(ShardStrategy.class);
Iterator i$ = shardStrategyBeans.keySet().iterator();
while (i$.hasNext()) {
String key = (String) i$.next();
this.shardStrategies.add(shardStrategyBeans.get(key));
}
}
public void afterPropertiesSet() throws Exception {
log.debug("初始化化 ShardFactory");
this.shardContext = new ShardContext();
if (this.shardStrategies.isEmpty()) {
throw new IllegalArgumentException("未知散表算法");
} else {
Iterator i$ = this.shardStrategies.iterator();
while (i$.hasNext()) {
ShardStrategy shardStrategy = (ShardStrategy) i$.next();
this.shardContext.addShardStrategy(shardStrategy);
}
}
}
}