RouteStrategyFactory.java
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package io.mycat.route.factory;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import io.mycat.MycatServer;
import io.mycat.config.model.SystemConfig;
import io.mycat.route.RouteStrategy;
import io.mycat.route.impl.DruidMycatRouteStrategy;
/**
* 路由策略工厂类
* @author wang.dw
*
*/
public class RouteStrategyFactory {
private static RouteStrategy defaultStrategy = null;
private static volatile boolean isInit = false;
private static ConcurrentMap<String,RouteStrategy> strategyMap = new ConcurrentHashMap<String,RouteStrategy>();
public static void init() {
SystemConfig config = MycatServer.getInstance().getConfig().getSystem();
String defaultSqlParser = config.getDefaultSqlParser();
defaultSqlParser = defaultSqlParser == null ? "" : defaultSqlParser;
//修改为ConcurrentHashMap,避免并发问题
strategyMap.putIfAbsent("druidparser", new DruidMycatRouteStrategy());
defaultStrategy = strategyMap.get(defaultSqlParser);
if(defaultStrategy == null) {
defaultStrategy = strategyMap.get("druidparser");
defaultSqlParser = "druidparser";
}
config.setDefaultSqlParser(defaultSqlParser);
isInit = true;
}
private RouteStrategyFactory() {
}
public static RouteStrategy getRouteStrategy() {
// if(!isInit) {
// synchronized(RouteStrategyFactory.class){
// if(!isInit){
// init();
// }
// }
// }
return defaultStrategy;
}
public static RouteStrategy getRouteStrategy(String parserType) {
// if(!isInit) {
// synchronized(RouteStrategyFactory.class){
// if(!isInit){
// init();
// }
// }
// }
return strategyMap.get(parserType);
}
}