Skip to content

Commit 10ff663

Browse files
committed
优化代码
1 parent b4fe979 commit 10ff663

File tree

6 files changed

+92
-40
lines changed

6 files changed

+92
-40
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"groups": [
3+
{
4+
"name": "hsweb.access-logger",
5+
"type": "java.lang.String",
6+
"sourceType": "java.lang.String"
7+
}
8+
],
9+
"properties": [
10+
{
11+
"name": "hsweb.access-logger",
12+
"type": "java.lang.String",
13+
"sourceType": "java.lang.String"
14+
}
15+
],
16+
"hints": [
17+
{
18+
"name": "hsweb.access-logger",
19+
"values": [
20+
{
21+
"value": "on",
22+
"description": "enable access logger."
23+
},
24+
{
25+
"value": "off",
26+
"description": "enable access logger."
27+
}
28+
]
29+
}
30+
]
31+
}

hsweb-web-core/src/main/java/org/hsweb/web/core/Install.java

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,39 @@
44
import org.hsweb.ezorm.executor.SqlExecutor;
55
import org.hsweb.ezorm.render.SqlAppender;
66
import org.hsweb.ezorm.render.support.simple.SimpleSQL;
7-
import org.slf4j.Logger;
8-
import org.slf4j.LoggerFactory;
7+
import org.hsweb.web.core.datasource.DataSourceHolder;
98
import org.springframework.beans.factory.annotation.Autowired;
10-
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
11-
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
12-
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
13-
import org.springframework.boot.context.properties.ConfigurationProperties;
14-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
159
import org.springframework.context.annotation.Configuration;
16-
import org.springframework.core.io.ClassPathResource;
1710
import org.springframework.core.io.Resource;
1811
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
1912
import org.springframework.util.Assert;
2013

2114
import javax.annotation.PostConstruct;
22-
import javax.sql.DataSource;
23-
import java.io.*;
15+
import java.io.BufferedReader;
16+
import java.io.IOException;
17+
import java.io.InputStream;
18+
import java.io.InputStreamReader;
19+
import java.sql.Connection;
2420
import java.util.ArrayList;
2521
import java.util.List;
2622

