Skip to content

Commit

Permalink
use DsStrUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
huayanYu committed Oct 23, 2023
1 parent 7994897 commit d54f06f
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
*/
public abstract class DsStrUtils {

public static boolean isEmpty(String str) {
return str == null || str.isEmpty();
}

/**
* 判断字符串是否为空
*
Expand Down
3 changes: 2 additions & 1 deletion dynamic-datasource-spring-boot3-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<properties>
<java.version>17</java.version>
<spring-boot-dependencies.version>3.1.3</spring-boot-dependencies.version>
<spring-boot-dependencies.version>3.1.5</spring-boot-dependencies.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -66,6 +66,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.strategy.DynamicDataSourceStrategy;
import com.baomidou.dynamic.datasource.strategy.LoadBalanceDynamicDataSourceStrategy;
import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.p6spy.engine.spy.P6DataSource;
import io.seata.rm.datasource.DataSourceProxy;
Expand All @@ -32,7 +33,6 @@
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -128,7 +128,7 @@ public Map<String, GroupDataSource> getGroupDataSources() {
* @return 数据源
*/
public DataSource getDataSource(String ds) {
if (StringUtils.isEmpty(ds)) {
if (DsStrUtils.isEmpty(ds)) {
return determinePrimaryDataSource();
} else if (!groupDataSources.isEmpty() && groupDataSources.containsKey(ds)) {
log.debug("dynamic-datasource switch to the datasource named [{}]", ds);
Expand Down Expand Up @@ -188,7 +188,7 @@ private void addGroupDataSource(String ds, DataSource dataSource) {
* @param ds 数据源名称
*/
public synchronized void removeDataSource(String ds) {
if (!StringUtils.hasText(ds)) {
if (!DsStrUtils.hasText(ds)) {
throw new RuntimeException("remove parameter could not be empty");
}
if (primary.equals(ds)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import com.baomidou.dynamic.datasource.event.DataSourceInitEvent;
import com.baomidou.dynamic.datasource.support.ScriptRunner;
import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;
import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import com.p6spy.engine.spy.P6DataSource;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.rm.datasource.xa.DataSourceProxyXA;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.util.List;
Expand Down Expand Up @@ -84,7 +84,7 @@ public DataSource createDataSource(DataSourceProperty dataSourceProperty) {
throw new IllegalStateException("creator must not be null,please check the DataSourceCreator");
}
String propertyPublicKey = dataSourceProperty.getPublicKey();
if (StringUtils.isEmpty(propertyPublicKey)) {
if (DsStrUtils.isEmpty(propertyPublicKey)) {
dataSourceProperty.setPublicKey(publicKey);
}
Boolean propertyLazy = dataSourceProperty.getLazy();
Expand Down Expand Up @@ -112,12 +112,12 @@ private void runScrip(DataSource dataSource, DataSourceProperty dataSourceProper
DatasourceInitProperties initProperty = dataSourceProperty.getInit();
String schema = initProperty.getSchema();
String data = initProperty.getData();
if (StringUtils.hasText(schema) || StringUtils.hasText(data)) {
if (DsStrUtils.hasText(schema) || DsStrUtils.hasText(data)) {
ScriptRunner scriptRunner = new ScriptRunner(initProperty.isContinueOnError(), initProperty.getSeparator());
if (StringUtils.hasText(schema)) {
if (DsStrUtils.hasText(schema)) {
scriptRunner.runScript(dataSource, schema);
}
if (StringUtils.hasText(data)) {
if (DsStrUtils.hasText(data)) {
scriptRunner.runScript(dataSource, data);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.baomidou.dynamic.datasource.ds;

import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.dynamic.datasource.tx.ConnectionFactory;
import com.baomidou.dynamic.datasource.tx.ConnectionProxy;
import com.baomidou.dynamic.datasource.tx.TransactionContext;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.sql.Connection;
Expand Down Expand Up @@ -50,11 +50,11 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource {
@Override
public Connection getConnection() throws SQLException {
String xid = TransactionContext.getXID();
if (StringUtils.isEmpty(xid)) {
if (DsStrUtils.isEmpty(xid)) {
return determineDataSource().getConnection();
} else {
String ds = DynamicDataSourceContextHolder.peek();
ds = StringUtils.isEmpty(ds) ? getPrimary() : ds;
ds = DsStrUtils.isEmpty(ds) ? getPrimary() : ds;
ConnectionProxy connection = ConnectionFactory.getConnection(xid, ds);
return connection == null ? getConnectionProxy(xid, ds, determineDataSource().getConnection()) : connection;
}
Expand All @@ -63,11 +63,11 @@ public Connection getConnection() throws SQLException {
@Override
public Connection getConnection(String username, String password) throws SQLException {
String xid = TransactionContext.getXID();
if (StringUtils.isEmpty(xid)) {
if (DsStrUtils.isEmpty(xid)) {
return determineDataSource().getConnection(username, password);
} else {
String ds = DynamicDataSourceContextHolder.peek();
ds = StringUtils.isEmpty(ds) ? getPrimary() : ds;
ds = DsStrUtils.isEmpty(ds) ? getPrimary() : ds;
ConnectionProxy connection = ConnectionFactory.getConnection(xid, ds);
return connection == null ? getConnectionProxy(xid, ds, determineDataSource().getConnection(username, password))
: connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;
import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.util.regex.Matcher;
Expand All @@ -40,7 +40,7 @@ public class EncDataSourceInitEvent implements DataSourceInitEvent {
@Override
public void beforeCreate(DataSourceProperty dataSourceProperty) {
String publicKey = dataSourceProperty.getPublicKey();
if (StringUtils.hasText(publicKey)) {
if (DsStrUtils.hasText(publicKey)) {
dataSourceProperty.setUrl(decrypt(publicKey, dataSourceProperty.getUrl()));
dataSourceProperty.setUsername(decrypt(publicKey, dataSourceProperty.getUsername()));
dataSourceProperty.setPassword(decrypt(publicKey, dataSourceProperty.getPassword()));
Expand All @@ -56,7 +56,7 @@ public void afterCreate(DataSource dataSource) {
* 字符串解密
*/
private String decrypt(String publicKey, String cipherText) {
if (StringUtils.hasText(cipherText)) {
if (DsStrUtils.hasText(cipherText)) {
Matcher matcher = ENC_PATTERN.matcher(cipherText);
if (matcher.find()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.sql.Connection;
Expand Down Expand Up @@ -106,7 +106,7 @@ public Map<String, DataSource> loadDataSources() {
try {
// 由于 SPI 的支持,现在已无需显示加载驱动了
// 但在用户显示配置的情况下,进行主动加载
if (!StringUtils.isEmpty(driverClassName)) {
if (!DsStrUtils.isEmpty(driverClassName)) {
Class.forName(driverClassName);
log.info("成功加载数据库驱动程序");
}
Expand All @@ -116,7 +116,7 @@ public Map<String, DataSource> loadDataSources() {
Map<String, DataSourceProperty> dataSourcePropertiesMap = executeStmt(stmt);
return createDataSourceMap(dataSourcePropertiesMap);
} catch (Exception e) {
e.printStackTrace();
log.error("loadDataSources error", e);
} finally {
closeResource(conn);
closeResource(stmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private <T> BasicAttribute<T> findDataSourceAttribute(AnnotatedElement ae, Class
//AnnotatedElementUtils.findMergedAnnotation()会委托给findMergedAnnotationAttributes()
DS ds = AnnotatedElementUtils.findMergedAnnotation(ae, DS.class);
if (ds != null) {
return (BasicAttribute<T>) new BasicAttribute<>(ds.value());
return new BasicAttribute(ds.value());
}
} else if (annotation.isAssignableFrom(DSTransactional.class)) {
DSTransactional dsTransactional = AnnotatedElementUtils.findMergedAnnotation(ae, DSTransactional.class);
Expand All @@ -283,7 +283,7 @@ private <T> BasicAttribute<T> findDataSourceAttribute(AnnotatedElement ae, Class
transactionalInfo.setPropagation(dsTransactional.propagation());
transactionalInfo.setRollbackFor(dsTransactional.rollbackFor());
transactionalInfo.setNoRollbackFor(dsTransactional.noRollbackFor());
return (BasicAttribute<T>) new BasicAttribute(transactionalInfo);
return new BasicAttribute(transactionalInfo);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package com.baomidou.dynamic.datasource.support;

import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

Expand Down Expand Up @@ -50,7 +50,7 @@ public class ScriptRunner {
* @param location 脚本位置
*/
public void runScript(DataSource dataSource, String location) {
if (StringUtils.hasText(location)) {
if (DsStrUtils.hasText(location)) {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.setContinueOnError(continueOnError);
populator.setSeparator(separator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.baomidou.dynamic.datasource.toolkit;

import org.springframework.core.NamedThreadLocal;
import org.springframework.util.StringUtils;

import java.util.ArrayDeque;
import java.util.Deque;
Expand Down Expand Up @@ -66,7 +65,7 @@ public static String peek() {
* @return 数据源名称
*/
public static String push(String ds) {
String dataSourceStr = StringUtils.isEmpty(ds) ? "" : ds;
String dataSourceStr = DsStrUtils.isEmpty(ds) ? "" : ds;
LOOKUP_KEY_HOLDER.get().push(dataSourceStr);
return dataSourceStr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.baomidou.dynamic.datasource.tx;

import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.security.SecureRandom;
import java.util.UUID;
Expand All @@ -33,12 +33,7 @@ public final class LocalTxUtil {
/**
* SecureRandom instance used to generate UUIDs.
*/
private static final ThreadLocal<SecureRandom> SECURE_RANDOM_HOLDER = new ThreadLocal<SecureRandom>() {
@Override
protected SecureRandom initialValue() {
return new SecureRandom();
}
};
private static final ThreadLocal<SecureRandom> SECURE_RANDOM_HOLDER = ThreadLocal.withInitial(SecureRandom::new);

/**
* 随机生成UUID
Expand Down Expand Up @@ -75,7 +70,7 @@ public static UUID randomUUID() {
*/
public static String startTransaction() {
String xid = TransactionContext.getXID();
if (!StringUtils.isEmpty(xid)) {
if (!DsStrUtils.isEmpty(xid)) {
log.debug("dynamic-datasource exist local tx [{}]", xid);
} else {
xid = randomUUID().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.baomidou.dynamic.datasource.tx;

import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.util.StringUtils;

import java.util.*;

Expand All @@ -28,7 +28,7 @@ public class TransactionContext {

private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
private static final ThreadLocal<Set<TransactionSynchronization>> SYNCHRONIZATION_HOLDER =
ThreadLocal.withInitial(() -> new LinkedHashSet<>());
ThreadLocal.withInitial(LinkedHashSet::new);

/**
* Gets xid.
Expand All @@ -37,7 +37,7 @@ public class TransactionContext {
*/
public static String getXID() {
String xid = CONTEXT_HOLDER.get();
if (!StringUtils.isEmpty(xid)) {
if (DsStrUtils.hasText(xid)) {
return xid;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package com.baomidou.dynamic.datasource.tx;

import com.baomidou.dynamic.datasource.exception.TransactionException;
import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.util.StringUtils;

import java.util.Objects;

Expand Down Expand Up @@ -107,7 +107,7 @@ public Object execute(TransactionalExecutor transactionalExecutor) throws Throwa
private Object doExecute(TransactionalExecutor transactionalExecutor) throws Throwable {
TransactionalInfo transactionInfo = transactionalExecutor.getTransactionInfo();
DsPropagation propagation = transactionInfo.propagation;
if (!StringUtils.isEmpty(TransactionContext.getXID()) && !propagation.equals(DsPropagation.NESTED)) {
if (!DsStrUtils.isEmpty(TransactionContext.getXID()) && !propagation.equals(DsPropagation.NESTED)) {
return transactionalExecutor.execute();
}
boolean state = true;
Expand Down Expand Up @@ -215,7 +215,7 @@ public SuspendedResourcesHolder suspend() {
* @return 是否存在事务
*/
public boolean existingTransaction() {
return !StringUtils.isEmpty(TransactionContext.getXID());
return !DsStrUtils.isEmpty(TransactionContext.getXID());
}

/**
Expand Down
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
<spring-boot-dependencies.version>2.7.15</spring-boot-dependencies.version>
<mybatis.plus.version>3.5.3.2</mybatis.plus.version>
<hikaricp.version>2.4.13</hikaricp.version>
<druid.version>1.2.19</druid.version>
<beeCp.version>3.4.1</beeCp.version>
<commons-dbcp2.version>2.9.0</commons-dbcp2.version>
<druid.version>1.2.20</druid.version>
<beeCp.version>3.4.2</beeCp.version>
<commons-dbcp2.version>2.10.0</commons-dbcp2.version>
<p6spy.version>3.9.1</p6spy.version>
<seata.version>1.4.2</seata.version>
<lombok.version>1.18.30</lombok.version>
<h2.version>2.2.222</h2.version>
<h2.version>2.2.224</h2.version>
<atomikos.version>4.0.6</atomikos.version>
<junit-jupiter.version>5.10.0</junit-jupiter.version>

Expand All @@ -69,13 +69,13 @@
<maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
<maven-surefire-plugin.version>3.1.2</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<maven-javadoc-plugin.version>3.5.0</maven-javadoc-plugin.version>
<maven-javadoc-plugin.version>3.6.0</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.3.0</maven-source-plugin.version>

<license-maven-plugin.version>4.2</license-maven-plugin.version>
<license-maven-plugin.version>4.3</license-maven-plugin.version>
<maven-gpg-plugin.version>3.1.0</maven-gpg-plugin.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<native-maven-plugin.version>0.9.27</native-maven-plugin.version>
<native-maven-plugin.version>0.9.28</native-maven-plugin.version>
</properties>

<dependencyManagement>
Expand Down

0 comments on commit d54f06f

Please sign in to comment.