Skip to content

Commit 4b42185

Browse files
authored
Merge pull request #46 from hellojavaer/1.0.x
1.0.x
2 parents f3a7e64 + 8bb45c4 commit 4b42185

File tree

9 files changed

+106
-34
lines changed

9 files changed

+106
-34
lines changed

ddal-datasource/src/main/java/org/hellojavaer/ddal/datasource/DefaultDDALDataSource.java

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
import javax.sql.DataSource;
2828
import java.io.*;
29+
import java.lang.reflect.InvocationTargetException;
30+
import java.lang.reflect.Method;
2931
import java.net.HttpURLConnection;
32+
import java.net.MalformedURLException;
3033
import java.net.URL;
3134
import java.net.URLEncoder;
3235
import java.sql.Connection;
@@ -54,9 +57,7 @@ public DefaultDDALDataSource(String url) {
5457
}
5558

5659
/**
57-
*
5860
* jdbc:ddal:thick:
59-
*
6061
* jdbc:ddal:thin:
6162
*
6263
*/
@@ -71,8 +72,8 @@ public DefaultDDALDataSource(String url, String user, String password) {
7172
throw new IllegalArgumentException("url must be start with '" + JDBC_DDAL_PROTOCOL_PREFIX + "'");
7273
}
7374
String url1 = url.substring(JDBC_DDAL_PROTOCOL_PREFIX.length()).trim();
74-
ApplicationContext context;
75-
if (url1.startsWith(THICK_PROTOCOL_PREFIX)) {
75+
if (url1.startsWith(THICK_PROTOCOL_PREFIX)) {// jdbc:ddal:thick:
76+
ApplicationContext context;
7677
String url2 = url1.substring(THICK_PROTOCOL_PREFIX.length());
7778
if (url2.startsWith("classpath:") || url2.startsWith("classpath*:")) {
7879
context = new ClassPathXmlApplicationContext(url2);
@@ -91,15 +92,58 @@ public DefaultDDALDataSource(String url, String user, String password) {
9192
} else {
9293
throw new IllegalArgumentException("Unsupported protocol " + url);
9394
}
94-
} else if (url1.startsWith(THIN_PROTOCOL_PREFIX)) {
95+
this.dataSource = getBean(context, "dataSource", DataSource.class);
96+
this.sequence = getBean(context, "sequence", Sequence.class);
97+
this.shardRouter = getBean(context, "shardRouter", ShardRouter.class);
98+
} else if (url1.startsWith(THIN_PROTOCOL_PREFIX)) {// jdbc:ddal:thin:
9599
// TODO
96-
throw new IllegalArgumentException("Unsupported protocol jdbc:ddal:thin:");
97-
} else {
98100
throw new IllegalArgumentException("Unsupported protocol " + url);
101+
} else { // jdbc:ddal:
102+
procCustomProtocol(url);
103+
}
104+
}
105+
106+
private void procCustomProtocol(String url) {
107+
try {
108+
URL url0 = new URL(url);
109+
Object obj = url0.getContent();
110+
if (obj == null) {
111+
return;
112+
}
113+
Class<?> clazz = obj.getClass();
114+
Method getDataSourceMethod = getMethod(clazz, "getDataSource");
115+
if (getDataSourceMethod != null) {
116+
this.dataSource = (DataSource) getDataSourceMethod.invoke(obj);
117+
}
118+
Method getSequenceMethod = getMethod(clazz, "getSequence");
119+
if (getSequenceMethod != null) {
120+
this.sequence = (Sequence) getSequenceMethod.invoke(obj);
121+
}
122+
Method getShardRouterMethod = getMethod(clazz, "getShardRouter");
123+
if (getShardRouterMethod != null) {
124+
this.shardRouter = (ShardRouter) getShardRouterMethod.invoke(obj);
125+
}
126+
} catch (MalformedURLException e) {
127+
throw new RuntimeException(e);
128+
} catch (IOException e) {
129+
throw new RuntimeException(e);
130+
} catch (InvocationTargetException e) {
131+
throw new RuntimeException(e);
132+
} catch (IllegalAccessException e) {
133+
throw new RuntimeException(e);
134+
}
135+
}
136+
137+
private Method getMethod(Class<?> clazz, String methodName) {
138+
try {
139+
Method method = clazz.getMethod(methodName);
140+
if (method.isAccessible() == false) {
141+
method.setAccessible(true);
142+
}
143+
return method;
144+
} catch (NoSuchMethodException e) {
145+
return null;
99146
}
100-
this.dataSource = getBean(context, "dataSource", DataSource.class);
101-
this.sequence = getBean(context, "sequence", Sequence.class);
102-
this.shardRouter = getBean(context, "shardRouter", ShardRouter.class);
103147
}
104148

105149
private <T> T getBean(ApplicationContext context, String name, Class<T> requiredType) {

ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/datasource/jdbc/DefaultDDRDataSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ public DataSourceManager getDataSourceManager() {
5454
return dataSourceManager;
5555
}
5656

57-
private void setDataSourceManager(DataSourceManager dataSourceManager) {
57+
public void setDataSourceManager(DataSourceManager dataSourceManager) {
5858
this.dataSourceManager = dataSourceManager;
5959
}
6060

6161
public ShardParser getShardParser() {
6262
return shardParser;
6363
}
6464

65-
private void setShardParser(ShardParser shardParser) {
65+
public void setShardParser(ShardParser shardParser) {
6666
this.shardParser = shardParser;
6767
}
6868

ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/datasource/manager/rw/DefaultReadWriteDataSourceManager.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public synchronized List<ReadOnlyDataSourceBinding> getReadOnlyDataSources() {
110110
return readOnlyDataSources;
111111
}
112112

113-
private synchronized void setReadOnlyDataSources(List<ReadOnlyDataSourceBinding> readOnlyDataSources) {
113+
public synchronized void setReadOnlyDataSources(List<ReadOnlyDataSourceBinding> readOnlyDataSources) {
114114
initReadOnlyDataSource(readOnlyDataSources);
115115
check(readOnlyDataSourceIndexCacheOriginalValues);
116116
this.readOnlyDataSources = readOnlyDataSources;
@@ -121,15 +121,15 @@ public ReadOnlyDataSourceMonitorServer getReadOnlyDataSourceMonitorServer() {
121121
return this.readOnlyDataSourceMonitorServer;
122122
}
123123

124-
private void setReadOnlyDataSourceMonitorServer(ReadOnlyDataSourceMonitorServer readOnlyDataSourceMonitorServer) {
124+
public void setReadOnlyDataSourceMonitorServer(ReadOnlyDataSourceMonitorServer readOnlyDataSourceMonitorServer) {
125125
this.readOnlyDataSourceMonitorServer = readOnlyDataSourceMonitorServer;
126126
}
127127

128128
public synchronized List<WriteOnlyDataSourceBinding> getWriteOnlyDataSources() {
129129
return writeOnlyDataSources;
130130
}
131131

132-
private synchronized void setWriteOnlyDataSources(List<WriteOnlyDataSourceBinding> writeOnlyDataSources) {
132+
public synchronized void setWriteOnlyDataSources(List<WriteOnlyDataSourceBinding> writeOnlyDataSources) {
133133
initWriteOnlyDataSource(writeOnlyDataSources);
134134
check(writeOnlyDataSourceQueryCache);
135135
this.writeOnlyDataSources = writeOnlyDataSources;
@@ -139,15 +139,15 @@ public ShardRouter getShardRouter() {
139139
return shardRouter;
140140
}
141141

142-
private void setShardRouter(ShardRouter shardRouter) {
142+
public void setShardRouter(ShardRouter shardRouter) {
143143
this.shardRouter = shardRouter;
144144
}
145145

146146
public MetaDataChecker getMetaDataChecker() {
147147
return metaDataChecker;
148148
}
149149

150-
private void setMetaDataChecker(MetaDataChecker metaDataChecker) {
150+
public void setMetaDataChecker(MetaDataChecker metaDataChecker) {
151151
this.metaDataChecker = metaDataChecker;
152152
}
153153

ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/shard/rule/SpelShardRouteRule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public SpelShardRouteRule(String scRouteRule, String tbRouteRule, Integer rangeS
6464
this.rangeSizeLimit = rangeSizeLimit;
6565
}
6666

67-
private void setScRouteRule(String scRouteRule) {
67+
public void setScRouteRule(String scRouteRule) {
6868
scRouteRule = filter(scRouteRule);
6969
this.scRouteRule = scRouteRule;
7070
if (scRouteRule != null) {
@@ -77,7 +77,7 @@ public String getScRouteRule() {
7777
return scRouteRule;
7878
}
7979

80-
private void setTbRouteRule(String tbRouteRule) {
80+
public void setTbRouteRule(String tbRouteRule) {
8181
tbRouteRule = filter(tbRouteRule);
8282
this.tbRouteRule = tbRouteRule;
8383
if (tbRouteRule != null) {
@@ -94,7 +94,7 @@ public Integer getRangeSizeLimit() {
9494
return rangeSizeLimit;
9595
}
9696

97-
private void setRangeSizeLimit(Integer rangeSizeLimit) {
97+
public void setRangeSizeLimit(Integer rangeSizeLimit) {
9898
this.rangeSizeLimit = rangeSizeLimit;
9999
}
100100

ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/shard/simple/SimpleShardParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public ShardRouter getShardRouter() {
4444
return shardRouter;
4545
}
4646

47-
private void setShardRouter(ShardRouter shardRouter) {
47+
public void setShardRouter(ShardRouter shardRouter) {
4848
this.shardRouter = shardRouter;
4949
}
5050

5151
public SQLParser getSqlParser() {
5252
return sqlParser;
5353
}
5454

55-
private void setSqlParser(SQLParser sqlParser) {
55+
public void setSqlParser(SQLParser sqlParser) {
5656
this.sqlParser = sqlParser;
5757
}
5858

ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/shard/simple/SimpleShardRouter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public List<SimpleShardRouteRuleBinding> getRouteRuleBindings() {
4848
return routeRuleBindings;
4949
}
5050

51-
private void setRouteRuleBindings(List<SimpleShardRouteRuleBinding> bindings) {
51+
public void setRouteRuleBindings(List<SimpleShardRouteRuleBinding> bindings) {
5252
Map<String, InnerSimpleShardRouteRuleBindingWrapper> cache = new HashMap<>();
5353
Map<String, List<ShardRouteInfo>> routeInfoMap = new HashMap<>();
5454
Map<String, Set<String>> routedTables = new HashMap<>();

ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/sqlparse/cache/LRUSQLParserCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public Integer getCapacity() {
4444
return capacity;
4545
}
4646

47-
private void setCapacity(Integer capacity) {
47+
public void setCapacity(Integer capacity) {
4848
this.capacity = capacity;
4949
}
5050

5151
public SQLParser getSqlParser() {
5252
return sqlParser;
5353
}
5454

55-
private void setSqlParser(SQLParser sqlParser) {
55+
public void setSqlParser(SQLParser sqlParser) {
5656
this.sqlParser = sqlParser;
5757
}
5858

ddal-jsqlparser/src/main/java/org/hellojavaer/ddal/jsqlparser/JSQLParserAdapter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ private static void checkJSqlParserFeature() throws JSQLParserException {
9393
JdbcParameter jdbcParameter = (JdbcParameter) equalsTo.getRightExpression();
9494
Integer index1 = jdbcParameter.getIndex();
9595
if (index1 != 1) {
96-
throw new IllegalStateException(
97-
"Current version of JSQLParser dones't support the feture of 'support get jdbc parameter index'");
96+
throw new IllegalStateException("Current version of JSQLParser doesn't support the feature of 'support "
97+
+ "get jdbc parameter index'");
9898
}
9999
//
100100
InExpression inExpression = (InExpression) andExpression.getRightExpression();
@@ -104,20 +104,20 @@ private static void checkJSqlParserFeature() throws JSQLParserException {
104104
LikeExpression likeExpression = (LikeExpression) subAndExpression.getLeftExpression();
105105
if (((JdbcParameter) likeExpression.getRightExpression()).getIndex() != 2) {
106106
throw new IllegalStateException(
107-
"Current version of JSQLParser dones't support the feture of 'support get jdbc parameter index'");
107+
"Current version of JSQLParser doesn't support the feature of 'support get jdbc parameter index'");
108108
}
109109
//
110110
GreaterThan greaterThan = (GreaterThan) subAndExpression.getRightExpression();
111111
if (((JdbcParameter) greaterThan.getRightExpression()).getIndex() != 3) {
112112
throw new IllegalStateException(
113-
"Current version of JSQLParser dones't support the feture of 'support get jdbc parameter index'");
113+
"Current version of JSQLParser doesn't support the feature of 'support get jdbc parameter index'");
114114
}
115115
//
116116
Expression offset = selectBody.getLimit().getOffset();
117117
Expression rowCount = selectBody.getLimit().getRowCount();
118118
if (((JdbcParameter) offset).getIndex() != 4 || ((JdbcParameter) rowCount).getIndex() != 5) {
119119
throw new IllegalStateException(
120-
"Current version of JSQLParser dones't support the feture of 'support get jdbc parameter index'");
120+
"Current version of JSQLParser doesn't support the feature of 'support get jdbc parameter index'");
121121
}
122122
}
123123

@@ -711,15 +711,15 @@ public TableWrapper(Table table, ShardRouteConfig routeConfig) {
711711
}
712712
}
713713

714-
private Table table;
714+
private Table table;
715715

716-
private Table originalConfig = new Table();
716+
private Table originalConfig = new Table();
717717

718718
private ShardRouteConfig routeConfig; // route config info
719719

720-
private String routedFullTableName; // 由routeInfo计算出,如果有sql路由时该字段不为空,如果该参数为空,表示需要jdbc路由
720+
private String routedFullTableName; // 由routeInfo计算出,如果有sql路由时该字段不为空,如果该参数为空,表示需要jdbc路由
721721

722-
private List<Object> jdbcParamKeys = new ArrayList<>(); // table 关联的jdbc列
722+
private List<Object> jdbcParamKeys = new ArrayList<>(); // table 关联的jdbc列
723723

724724
public ShardRouteConfig getRouteConfig() {
725725
return routeConfig;

ddal-server/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
<module>ddal-server-datasource</module>
1818
</modules>
1919

20+
<properties>
21+
<maven.test.skip>true</maven.test.skip>
22+
<jdk.version>1.7</jdk.version>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
</properties>
25+
2026
<developers>
2127
<developer>
2228
<id>hellojavaer</id>
@@ -42,8 +48,30 @@
4248
</license>
4349
</licenses>
4450
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.2</version>
56+
<configuration>
57+
<source>${jdk.version}</source>
58+
<target>${jdk.version}</target>
59+
<encoding>${project.build.sourceEncoding}</encoding>
60+
</configuration>
61+
</plugin>
62+
</plugins>
4563
<pluginManagement>
4664
<plugins>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-install-plugin</artifactId>
68+
<executions>
69+
<execution>
70+
<id>default-install</id>
71+
<phase>none</phase>
72+
</execution>
73+
</executions>
74+
</plugin>
4775
<plugin>
4876
<groupId>org.apache.maven.plugins</groupId>
4977
<artifactId>maven-deploy-plugin</artifactId>

0 commit comments

Comments
 (0)