1.20 发布

This commit is contained in:
cheney 2025-07-14 14:55:40 +08:00
parent a936011a8d
commit 729b6b217e
12 changed files with 336 additions and 272 deletions

34
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>com.sunyard.sge.database</groupId>
<artifactId>sydapi-database</artifactId>
<version>1.19</version>
<version>1.20</version>
<name>sydapi-database</name>
@ -29,25 +29,35 @@
<artifactId>groovy</artifactId>
<version>2.4.15</version>
</dependency>
<dependency>
<groupId>com.sunyard</groupId>
<artifactId>proto</artifactId>
<version>1.0-20220905</version>
</dependency>
<dependency>
<groupId>com.sunyard</groupId>
<artifactId>sydapi4j</artifactId>
<version>1.15.20250711</version>
<exclusions>
<!-- 排除 log4j-api -->
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
<!-- 排除 log4j-core -->
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log.version}</version>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>2.4.15</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log.version}</version>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -1,9 +1,6 @@
package com.sunyard.sge.database;
import com.sunyard.sge.log.LogFactory;
import com.sunyard.sge.plus.SydLibrary;
import org.apache.logging.log4j.Logger;
import racal.sunyard.main.SydApi4j;
import java.util.List;
@ -29,8 +26,7 @@ public interface SydApi {
if (pcLogPath == null || iLogLevel < 0 || iLogLevel > 3) {
throw new IllegalArgumentException("地址不能为空且等级范围为0到3");
}
Logger logger = LogFactory.createLogger(pcLogPath, iLogLevel);
SydApi4j.setLogger(logger);
// logback 版本的日志直接由外部配置文件控制
// 初始化 C 部分日志
SydLibrary.INSTANCE.SYD_SetLogConfig(pcLogPath, pcLogPath.getBytes().length, iLogLevel);
@ -278,10 +274,10 @@ public interface SydApi {
*
* @param iEncKeyIndex输入加密所需要的密钥索引值
* @param iMacKeyIndex输入计算mac所需要的密钥索引值
* @param pcData 输入计算mac所需要的数据
* @param pcData 输入计算mac所需要的数据
* @return DataAndMac
* - data 密文
* - mac值 (固定长度16字节)转HEX 字符串格式
* - data 密文
* - mac值 (固定长度16字节)转HEX 字符串格式
*/
DataAndMac SYD_SM4_EncryptAndMac_ShortData(
int iEncKeyIndex,
@ -291,10 +287,11 @@ public interface SydApi {
/**
* 短数据校验 MAC 并解密
*
* @param iEncKeyIndex输入加密所需要的密钥索引值
* @param iMacKeyIndex输入计算mac所需要的密钥索引值
* @param pcData 输入密文
* @param mac 输入密文的 MAC
* @param pcData 输入密文
* @param mac 输入密文的 MAC
* @return 明文
*/
DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(
@ -310,6 +307,7 @@ public interface SydApi {
int[] iMacKeyIndex,
List<byte[]> pcData
);
List<DataAndMacCheck> SYD_SM4_CheckMacAndDecrypt_BatchData(
int[] iEncKeyIndex,
int[] iMacKeyIndex,

View File

@ -1,6 +1,9 @@
package com.sunyard.sge.database;
import com.sunyard.log.ILogFactory;
import com.sunyard.log.ILogger;
import com.sunyard.sge.database.pool.HsmLinkInfo;
import com.sunyard.sge.log.LogbackFactory;
import racal.sunyard.main.SydApi4j;
import java.util.ArrayList;

View File

@ -1,12 +1,13 @@
package com.sunyard.sge.database;
import com.sunyard.SydApiException;
import com.sunyard.log.ILogFactory;
import com.sunyard.log.ILogger;
import com.sunyard.proto.Util;
import com.sunyard.sge.database.pool.HsmLinkInfo;
import com.sunyard.sge.log.LogFactory;
import com.sunyard.sge.log.LogbackFactory;
import com.sunyard.sge.pool.ObjectInPool;
import com.sunyard.util.SYMUtil;
import org.apache.logging.log4j.Logger;
import racal.sunyard.main.SydApi4j;
import java.util.ArrayList;
@ -20,8 +21,10 @@ public class SydApiBaseFunction implements SydApi {
private SydApi4j[] hsms = null;
private HsmLinkInfo[] linkInfos = null;
private ObjectInPool oip = null;
Logger log = LogFactory.getLogger();
private boolean isShortLinkMode = false;
private ILogFactory iLogFactory = new LogbackFactory();
private ILogger log = iLogFactory.getLogger(SydApiBaseFunction.class);
public SydApiBaseFunction() {
isShortLinkMode = true;
@ -52,8 +55,8 @@ public class SydApiBaseFunction implements SydApi {
@Override
public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int allDealTimeOut) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_Connect_Ex方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList), Arrays.toString(iPortList), iConnectTimeOut, allDealTimeOut);
}
@ -118,7 +121,7 @@ public class SydApiBaseFunction implements SydApi {
@Override
public byte[] SYD_SM3_Hash_ShortData(byte[] pcData) {
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM3_Hash_ShortData方法接收参数pcData{}", Arrays.toString(pcData));
}
if (null == this.hsms || this.hsms.length < 1) {
@ -179,8 +182,8 @@ public class SydApiBaseFunction implements SydApi {
@Override
public LongDataReturn<byte[], byte[]> SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, byte[] pcProcData) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM3_Hash_LongData 方法接收参数pcData{},iPkgNum{},pcProcData{}", Arrays.toString(pcData), iPkgNum, pcProcData);
}
@ -246,11 +249,11 @@ public class SydApiBaseFunction implements SydApi {
@Override
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
Logger log = LogFactory.getLogger();
if (iKeyIndex < 0) {
throw new SydApiException(0x01000001);
}
if (LogFactory.iLogLevel == 3) {
if ( log.isDebugEnabled() ) {
log.debug("SYD_SM4_ShortData方法接收参数iKeyIndex{},pcData{},iFlag{}", iKeyIndex, Arrays.toString(pcData), iFlag);
}
if (iFlag == Consts.ECB_DEC && null == pcData) {
@ -333,8 +336,8 @@ public class SydApiBaseFunction implements SydApi {
@Override
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4_BatchData方法接收参数iKeyIndex{},pcData元素个数为{},iFlag{}", iKeyIndex, pcData.size(), iFlag);
}
@ -422,8 +425,8 @@ public class SydApiBaseFunction implements SydApi {
@Override
public byte[] SYD_SM4_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4_LongData方法接收参数iKeyIndex{},pcData{},iPkgNum{},iFlag{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag);
}
@ -512,8 +515,8 @@ public class SydApiBaseFunction implements SydApi {
@Override
public List<byte[]> SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4Mac_BatchDataiKeyIndex{},pcData元素个数为{}", iKeyIndex, pcData.size());
}
@ -590,8 +593,8 @@ public class SydApiBaseFunction implements SydApi {
@Override
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, List<byte[]> pcMac) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4Mac_BatchData 方法接收参数iKeyIndex{}, pcData元素个数为{},pcMac{}", iKeyIndex, pcData.size(), pcMac.size());
}
@ -666,8 +669,8 @@ public class SydApiBaseFunction implements SydApi {
@Override
public LongDataReturn<byte[], byte[]> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, byte[] pcProcData) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4Mac_LongData方法接收参数iKeyIndex{},pcData{},iPkgNum{},pcProcData{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData);
}
if (null == this.hsms || this.hsms.length < 1) {
@ -756,8 +759,8 @@ public class SydApiBaseFunction implements SydApi {
@Override
public LongDataReturn<byte[], Boolean> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, byte[] pcProcData, byte[] pcMac) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4Mac_LongData方法接收参数iKeyIndex{}, iPkgNum{},pcProcData{}", iKeyIndex, iPkgNum, pcProcData);
}
updateLastUseTime();
@ -778,8 +781,8 @@ public class SydApiBaseFunction implements SydApi {
}
public byte[] SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, int limitDataLen) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4Mac_ShortData方法接收参数iKeyIndex{},pcData{},iFlag{}", iKeyIndex, Arrays.toString(pcData));
}
@ -844,8 +847,8 @@ public class SydApiBaseFunction implements SydApi {
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, int limitDataLen, byte[] pcMac) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4Mac_ShortData 方法接收参数:{}", iKeyIndex, Util.bytes2HexString(pcData), Util.bytes2HexString(pcMac));
}
@ -877,8 +880,8 @@ public class SydApiBaseFunction implements SydApi {
*/
@Override
public DataAndMac SYD_SM4_EncryptAndMac_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4_EncryptAndMac_ShortDatak1={} k2={}", iEncKeyIndex, iMacKeyIndex);
}
// 参数检查在函数内部
@ -891,8 +894,8 @@ public class SydApiBaseFunction implements SydApi {
@Override
public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, byte[] mac) {
Logger log = LogFactory.getLogger();
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4_CheckMacAndDecrypt_ShortDatak1={} k2={}", iEncKeyIndex, iMacKeyIndex);
}
@ -909,7 +912,7 @@ public class SydApiBaseFunction implements SydApi {
@Override
public List<DataAndMac> SYD_SM4_EncryptAndMac_BatchData(int[] iEncKeyIndex, int[] iMacKeyIndex, List<byte[]> pcData) {
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4_EncryptAndMac_BatchData");
}
@ -940,7 +943,7 @@ public class SydApiBaseFunction implements SydApi {
@Override
public List<DataAndMacCheck> SYD_SM4_CheckMacAndDecrypt_BatchData(int[] iEncKeyIndex, int[] iMacKeyIndex, List<DataAndMac> pcData) {
if (LogFactory.iLogLevel == 3) {
if (log.isDebugEnabled()) {
log.debug("SYD_SM4_CheckMacAndDecrypt_BatchData");
}

View File

@ -3,12 +3,11 @@ package com.sunyard.sge.database;
import com.sunyard.SydApiException;
import com.sunyard.sge.database.pool.HsmLinkInfo;
import com.sunyard.sge.database.pool.SydApiPool;
import com.sunyard.sge.log.LogFactory;
import com.sunyard.sge.plus.ConnectHandlerManager;
import com.sunyard.sge.plus.SydLibrary;
import com.sunyard.sge.pool.CommWorker;
import com.sunyard.sge.pool.PoolConfig;
import org.apache.logging.log4j.Logger;
import com.sunyard.sge.pool.ThreadBasedConnectPool;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
@ -22,9 +21,10 @@ public class SydApiBuilder {
int[] iPortList,
int iConnectTimeOut,
int iDealTimeOut
) {
this( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut, new PoolConfig() );
) {
this(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut, new PoolConfig());
}
public SydApiBuilder(
String[] pcIpList,
int[] iPortList,
@ -32,18 +32,18 @@ public class SydApiBuilder {
int iDealTimeOut,
PoolConfig config
) {
Logger log = LogFactory.getLogger();
org.slf4j.Logger log = LoggerFactory.getLogger(ThreadBasedConnectPool.class);
if ( log.isDebugEnabled() ) {
if (log.isDebugEnabled()) {
log.debug("创建链接");
log.debug("SydApiBuilder构造方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iDealTimeOut);
log.debug("SydApiBuilder构造方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList), Arrays.toString(iPortList), iConnectTimeOut, iDealTimeOut);
log.debug("pool config {}", config);
}
// 同时初始化 jna c 的链接
int ret = ConnectHandlerManager.init(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
if ( 0 != ret ) {
if (0 != ret) {
throw new SydApiException(-1);
}
@ -51,7 +51,7 @@ public class SydApiBuilder {
HsmLinkInfo[] linkInfo = HsmLinkInfo.mutiHsmLinkInfo(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
this.pool = new SydApiPool(config, linkInfo);
longLinkWorks = new CommWorker();
warmUp( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut );
warmUp(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
}
public CommWorker getLongLinkWorks() {
@ -60,52 +60,54 @@ public class SydApiBuilder {
/**
* 获取池内对象
*
* @return 池内对象
*/
public SydApi get(){
public SydApi get() {
return pool.get();
}
/**
* 升级
* 修改了原实现
*
* @return 包装对象
*/
public SydApi build() {
return new SydApiLongLinkWrap( this );
return new SydApiLongLinkWrap(this);
}
private void warmUp(String[] pcIpList,
int[] iPortList,
int iConnectTimeOut,
int iDealTimeOut){
int iDealTimeOut) {
SydApi api = null;
try {
api = new SydApi4Database();
api.SYD_Short_Connect_Ex( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut );
api.SYD_Short_Connect_Ex(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
try {
api.SYD_SM3_Hash_ShortData( new byte[8] );
api.SYD_SM3_Hash_ShortData(new byte[8]);
}catch ( Exception e) {
} catch (Exception e) {
// 忽略
}
try {
api.SYD_SM4_ShortData( 0 , new byte[16], Consts.ECB_ENC );
api.SYD_SM4_ShortData(0, new byte[16], Consts.ECB_ENC);
}catch ( Exception e) {
} catch (Exception e) {
// 忽略
}
} catch ( Exception e ){
} catch (Exception e) {
// 忽略预热失败
} finally {
if ( null != api ) {
if (null != api) {
api.SYD_Disconnect_Ex();
}
}

View File

@ -6,10 +6,10 @@ import com.sun.jna.ptr.IntByReference;
import com.sunyard.SydApiException;
import com.sunyard.proto.Util;
import com.sunyard.sge.bytes.BytesUtil;
import com.sunyard.sge.log.LogFactory;
import com.sunyard.sge.plus.ConnectHandlerManager;
import com.sunyard.sge.plus.SydLibrary;
import org.apache.logging.log4j.Logger;
import com.sunyard.sge.pool.ThreadBasedConnectPool;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
@ -26,7 +26,7 @@ import static com.sunyard.sge.database.SydApiConfig.BATCH_SIZE;
*/
public class SydApiLongLinkWrap implements SydApi {
private Logger log = LogFactory.getLogger();
org.slf4j.Logger log = LoggerFactory.getLogger(ThreadBasedConnectPool.class);
private SydApiBuilder builder;
@ -55,7 +55,7 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -73,7 +73,7 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -91,7 +91,7 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -109,7 +109,7 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -128,7 +128,7 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -146,7 +146,7 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -164,7 +164,7 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -182,14 +182,14 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
private DataAndMac SYD_SM4_AllInOne(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData){
private DataAndMac SYD_SM4_AllInOne(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData) {
try {
FutureTask<byte[]> taskEnc = builder.getLongLinkWorks().fTask(new Callable<byte[]>() {
@Override
@ -209,12 +209,10 @@ public class SydApiLongLinkWrap implements SydApi {
byte[] mac = taskMac.get();
return new DataAndMac(enData, mac);
}
catch (SydApiException e) {
} catch (SydApiException e) {
throw e;
}
catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
} catch (Exception e) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -225,7 +223,7 @@ public class SydApiLongLinkWrap implements SydApi {
public DataAndMac SYD_SM4_EncryptAndMac_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData) {
log.info("SYD_SM4_EncryptAndMac_ShortData {} {}", iEncKeyIndex, iMacKeyIndex);
try {
if ( ! ConnectHandlerManager.isInited() ) {
if (!ConnectHandlerManager.isInited()) {
throw new SydApiException(-1);
}
@ -245,9 +243,9 @@ public class SydApiLongLinkWrap implements SydApi {
mac
);
if ( 0 == i ){
if (0 == i) {
byte[] data = new byte[encDataLen.getValue()];
if( data.length == encData.length ) {
if (data.length == encData.length) {
data = encData;
} else {
System.arraycopy(encData, 0, data, 0, data.length);
@ -318,12 +316,10 @@ public class SydApiLongLinkWrap implements SydApi {
//
//
// return new DataAndMac(enData, mac.getData());
}
catch (SydApiException e) {
} catch (SydApiException e) {
throw e;
}
catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
} catch (Exception e) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -334,7 +330,7 @@ public class SydApiLongLinkWrap implements SydApi {
public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, byte[] macs) {
log.info("SYD_SM4_CheckMacAndDecrypt_ShortData {} {}", iEncKeyIndex, iMacKeyIndex);
try {
if ( ! ConnectHandlerManager.isInited() ) {
if (!ConnectHandlerManager.isInited()) {
throw new SydApiException(-1);
}
@ -357,19 +353,18 @@ public class SydApiLongLinkWrap implements SydApi {
System.arraycopy(decryptedData, 0, data, 0, data.length);
System.out.println("outlen=" + data.length);
if ( 0 == i ){
if (0 == i) {
return new DataAndMacCheck(data, true);
} if (0x01000010 == i) {
}
if (0x01000010 == i) {
return new DataAndMacCheck(data, false);
} else {
throw new SydApiException(i);
}
}
catch (SydApiException e) {
} catch (SydApiException e) {
throw e;
}
catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
} catch (Exception e) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -393,7 +388,7 @@ public class SydApiLongLinkWrap implements SydApi {
public List<DataAndMac> SYD_SM4_EncryptAndMac_BatchData(int[] iEncKeyIndex, int[] iMacKeyIndex, List<byte[]> pcData) {
log.info("SYD_SM4_EncryptAndMac_BatchData {} {}", iEncKeyIndex, iMacKeyIndex);
if ( ! ConnectHandlerManager.isInited() ) {
if (!ConnectHandlerManager.isInited()) {
throw new SydApiException(-1);
}
@ -420,7 +415,7 @@ public class SydApiLongLinkWrap implements SydApi {
pcDataArray[i] = memory;
iDataLen[i] = bytes.length;
iEncDataLen[i] = BytesUtil.calculatePaddedLength(bytes.length);
pcEncArray[i] = new Memory( iEncDataLen[i] );
pcEncArray[i] = new Memory(iEncDataLen[i]);
pcMacArray[i] = new Memory(16);
}
@ -440,19 +435,17 @@ public class SydApiLongLinkWrap implements SydApi {
// 转换结果为Java风格的对象列表
List<DataAndMac> results = new ArrayList<>();
for (int i = 0; i < size; i++) {
results.add(new DataAndMac(pcEncArray[i].getByteArray(0, iEncDataLen[i]), pcMacArray[i].getByteArray(0,16)));
results.add(new DataAndMac(pcEncArray[i].getByteArray(0, iEncDataLen[i]), pcMacArray[i].getByteArray(0, 16)));
}
return results;
} else {
throw new SydApiException(result);
}
}
catch (SydApiException e) {
} catch (SydApiException e) {
throw e;
}
catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
} catch (Exception e) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -478,7 +471,7 @@ public class SydApiLongLinkWrap implements SydApi {
log.info("SYD_SM4_CheckMacAndDecrypt_BatchData {} {}", iEncKeyIndex, iMacKeyIndex);
int size = pcData.size();
log.debug("size {}" , size);
log.debug("size {}", size);
// 1. 准备明文参数
Pointer[] pcDataArray = new Pointer[pcData.size()];
@ -499,23 +492,23 @@ public class SydApiLongLinkWrap implements SydApi {
// 为每个 byte[] 分配本地内存并复制数据
Memory memory = new Memory( dm.getData().length );
Memory memory = new Memory(dm.getData().length);
memory.write(0, dm.getData(), 0, dm.getData().length);
pcEncArray[i] = memory;
iEncDataLen[i] = dm.getData().length;
log.debug("pcEncArray={}" , dm.getData().length);
log.debug("pcEncArray={}", dm.getData().length);
pcDataArray[i] = new Memory( dm.getData().length );
pcDataArray[i] = new Memory(dm.getData().length);
iDataLen[i] = dm.getData().length;
log.debug("iDataLen= {}" , dm.getData().length);
log.debug("iDataLen= {}", dm.getData().length);
Memory mem_mac = new Memory(16);
mem_mac.write(0, dm.getMac(), 0, 16);
pcMacArray[i] = mem_mac;
if ( log.isDebugEnabled() ) {
if (log.isDebugEnabled()) {
log.debug("mem_mac=" + Util.bytes2HexString(dm.getMac()));
}
}
@ -577,15 +570,15 @@ public class SydApiLongLinkWrap implements SydApi {
@Override
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
if ( null == iKeyIndex || null == pcData ) {
if (null == iKeyIndex || null == pcData) {
throw new IllegalArgumentException("批量输入数据为 null");
}
if ( iKeyIndex.length != pcData.size() ) {
if (iKeyIndex.length != pcData.size()) {
throw new IllegalArgumentException("批量输入数据密钥和数据数量不等");
}
if ( 0 == iKeyIndex.length ) {
if (0 == iKeyIndex.length) {
return new ArrayList<>();
}
// 将数据进行分组防止超长
@ -646,30 +639,28 @@ public class SydApiLongLinkWrap implements SydApi {
}
return ret;
}
catch (ExecutionException e) {
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof SydApiException) {
throw (SydApiException) cause;
}
throw new SydApiException("并行任务执行失败", 0xC101);
}
catch (Exception e) {
} catch (Exception e) {
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@Override
public List<byte[]> SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
if ( null == iKeyIndex || null == pcData ) {
if (null == iKeyIndex || null == pcData) {
throw new IllegalArgumentException("批量输入数据为 null");
}
if ( iKeyIndex.length != pcData.size() ) {
if (iKeyIndex.length != pcData.size()) {
throw new IllegalArgumentException("批量输入数据密钥和数据数量不等");
}
if ( 0 == iKeyIndex.length ) {
if (0 == iKeyIndex.length) {
return new ArrayList<>();
}
// 将数据进行分组防止超长
@ -730,7 +721,7 @@ public class SydApiLongLinkWrap implements SydApi {
return ret;
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
@ -739,15 +730,15 @@ public class SydApiLongLinkWrap implements SydApi {
@Override
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, List<byte[]> pcMac) {
if ( null == iKeyIndex || null == pcData ) {
if (null == iKeyIndex || null == pcData) {
throw new IllegalArgumentException("批量输入数据为 null");
}
if ( iKeyIndex.length != pcData.size() ) {
if (iKeyIndex.length != pcData.size()) {
throw new IllegalArgumentException("批量输入数据密钥和数据数量不等");
}
if ( 0 == iKeyIndex.length ) {
if (0 == iKeyIndex.length) {
return new boolean[0];
}
@ -822,7 +813,7 @@ public class SydApiLongLinkWrap implements SydApi {
return toBoolArray(ret);
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
if (e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);

View File

@ -4,11 +4,11 @@ import com.sunyard.SydApiException;
import com.sunyard.sge.database.SydApi;
import com.sunyard.sge.database.SydApi4Database;
import com.sunyard.sge.database.SydApiBaseFunction;
import com.sunyard.sge.log.LogFactory;
import com.sunyard.sge.pool.ObjectInPool;
import com.sunyard.sge.pool.PoolConfig;
import com.sunyard.sge.pool.ThreadBasedConnectPool;
import org.apache.logging.log4j.Logger;
import org.slf4j.LoggerFactory;
import racal.sunyard.main.SydApi4j;
public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
@ -24,7 +24,7 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
// 继承 初始化
protected SydApi initialValue() {
Logger log = LogFactory.getLogger();
org.slf4j.Logger log = LoggerFactory.getLogger(ThreadBasedConnectPool.class);
// 所有加密机一次性链接
SydApi4j[] apis = new SydApi4j[linkInfos.length];

View File

@ -1,122 +1,122 @@
package com.sunyard.sge.log;
import com.sunyard.sge.database.Consts;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.Logger;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 金交所定制日志
*/
public class LogFactory {
// 金交所定制日志
public static final String logName = "sge-hsm-sydapi-log";
// 日志级别
public static int iLogLevel = Consts.LogLevelOFF;
// Levels
private static final Level[] levels = new Level[]{ Level.OFF, Level.ERROR, Level.INFO, Level.DEBUG };
// 日志目录
private static String pcLogPath = "./sydapi-logs/";
// // 日期格式
// private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
private LogFactory() {
}
public static void start() {
// 上下文
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
// 创建一个展示的样式
Layout layout = PatternLayout.createLayout(
"[%level{DEBUG=3, INFO=2, ERROR=1, OFF=0}: %d{ISO8601} <%thread>] %message%n%throwable",
null,
config, null, null, true, false, null, null);
TriggeringPolicy tp = SizeBasedTriggeringPolicy.createPolicy("10MB");
// Appender appender = RollingFileAppender.createAppender(String.format("logs/test/syncshows-job-%s.log", jobID),
// "/logs/test/" + jobID + "/syncshows-job-" + jobID + ".log.gz",
// "true", jobID, null, null, null, tp, null, layout, null,
// null, null, null, config);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// 日志打印方式输出为文件   
Appender appender = FileAppender.createAppender(
String.format( pcLogPath + "sydapi_"+ format.format(new Date()) + ".log"),
"true", "false",
logName, null, "true", "true", null, layout, null, null, null, config);
appender.start();
config.addAppender(appender);
AppenderRef ref = AppenderRef.createAppenderRef( logName, levels[ iLogLevel ], null);
AppenderRef[] refs = new AppenderRef[]{ref};
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", levels[ iLogLevel ], logName,
"true", refs, null, config, null);
loggerConfig.addAppender(appender, levels[ iLogLevel ], null);
config.addLogger( logName, loggerConfig);
ctx.updateLoggers();
}
public static void stop() {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
config.getAppender( logName ).stop();
config.getLoggerConfig(logName).removeAppender( logName);
config.removeLogger(logName);
ctx.updateLoggers();
}
public static int getiLogLevel() {
return iLogLevel;
}
/**
* 创建 Logger
* @return Logger
*/
public static Logger createLogger(String pcLogPath, int iLogLevel) {
if( null == pcLogPath || pcLogPath.trim().equals("") ) {
pcLogPath = "./";
}
if( ! pcLogPath.endsWith("/") ) {
pcLogPath = pcLogPath + "/";
}
LogFactory.pcLogPath = pcLogPath;
LogFactory.iLogLevel = iLogLevel;
start();
return LogManager.getLogger(logName);
}
/**
* 获取 Logger
* @return Logger
*/
public static Logger getLogger() {
return LogManager.getLogger(logName);
}
}
//package com.sunyard.sge.log;
//
//import com.sunyard.sge.database.Consts;
//import org.apache.logging.log4j.Level;
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.core.Appender;
//import org.apache.logging.log4j.core.Layout;
//import org.apache.logging.log4j.core.LoggerContext;
//import org.apache.logging.log4j.core.appender.FileAppender;
//import org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy;
//import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
//import org.apache.logging.log4j.core.config.AppenderRef;
//import org.apache.logging.log4j.core.config.Configuration;
//import org.apache.logging.log4j.core.config.LoggerConfig;
//import org.apache.logging.log4j.core.layout.PatternLayout;
//import org.apache.logging.log4j.Logger;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//
///**
// * 金交所定制日志
// */
//public class LogFactory {
//
// // 金交所定制日志
// public static final String logName = "sge-hsm-sydapi-log";
//
// // 日志级别
// public static int iLogLevel = Consts.LogLevelOFF;
//
// // Levels
// private static final Level[] levels = new Level[]{ Level.OFF, Level.ERROR, Level.INFO, Level.DEBUG };
//
// // 日志目录
// private static String pcLogPath = "./sydapi-logs/";
//
//// // 日期格式
//// private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//
// private LogFactory() {
// }
//
// public static void start() {
// // 上下文
// final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
// final Configuration config = ctx.getConfiguration();
//
// // 创建一个展示的样式
// Layout layout = PatternLayout.createLayout(
// "[%level{DEBUG=3, INFO=2, ERROR=1, OFF=0}: %d{ISO8601} <%thread>] %message%n%throwable",
// null,
// config, null, null, true, false, null, null);
//
//
// TriggeringPolicy tp = SizeBasedTriggeringPolicy.createPolicy("10MB");
//
//// Appender appender = RollingFileAppender.createAppender(String.format("logs/test/syncshows-job-%s.log", jobID),
//// "/logs/test/" + jobID + "/syncshows-job-" + jobID + ".log.gz",
//// "true", jobID, null, null, null, tp, null, layout, null,
//// null, null, null, config);
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// // 日志打印方式输出为文件   
// Appender appender = FileAppender.createAppender(
// String.format( pcLogPath + "sydapi_"+ format.format(new Date()) + ".log"),
// "true", "false",
// logName, null, "true", "true", null, layout, null, null, null, config);
//
// appender.start();
// config.addAppender(appender);
// AppenderRef ref = AppenderRef.createAppenderRef( logName, levels[ iLogLevel ], null);
// AppenderRef[] refs = new AppenderRef[]{ref};
// LoggerConfig loggerConfig = LoggerConfig.createLogger("false", levels[ iLogLevel ], logName,
// "true", refs, null, config, null);
// loggerConfig.addAppender(appender, levels[ iLogLevel ], null);
// config.addLogger( logName, loggerConfig);
// ctx.updateLoggers();
// }
//
//
// public static void stop() {
// final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
// final Configuration config = ctx.getConfiguration();
// config.getAppender( logName ).stop();
// config.getLoggerConfig(logName).removeAppender( logName);
// config.removeLogger(logName);
// ctx.updateLoggers();
// }
//
// public static int getiLogLevel() {
// return iLogLevel;
// }
//
// /**
// * 创建 Logger
// * @return Logger
// */
// public static Logger createLogger(String pcLogPath, int iLogLevel) {
// if( null == pcLogPath || pcLogPath.trim().equals("") ) {
// pcLogPath = "./";
// }
// if( ! pcLogPath.endsWith("/") ) {
// pcLogPath = pcLogPath + "/";
// }
//
// LogFactory.pcLogPath = pcLogPath;
// LogFactory.iLogLevel = iLogLevel;
// start();
// return LogManager.getLogger(logName);
//
// }
//
// /**
// * 获取 Logger
// * @return Logger
// */
// public static Logger getLogger() {
// return LogManager.getLogger(logName);
// }
//
//
//}

View File

@ -0,0 +1,19 @@
package com.sunyard.sge.log;
import com.sunyard.log.ILogFactory;
import com.sunyard.log.ILogger;
import racal.sunyard.main.SydApi4j;
public class LogbackFactory implements ILogFactory {
static {
SydApi4j.setLogFactory(new LogbackFactory());
}
@Override
public ILogger getLogger(Class cls) {
return new LogbackLogger(cls);
}
}

View File

@ -0,0 +1,44 @@
package com.sunyard.sge.log;
import com.sunyard.log.ILogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogbackLogger implements ILogger {
private Class aClass;
public LogbackLogger(Class cls) {
this.aClass = cls;
}
@Override
public int getCurrentLogLevel() {
return -1;
}
@Override
public void error(String msg, Object... params) {
Logger logger = LoggerFactory.getLogger(aClass);
logger.error(msg, params);
}
@Override
public void info(String msg, Object... params) {
Logger logger = LoggerFactory.getLogger(aClass);
logger.info(msg, params);
}
@Override
public void debug(String msg, Object... params) {
Logger logger = LoggerFactory.getLogger(aClass);
logger.debug(msg, params);
}
@Override
public boolean isDebugEnabled() {
Logger logger = LoggerFactory.getLogger(aClass);
return logger.isDebugEnabled();
}
}

View File

@ -1,10 +1,8 @@
package com.sunyard.sge.pool;
import com.sunyard.sge.log.LogFactory;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Iterator;
@ -24,9 +22,11 @@ public class ThreadBasedConnectPool<T> {
// 配置
private PoolConfig config;
private Logger log = LoggerFactory.getLogger(ThreadBasedConnectPool.class);
public ThreadBasedConnectPool(PoolConfig config) {
this.config = config;
Logger log = LogFactory.getLogger();
threadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {

View File

@ -3,12 +3,6 @@ package test;
import com.sunyard.SydApiException;
import com.sunyard.proto.Util;
import com.sunyard.sge.database.*;
import com.sunyard.sge.log.LogFactory;
import com.sunyard.sge.plus.ConnectHandlerManager;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;