Changes
This commit is contained in:
parent
8532ad12b1
commit
dd7bd5577a
@ -1,7 +1,7 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.util.List;
|
||||
@ -26,10 +26,18 @@ public interface SydApi {
|
||||
static void SYD_SetLogConfig(
|
||||
String pcLogPath,
|
||||
int iLogLevel) {
|
||||
|
||||
Logger log = LogFactory.getLogger();
|
||||
try {
|
||||
if (iLogLevel<0||iLogLevel>3){
|
||||
throw new IllegalArgumentException("IllegalArgumentException");
|
||||
}
|
||||
Logger logger = LogFactory.createLogger(pcLogPath, iLogLevel);
|
||||
SydApi4j.setLogger( logger );
|
||||
}
|
||||
catch (IllegalArgumentException e){
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立到指定地址和端口号的签名服务器的 TCP/IP 连接,返回 API 操作对象 SydApi。
|
||||
@ -280,8 +288,4 @@ public interface SydApi {
|
||||
byte[] pcData,
|
||||
String pcMac
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -2,16 +2,19 @@ package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import com.sunyard.util.SYMUtil;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 上海黄金交易所 - 数据库加密方案
|
||||
*/
|
||||
public class SydApi4Database implements SydApi {
|
||||
|
||||
Logger log = LogFactory.getLogger();
|
||||
private SydApi4j[] hsms = null;
|
||||
|
||||
@Deprecated
|
||||
@ -24,6 +27,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public SydApi SYD_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int iDealTimeOut) {
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_Connect_Ex方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList), Arrays.toString(iPortList), iConnectTimeOut, iDealTimeOut);
|
||||
}
|
||||
SydApi4j api = (SydApi4j) new SydApi4j().connect(pcIpList[0], iPortList[0], null, iConnectTimeOut);
|
||||
hsms = new SydApi4j[]{ api };
|
||||
return this;
|
||||
@ -43,6 +49,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public String SYD_SM3_Hash_ShortData(byte[] pcData) {
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM3_Hash_ShortData方法接收参数:pcData:{}", Arrays.toString(pcData));
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@ -51,6 +60,7 @@ public class SydApi4Database implements SydApi {
|
||||
for (SydApi4j api : this.hsms) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
||||
log.debug("api.SYD_SM3_LongData方法返回值:{}",Arrays.toString(hash));
|
||||
return Util.bytes2HexString(hash);
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
@ -65,6 +75,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM3_Hash_LongData方法接收参数:pcData:{},iPkgNum:{},pcProcData:{}", Arrays.toString(pcData), iPkgNum, pcProcData);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@ -72,12 +85,14 @@ public class SydApi4Database implements SydApi {
|
||||
byte[] procData = null;
|
||||
if (2 == iPkgNum || 3 == iPkgNum) {
|
||||
procData = Util.hexString2Bytes(pcProcData);
|
||||
log.debug("Util.hexString2Bytes方法返回值:{}",Arrays.toString(procData));
|
||||
|
||||
}
|
||||
SydApiException le = null;
|
||||
for (SydApi4j api : this.hsms) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, procData, iPkgNum);
|
||||
|
||||
log.debug("api.SYD_SM3_LongData方法返回值:{}",Arrays.toString(hash));
|
||||
if (0 == iPkgNum || 3 == iPkgNum) {
|
||||
return new LongDataReturn<String, String>(null, Util.bytes2HexString(hash));
|
||||
} else {
|
||||
@ -96,7 +111,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
|
||||
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM4_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iFlag);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@ -132,6 +149,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM4_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},iFlag:{}", iKeyIndex, pcData.size(), iFlag);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@ -146,6 +166,7 @@ public class SydApi4Database implements SydApi {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYMEncryptDecryptBatchData(8, 1, null, keys, 1, pcData);
|
||||
|
||||
} else {
|
||||
return api.SYMEncryptDecryptBatchData(9, 1, null, keys, 1, pcData);
|
||||
}
|
||||
@ -162,6 +183,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public byte[] SYD_SM4_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM4_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@ -200,6 +224,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public String[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM4Mac_BatchData:iKeyIndex:{},pcData元素个数为:{}", iKeyIndex, pcData.size());
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@ -234,6 +261,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, String[] pcMac) {
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM4Mac_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},pcMac:{}", iKeyIndex, pcData.size(), Arrays.toString(pcMac));
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@ -245,7 +275,6 @@ public class SydApi4Database implements SydApi {
|
||||
SydApiException le = null;
|
||||
for (SydApi4j api : this.hsms) {
|
||||
try {
|
||||
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
boolean[] ret = new boolean[list.size()];
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
@ -269,6 +298,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@ -316,7 +348,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, Boolean> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData, String pcMac) {
|
||||
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData, pcMac);
|
||||
}
|
||||
LongDataReturn<String, String> ret = SYD_SM4Mac_LongData(iKeyIndex, pcData, iPkgNum, pcProcData);
|
||||
|
||||
// 仅一包 和 尾包 返回最终结果
|
||||
@ -330,13 +364,15 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
|
||||
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM4Mac_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData));
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex));
|
||||
|
||||
log.debug("\"SK\" + SYMUtil.genPrivateKeyMark方法返回值:{}",keyIndex);
|
||||
SydApiException le = null;
|
||||
for (SydApi4j api : this.hsms) {
|
||||
try {
|
||||
@ -356,6 +392,9 @@ public class SydApi4Database implements SydApi {
|
||||
|
||||
@Override
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, String pcMac) {
|
||||
if (LogFactory.iLogLevel==3) {
|
||||
log.debug("SYD_SM4Mac_ShortData方法接收参数:{}", iKeyIndex, Arrays.toString(pcData), pcMac);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@ -365,7 +404,7 @@ public class SydApi4Database implements SydApi {
|
||||
}
|
||||
|
||||
String mac = this.SYD_SM4Mac_ShortData(iKeyIndex, pcData);
|
||||
|
||||
log.debug(this+".SYD_SM4Mac_ShortData方法返回值:{}",mac);
|
||||
return pcMac.equals(mac);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,25 +3,24 @@ package com.sunyard.sge.database;
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
import com.sunyard.sge.database.pool.SydApiPool;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SydApiBuilder {
|
||||
|
||||
Logger log = LogFactory.getLogger();
|
||||
private SydApiPool pool;
|
||||
|
||||
public SydApiBuilder(
|
||||
String[] pcIpList,
|
||||
int[] iPortList,
|
||||
int iConnectTimeOut,
|
||||
int iDealTimeOut
|
||||
) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
log.debug("创建链接");
|
||||
|
||||
log.debug("SydApiBuilder构造方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iDealTimeOut);
|
||||
HsmLinkInfo[] linkInfo = HsmLinkInfo.mutiHsmLinkInfo(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
|
||||
log.debug("mutiHsmLinkInfo方法返回值:linkInfo:{}",linkInfo);
|
||||
this.pool = new SydApiPool(linkInfo);
|
||||
|
||||
warmUp( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut );
|
||||
}
|
||||
|
||||
|
||||
@ -2,13 +2,15 @@ package com.sunyard.sge.database.pool;
|
||||
|
||||
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* 到加密机的链接信息
|
||||
*/
|
||||
public class HsmLinkInfo {
|
||||
|
||||
private String pcIp;
|
||||
private int iPort;
|
||||
private int iConnectTimeOut;
|
||||
@ -24,8 +26,8 @@ public class HsmLinkInfo {
|
||||
|
||||
public static HsmLinkInfo[] mutiHsmLinkInfo(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int iAllDealTimeOut) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
log.debug("mutiHsmLinkInfo方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iAllDealTimeOut);
|
||||
log.debug("链接参数整理");
|
||||
|
||||
if (null == pcIpList &&
|
||||
null == iPortList &&
|
||||
pcIpList.length != iPortList.length) {
|
||||
@ -66,4 +68,14 @@ public class HsmLinkInfo {
|
||||
public int getiDealTimeOut() {
|
||||
return iDealTimeOut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "HsmLinkInfo{" +
|
||||
"pcIp='" + pcIp + '\'' +
|
||||
", iPort=" + iPort +
|
||||
", iConnectTimeOut=" + iConnectTimeOut +
|
||||
", iDealTimeOut=" + iDealTimeOut +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,8 +13,7 @@ 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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
@ -49,7 +48,7 @@ public class LogFactory {
|
||||
|
||||
// 创建一个展示的样式
|
||||
Layout layout = PatternLayout.createLayout(
|
||||
"[%level{DEBUG=3, INFO=2, ERROR=1, OFF=0}: %d{ISO8601} <%thread>] %message%n%throwable%n",
|
||||
"[%level{DEBUG=3, INFO=2, ERROR=1, OFF=0}: %d{ISO8601} <%thread>] %message%n%throwable",
|
||||
null,
|
||||
config, null, null, true, false, null, null);
|
||||
|
||||
@ -101,7 +100,8 @@ public class LogFactory {
|
||||
LogFactory.pcLogPath = pcLogPath;
|
||||
LogFactory.iLogLevel = iLogLevel;
|
||||
start();
|
||||
return LoggerFactory.getLogger( logName );
|
||||
return LogManager.getLogger(logName);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,6 +109,8 @@ public class LogFactory {
|
||||
* @return Logger
|
||||
*/
|
||||
public static Logger getLogger() {
|
||||
return LoggerFactory.getLogger( logName );
|
||||
return LogManager.getLogger(logName);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
package com.sunyard.sge.pool;
|
||||
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@ -12,6 +13,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ThreadBasedConnectPool<T> {
|
||||
|
||||
|
||||
// ThreadLocal
|
||||
private final Map<Thread, ObjectInPool<T>> map = new HashMap<>();
|
||||
|
||||
|
||||
@ -4,8 +4,8 @@ import com.sunyard.proto.Util;
|
||||
import com.sunyard.sge.database.*;
|
||||
import com.sunyard.sge.database.Consts;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.Assert;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
@ -16,7 +16,7 @@ public class Sample
|
||||
public static void main(String[] args) {
|
||||
|
||||
// 路径要以 / 或 \\ 结尾表示目录
|
||||
SydApi.SYD_SetLogConfig("./logs/", Consts.LogLevelDebug );
|
||||
SydApi.SYD_SetLogConfig("./logs/",2);
|
||||
|
||||
Logger log = LogFactory.getLogger();
|
||||
log.info("start");
|
||||
|
||||
@ -2,18 +2,29 @@ package test;
|
||||
|
||||
import com.sunyard.sge.database.SydApi;
|
||||
import com.sunyard.sge.database.SydApiBuilder;
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
import com.sunyard.sge.database.pool.SydApiPool;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 功能确认
|
||||
*/
|
||||
public class FuncTest {
|
||||
|
||||
private SydApi api;
|
||||
|
||||
@Before
|
||||
public void init(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void sm3(){
|
||||
SydApiBuilder builder = new SydApiBuilder(
|
||||
new String[]{ "192.168.0.200" },
|
||||
new int[]{ 8889 },
|
||||
@ -24,9 +35,4 @@ public class FuncTest {
|
||||
api = builder.build();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void sm3(){
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,15 +1,22 @@
|
||||
package test;
|
||||
|
||||
import com.sunyard.sge.database.SydApi;
|
||||
import com.sunyard.sge.database.SydApi4Database;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public class LogTest {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Logger log = LogFactory.createLogger("./test-log/", 2);
|
||||
log.error("error");
|
||||
log.info("info");
|
||||
log.debug("debug");
|
||||
SydApi.SYD_SetLogConfig("./test-log/",11);
|
||||
// int[] a1 = {1,2,3};
|
||||
// Logger log = LogFactory.createLogger("./test-log/", 9);
|
||||
// log.error("error");
|
||||
// log.info(Arrays.toString(a1));
|
||||
// log.debug("debug");
|
||||
// System.out.println("发送数据");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user