From bf678bba52ba94aaa62e0d484d4b6daa98a28a54 Mon Sep 17 00:00:00 2001 From: TanYibin <5491541@qq.com> Date: Thu, 16 Feb 2023 08:56:12 +0800 Subject: [PATCH] 工具类包路径优化 --- huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java | 2 ++ huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/controller/AcsController.java | 5 +++-- huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/service/impl/AcsServiceImpl.java | 2 +- huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/erp/service/impl/ErpServiceImpl.java | 2 +- huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/mes/servuce/impl/MesServiceImpl.java | 2 +- huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java | 2 +- huaheng-wms-core/src/main/java/org/jeecg/modules/wms/config/address/service/impl/AddressServiceImpl.java | 3 ++- huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java | 2 +- huaheng-wms-core/src/main/java/org/jeecg/utils/CustomLogContextListener.java | 67 ------------------------------------------------------------------- huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengShiroConfig.java | 43 ------------------------------------------- huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java | 283 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ huaheng-wms-core/src/main/java/org/jeecg/utils/RedissonDistributedLocker.java | 75 --------------------------------------------------------------------------- huaheng-wms-core/src/main/java/org/jeecg/utils/StringUtils.java | 24 ++---------------------- huaheng-wms-core/src/main/java/org/jeecg/utils/config/HuahengShiroConfig.java | 43 +++++++++++++++++++++++++++++++++++++++++++ huaheng-wms-core/src/main/java/org/jeecg/utils/http/OkHttpUtils.java | 287 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- huaheng-wms-core/src/main/java/org/jeecg/utils/listener/CustomLogContextListener.java | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ huaheng-wms-core/src/main/java/org/jeecg/utils/support/RedissonDistributedLocker.java | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ huaheng-wms-core/src/main/java/org/jeecg/utils/support/StrFormatter.java | 77 ----------------------------------------------------------------------------- 18 files changed, 482 insertions(+), 579 deletions(-) delete mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/CustomLogContextListener.java delete mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengShiroConfig.java create mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java delete mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/RedissonDistributedLocker.java create mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/config/HuahengShiroConfig.java delete mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/http/OkHttpUtils.java create mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/listener/CustomLogContextListener.java create mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/support/RedissonDistributedLocker.java delete mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/support/StrFormatter.java diff --git a/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java b/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java index c6b2cb4..79a828b 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java @@ -18,6 +18,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; @@ -28,6 +29,7 @@ import net.bytebuddy.asm.Advice.This; * 单体启动类(采用此类启动为单体模式) */ @Slf4j +@EnableCaching @SpringBootApplication @EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class}) public class JeecgSystemApplication extends SpringBootServletInitializer { diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/controller/AcsController.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/controller/AcsController.java index 0320703..e0de00c 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/controller/AcsController.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/controller/AcsController.java @@ -15,9 +15,9 @@ import org.jeecg.modules.wms.framework.aspectj.PassApiAuthentication; import org.jeecg.modules.wms.framework.aspectj.lang.annotation.ApiLogger; import org.jeecg.modules.wms.framework.controller.HuahengBaseController; import org.jeecg.modules.wms.task.agvTask.service.IAgvTaskService; +import org.jeecg.utils.OkHttpUtils; import org.jeecg.utils.StringUtils; import org.jeecg.utils.constant.QuantityConstant; -import org.jeecg.utils.http.OkHttpUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -47,7 +47,8 @@ public class AcsController extends HuahengBaseController { @ResponseBody @PostMapping(value = "/testTokenCheck") public Result<?> testTokenCheck(@RequestBody Map<String, String> paramMap, HttpServletRequest request) { - String url = "http://127.0.0.1:8080/wms/api/wms/acs/testTokenCheck"; +// String url = "http://127.0.0.1:8080/wms/api/wms/acs/testTokenCheck"; + String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_ERP_BACK_RECEIPT); String body = OkHttpUtils.sendPostByJsonStr(url, JSON.toJSONString(paramMap)); return Result.ok(body); } diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/service/impl/AcsServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/service/impl/AcsServiceImpl.java index 5ab2f0c..30ea8f9 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/service/impl/AcsServiceImpl.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/service/impl/AcsServiceImpl.java @@ -10,9 +10,9 @@ import org.jeecg.modules.wms.config.location.entity.Location; import org.jeecg.modules.wms.config.location.service.ILocationService; import org.jeecg.modules.wms.task.agvTask.entity.AgvTask; import org.jeecg.modules.wms.task.agvTask.service.IAgvTaskService; +import org.jeecg.utils.OkHttpUtils; import org.jeecg.utils.StringUtils; import org.jeecg.utils.constant.QuantityConstant; -import org.jeecg.utils.http.OkHttpUtils; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSON; diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/erp/service/impl/ErpServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/erp/service/impl/ErpServiceImpl.java index 087c8ef..91e5d7e 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/erp/service/impl/ErpServiceImpl.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/erp/service/impl/ErpServiceImpl.java @@ -38,9 +38,9 @@ import org.jeecg.modules.wms.shipment.shipmentHeader.entity.ShipmentDetail; import org.jeecg.modules.wms.shipment.shipmentHeader.entity.ShipmentHeader; import org.jeecg.modules.wms.shipment.shipmentHeader.service.IShipmentDetailService; import org.jeecg.modules.wms.shipment.shipmentHeader.service.IShipmentHeaderService; +import org.jeecg.utils.OkHttpUtils; import org.jeecg.utils.StringUtils; import org.jeecg.utils.constant.QuantityConstant; -import org.jeecg.utils.http.OkHttpUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/mes/servuce/impl/MesServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/mes/servuce/impl/MesServiceImpl.java index ca70e23..909c8c4 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/mes/servuce/impl/MesServiceImpl.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/mes/servuce/impl/MesServiceImpl.java @@ -33,9 +33,9 @@ import org.jeecg.modules.wms.task.taskHeader.entity.TaskDetail; import org.jeecg.modules.wms.task.taskHeader.entity.TaskHeader; import org.jeecg.modules.wms.task.taskHeader.service.ITaskDetailService; import org.jeecg.modules.wms.task.taskHeader.service.ITaskHeaderService; +import org.jeecg.utils.OkHttpUtils; import org.jeecg.utils.StringUtils; import org.jeecg.utils.constant.QuantityConstant; -import org.jeecg.utils.http.OkHttpUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java index dba4ffc..28440d9 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java @@ -35,9 +35,9 @@ import org.jeecg.modules.wms.task.taskHeader.entity.TaskDetail; import org.jeecg.modules.wms.task.taskHeader.entity.TaskHeader; import org.jeecg.modules.wms.task.taskHeader.service.ITaskDetailService; import org.jeecg.modules.wms.task.taskHeader.service.ITaskHeaderService; +import org.jeecg.utils.OkHttpUtils; import org.jeecg.utils.StringUtils; import org.jeecg.utils.constant.QuantityConstant; -import org.jeecg.utils.http.OkHttpUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/config/address/service/impl/AddressServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/config/address/service/impl/AddressServiceImpl.java index eb737a6..eb16779 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/config/address/service/impl/AddressServiceImpl.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/config/address/service/impl/AddressServiceImpl.java @@ -6,6 +6,7 @@ import org.apache.commons.math3.analysis.function.Add; import org.jeecg.modules.wms.config.address.entity.Address; import org.jeecg.modules.wms.config.address.mapper.AddressMapper; import org.jeecg.modules.wms.config.address.service.IAddressService; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -40,6 +41,7 @@ public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> impl } @Override + @Cacheable(cacheNames = "getUrlByParam", key = "#root.methodName + '_' + #param") public String getUrlByParam(String param) { LambdaQueryWrapper<Address> addressLambdaQueryWrapper = Wrappers.lambdaQuery(); addressLambdaQueryWrapper.eq(Address::getParam, param); @@ -49,5 +51,4 @@ public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> impl } return address.getUrl(); } - } diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java index d313b3b..83da530 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/HuahengBaseController.java @@ -5,7 +5,7 @@ import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; import org.jeecg.common.api.vo.Result; -import org.jeecg.utils.RedissonDistributedLocker; +import org.jeecg.utils.support.RedissonDistributedLocker; import org.springframework.beans.factory.annotation.Autowired; import cn.hutool.core.date.SystemClock; diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/CustomLogContextListener.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/CustomLogContextListener.java deleted file mode 100644 index 46735d8..0000000 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/CustomLogContextListener.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.jeecg.utils; - -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.classic.spi.LoggerContextListener; -import ch.qos.logback.core.spi.ContextAwareBase; -import ch.qos.logback.core.spi.LifeCycle; -import ch.qos.logback.core.Context; - -/** - * @version 1.0 - * @class: CustomLogContextListener - * @author: carlo - * @mail: carlo_cwh@qq.com - * @date: 2021/8/25 10:01 - * @description: 定义logback 日志监听器,指定日志文件存放目录 - */ -public class CustomLogContextListener extends ContextAwareBase implements LoggerContextListener, LifeCycle { - - /** 存储日志路径标识 */ - public static final String LOG_PAHT_KEY = "LOG_PATH"; - - @Override - public boolean isResetResistant() { - return false; - } - - @Override - public void onStart(LoggerContext loggerContext) { - - } - - @Override - public void onReset(LoggerContext loggerContext) { - - } - - @Override - public void onStop(LoggerContext loggerContext) { - - } - - @Override - public void onLevelChange(Logger logger, Level level) { - - } - - @Override - public void start() { - // "user.dir"是指用户当前工作目录 - String s = System.getProperty("user.dir"); - System.setProperty(LOG_PAHT_KEY, s); - Context context = getContext(); - context.putProperty(LOG_PAHT_KEY, s); - } - - @Override - public void stop() { - - } - - @Override - public boolean isStarted() { - return false; - } -} \ No newline at end of file diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengShiroConfig.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengShiroConfig.java deleted file mode 100644 index 8a80708..0000000 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengShiroConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.jeecg.utils; - -import java.util.LinkedHashMap; -import java.util.Map; - -import org.apache.shiro.spring.web.ShiroFilterFactoryBean; -import org.apache.shiro.web.mgt.DefaultWebSecurityManager; -import org.jeecg.common.util.oConvertUtils; -import org.jeecg.config.JeeccgBaseConfig; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class HuahengShiroConfig { - - @Autowired - JeeccgBaseConfig jeeccgBaseConfig; - - /** - * 创建 ShiroFilterFactoryBean - */ - @Bean - public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) { - ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean(); - // 设置安全管理器 - filterFactoryBean.setSecurityManager(securityManager); - - // 拦截器 - Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>(); - String shiroExcludeUrls = jeeccgBaseConfig.getShiro().getExcludeUrls(); - if (oConvertUtils.isNotEmpty(shiroExcludeUrls)) { - String[] permissionUrl = shiroExcludeUrls.split(","); - for (String url : permissionUrl) { - filterChainDefinitionMap.put(url, "anon"); - } - } - - return filterFactoryBean; - } - -} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java new file mode 100644 index 0000000..13e4e74 --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/OkHttpUtils.java @@ -0,0 +1,283 @@ +package org.jeecg.utils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import com.alibaba.fastjson.parser.Feature; +import com.aliyun.oss.ServiceException; +import com.xkcoding.http.util.StringUtil; + +import antlr.StringUtils; +import cn.hutool.core.util.StrUtil; +import net.bytebuddy.asm.Advice.This; +import okhttp3.*; +import org.jeecg.modules.wms.framework.aspectj.ApiLogAspect; +import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.messaging.support.ErrorMessage; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import javax.validation.constraints.NotNull; + +/** + * OkHttp发送请求 + * @author huaheng + * @Date 2022-5-30 + */ +public class OkHttpUtils { + + private static final Logger log = LoggerFactory.getLogger(OkHttpUtils.class); + + /** + * 最多重试次数 + */ + public final static int MAX_RENTRY_COUNT = 3; + + /** + * 最大连接时间(秒) + */ + public final static int CONNECTION_TIMEOUT = 1; + + /** + * 最大读取时间(秒) + */ + public final static int READ_TIMEOUT = 30; + + /** + * 最大写入时间(秒) + */ + public final static int WRITE_TIMEOUT = 30; + + /** + * JSON格式 + */ + public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); + /** + * OkHTTP线程池最大空闲线程数 + */ + public final static int MAX_IDLE_CONNECTIONS = 100; + /** + * OkHTTP线程池空闲线程存活时间(秒) + */ + public final static long KEEP_ALIVE_DURATION = 60; + + private static final String CONTENT_TYPE = "Content-Type"; + + /** 访问接口参数配置 */ + private final static OkHttpClient HTTP_CLIENT = + new OkHttpClient.Builder().connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS).readTimeout(READ_TIMEOUT, TimeUnit.SECONDS) + .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS).addInterceptor(new OkHttpUtils.OkhttpInterceptor(MAX_RENTRY_COUNT)) // 过滤器,设置最大重试次数 + .retryOnConnectionFailure(false).connectionPool(new ConnectionPool(MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION, TimeUnit.SECONDS)).build(); + + /** + * 向指定 URL 发送GET方法的请求 + * @param url 发送请求的 URL + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式 + * @return 所代表远程资源的响应结果 + * @throws Exception + */ + public static String sendGet(String url, String param) { + Map<String, String> headers = new HashMap<String, String>(); + return sendGet(url, headers, param); + } + + /** + * 向指定 URL 发送GET方法的请求 + * @param url 发送请求的 URL + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式 + * @param headers 请求头 + * @return 所代表远程资源的响应结果 + * @throws Exception + */ + public static String sendGet(String url, Map<String, String> headers, String param) { + ApiLog apiLog = new ApiLog(); + Request.Builder builder = new Request.Builder(); + buildHeader(builder, headers); + Request request = builder.url(url + "?" + param).get().build(); + Response response = null; + String result = null; + try { + ApiLogAspect.initApiLog(apiLog, request, param); + response = HTTP_CLIENT.newCall(request).execute(); + if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功 + result = response.body().string(); + log.info("执行GET请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), param, result); + } else { + String errorString = StrUtil.format("执行GET请求失败!url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers), param, + response.code(), response.message()); + log.error(errorString); + throw new ServiceException(errorString); + } + } catch (Exception e) { + String errorString = StrUtil.format("执行GET请求异常!url:{},header:{},param:{} {}", url, JSON.toJSONString(headers), param, e.getMessage()); + log.error(errorString); + ApiLogAspect.setApiLogException(apiLog, e); + throw new RuntimeException(errorString, e); + } finally { + ApiLogAspect.finishApiLog(apiLog, response, result); + } + return result; + } + + /** + * 向指定 URL 发送POST方法的请求 + * @param url 发送请求的 URL + * @param param 请求参数,请求参数是 name1=value1&name2=value2 的形式 + * @return 远程资源的响应结果 + * @throws IOException + */ + public static String sendPost(String url, String param) throws IOException { + Map<String, String> headers = new HashMap<String, String>(); + return sendPost(url, headers, param); + } + + /** + * 向指定 URL 发送POST方法的请求 + * @param url 发送请求的 URL + * @param param 请求参数,请求参数是 name1=value1&name2=value2 的形式 + * @param headers 请求头 + * @return 远程资源的响应结果 + * @throws IOException + */ + public static String sendPost(String url, Map<String, String> headers, String param) { + ApiLog apiLog = new ApiLog(); + Request.Builder builder = new Request.Builder(); + buildHeader(builder, headers); + FormBody body = new FormBody.Builder().build(); + Request request = builder.url(url + "?" + param).post(body).build(); + Response response = null; + String result = null; + try { + ApiLogAspect.initApiLog(apiLog, request, param); + response = HTTP_CLIENT.newCall(request).execute(); + if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功 + result = response.body().string(); + log.info("执行POST请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), param, result); + } else { + String errorString = StrUtil.format("执行POST请求失败!url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers), + param, response.code(), response.message()); + log.error(errorString); + throw new ServiceException(errorString); + } + } catch (Exception e) { + String errorString = StrUtil.format("执行POST请求异常!url:{},header:{},param:{} {}", url, JSON.toJSONString(headers), param, e.getMessage()); + log.error(errorString); + ApiLogAspect.setApiLogException(apiLog, e); + throw new RuntimeException(errorString, e); + } finally { + ApiLogAspect.finishApiLog(apiLog, response, result); + } + return result; + } + + public static String sendPostByJsonStr(String url, String jsonString) { + Map<String, String> headers = new HashMap<String, String>(); + return sendPostByJsonStr(url, headers, jsonString); + } + + /** + * JSONString形式发送POST请求 + * @throws Exception + * @throws IOException + */ + public static String sendPostByJsonStr(String url, Map<String, String> headers, String jsonString) { + ApiLog apiLog = new ApiLog(); + headers.put("Accept", "application/json");// 设置接收数据的格式 + headers.put("Content-Type", "application/json");// 设置发送数据的格式 + Request.Builder builder = new Request.Builder(); + buildHeader(builder, headers); + RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, jsonString); + Request request = builder.url(url).post(body).build(); + Response response = null; + String result = null; + try { + ApiLogAspect.initApiLog(apiLog, request, jsonString); + response = HTTP_CLIENT.newCall(request).execute(); + if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功 + result = response.body().string(); + log.info("执行POST请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), jsonString, result); + } else { + String errorString = StrUtil.format("执行POST请求失败!url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers), + jsonString, response.code(), response.message()); + log.error(errorString); + throw new ServiceException(errorString); + } + } catch (Exception e) { + String errorString = StrUtil.format("执行POST请求异常!url:{},header:{},param:{} {}", url, JSON.toJSONString(headers), jsonString, e.getMessage()); + log.error(errorString); + ApiLogAspect.setApiLogException(apiLog, e); + throw new RuntimeException(errorString, e); + } finally { + ApiLogAspect.finishApiLog(apiLog, response, result); + } + return result; + } + + /** + * 设置请求头 + * @param builder . + * @param headers 请求头 + */ + private static void buildHeader(Request.Builder builder, Map<String, String> headers) { + if (Objects.nonNull(headers) && headers.size() > 0) { + headers.forEach((k, v) -> { + if (Objects.nonNull(k) && Objects.nonNull(v)) { + builder.addHeader(k, v); + } + }); + } + } + + /** + * 支持嵌套泛型的POST请求 + * @param url 链接 + * @param json 请求json + * @param type 嵌套泛型 + * @return 响应对象, 可进行强转。 + * @throws Exception + */ + public static <T> T sendPostByJsonStr(String url, String jsonString, TypeReference<T> type) throws Exception { + String result = sendPostByJsonStr(url, jsonString); + if (Objects.nonNull(result) && Objects.nonNull(type)) { + return JSON.parseObject(result, type); + } + return null; + } + + public static class OkhttpInterceptor implements Interceptor { + // 最大重试次数 + private int maxRentry; + + public OkhttpInterceptor(int maxRentry) { + this.maxRentry = maxRentry; + } + + /** 递归 2次下发请求,如果仍然失败 则返回 null 但是 intercept must not return null. 返回 null 会报 IllegalStateException 异常 */ + public Response intercept(@NotNull Chain chain) throws IOException { + return retry(chain, 0); + } + + private Response retry(Chain chain, int retryCent) throws IOException { + Request request = chain.request(); + Response response = null; + try { + response = chain.proceed(request); + } catch (Exception e) { + if (maxRentry > retryCent) { + return retry(chain, retryCent + 1); + } + if (Objects.isNull(response)) { + throw e; + } + } + return response; + } + } +} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/RedissonDistributedLocker.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/RedissonDistributedLocker.java deleted file mode 100644 index 82d2210..0000000 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/RedissonDistributedLocker.java +++ /dev/null @@ -1,75 +0,0 @@ -package org.jeecg.utils; - -import java.util.concurrent.TimeUnit; - -import org.redisson.api.RLock; -import org.redisson.api.RedissonClient; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -/** - * @author TanYibin - */ -@Component -public class RedissonDistributedLocker { - - @Autowired - private RedissonClient redissonClient; - - public RLock lock(String lockKey) { - RLock lock = this.redissonClient.getLock(lockKey); - lock.lock(); - return lock; - } - - public RLock lock(String lockKey, long timeout) { - RLock lock = this.redissonClient.getLock(lockKey); - lock.lock(timeout, TimeUnit.SECONDS); - return lock; - } - - public RLock lock(String lockKey, TimeUnit unit, long timeout) { - RLock lock = this.redissonClient.getLock(lockKey); - lock.lock(timeout, unit); - return lock; - } - - public boolean tryLock(String lockKey, TimeUnit unit, long leaseTime) { - RLock lock = this.redissonClient.getLock(lockKey); - try { - return lock.tryLock(0L, leaseTime, unit); - } catch (InterruptedException var7) { - return false; - } - } - - public boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime) { - RLock lock = redissonClient.getLock(lockKey); - try { - return lock.tryLock(waitTime, leaseTime, unit); - } catch (InterruptedException e) { - return false; - } - } - - public void unlock(String lockKey) { - RLock lock = redissonClient.getLock(lockKey); - try { - if (lock.isLocked()) { - lock.unlock(); - } - } catch (IllegalMonitorStateException e) { - e.printStackTrace(); - } - } - - public void unlock(RLock lock) { - try { - if (lock.isLocked()) { - lock.unlock(); - } - } catch (IllegalMonitorStateException e) { - e.printStackTrace(); - } - } -} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/StringUtils.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/StringUtils.java index fd6a744..cf3b904 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/StringUtils.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/StringUtils.java @@ -1,12 +1,11 @@ package org.jeecg.utils; -import org.apache.commons.lang.text.StrBuilder; -import org.jeecg.utils.support.StrFormatter; - import java.lang.reflect.Field; import java.util.Collection; import java.util.Map; +import org.apache.commons.lang.text.StrBuilder; + /** * 字符串工具类 * @author huaheng @@ -238,25 +237,6 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils { } /** - * 格式化文本, {} 表示占位符<br> - * 此方法只是简单将占位符 {} 按照顺序替换为参数<br> - * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br> - * 例:<br> - * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br> - * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br> - * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> - * @param template 文本模板,被替换的部分用 {} 表示 - * @param params 参数值 - * @return 格式化后的文本 - */ - public static String format(String template, Object... params) { - if (isEmpty(params) || isEmpty(template)) { - return template; - } - return StrFormatter.format(template, params); - } - - /** * 驼峰首字符小写 */ public static String uncapitalize(String str) { diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/config/HuahengShiroConfig.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/config/HuahengShiroConfig.java new file mode 100644 index 0000000..ed6c1e2 --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/config/HuahengShiroConfig.java @@ -0,0 +1,43 @@ +package org.jeecg.utils.config; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.shiro.spring.web.ShiroFilterFactoryBean; +import org.apache.shiro.web.mgt.DefaultWebSecurityManager; +import org.jeecg.common.util.oConvertUtils; +import org.jeecg.config.JeeccgBaseConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class HuahengShiroConfig { + + @Autowired + JeeccgBaseConfig jeeccgBaseConfig; + + /** + * 创建 ShiroFilterFactoryBean + */ + @Bean + public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) { + ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean(); + // 设置安全管理器 + filterFactoryBean.setSecurityManager(securityManager); + + // 拦截器 + Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>(); + String shiroExcludeUrls = jeeccgBaseConfig.getShiro().getExcludeUrls(); + if (oConvertUtils.isNotEmpty(shiroExcludeUrls)) { + String[] permissionUrl = shiroExcludeUrls.split(","); + for (String url : permissionUrl) { + filterChainDefinitionMap.put(url, "anon"); + } + } + + return filterFactoryBean; + } + +} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/http/OkHttpUtils.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/http/OkHttpUtils.java deleted file mode 100644 index 003b5d0..0000000 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/http/OkHttpUtils.java +++ /dev/null @@ -1,287 +0,0 @@ -package org.jeecg.utils.http; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.TypeReference; -import com.alibaba.fastjson.parser.Feature; -import com.aliyun.oss.ServiceException; -import com.xkcoding.http.util.StringUtil; - -import antlr.StringUtils; -import cn.hutool.core.util.StrUtil; -import net.bytebuddy.asm.Advice.This; -import okhttp3.*; -import org.jeecg.modules.wms.framework.aspectj.ApiLogAspect; -import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.messaging.support.ErrorMessage; - -import java.io.IOException; -import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import javax.validation.constraints.NotNull; - -/** - * OkHttp发送请求 - * @author huaheng - * @Date 2022-5-30 - */ -public class OkHttpUtils { - - private static final Logger log = LoggerFactory.getLogger(OkHttpUtils.class); - - /** - * 最多重试次数 - */ - public final static int MAX_RENTRY_COUNT = 3; - - /** - * 最大连接时间(秒) - */ - public final static int CONNECTION_TIMEOUT = 1; - - /** - * 最大读取时间(秒) - */ - public final static int READ_TIMEOUT = 30; - - /** - * 最大写入时间(秒) - */ - public final static int WRITE_TIMEOUT = 30; - - /** - * JSON格式 - */ - public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); - /** - * OkHTTP线程池最大空闲线程数 - */ - public final static int MAX_IDLE_CONNECTIONS = 100; - /** - * OkHTTP线程池空闲线程存活时间(秒) - */ - public final static long KEEP_ALIVE_DURATION = 60; - - private static final String CONTENT_TYPE = "Content-Type"; - - /** 访问接口参数配置 */ - private final static OkHttpClient HTTP_CLIENT = - new OkHttpClient.Builder().connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS).readTimeout(READ_TIMEOUT, TimeUnit.SECONDS) - .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS).addInterceptor(new OkHttpUtils.OkhttpInterceptor(MAX_RENTRY_COUNT)) // 过滤器,设置最大重试次数 - .retryOnConnectionFailure(false).connectionPool(new ConnectionPool(MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION, TimeUnit.SECONDS)).build(); - - /** - * 向指定 URL 发送GET方法的请求 - * @param url 发送请求的 URL - * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式 - * @return 所代表远程资源的响应结果 - * @throws Exception - */ - public static String sendGet(String url, String param) { - Map<String, String> headers = new HashMap<String, String>(); - return sendGet(url, headers, param); - } - - /** - * 向指定 URL 发送GET方法的请求 - * @param url 发送请求的 URL - * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式 - * @param headers 请求头 - * @return 所代表远程资源的响应结果 - * @throws Exception - */ - public static String sendGet(String url, Map<String, String> headers, String param) { - ApiLog apiLog = new ApiLog(); - Request.Builder builder = new Request.Builder(); - buildHeader(builder, headers); - Request request = builder.url(url + "?" + param).get().build(); - Response response = null; - String result = null; - try { - ApiLogAspect.initApiLog(apiLog, request, param); - response = HTTP_CLIENT.newCall(request).execute(); - if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功 - result = response.body().string(); - log.info("执行GET请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), param, result); - } else { - String errorString = StrUtil.format("执行GET请求失败!url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers), param, - response.code(), response.message()); - log.error(errorString); - throw new ServiceException(errorString); - } - } catch (Exception e) { - String errorString = StrUtil.format("执行GET请求异常!url:{},header:{},param:{} {}", url, JSON.toJSONString(headers), param, e.getMessage()); - log.error(errorString, e); - ApiLogAspect.setApiLogException(apiLog, e); - throw new RuntimeException(errorString, e); - } finally { - ApiLogAspect.finishApiLog(apiLog, response, result); - } - return result; - } - - /** - * 向指定 URL 发送POST方法的请求 - * @param url 发送请求的 URL - * @param param 请求参数,请求参数是 name1=value1&name2=value2 的形式 - * @return 远程资源的响应结果 - * @throws IOException - */ - public static String sendPost(String url, String param) throws IOException { - Map<String, String> headers = new HashMap<String, String>(); - return sendPost(url, headers, param); - } - - /** - * 向指定 URL 发送POST方法的请求 - * @param url 发送请求的 URL - * @param param 请求参数,请求参数是 name1=value1&name2=value2 的形式 - * @param headers 请求头 - * @return 远程资源的响应结果 - * @throws IOException - */ - public static String sendPost(String url, Map<String, String> headers, String param) { - ApiLog apiLog = new ApiLog(); - Request.Builder builder = new Request.Builder(); - buildHeader(builder, headers); - FormBody body = new FormBody.Builder().build(); - Request request = builder.url(url + "?" + param).post(body).build(); - Response response = null; - String result = null; - try { - ApiLogAspect.initApiLog(apiLog, request, param); - response = HTTP_CLIENT.newCall(request).execute(); - if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功 - result = response.body().string(); - log.info("执行POST请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), param, result); - } else { - String errorString = StrUtil.format("执行POST请求失败!url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers), - param, response.code(), response.message()); - log.error(errorString); - throw new ServiceException(errorString); - } - } catch (Exception e) { - String errorString = StrUtil.format("执行POST请求异常!url:{},header:{},param:{} {}", url, JSON.toJSONString(headers), param, e.getMessage()); - log.error(errorString, e); - ApiLogAspect.setApiLogException(apiLog, e); - throw new RuntimeException(errorString, e); - } finally { - ApiLogAspect.finishApiLog(apiLog, response, result); - } - return result; - } - - public static String sendPostByJsonStr(String url, String jsonString) { - Map<String, String> headers = new HashMap<String, String>(); - return sendPostByJsonStr(url, headers, jsonString); - } - - /** - * JSONString形式发送POST请求 - * @throws Exception - * @throws IOException - */ - public static String sendPostByJsonStr(String url, Map<String, String> headers, String jsonString) { - ApiLog apiLog = new ApiLog(); - headers.put("Accept", "application/json");// 设置接收数据的格式 - headers.put("Content-Type", "application/json");// 设置发送数据的格式 - Request.Builder builder = new Request.Builder(); - buildHeader(builder, headers); - // using above json body as a input to post API call - RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, jsonString); - Request request = builder.url(url).post(body).build(); - Response response = null; - String result = null; - try { - ApiLogAspect.initApiLog(apiLog, request, jsonString); - response = HTTP_CLIENT.newCall(request).execute(); - if (response.isSuccessful() && Objects.nonNull(response.body())) {// 调用成功 - result = response.body().string(); - log.info("执行POST请求成功,url:{},header:{},param:{},result:{}", url, JSON.toJSONString(headers), jsonString, result); - } else { - String errorString = StrUtil.format("执行POST请求失败!url:{},header:{},param:{},responseCode:{},responseMessage:{}", url, JSON.toJSONString(headers), - jsonString, response.code(), response.message()); - log.error(errorString); - throw new ServiceException(errorString); - } - } catch (Exception e) { - String errorString = StrUtil.format("执行POST请求异常!url:{},header:{},param:{} {}", url, JSON.toJSONString(headers), jsonString, e.getMessage()); - log.error(errorString, e); - ApiLogAspect.setApiLogException(apiLog, e); - throw new RuntimeException(errorString, e); - } finally { - ApiLogAspect.finishApiLog(apiLog, response, result); - } - return result; - } - - /** - * 设置请求头 - * @param builder . - * @param headers 请求头 - */ - private static void buildHeader(Request.Builder builder, Map<String, String> headers) { - if (Objects.nonNull(headers) && headers.size() > 0) { - headers.forEach((k, v) -> { - if (Objects.nonNull(k) && Objects.nonNull(v)) { - builder.addHeader(k, v); - } - }); - } - } - - /** - * 支持嵌套泛型的POST请求 - * @param url 链接 - * @param json 请求json - * @param type 嵌套泛型 - * @return 响应对象, 可进行强转。 - * @throws Exception - */ - public static <T> T sendPostByJsonStr(String url, String jsonString, TypeReference<T> type) throws Exception { - String result = sendPostByJsonStr(url, jsonString); - if (Objects.nonNull(result) && Objects.nonNull(type)) { - return JSON.parseObject(result, type); - } - return null; - } - - public static class OkhttpInterceptor implements Interceptor { - // 最大重试次数 - private int maxRentry; - - public OkhttpInterceptor(int maxRentry) { - this.maxRentry = maxRentry; - } - - public Response intercept(@NotNull Chain chain) throws IOException { - /* - * 递归 2次下发请求,如果仍然失败 则返回 null 但是 intercept must not return null. 返回 null 会报 IllegalStateException 异常 - */ - return retry(chain, 0); - } - - private Response retry(Chain chain, int retryCent) throws IOException { - Request request = chain.request(); - Response response = null; - try { - response = chain.proceed(request); - } catch (Exception e) { - if (maxRentry > retryCent) { - return retry(chain, retryCent + 1); - } - if (Objects.isNull(response)) { - throw e; - } - } - return response; - } - } - -} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/listener/CustomLogContextListener.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/listener/CustomLogContextListener.java new file mode 100644 index 0000000..dfa74f5 --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/listener/CustomLogContextListener.java @@ -0,0 +1,67 @@ +package org.jeecg.utils.listener; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.spi.LoggerContextListener; +import ch.qos.logback.core.spi.ContextAwareBase; +import ch.qos.logback.core.spi.LifeCycle; +import ch.qos.logback.core.Context; + +/** + * @version 1.0 + * @class: CustomLogContextListener + * @author: carlo + * @mail: carlo_cwh@qq.com + * @date: 2021/8/25 10:01 + * @description: 定义logback 日志监听器,指定日志文件存放目录 + */ +public class CustomLogContextListener extends ContextAwareBase implements LoggerContextListener, LifeCycle { + + /** 存储日志路径标识 */ + public static final String LOG_PAHT_KEY = "LOG_PATH"; + + @Override + public boolean isResetResistant() { + return false; + } + + @Override + public void onStart(LoggerContext loggerContext) { + + } + + @Override + public void onReset(LoggerContext loggerContext) { + + } + + @Override + public void onStop(LoggerContext loggerContext) { + + } + + @Override + public void onLevelChange(Logger logger, Level level) { + + } + + @Override + public void start() { + // "user.dir"是指用户当前工作目录 + String s = System.getProperty("user.dir"); + System.setProperty(LOG_PAHT_KEY, s); + Context context = getContext(); + context.putProperty(LOG_PAHT_KEY, s); + } + + @Override + public void stop() { + + } + + @Override + public boolean isStarted() { + return false; + } +} \ No newline at end of file diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/support/RedissonDistributedLocker.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/support/RedissonDistributedLocker.java new file mode 100644 index 0000000..a564ef5 --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/support/RedissonDistributedLocker.java @@ -0,0 +1,75 @@ +package org.jeecg.utils.support; + +import java.util.concurrent.TimeUnit; + +import org.redisson.api.RLock; +import org.redisson.api.RedissonClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * @author TanYibin + */ +@Component +public class RedissonDistributedLocker { + + @Autowired + private RedissonClient redissonClient; + + public RLock lock(String lockKey) { + RLock lock = this.redissonClient.getLock(lockKey); + lock.lock(); + return lock; + } + + public RLock lock(String lockKey, long timeout) { + RLock lock = this.redissonClient.getLock(lockKey); + lock.lock(timeout, TimeUnit.SECONDS); + return lock; + } + + public RLock lock(String lockKey, TimeUnit unit, long timeout) { + RLock lock = this.redissonClient.getLock(lockKey); + lock.lock(timeout, unit); + return lock; + } + + public boolean tryLock(String lockKey, TimeUnit unit, long leaseTime) { + RLock lock = this.redissonClient.getLock(lockKey); + try { + return lock.tryLock(0L, leaseTime, unit); + } catch (InterruptedException var7) { + return false; + } + } + + public boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime) { + RLock lock = redissonClient.getLock(lockKey); + try { + return lock.tryLock(waitTime, leaseTime, unit); + } catch (InterruptedException e) { + return false; + } + } + + public void unlock(String lockKey) { + RLock lock = redissonClient.getLock(lockKey); + try { + if (lock.isLocked()) { + lock.unlock(); + } + } catch (IllegalMonitorStateException e) { + e.printStackTrace(); + } + } + + public void unlock(RLock lock) { + try { + if (lock.isLocked()) { + lock.unlock(); + } + } catch (IllegalMonitorStateException e) { + e.printStackTrace(); + } + } +} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/support/StrFormatter.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/support/StrFormatter.java deleted file mode 100644 index aa86bae..0000000 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/support/StrFormatter.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.jeecg.utils.support; - -import org.jeecg.utils.StringUtils; - -/** - * 字符串格式化 - * @author huaheng - */ -public class StrFormatter { - - public static final String EMPTY_JSON = "{}"; - public static final char C_BACKSLASH = '\\'; - public static final char C_DELIM_START = '{'; - public static final char C_DELIM_END = '}'; - - /** - * 格式化字符串<br> - * 此方法只是简单将占位符 {} 按照顺序替换为参数<br> - * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br> - * 例:<br> - * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br> - * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br> - * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> - * @param strPattern 字符串模板 - * @param argArray 参数列表 - * @return 结果 - */ - public static String format(final String strPattern, final Object... argArray) { - if (StringUtils.isEmpty(strPattern) || StringUtils.isEmpty(argArray)) { - return strPattern; - } - final int strPatternLength = strPattern.length(); - - // 初始化定义好的长度以获得更好的性能 - StringBuilder sbuf = new StringBuilder(strPatternLength + 50); - - int handledPosition = 0; - int delimIndex;// 占位符所在位置 - for (int argIndex = 0; argIndex < argArray.length; argIndex++) { - delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition); - if (delimIndex == -1) { - if (handledPosition == 0) { - return strPattern; - } else { // 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果 - sbuf.append(strPattern, handledPosition, strPatternLength); - return sbuf.toString(); - } - } else { - if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) { - if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) { - // 转义符之前还有一个转义符,占位符依旧有效 - sbuf.append(strPattern, handledPosition, delimIndex - 1); - sbuf.append(Convert.utf8Str(argArray[argIndex])); - handledPosition = delimIndex + 2; - } else { - // 占位符被转义 - argIndex--; - sbuf.append(strPattern, handledPosition, delimIndex - 1); - sbuf.append(C_DELIM_START); - handledPosition = delimIndex + 1; - } - } else { - // 正常占位符 - sbuf.append(strPattern, handledPosition, delimIndex); - sbuf.append(Convert.utf8Str(argArray[argIndex])); - handledPosition = delimIndex + 2; - } - } - } - // append the characters following the last {} pair. - // 加入最后一个占位符后所有的字符 - sbuf.append(strPattern, handledPosition, strPattern.length()); - - return sbuf.toString(); - } - -} -- libgit2 0.22.2