2723
@Configuration
28-
@EnableConfigurationProperties({DataSourceProperties.class})
29-
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
30-
@ConfigurationProperties(
31-
prefix = "spring.datasource"
32-
)
3324
public class Install {
34-
private static String DATABASE_TYPE = "h2";
35-
3625
/**
3726
* 获取当前数据库类型
3827
*
3928
* @return
4029
*/
4130
public static String getDatabaseType() {
42-
return DATABASE_TYPE;
31+
return DataSourceHolder.getActiveDatabaseType().name();
4332
}
4433

45-
@Autowired
46-
private DataSourceProperties properties;
47-
4834
@Autowired
4935
private SqlExecutor sqlExecutor;
5036

51-
private Logger logger = LoggerFactory.getLogger(this.getClass());
52-
5337
@PostConstruct
5438
public void install() throws Exception {
55-
String dc = properties.getDriverClassName();
56-
String dbType = dc.contains("mysql") ? "mysql" : dc.contains("oracle") ? "oracle" : dc.contains("h2") ? "h2" : null;
57-
DATABASE_TYPE = dbType;
39+
String dbType = DataSourceHolder.getActiveDatabaseType().name();
5840
Assert.notNull(dbType, "不支持的数据库类型");
5941
try {
6042
boolean firstInstall = false;
@@ -80,14 +62,22 @@ public void install() throws Exception {
8062
}
8163
}
8264

83-
protected void execInstallSql(InputStream sqlStream) throws UnsupportedEncodingException {
84-
String username = properties.getUsername();
65+
protected void execInstallSql(InputStream sqlStream) throws Exception {
66+
String username = "";
67+
Connection connection = null;
68+
try {
69+
connection = DataSourceHolder.getActiveSource().getConnection();
70+
username = connection.getMetaData().getUserName();
71+
} finally {
72+
if (null != connection) connection.close();
73+
}
8574
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(sqlStream, "utf-8"));
8675
List<String> sqlList = new ArrayList<>();
8776
SqlAppender tmp = new SqlAppender();
77+
String uname = username;
8878
bufferedReader.lines().forEach((line) -> {
8979
if (line.startsWith("--")) return;
90-
line = line.replace("${jdbc.username}", username);
80+
line = line.replace("${jdbc.username}", uname);
9181
//去除sql中的;
9282
if (line.endsWith(";"))
9383
tmp.add(line.substring(0, line.length() - 1));

hsweb-web-core/src/main/java/org/hsweb/web/core/datasource/DynamicDataSource.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,76 @@
1717
package org.hsweb.web.core.datasource;
1818

1919
import org.hsweb.web.core.utils.ThreadLocalUtils;
20-
import org.springframework.jca.cci.connection.ConnectionHolder;
21-
import org.springframework.transaction.support.TransactionSynchronizationManager;
2220

23-
import javax.sql.CommonDataSource;
2421
import javax.sql.DataSource;
2522

23+
/**
24+
* 动态数据源接口,此接口实现多数据源的动态切换
25+
*
26+
* @see DataSourceHolder
27+
*/
2628
public interface DynamicDataSource extends DataSource {
2729
String DATA_SOURCE_FLAG = "data-source-id";
2830

2931
String DATA_SOURCE_FLAG_LAST = "data-source-id-last";
3032

33+
/**
34+
* 使用上一次调用的数据源
35+
*/
3136
static void useLast() {
3237
use(ThreadLocalUtils.get(DATA_SOURCE_FLAG_LAST));
3338
}
3439

40+
/**
41+
* 选中参数(数据源ID)对应的数据源,如果数据源不存在,将使用默认数据源
42+
*
43+
* @param dataSourceId 数据源ID
44+
*/
3545
static void use(String dataSourceId) {
3646
ThreadLocalUtils.put(DATA_SOURCE_FLAG, dataSourceId);
3747
}
3848

49+
/**
50+
* 获取当前使用的数据源ID,如果不存在则返回null
51+
*
52+
* @return 数据源ID
53+
*/
3954
static String getActiveDataSourceId() {
4055
return ThreadLocalUtils.get(DATA_SOURCE_FLAG);
4156
}
4257

58+
/**
59+
* 切换为默认数据源,并指定是否记住上一次选中的数据源
60+
*
61+
* @param rememberLast 是否记住上一次选中的数据源
62+
*/
4363
static void useDefault(boolean rememberLast) {
4464
if (getActiveDataSourceId() != null && rememberLast)
4565
ThreadLocalUtils.put(DATA_SOURCE_FLAG_LAST, getActiveDataSourceId());
4666
ThreadLocalUtils.remove(DATA_SOURCE_FLAG);
4767
}
4868

69+
/**
70+
* 切换为默认数据源并记住上一次使用的数据源
71+
*
72+
* @see this#useDefault(boolean)
73+
*/
4974
static void useDefault() {
5075
useDefault(true);
5176
}
5277

78+
/**
79+
* 获取当前激活的数据源
80+
*
81+
* @return
82+
*/
5383
DataSource getActiveDataSource();
5484

85+
/**
86+
* 获取当前激活数据源的数据库类型
87+
*
88+
* @return 数据库类型
89+
* @see DatabaseType
90+
*/
5591
DatabaseType getActiveDataBaseType();
5692
}

hsweb-web-core/src/main/java/org/hsweb/web/core/logger/Slf4jAccessLoggerPersisting.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.stereotype.Component;
1010

11-
/**
12-
* Created by zhouhao on 16-4-28.
13-
*/
14-
@Component
1511
public class Slf4jAccessLoggerPersisting implements AccessLoggerPersisting {
1612
protected Logger logger = LoggerFactory.getLogger(this.getClass());
1713
@Autowired(required = false)

hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicDataSourceProperties.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.hsweb.web.datasource.dynamic;
1818

19-
import com.atomikos.datasource.pool.ConnectionPool;
2019
import com.atomikos.jdbc.AtomikosDataSourceBean;
2120
import org.hsweb.web.core.datasource.DatabaseType;
2221
import org.springframework.beans.factory.BeanClassLoaderAware;
@@ -26,7 +25,6 @@
2625
import org.springframework.util.ClassUtils;
2726
import org.springframework.util.StringUtils;
2827

29-
import java.io.PrintWriter;
3028
import java.sql.SQLException;
3129
import java.util.LinkedList;
3230
import java.util.List;

hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicXaDataSourceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import java.sql.SQLException;
3434

3535
public class DynamicXaDataSourceImpl extends AbstractDataSource implements DynamicDataSource, XADataSource, Closeable {
36-
private Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);
37-
private javax.sql.DataSource defaultDataSource;
36+
private Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);
37+
private javax.sql.DataSource defaultDataSource = null;
3838
private DatabaseType defaultDatabaseType;
3939
protected DynamicDataSourceService dynamicDataSourceService;
4040

@@ -60,6 +60,7 @@ public DataSource getActiveDataSource() {
6060
logger.info("use datasource:{}", sourceId == null ? "default" : sourceId);
6161
if (sourceId == null || dynamicDataSourceService == null) return defaultDataSource;
6262
DataSource dataSource = dynamicDataSourceService.getDataSource(sourceId);
63+
logger.info("use datasource:{} fail,because its not exists! use default datasource now.", sourceId);
6364
if (dataSource == null) return defaultDataSource;
6465
return dataSource;
6566
}

0 commit comments

Comments
 (0)