Report 기능 테스트중
@8976f34e7aab8683abcc1996f49318ff625fd229
--- src/main/java/com/munjaon/client/config/RunnerConfiguration.java
+++ src/main/java/com/munjaon/client/config/RunnerConfiguration.java
... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 |
package com.munjaon.client.config; |
| 2 | 2 |
|
| 3 |
+import com.munjaon.client.server.service.CollectClientService; |
|
| 3 | 4 |
import com.munjaon.client.server.service.PropertyLoader; |
| 4 | 5 |
import lombok.RequiredArgsConstructor; |
| 5 | 6 |
import lombok.extern.slf4j.Slf4j; |
... | ... | @@ -17,27 +18,35 @@ |
| 17 | 18 |
|
| 18 | 19 |
@Bean |
| 19 | 20 |
@Order(1) |
| 20 |
- public CommandLineRunner getRunnerBeanForProperty() {
|
|
| 21 |
+ public CommandLineRunner getRunnerBeanForProperty() throws ConfigurationException {
|
|
| 21 | 22 |
System.setProperty("PROPS", serverConfig.getServerProperyFile());
|
| 22 | 23 |
System.setProperty("ROOTPATH", serverConfig.getServerRootPath());
|
| 24 |
+ System.setProperty("DBMS", serverConfig.getString("DB.DBMS"));
|
|
| 23 | 25 |
PropertyLoader.load(); |
| 24 |
- try {
|
|
| 25 |
- String[] array = serverConfig.getStringArray("test.list");
|
|
| 26 |
- if (array != null && array.length > 0) {
|
|
| 27 |
- for (String s : array) {
|
|
| 28 |
- System.out.println("List : " + s);
|
|
| 29 |
- } |
|
| 30 |
- } |
|
| 31 |
- } catch (ConfigurationException e) {
|
|
| 32 |
- throw new RuntimeException(e); |
|
| 33 |
- } |
|
| 26 |
+// try {
|
|
| 27 |
+// String[] array = serverConfig.getStringArray("test.list");
|
|
| 28 |
+// if (array != null && array.length > 0) {
|
|
| 29 |
+// for (String s : array) {
|
|
| 30 |
+// System.out.println("List : " + s);
|
|
| 31 |
+// } |
|
| 32 |
+// } |
|
| 33 |
+// } catch (ConfigurationException e) {
|
|
| 34 |
+// throw new RuntimeException(e); |
|
| 35 |
+// } |
|
| 34 | 36 |
return args -> System.out.println("Runner Bean #1 : " + serverConfig.getServerProperyFile());
|
| 35 | 37 |
} |
| 36 | 38 |
|
| 37 | 39 |
@Bean |
| 38 | 40 |
@Order(2) |
| 39 |
- public CommandLineRunner getRunnerBeanForSmsQueue() {
|
|
| 40 |
- |
|
| 41 |
+ public CommandLineRunner getRunnerBeanForSms() {
|
|
| 42 |
+ try {
|
|
| 43 |
+ String serviceName = "SMS"; |
|
| 44 |
+ String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); |
|
| 45 |
+ CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); |
|
| 46 |
+ collectClientService.start(); |
|
| 47 |
+ } catch (Exception e) {
|
|
| 48 |
+ throw new RuntimeException(e); |
|
| 49 |
+ } |
|
| 41 | 50 |
return args -> System.out.println("Runner Bean #2");
|
| 42 | 51 |
} |
| 43 | 52 |
} |
--- src/main/java/com/munjaon/client/config/ServerConfig.java
+++ src/main/java/com/munjaon/client/config/ServerConfig.java
... | ... | @@ -34,14 +34,6 @@ |
| 34 | 34 |
void init() throws ConfigurationException {
|
| 35 | 35 |
// this.serverRootPath = System.getProperty("SERVICE_HOME");
|
| 36 | 36 |
this.serverProperyFile = serverRootPath + File.separator + "config" + File.separator + "munjaonAgent.conf"; |
| 37 |
- log.debug("serverProperyFile : {}", serverProperyFile);
|
|
| 38 |
- log.debug("serverProperyFile : {}", serverProperyFile);
|
|
| 39 |
- log.debug("serverProperyFile : {}", serverProperyFile);
|
|
| 40 |
- System.out.println(serverProperyFile); |
|
| 41 |
- System.out.println(serverProperyFile); |
|
| 42 |
- System.out.println(serverProperyFile); |
|
| 43 |
- System.out.println(serverProperyFile); |
|
| 44 |
- System.out.println(serverProperyFile); |
|
| 45 | 37 |
builder = new ReloadingFileBasedConfigurationBuilder<>(PropertiesConfiguration.class).configure(new Parameters().fileBased().setFile(new File(serverProperyFile))); |
| 46 | 38 |
|
| 47 | 39 |
builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST, new EventListener<Event>() {
|
--- src/main/java/com/munjaon/client/mariadb/service/MariaDBService.java
+++ src/main/java/com/munjaon/client/mariadb/service/MariaDBService.java
... | ... | @@ -17,9 +17,11 @@ |
| 17 | 17 |
public boolean checkTableExists() {
|
| 18 | 18 |
boolean isExist = false; |
| 19 | 19 |
try {
|
| 20 |
- mariaDBMapper.checkTableForMessage(); |
|
| 21 |
- mariaDBMapper.checkTableForLog(); |
|
| 22 |
- isExist = true; |
|
| 20 |
+ int msgCount = mariaDBMapper.checkTableForMessage(); |
|
| 21 |
+ int logCount = mariaDBMapper.checkTableForLog(); |
|
| 22 |
+ if (msgCount == 1 && logCount == 1) {
|
|
| 23 |
+ isExist = true; |
|
| 24 |
+ } |
|
| 23 | 25 |
} catch (Exception e) {
|
| 24 | 26 |
log.error("Check table exists error : {}", e);
|
| 25 | 27 |
} |
--- src/main/java/com/munjaon/client/model/MunjaonMsg.java
+++ src/main/java/com/munjaon/client/model/MunjaonMsg.java
... | ... | @@ -11,16 +11,18 @@ |
| 11 | 11 |
private String msgId; |
| 12 | 12 |
private String msgType; |
| 13 | 13 |
private String sendStatus; |
| 14 |
+ private String reserveDate; |
|
| 14 | 15 |
private String requestDate; |
| 15 | 16 |
private String recvPhone; |
| 16 | 17 |
private String sendPhone; |
| 17 | 18 |
private String subject; |
| 18 | 19 |
private String message; |
| 19 | 20 |
private String telecom; |
| 21 |
+ private String agentCode; |
|
| 20 | 22 |
private String filename01; |
| 21 | 23 |
private String filename02; |
| 22 | 24 |
private String filename03; |
| 23 | 25 |
private String deliverDate; |
| 24 |
- private String sentDate; |
|
| 26 |
+ private String sendDate; |
|
| 25 | 27 |
private String reportDate; |
| 26 | 28 |
} |
+++ src/main/java/com/munjaon/client/server/config/ServerConfig.java
... | ... | @@ -0,0 +1,13 @@ |
| 1 | +package com.munjaon.client.server.config; | |
| 2 | + | |
| 3 | +public final class ServerConfig { | |
| 4 | + /* 서버 타임아웃 체크 시간 */ | |
| 5 | + public static final int CYCLE_SOCKET_TIMEOUT = 3000; | |
| 6 | + /* 서버 연결후 로그인 만료 시간 */ | |
| 7 | + public static final int LIMIT_BIND_TIMEOUT = 5000; | |
| 8 | + /* Session Check 만료 시간 */ | |
| 9 | + public static final int LIMIT_LINK_CHECK_TIMEOUT = 35000; | |
| 10 | + | |
| 11 | + /* 서버 프로퍼티 reload interval 시간 */ | |
| 12 | + public static final Long INTERVAL_PROPERTY_RELOAD_TIME = 3000L; | |
| 13 | +} |
+++ src/main/java/com/munjaon/client/server/packet/Bind.java
... | ... | @@ -0,0 +1,100 @@ |
| 1 | +package com.munjaon.client.server.packet; | |
| 2 | + | |
| 3 | +import java.nio.ByteBuffer; | |
| 4 | +import java.util.Arrays; | |
| 5 | + | |
| 6 | +public final class Bind { | |
| 7 | + public static final int BIND_BODY_LENGTH = 41; | |
| 8 | + | |
| 9 | + public static final int BIND_ID_LENGTH = 20; | |
| 10 | + public static final int BIND_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 11 | + public static final int BIND_PWD_LENGTH = 20; | |
| 12 | + public static final int BIND_PWD_POSITION = BIND_ID_POSITION + BIND_ID_LENGTH; | |
| 13 | + public static final int BIND_ENCRYPTION_LENGTH = 1; | |
| 14 | + public static final int BIND_ENCRYPTION_POSITION = BIND_PWD_POSITION + BIND_PWD_LENGTH; | |
| 15 | + | |
| 16 | + public static final int BIND_ACK_BODY_LENGTH = 2; | |
| 17 | + public static final int BIND_ACK_RESULT_CODE_LENGTH = 2; | |
| 18 | + public static final int BIND_ACK_RESULT_CODE_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 19 | + | |
| 20 | + public static final String ENCRYPTION = "0"; | |
| 21 | + | |
| 22 | + public static ByteBuffer makeBindBuffer(String id, String pwd) { | |
| 23 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + BIND_BODY_LENGTH); | |
| 24 | + Packet.setDefaultByte(buffer); | |
| 25 | + Header.putHeader(buffer, Header.COMMAND_BIND, BIND_BODY_LENGTH); | |
| 26 | + /* ID */ | |
| 27 | + if (id != null) { | |
| 28 | + buffer.put(BIND_ID_POSITION, id.getBytes()); | |
| 29 | + } | |
| 30 | + /* PWD */ | |
| 31 | + if (pwd != null) { | |
| 32 | + buffer.put(BIND_PWD_POSITION, pwd.getBytes()); | |
| 33 | + } | |
| 34 | + /* ENCRYPTION */ | |
| 35 | + buffer.put(BIND_ENCRYPTION_POSITION, ENCRYPTION.getBytes()); | |
| 36 | +// buffer.limit(buffer.capacity()); | |
| 37 | + | |
| 38 | + return buffer; | |
| 39 | + } | |
| 40 | + | |
| 41 | + public static ByteBuffer makeBindAckBuffer(String resultCode) { | |
| 42 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + BIND_ACK_BODY_LENGTH); | |
| 43 | + Packet.setDefaultByte(buffer); | |
| 44 | + Header.putHeader(buffer, Header.COMMAND_BIND_ACK, BIND_ACK_BODY_LENGTH); | |
| 45 | + /* resultCode */ | |
| 46 | + if (resultCode != null) { | |
| 47 | + buffer.put(BIND_ACK_RESULT_CODE_POSITION, resultCode.getBytes()); | |
| 48 | + } | |
| 49 | + | |
| 50 | + return buffer; | |
| 51 | + } | |
| 52 | + | |
| 53 | + public static String getBindId(final ByteBuffer buffer) { | |
| 54 | + if (buffer == null) { | |
| 55 | + return null; | |
| 56 | + } | |
| 57 | + | |
| 58 | + buffer.position(BIND_ID_POSITION); | |
| 59 | + byte[] destArray = new byte[BIND_ID_LENGTH]; | |
| 60 | + buffer.get(destArray); | |
| 61 | + | |
| 62 | + return Packet.getString(destArray); | |
| 63 | + } | |
| 64 | + | |
| 65 | + public static String getBindPwd(final ByteBuffer buffer) { | |
| 66 | + if (buffer == null) { | |
| 67 | + return null; | |
| 68 | + } | |
| 69 | + | |
| 70 | + buffer.position(BIND_PWD_POSITION); | |
| 71 | + byte[] destArray = new byte[BIND_PWD_LENGTH]; | |
| 72 | + buffer.get(destArray); | |
| 73 | + | |
| 74 | + return Packet.getString(destArray); | |
| 75 | + } | |
| 76 | + | |
| 77 | + public static String getBindEncryption(final ByteBuffer buffer) { | |
| 78 | + if (buffer == null) { | |
| 79 | + return null; | |
| 80 | + } | |
| 81 | + | |
| 82 | + buffer.position(BIND_ENCRYPTION_POSITION); | |
| 83 | + byte[] destArray = new byte[BIND_ENCRYPTION_LENGTH]; | |
| 84 | + buffer.get(destArray); | |
| 85 | + | |
| 86 | + return Packet.getString(destArray); | |
| 87 | + } | |
| 88 | + | |
| 89 | + public static String getBindAckResultCode(final ByteBuffer buffer) { | |
| 90 | + if (buffer == null) { | |
| 91 | + return null; | |
| 92 | + } | |
| 93 | + | |
| 94 | + buffer.position(BIND_ACK_RESULT_CODE_POSITION); | |
| 95 | + byte[] destArray = new byte[BIND_ACK_RESULT_CODE_LENGTH]; | |
| 96 | + buffer.get(destArray); | |
| 97 | + | |
| 98 | + return Packet.getString(destArray); | |
| 99 | + } | |
| 100 | +} |
+++ src/main/java/com/munjaon/client/server/packet/CommonMessage.java
... | ... | @@ -0,0 +1,183 @@ |
| 1 | +package com.munjaon.client.server.packet; | |
| 2 | + | |
| 3 | +import com.munjaon.client.util.CommonUtil; | |
| 4 | + | |
| 5 | +import java.nio.ByteBuffer; | |
| 6 | +import java.util.Arrays; | |
| 7 | + | |
| 8 | +public final class CommonMessage { | |
| 9 | + /* DELIVER */ | |
| 10 | + /* MSG_ID */ | |
| 11 | + public static final int DELIVER_MESSAGE_ID_LENGTH = 20; | |
| 12 | + public static final int DELIVER_MESSAGE_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 13 | + /* SENDER */ | |
| 14 | + public static final int DELIVER_SENDER_LENGTH = 15; | |
| 15 | + public static final int DELIVER_SENDER_POSITION = DELIVER_MESSAGE_ID_POSITION + DELIVER_MESSAGE_ID_LENGTH; | |
| 16 | + /* RECEIVER */ | |
| 17 | + public static final int DELIVER_RECEIVER_LENGTH = 15; | |
| 18 | + public static final int DELIVER_RECEIVER_POSITION = DELIVER_SENDER_POSITION + DELIVER_SENDER_LENGTH; | |
| 19 | + /* RESERVE_TIME */ | |
| 20 | + public static final int DELIVER_RESERVE_TIME_LENGTH = 14; | |
| 21 | + public static final int DELIVER_RESERVE_TIME_POSITION = DELIVER_RECEIVER_POSITION + DELIVER_RECEIVER_LENGTH; | |
| 22 | + /* REQUEST_TIME */ | |
| 23 | + public static final int DELIVER_REQUEST_TIME_LENGTH = 14; | |
| 24 | + public static final int DELIVER_REQUEST_TIME_POSITION = DELIVER_RESERVE_TIME_POSITION + DELIVER_RESERVE_TIME_LENGTH; | |
| 25 | + /* MSG_TYPE */ | |
| 26 | + public static final int DELIVER_MSG_TYPE_LENGTH = 1; | |
| 27 | + public static final int DELIVER_MSG_TYPE_POSITION = DELIVER_REQUEST_TIME_POSITION + DELIVER_REQUEST_TIME_LENGTH; | |
| 28 | + | |
| 29 | + /* DELIVER_ACK */ | |
| 30 | + /* MSG_ID */ | |
| 31 | + public static final int DELIVER_ACK_MESSAGE_ID_LENGTH = 20; | |
| 32 | + public static final int DELIVER_ACK_MESSAGE_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 33 | + /* RESULT */ | |
| 34 | + public static final int DELIVER_ACK_RESULT_LENGTH = 1; | |
| 35 | + public static final int DELIVER_ACK_RESULT_POSITION = DELIVER_ACK_MESSAGE_ID_POSITION + DELIVER_ACK_MESSAGE_ID_LENGTH; | |
| 36 | + | |
| 37 | + public static void putMessageIdForDeliver(ByteBuffer buffer, String messageId) { | |
| 38 | + if (buffer == null || messageId == null) { | |
| 39 | + return; | |
| 40 | + } | |
| 41 | + buffer.put(DELIVER_MESSAGE_ID_POSITION, messageId.getBytes()); | |
| 42 | + } | |
| 43 | + public static String getMessageIdForDeliver(ByteBuffer buffer) { | |
| 44 | + if (buffer == null) { | |
| 45 | + return null; | |
| 46 | + } | |
| 47 | + | |
| 48 | + buffer.position(DELIVER_MESSAGE_ID_POSITION); | |
| 49 | + byte[] destArray = new byte[DELIVER_MESSAGE_ID_LENGTH]; | |
| 50 | + buffer.get(destArray); | |
| 51 | + | |
| 52 | + return Packet.getString(destArray); | |
| 53 | + } | |
| 54 | + | |
| 55 | + public static void putSenderForDeliver(ByteBuffer buffer, String sender) { | |
| 56 | + if (buffer == null || sender == null) { | |
| 57 | + return; | |
| 58 | + } | |
| 59 | + sender = CommonUtil.cutString(CommonUtil.doNumber(sender), DELIVER_SENDER_LENGTH); | |
| 60 | + buffer.put(DELIVER_SENDER_POSITION, sender.getBytes()); | |
| 61 | + } | |
| 62 | + public static String getSenderForDeliver(ByteBuffer buffer) { | |
| 63 | + if (buffer == null) { | |
| 64 | + return null; | |
| 65 | + } | |
| 66 | + | |
| 67 | + buffer.position(DELIVER_SENDER_POSITION); | |
| 68 | + byte[] destArray = new byte[DELIVER_SENDER_LENGTH]; | |
| 69 | + buffer.get(destArray); | |
| 70 | + | |
| 71 | + return Packet.getString(destArray); | |
| 72 | + } | |
| 73 | + | |
| 74 | + public static void putReceiverForDeliver(ByteBuffer buffer, String receiver) { | |
| 75 | + if (buffer == null || receiver == null) { | |
| 76 | + return; | |
| 77 | + } | |
| 78 | + receiver = CommonUtil.cutString(CommonUtil.doNumber(receiver), DELIVER_RECEIVER_LENGTH); | |
| 79 | + buffer.put(DELIVER_RECEIVER_POSITION, receiver.getBytes()); | |
| 80 | + } | |
| 81 | + public static String getReceiverForDeliver(ByteBuffer buffer) { | |
| 82 | + if (buffer == null) { | |
| 83 | + return null; | |
| 84 | + } | |
| 85 | + | |
| 86 | + buffer.position(DELIVER_RECEIVER_POSITION); | |
| 87 | + byte[] destArray = new byte[DELIVER_RECEIVER_LENGTH]; | |
| 88 | + buffer.get(destArray); | |
| 89 | + | |
| 90 | + return Packet.getString(destArray); | |
| 91 | + } | |
| 92 | + | |
| 93 | + public static void putReserveTimeForDeliver(ByteBuffer buffer, String reserveTime) { | |
| 94 | + if (buffer == null || reserveTime == null) { | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + buffer.put(DELIVER_RESERVE_TIME_POSITION, reserveTime.getBytes()); | |
| 98 | + } | |
| 99 | + public static String getReserveTimeForDeliver(ByteBuffer buffer) { | |
| 100 | + if (buffer == null) { | |
| 101 | + return null; | |
| 102 | + } | |
| 103 | + | |
| 104 | + buffer.position(DELIVER_RESERVE_TIME_POSITION); | |
| 105 | + byte[] destArray = new byte[DELIVER_RESERVE_TIME_LENGTH]; | |
| 106 | + buffer.get(destArray); | |
| 107 | + | |
| 108 | + return Packet.getString(destArray); | |
| 109 | + } | |
| 110 | + | |
| 111 | + public static void putRequestTimeForDeliver(ByteBuffer buffer, String requestTime) { | |
| 112 | + if (buffer == null || requestTime == null) { | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + buffer.put(DELIVER_REQUEST_TIME_POSITION, requestTime.getBytes()); | |
| 116 | + } | |
| 117 | + public static String getRequestTimeForDeliver(ByteBuffer buffer) { | |
| 118 | + if (buffer == null) { | |
| 119 | + return null; | |
| 120 | + } | |
| 121 | + | |
| 122 | + buffer.position(DELIVER_REQUEST_TIME_POSITION); | |
| 123 | + byte[] destArray = new byte[DELIVER_REQUEST_TIME_LENGTH]; | |
| 124 | + buffer.get(destArray); | |
| 125 | + | |
| 126 | + return Packet.getString(destArray); | |
| 127 | + } | |
| 128 | + | |
| 129 | + public static void putMsgTypeForDeliver(ByteBuffer buffer, String msgType) { | |
| 130 | + if (buffer == null || msgType == null) { | |
| 131 | + return; | |
| 132 | + } | |
| 133 | + buffer.put(DELIVER_MSG_TYPE_POSITION, msgType.getBytes()); | |
| 134 | + } | |
| 135 | + public static String getMsgTypeForDeliver(ByteBuffer buffer) { | |
| 136 | + if (buffer == null) { | |
| 137 | + return null; | |
| 138 | + } | |
| 139 | + | |
| 140 | + buffer.position(DELIVER_MSG_TYPE_POSITION); | |
| 141 | + byte[] destArray = new byte[DELIVER_MSG_TYPE_LENGTH]; | |
| 142 | + buffer.get(destArray); | |
| 143 | + | |
| 144 | + return Packet.getString(destArray); | |
| 145 | + } | |
| 146 | + | |
| 147 | + | |
| 148 | + public static void putMessageIdForDeliverAck(ByteBuffer buffer, String messageId) { | |
| 149 | + if (buffer == null || messageId == null) { | |
| 150 | + return; | |
| 151 | + } | |
| 152 | + buffer.put(DELIVER_ACK_MESSAGE_ID_POSITION, messageId.getBytes()); | |
| 153 | + } | |
| 154 | + public static String getMessageIdForDeliverAck(ByteBuffer buffer) { | |
| 155 | + if (buffer == null) { | |
| 156 | + return null; | |
| 157 | + } | |
| 158 | + | |
| 159 | + buffer.position(DELIVER_ACK_MESSAGE_ID_POSITION); | |
| 160 | + byte[] destArray = new byte[DELIVER_ACK_MESSAGE_ID_LENGTH]; | |
| 161 | + buffer.get(destArray); | |
| 162 | + | |
| 163 | + return Packet.getString(destArray); | |
| 164 | + } | |
| 165 | + | |
| 166 | + public static void putResultForDeliverAck(ByteBuffer buffer, String result) { | |
| 167 | + if (buffer == null || result == null) { | |
| 168 | + return; | |
| 169 | + } | |
| 170 | + buffer.put(DELIVER_ACK_RESULT_POSITION, result.getBytes()); | |
| 171 | + } | |
| 172 | + public static String getResultForDeliverAck(ByteBuffer buffer) { | |
| 173 | + if (buffer == null) { | |
| 174 | + return null; | |
| 175 | + } | |
| 176 | + | |
| 177 | + buffer.position(DELIVER_ACK_RESULT_POSITION); | |
| 178 | + byte[] destArray = new byte[DELIVER_ACK_RESULT_LENGTH]; | |
| 179 | + buffer.get(destArray); | |
| 180 | + | |
| 181 | + return Packet.getString(destArray); | |
| 182 | + } | |
| 183 | +} |
+++ src/main/java/com/munjaon/client/server/packet/Header.java
... | ... | @@ -0,0 +1,110 @@ |
| 1 | +package com.munjaon.client.server.packet; | |
| 2 | + | |
| 3 | +import com.munjaon.client.util.ByteUtil; | |
| 4 | + | |
| 5 | +import java.nio.ByteBuffer; | |
| 6 | +import java.util.Arrays; | |
| 7 | + | |
| 8 | +public final class Header { | |
| 9 | + public static final int HEADER_LENGTH = 10; | |
| 10 | + | |
| 11 | + public static final String VERSION = "ITN10"; | |
| 12 | + public static final int VERSION_LENGTH = 5; | |
| 13 | + public static final int VERSION_POSITION = 0; | |
| 14 | + | |
| 15 | + public static final int COMMAND_LENGTH = 1; | |
| 16 | + public static final int COMMAND_POSITION = VERSION_POSITION + VERSION_LENGTH; | |
| 17 | + | |
| 18 | + public static final String COMMAND_BIND = "1"; | |
| 19 | + public static final String COMMAND_BIND_ACK = "2"; | |
| 20 | + public static final String COMMAND_DELIVER = "3"; | |
| 21 | + public static final String COMMAND_DELIVER_ACK = "4"; | |
| 22 | + public static final String COMMAND_REPORT = "5"; | |
| 23 | + public static final String COMMAND_REPORT_ACK = "6"; | |
| 24 | + public static final String COMMAND_LINK_CHECK = "7"; | |
| 25 | + public static final String COMMAND_LINK_CHECK_ACK = "8"; | |
| 26 | + | |
| 27 | + public static final int BODY_LENGTH = 4; | |
| 28 | + public static final int BODY_POSITION = COMMAND_POSITION + COMMAND_LENGTH; | |
| 29 | + | |
| 30 | + public static final int BODY_BIND_LENGTH = 41; | |
| 31 | + public static final int BODY_BIND_ACK_LENGTH = 2; | |
| 32 | + public static final int BODY_LINK_CHECK_LENGTH = 3; | |
| 33 | + public static final int BODY_LINK_CHECK_ACK_LENGTH = 3; | |
| 34 | + public static final int BODY_DELIVER_SMS_LENGTH = 239; | |
| 35 | + public static final int BODY_DELIVER_SMS_ACK_LENGTH = 21; | |
| 36 | + public static final int BODY_DELIVER_LMS_LENGTH = 2091; | |
| 37 | + public static final int BODY_DELIVER_LMS_ACK_LENGTH = 21; | |
| 38 | + public static final int BODY_DELIVER_MMS_LENGTH = 2091; | |
| 39 | + public static final int BODY_DELIVER_MMS_ACK_LENGTH = 21; | |
| 40 | + public static final int BODY_REPORT_LENGTH = 58; | |
| 41 | + public static final int BODY_REPORT_ACK_LENGTH = 1; | |
| 42 | + | |
| 43 | + public static void putVersion(final ByteBuffer buffer) { | |
| 44 | + if (buffer == null) { | |
| 45 | + return; | |
| 46 | + } | |
| 47 | + buffer.put(VERSION_POSITION, VERSION.getBytes()); | |
| 48 | + } | |
| 49 | + public static String getVersion(final ByteBuffer buffer) { | |
| 50 | + if (buffer == null) { | |
| 51 | + return null; | |
| 52 | + } | |
| 53 | + | |
| 54 | + buffer.position(VERSION_POSITION); | |
| 55 | + byte[] destArray = new byte[VERSION_LENGTH]; | |
| 56 | + buffer.get(destArray); | |
| 57 | + | |
| 58 | + return Packet.getString(destArray); | |
| 59 | + } | |
| 60 | + | |
| 61 | + public static void putCommand(final ByteBuffer buffer, String command) { | |
| 62 | + if (buffer == null) { | |
| 63 | + return; | |
| 64 | + } | |
| 65 | + buffer.put(COMMAND_POSITION, command.getBytes()); | |
| 66 | + } | |
| 67 | + public static String getCommand(final ByteBuffer buffer) { | |
| 68 | + if (buffer == null) { | |
| 69 | + return null; | |
| 70 | + } | |
| 71 | + | |
| 72 | + buffer.position(COMMAND_POSITION); | |
| 73 | + byte[] destArray = new byte[COMMAND_LENGTH]; | |
| 74 | + buffer.get(destArray); | |
| 75 | + | |
| 76 | + return Packet.getString(destArray); | |
| 77 | + } | |
| 78 | + | |
| 79 | + public static void putBodyLength(final ByteBuffer buffer, int bodyLength) { | |
| 80 | + putBodyLength(buffer, Integer.toString(bodyLength)); | |
| 81 | + } | |
| 82 | + public static String getBodyLength(final ByteBuffer buffer) { | |
| 83 | + if (buffer == null) { | |
| 84 | + return null; | |
| 85 | + } | |
| 86 | + | |
| 87 | + buffer.position(BODY_POSITION); | |
| 88 | + byte[] destArray = new byte[BODY_LENGTH]; | |
| 89 | + buffer.get(destArray); | |
| 90 | + | |
| 91 | + return Packet.getString(destArray); | |
| 92 | + } | |
| 93 | + | |
| 94 | + public static void putBodyLength(final ByteBuffer buffer, String bodyLength) { | |
| 95 | + if (buffer == null) { | |
| 96 | + return; | |
| 97 | + } | |
| 98 | + System.out.println(ByteUtil.byteToHex(bodyLength.getBytes(), true)); | |
| 99 | + | |
| 100 | + buffer.put(BODY_POSITION, bodyLength.getBytes()); | |
| 101 | + } | |
| 102 | + public static void putHeader(final ByteBuffer buffer, String command, int bodyLength) { | |
| 103 | + putHeader(buffer, command, Integer.toString(bodyLength)); | |
| 104 | + } | |
| 105 | + public static void putHeader(final ByteBuffer buffer, String command, String bodyLength) { | |
| 106 | + putVersion(buffer); | |
| 107 | + putCommand(buffer, command); | |
| 108 | + putBodyLength(buffer, bodyLength); | |
| 109 | + } | |
| 110 | +} |
+++ src/main/java/com/munjaon/client/server/packet/LinkCheck.java
... | ... | @@ -0,0 +1,31 @@ |
| 1 | +package com.munjaon.client.server.packet; | |
| 2 | + | |
| 3 | +import java.nio.ByteBuffer; | |
| 4 | + | |
| 5 | +public final class LinkCheck { | |
| 6 | + public static final int LINK_CHECK_BODY_LENGTH = 3; | |
| 7 | + public static final int LINK_CHECK_BODY_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 8 | + public static final int LINK_CHECK_ACK_BODY_LENGTH = 3; | |
| 9 | + public static final int LINK_CHECK_ACK_BODY_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 10 | + | |
| 11 | + public static String LINK_CHECK_VALUE = "100"; | |
| 12 | + public static String LINK_CHECK_ACK_VALUE = "100"; | |
| 13 | + | |
| 14 | + public static ByteBuffer makeLinkCheckBuffer() { | |
| 15 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + LINK_CHECK_BODY_LENGTH); | |
| 16 | + Packet.setDefaultByte(buffer); | |
| 17 | + Header.putHeader(buffer, Header.COMMAND_LINK_CHECK, LINK_CHECK_BODY_LENGTH); | |
| 18 | + buffer.put(LINK_CHECK_BODY_POSITION, LINK_CHECK_VALUE.getBytes()); | |
| 19 | + | |
| 20 | + return buffer; | |
| 21 | + } | |
| 22 | + | |
| 23 | + public static ByteBuffer makeLinkCheckAckBuffer() { | |
| 24 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + LINK_CHECK_ACK_BODY_LENGTH); | |
| 25 | + Packet.setDefaultByte(buffer); | |
| 26 | + Header.putHeader(buffer, Header.COMMAND_LINK_CHECK, LINK_CHECK_ACK_BODY_LENGTH); | |
| 27 | + buffer.put(LINK_CHECK_ACK_BODY_POSITION, LINK_CHECK_ACK_VALUE.getBytes()); | |
| 28 | + | |
| 29 | + return buffer; | |
| 30 | + } | |
| 31 | +} |
+++ src/main/java/com/munjaon/client/server/packet/Packet.java
... | ... | @@ -0,0 +1,46 @@ |
| 1 | +package com.munjaon.client.server.packet; | |
| 2 | + | |
| 3 | +import java.nio.ByteBuffer; | |
| 4 | + | |
| 5 | +public final class Packet { | |
| 6 | + public static final byte SET_DEFAULT_BYTE = (byte) 0x00; | |
| 7 | + public static final long LINK_CHECK_CYCLE = 3000L; | |
| 8 | +// public static final long LINK_CHECK_CYCLE = 30000L; | |
| 9 | + | |
| 10 | + public static void setDefaultByte(ByteBuffer buffer) { | |
| 11 | + if (buffer == null) { | |
| 12 | + return; | |
| 13 | + } | |
| 14 | +// buffer.clear(); | |
| 15 | + for (int i = 0; i < buffer.capacity(); i++) { | |
| 16 | + buffer.put(i, SET_DEFAULT_BYTE); | |
| 17 | + } | |
| 18 | +// buffer.position(0); | |
| 19 | + } | |
| 20 | + | |
| 21 | + public static String getString(byte[] srcArray) { | |
| 22 | + if (srcArray == null) { | |
| 23 | + return null; | |
| 24 | + } | |
| 25 | + int size = 0; | |
| 26 | + for (int i = 0, len = srcArray.length; i < len; i++) { | |
| 27 | + if (srcArray[i] == SET_DEFAULT_BYTE) { | |
| 28 | + continue; | |
| 29 | + } | |
| 30 | + size++; | |
| 31 | + } | |
| 32 | + byte[] destArray = null; | |
| 33 | + if (size > 0) { | |
| 34 | + destArray = new byte[size]; | |
| 35 | + int index = 0; | |
| 36 | + for (int i = 0, len = srcArray.length; i < len; i++) { | |
| 37 | + if (srcArray[i] == SET_DEFAULT_BYTE) { | |
| 38 | + continue; | |
| 39 | + } | |
| 40 | + destArray[index++] = srcArray[i]; | |
| 41 | + } | |
| 42 | + } | |
| 43 | + | |
| 44 | + return destArray == null ? null : new String(destArray); | |
| 45 | + } | |
| 46 | +} |
+++ src/main/java/com/munjaon/client/server/packet/Report.java
... | ... | @@ -0,0 +1,142 @@ |
| 1 | +package com.munjaon.client.server.packet; | |
| 2 | + | |
| 3 | +import com.munjaon.client.model.MunjaonMsg; | |
| 4 | + | |
| 5 | +import java.nio.ByteBuffer; | |
| 6 | + | |
| 7 | +public final class Report { | |
| 8 | + public static final int REPORT_BODY_LENGTH = 44; | |
| 9 | + | |
| 10 | + public static final int REPORT_MSG_ID_LENGTH = 20; | |
| 11 | + public static final int REPORT_MSG_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 12 | + public static final int REPORT_AGENT_CODE_LENGTH = 2; | |
| 13 | + public static final int REPORT_AGENT_CODE_POSITION = REPORT_MSG_ID_POSITION + REPORT_MSG_ID_LENGTH; | |
| 14 | + public static final int REPORT_SEND_TIME_LENGTH = 14; | |
| 15 | + public static final int REPORT_SEND_TIME_POSITION = REPORT_AGENT_CODE_POSITION + REPORT_AGENT_CODE_LENGTH; | |
| 16 | + public static final int REPORT_TELECOM_LENGTH = 3; | |
| 17 | + public static final int REPORT_TELECOM_POSITION = REPORT_SEND_TIME_POSITION + REPORT_SEND_TIME_LENGTH; | |
| 18 | + public static final int REPORT_RESULT_LENGTH = 5; | |
| 19 | + public static final int REPORT_RESULT_POSITION = REPORT_TELECOM_POSITION + REPORT_TELECOM_LENGTH; | |
| 20 | + | |
| 21 | + public static final int REPORT_ACK_BODY_LENGTH = 1; | |
| 22 | + public static final int REPORT_ACK_RESULT_CODE_LENGTH = 1; | |
| 23 | + public static final int REPORT_ACK_RESULT_CODE_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH; | |
| 24 | + | |
| 25 | + public static ByteBuffer makeReport(MunjaonMsg msgData) { | |
| 26 | + ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + REPORT_BODY_LENGTH); | |
| 27 | + Packet.setDefaultByte(buffer); | |
| 28 | + Header.putHeader(buffer, Header.COMMAND_REPORT, REPORT_BODY_LENGTH); | |
| 29 | + /* 1. MSG_ID */ | |
| 30 | + makeReportForMsgId(buffer, msgData.getMsgId()); | |
| 31 | + /* 2. AGENT_CODE */ | |
| 32 | + makeReportForAgentCode(buffer, msgData.getAgentCode()); | |
| 33 | + /* 3. SEND TIME */ | |
| 34 | + makeReportForSendTime(buffer, msgData.getSendDate()); | |
| 35 | + /* 4. TELECOM */ | |
| 36 | + makeReportForTelecom(buffer, msgData.getTelecom()); | |
| 37 | + /* 5. RESULT */ | |
| 38 | + makeReportForResult(buffer, msgData.getSendStatus()); | |
| 39 | + | |
| 40 | + return buffer; | |
| 41 | + } | |
| 42 | + | |
| 43 | + public static void makeReportForMsgId(final ByteBuffer buffer, final String msgId) { | |
| 44 | + if (buffer == null || msgId == null) { | |
| 45 | + return; | |
| 46 | + } | |
| 47 | + | |
| 48 | + buffer.put(REPORT_MSG_ID_POSITION, msgId.getBytes()); | |
| 49 | + } | |
| 50 | + | |
| 51 | + public static String getReportForMsgId(final ByteBuffer buffer) { | |
| 52 | + if (buffer == null) { | |
| 53 | + return null; | |
| 54 | + } | |
| 55 | + | |
| 56 | + buffer.position(REPORT_MSG_ID_POSITION); | |
| 57 | + byte[] destArray = new byte[REPORT_MSG_ID_LENGTH]; | |
| 58 | + buffer.get(destArray); | |
| 59 | + | |
| 60 | + return Packet.getString(destArray); | |
| 61 | + } | |
| 62 | + | |
| 63 | + public static void makeReportForAgentCode(final ByteBuffer buffer, final String agentCode) { | |
| 64 | + if (buffer == null || agentCode == null) { | |
| 65 | + return; | |
| 66 | + } | |
| 67 | + | |
| 68 | + buffer.put(REPORT_AGENT_CODE_POSITION, agentCode.getBytes()); | |
| 69 | + } | |
| 70 | + | |
| 71 | + public static String getReportForAgentCode(final ByteBuffer buffer) { | |
| 72 | + if (buffer == null) { | |
| 73 | + return null; | |
| 74 | + } | |
| 75 | + | |
| 76 | + buffer.position(REPORT_AGENT_CODE_POSITION); | |
| 77 | + byte[] destArray = new byte[REPORT_AGENT_CODE_LENGTH]; | |
| 78 | + buffer.get(destArray); | |
| 79 | + | |
| 80 | + return Packet.getString(destArray); | |
| 81 | + } | |
| 82 | + | |
| 83 | + public static void makeReportForSendTime(final ByteBuffer buffer, final String sendTime) { | |
| 84 | + if (buffer == null || sendTime == null) { | |
| 85 | + return; | |
| 86 | + } | |
| 87 | + | |
| 88 | + buffer.put(REPORT_SEND_TIME_POSITION, sendTime.getBytes()); | |
| 89 | + } | |
| 90 | + | |
| 91 | + public static String getReportForSendTime(final ByteBuffer buffer) { | |
| 92 | + if (buffer == null) { | |
| 93 | + return null; | |
| 94 | + } | |
| 95 | + | |
| 96 | + buffer.position(REPORT_SEND_TIME_POSITION); | |
| 97 | + byte[] destArray = new byte[REPORT_SEND_TIME_LENGTH]; | |
| 98 | + buffer.get(destArray); | |
| 99 | + | |
| 100 | + return Packet.getString(destArray); | |
| 101 | + } | |
| 102 | + | |
| 103 | + public static void makeReportForTelecom(final ByteBuffer buffer, final String telecom) { | |
| 104 | + if (buffer == null || telecom == null) { | |
| 105 | + return; | |
| 106 | + } | |
| 107 | + | |
| 108 | + buffer.put(REPORT_TELECOM_POSITION, telecom.getBytes()); | |
| 109 | + } | |
| 110 | + | |
| 111 | + public static String getReportForTelecom(final ByteBuffer buffer) { | |
| 112 | + if (buffer == null) { | |
| 113 | + return null; | |
| 114 | + } | |
| 115 | + | |
| 116 | + buffer.position(REPORT_TELECOM_POSITION); | |
| 117 | + byte[] destArray = new byte[REPORT_TELECOM_LENGTH]; | |
| 118 | + buffer.get(destArray); | |
| 119 | + | |
| 120 | + return Packet.getString(destArray); | |
| 121 | + } | |
| 122 | + | |
| 123 | + public static void makeReportForResult(final ByteBuffer buffer, final String result) { | |
| 124 | + if (buffer == null || result == null) { | |
| 125 | + return; | |
| 126 | + } | |
| 127 | + | |
| 128 | + buffer.put(REPORT_RESULT_POSITION, result.getBytes()); | |
| 129 | + } | |
| 130 | + | |
| 131 | + public static String getReportForResult(final ByteBuffer buffer) { | |
| 132 | + if (buffer == null) { | |
| 133 | + return null; | |
| 134 | + } | |
| 135 | + | |
| 136 | + buffer.position(REPORT_RESULT_POSITION); | |
| 137 | + byte[] destArray = new byte[REPORT_RESULT_LENGTH]; | |
| 138 | + buffer.get(destArray); | |
| 139 | + | |
| 140 | + return Packet.getString(destArray); | |
| 141 | + } | |
| 142 | +} |
+++ src/main/java/com/munjaon/client/server/packet/SmsMessage.java
... | ... | @@ -0,0 +1,55 @@ |
| 1 | +package com.munjaon.client.server.packet; | |
| 2 | + | |
| 3 | +import com.munjaon.client.model.MunjaonMsg; | |
| 4 | +import com.munjaon.client.util.CommonUtil; | |
| 5 | + | |
| 6 | +import java.nio.ByteBuffer; | |
| 7 | +import java.util.Arrays; | |
| 8 | + | |
| 9 | +public final class SmsMessage { | |
| 10 | + public static final int DELIVER_SMS_BODY_LENGTH = 239; | |
| 11 | + public static final int DELIVER_SMS_ACK_BODY_LENGTH = 21; | |
| 12 | + | |
| 13 | + /* DELIVER */ | |
| 14 | + /* MESSAGE */ | |
| 15 | + public static final int DELIVER_MESSAGE_LENGTH = 160; | |
| 16 | + public static final int DELIVER_MESSAGE_POSITION = CommonMessage.DELIVER_MSG_TYPE_POSITION + CommonMessage.DELIVER_MSG_TYPE_LENGTH; | |
| 17 | + | |
| 18 | + public static void putMessageForDeliver(ByteBuffer buffer, String message) { | |
| 19 | + if (buffer == null || message == null) { | |
| 20 | + return; | |
| 21 | + } | |
| 22 | + message = CommonUtil.cutString(message, DELIVER_MESSAGE_LENGTH); | |
| 23 | + buffer.put(DELIVER_MESSAGE_POSITION, message.getBytes()); | |
| 24 | + } | |
| 25 | + public static String getMessageForDeliver(ByteBuffer buffer) { | |
| 26 | + if (buffer == null) { | |
| 27 | + return null; | |
| 28 | + } | |
| 29 | + | |
| 30 | + buffer.position(DELIVER_MESSAGE_POSITION); | |
| 31 | + byte[] destArray = new byte[DELIVER_MESSAGE_LENGTH]; | |
| 32 | + buffer.get(destArray); | |
| 33 | + | |
| 34 | + return Packet.getString(destArray); | |
| 35 | + } | |
| 36 | + public static void makeDataForDeliver(ByteBuffer buffer, MunjaonMsg data) { | |
| 37 | + if (buffer == null || data == null) { | |
| 38 | + return; | |
| 39 | + } | |
| 40 | + /* MSG_ID */ | |
| 41 | + CommonMessage.putMessageIdForDeliver(buffer, data.getMsgId()); | |
| 42 | + /* SENDER */ | |
| 43 | + CommonMessage.putSenderForDeliver(buffer, data.getSendPhone()); | |
| 44 | + /* RECEIVER */ | |
| 45 | + CommonMessage.putReceiverForDeliver(buffer, data.getRecvPhone()); | |
| 46 | + /* RESERVE_TIME */ | |
| 47 | + CommonMessage.putReserveTimeForDeliver(buffer, data.getReserveDate()); | |
| 48 | + /* REQUEST_TIME */ | |
| 49 | + CommonMessage.putRequestTimeForDeliver(buffer, data.getRequestDate()); | |
| 50 | + /* MSG_TYPE */ | |
| 51 | + CommonMessage.putMsgTypeForDeliver(buffer, data.getMsgType()); | |
| 52 | + /* MSG */ | |
| 53 | + putMessageForDeliver(buffer, data.getMessage()); | |
| 54 | + } | |
| 55 | +} |
+++ src/main/java/com/munjaon/client/server/service/CollectClientService.java
... | ... | @@ -0,0 +1,186 @@ |
| 1 | +package com.munjaon.client.server.service; | |
| 2 | + | |
| 3 | +import com.munjaon.client.model.MunjaonMsg; | |
| 4 | +import com.munjaon.client.server.packet.*; | |
| 5 | +import com.munjaon.client.service.DatabaseTypeWorker; | |
| 6 | +import org.json.simple.JSONObject; | |
| 7 | + | |
| 8 | +import java.io.IOException; | |
| 9 | +import java.net.InetSocketAddress; | |
| 10 | +import java.nio.ByteBuffer; | |
| 11 | +import java.nio.channels.SocketChannel; | |
| 12 | +import java.util.List; | |
| 13 | + | |
| 14 | +public class CollectClientService extends Service { | |
| 15 | + private final String serviceType; | |
| 16 | + private SocketChannel socketChannel; | |
| 17 | + private long lastPacketSendTime = 0; | |
| 18 | + private String address; | |
| 19 | + private int port; | |
| 20 | + private String id; | |
| 21 | + private String pwd; | |
| 22 | + | |
| 23 | + public CollectClientService(String serviceName, String serviceType) { | |
| 24 | + super(serviceName); | |
| 25 | + this.serviceType = serviceType; | |
| 26 | + } | |
| 27 | + | |
| 28 | + @Override | |
| 29 | + public void checkReady() { | |
| 30 | + DatabaseTypeWorker worker = DatabaseTypeWorker.find(System.getProperty("DBMS")); | |
| 31 | + if (worker == null) { | |
| 32 | + return; | |
| 33 | + } | |
| 34 | + this.IS_READY_YN = worker.checkTableExists(); | |
| 35 | + } | |
| 36 | + | |
| 37 | + @Override | |
| 38 | + public void initResources() { | |
| 39 | + this.address = getProp("HOST"); | |
| 40 | + this.port = Integer.parseInt(getProp("DELIVER.PORT")); | |
| 41 | + this.id = getProp("ID"); | |
| 42 | + this.pwd = getProp("PASSWORD"); | |
| 43 | + saveSystemLog("Try Connect to " + this.address + ":" + this.port); | |
| 44 | + try { | |
| 45 | + socketChannel = SocketChannel.open(new InetSocketAddress(this.address, this.port)); | |
| 46 | + socketChannel.configureBlocking(false); | |
| 47 | + } catch (IOException e) { | |
| 48 | + saveSystemLog("Connect Fail " + this.address + ":" + this.port); | |
| 49 | + throw new RuntimeException(e); | |
| 50 | + } | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Override | |
| 54 | + public void releaseResources() { | |
| 55 | + if (socketChannel != null) { | |
| 56 | + try { | |
| 57 | + socketChannel.close(); | |
| 58 | + } catch (IOException e) { | |
| 59 | + throw new RuntimeException(e); | |
| 60 | + } | |
| 61 | + } | |
| 62 | + } | |
| 63 | + | |
| 64 | + @Override | |
| 65 | + public void doService() { | |
| 66 | + bind(); | |
| 67 | + while (isRun()) { | |
| 68 | + try { | |
| 69 | + messageService(); | |
| 70 | + linkCheckService(); | |
| 71 | + } catch (Exception e) { | |
| 72 | + throw new RuntimeException(e); | |
| 73 | + } | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + private void bind() { | |
| 78 | + ByteBuffer sendBuffer = Bind.makeBindBuffer(id, pwd); | |
| 79 | + ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + Bind.BIND_ACK_BODY_LENGTH); | |
| 80 | + try { | |
| 81 | + saveSystemLog("sendBuffer " + sendBuffer.position() + ":" + sendBuffer.limit()); | |
| 82 | + saveSystemLog("Bind Try Connect to " + this.address + ":" + this.port); | |
| 83 | + socketChannel.write(sendBuffer); | |
| 84 | + saveSystemLog("Bind Read to " + this.address + ":" + this.port); | |
| 85 | + while (true) { | |
| 86 | + int recvCount = socketChannel.read(recvBuffer); | |
| 87 | + if (recvCount == -1) { | |
| 88 | + throw new RuntimeException("BIND ERROR"); | |
| 89 | + } else if (recvCount > 0) { | |
| 90 | + String resultCode = Bind.getBindAckResultCode(recvBuffer); | |
| 91 | + saveSystemLog("resultCode " + resultCode); | |
| 92 | + if ("00".equals(resultCode)) { | |
| 93 | + lastPacketSendTime = System.currentTimeMillis(); | |
| 94 | + saveSystemLog("Bind OK"); | |
| 95 | + break; | |
| 96 | + } else { | |
| 97 | + saveSystemLog("Bind FAIL"); | |
| 98 | + throw new RuntimeException("Bind FAIL"); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + } catch (Exception e) { | |
| 104 | + throw new RuntimeException(e); | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | + private void messageService() { | |
| 109 | + DatabaseTypeWorker worker = DatabaseTypeWorker.find(System.getProperty("DBMS")); | |
| 110 | + if (worker == null) { | |
| 111 | + return; | |
| 112 | + } | |
| 113 | + List<MunjaonMsg> list = worker.selectToDeliver("S"); | |
| 114 | + if (list == null || list.isEmpty()) { | |
| 115 | + try { | |
| 116 | + Thread.sleep(1000); | |
| 117 | + } catch (InterruptedException e) { | |
| 118 | + throw new RuntimeException(e); | |
| 119 | + } | |
| 120 | + | |
| 121 | + return; | |
| 122 | + } | |
| 123 | + for (MunjaonMsg data : list) { | |
| 124 | + ByteBuffer sendBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + SmsMessage.DELIVER_SMS_BODY_LENGTH); | |
| 125 | + ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + SmsMessage.DELIVER_SMS_ACK_BODY_LENGTH); | |
| 126 | +// sendBuffer.flip(); | |
| 127 | + try { | |
| 128 | + Header.putHeader(sendBuffer, Header.COMMAND_DELIVER, SmsMessage.DELIVER_SMS_BODY_LENGTH); | |
| 129 | + SmsMessage.makeDataForDeliver(sendBuffer, data); | |
| 130 | +// CommonMessage.putMessageIdForDeliver(sendBuffer, data.getMsgId()); | |
| 131 | +// CommonMessage.putSenderForDeliver(sendBuffer, data.getSendPhone()); | |
| 132 | +// | |
| 133 | +// CommonMessage.putReceiverForDeliver(sendBuffer, data.getRecvPhone()); | |
| 134 | +// CommonMessage.putReserveTimeForDeliver(sendBuffer, data.getReserveDate()); | |
| 135 | +// CommonMessage.putRequestTimeForDeliver(sendBuffer, data.getRequestDate()); | |
| 136 | +// CommonMessage.putMsgTypeForDeliver(sendBuffer, data.getMsgType()); | |
| 137 | +// CommonMessage.putSenderForDeliver(sendBuffer, data.getSendPhone()); | |
| 138 | + saveSystemLog("Deliver Send"); | |
| 139 | + socketChannel.write(sendBuffer); | |
| 140 | + while (true) { | |
| 141 | + int recvCount = socketChannel.read(recvBuffer); | |
| 142 | + if (recvCount == -1) { | |
| 143 | + throw new RuntimeException("DELIVER ERROR"); | |
| 144 | + } else if (recvCount > 0) { | |
| 145 | + worker.updateToDeliver(data.getMsgId()); | |
| 146 | + saveSystemLog("Deliver OK"); | |
| 147 | + lastPacketSendTime = System.currentTimeMillis(); | |
| 148 | + break; | |
| 149 | + } | |
| 150 | + } | |
| 151 | + } catch (IOException e) { | |
| 152 | + throw new RuntimeException(e); | |
| 153 | + } | |
| 154 | + } | |
| 155 | + } | |
| 156 | + | |
| 157 | + private void linkCheckService() { | |
| 158 | + if (System.currentTimeMillis() - lastPacketSendTime < Packet.LINK_CHECK_CYCLE) { | |
| 159 | + saveSystemLog("LinkCheck Is Not"); | |
| 160 | + return; | |
| 161 | + } | |
| 162 | + | |
| 163 | + ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + LinkCheck.LINK_CHECK_ACK_BODY_LENGTH); | |
| 164 | + try { | |
| 165 | + saveSystemLog("LinkCheck Send"); | |
| 166 | + socketChannel.write(LinkCheck.makeLinkCheckBuffer()); | |
| 167 | + while (true) { | |
| 168 | + int recvCount = socketChannel.read(recvBuffer); | |
| 169 | + if (recvCount == -1) { | |
| 170 | + throw new RuntimeException("LINK_CHECK ERROR"); | |
| 171 | + } else if (recvCount > 0) { | |
| 172 | + saveSystemLog("LinkCheck OK"); | |
| 173 | + lastPacketSendTime = System.currentTimeMillis(); | |
| 174 | + break; | |
| 175 | + } | |
| 176 | + } | |
| 177 | + } catch (Exception e) { | |
| 178 | + throw new RuntimeException(e); | |
| 179 | + } | |
| 180 | + } | |
| 181 | + | |
| 182 | + @Override | |
| 183 | + public JSONObject monitorService() { | |
| 184 | + return null; | |
| 185 | + } | |
| 186 | +} |
--- src/main/java/com/munjaon/client/server/service/PropertyLoader.java
+++ src/main/java/com/munjaon/client/server/service/PropertyLoader.java
... | ... | @@ -30,7 +30,7 @@ |
| 30 | 30 |
System.err.println("Not Defined : [" + key + "]");
|
| 31 | 31 |
} |
| 32 | 32 |
else {
|
| 33 |
- value = value.replaceAll("\\$WORK_HOME", System.getProperty("WORK_HOME"));
|
|
| 33 |
+ value = value.replaceAll("\\$SERVICE_HOME", System.getProperty("SERVICE_HOME"));
|
|
| 34 | 34 |
} |
| 35 | 35 |
|
| 36 | 36 |
return value; |
+++ src/main/java/com/munjaon/client/server/service/ReportClientService.java
... | ... | @@ -0,0 +1,140 @@ |
| 1 | +package com.munjaon.client.server.service; | |
| 2 | + | |
| 3 | +import com.munjaon.client.server.packet.Bind; | |
| 4 | +import com.munjaon.client.server.packet.Header; | |
| 5 | +import com.munjaon.client.server.packet.LinkCheck; | |
| 6 | +import com.munjaon.client.server.packet.Packet; | |
| 7 | +import com.munjaon.client.service.DatabaseTypeWorker; | |
| 8 | +import org.json.simple.JSONObject; | |
| 9 | + | |
| 10 | +import java.io.IOException; | |
| 11 | +import java.net.InetSocketAddress; | |
| 12 | +import java.nio.ByteBuffer; | |
| 13 | +import java.nio.channels.SocketChannel; | |
| 14 | + | |
| 15 | +public class ReportClientService extends Service { | |
| 16 | + private SocketChannel socketChannel; | |
| 17 | + private long lastPacketSendTime = 0; | |
| 18 | + private String address; | |
| 19 | + private int port; | |
| 20 | + private String id; | |
| 21 | + private String pwd; | |
| 22 | + | |
| 23 | + public ReportClientService(String serviceName) { | |
| 24 | + super(serviceName); | |
| 25 | + } | |
| 26 | + | |
| 27 | + @Override | |
| 28 | + public void checkReady() { | |
| 29 | + DatabaseTypeWorker worker = DatabaseTypeWorker.find(System.getProperty("DBMS")); | |
| 30 | + if (worker == null) { | |
| 31 | + return; | |
| 32 | + } | |
| 33 | + this.IS_READY_YN = worker.checkTableExists(); | |
| 34 | + } | |
| 35 | + | |
| 36 | + @Override | |
| 37 | + public void initResources() { | |
| 38 | + this.address = getProp("HOST"); | |
| 39 | + this.port = Integer.parseInt(getProp("PORT")); | |
| 40 | + this.id = getProp("ID"); | |
| 41 | + this.pwd = getProp("PASSWORD"); | |
| 42 | + saveSystemLog("Try Connect to " + this.address + ":" + this.port); | |
| 43 | + try { | |
| 44 | + socketChannel = SocketChannel.open(new InetSocketAddress(this.address, this.port)); | |
| 45 | + socketChannel.configureBlocking(false); | |
| 46 | + } catch (IOException e) { | |
| 47 | + saveSystemLog("Connect Fail " + this.address + ":" + this.port); | |
| 48 | + throw new RuntimeException(e); | |
| 49 | + } | |
| 50 | + } | |
| 51 | + | |
| 52 | + @Override | |
| 53 | + public void releaseResources() { | |
| 54 | + if (socketChannel != null) { | |
| 55 | + try { | |
| 56 | + socketChannel.close(); | |
| 57 | + } catch (IOException e) { | |
| 58 | + throw new RuntimeException(e); | |
| 59 | + } | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public void doService() { | |
| 65 | + | |
| 66 | + } | |
| 67 | + | |
| 68 | + private void bind() { | |
| 69 | + ByteBuffer sendBuffer = Bind.makeBindBuffer(id, pwd); | |
| 70 | + ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + Bind.BIND_ACK_BODY_LENGTH); | |
| 71 | + try { | |
| 72 | + saveSystemLog("sendBuffer " + sendBuffer.position() + ":" + sendBuffer.limit()); | |
| 73 | + saveSystemLog("Bind Try Connect to " + this.address + ":" + this.port); | |
| 74 | + socketChannel.write(sendBuffer); | |
| 75 | + saveSystemLog("Bind Read to " + this.address + ":" + this.port); | |
| 76 | + while (true) { | |
| 77 | + int recvCount = socketChannel.read(recvBuffer); | |
| 78 | + if (recvCount == -1) { | |
| 79 | + throw new RuntimeException("BIND ERROR"); | |
| 80 | + } else if (recvCount > 0) { | |
| 81 | + String resultCode = Bind.getBindAckResultCode(recvBuffer); | |
| 82 | + saveSystemLog("resultCode " + resultCode); | |
| 83 | + if ("00".equals(resultCode)) { | |
| 84 | + lastPacketSendTime = System.currentTimeMillis(); | |
| 85 | + saveSystemLog("Bind OK"); | |
| 86 | + break; | |
| 87 | + } else { | |
| 88 | + saveSystemLog("Bind FAIL"); | |
| 89 | + throw new RuntimeException("Bind FAIL"); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + } | |
| 93 | + | |
| 94 | + } catch (Exception e) { | |
| 95 | + throw new RuntimeException(e); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + private void messageService() { | |
| 100 | + bind(); | |
| 101 | + while (isRun()) { | |
| 102 | + try { | |
| 103 | + messageService(); | |
| 104 | + linkCheckService(); | |
| 105 | + } catch (Exception e) { | |
| 106 | + throw new RuntimeException(e); | |
| 107 | + } | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + private void linkCheckService() { | |
| 112 | + if (System.currentTimeMillis() - lastPacketSendTime < Packet.LINK_CHECK_CYCLE) { | |
| 113 | + saveSystemLog("LinkCheck Is Not"); | |
| 114 | + return; | |
| 115 | + } | |
| 116 | + | |
| 117 | + ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + LinkCheck.LINK_CHECK_ACK_BODY_LENGTH); | |
| 118 | + try { | |
| 119 | + saveSystemLog("LinkCheck Send"); | |
| 120 | + socketChannel.write(LinkCheck.makeLinkCheckBuffer()); | |
| 121 | + while (true) { | |
| 122 | + int recvCount = socketChannel.read(recvBuffer); | |
| 123 | + if (recvCount == -1) { | |
| 124 | + throw new RuntimeException("LINK_CHECK ERROR"); | |
| 125 | + } else if (recvCount > 0) { | |
| 126 | + saveSystemLog("LinkCheck OK"); | |
| 127 | + lastPacketSendTime = System.currentTimeMillis(); | |
| 128 | + break; | |
| 129 | + } | |
| 130 | + } | |
| 131 | + } catch (Exception e) { | |
| 132 | + throw new RuntimeException(e); | |
| 133 | + } | |
| 134 | + } | |
| 135 | + | |
| 136 | + @Override | |
| 137 | + public JSONObject monitorService() { | |
| 138 | + return null; | |
| 139 | + } | |
| 140 | +} |
+++ src/main/java/com/munjaon/client/server/service/Server.java
... | ... | @@ -0,0 +1,193 @@ |
| 1 | +package com.munjaon.client.server.service; | |
| 2 | + | |
| 3 | +import java.io.IOException; | |
| 4 | +import java.net.InetSocketAddress; | |
| 5 | +import java.net.Socket; | |
| 6 | +import java.net.SocketAddress; | |
| 7 | +import java.nio.ByteBuffer; | |
| 8 | +import java.nio.channels.SelectionKey; | |
| 9 | +import java.nio.channels.Selector; | |
| 10 | +import java.nio.channels.ServerSocketChannel; | |
| 11 | +import java.nio.channels.SocketChannel; | |
| 12 | +import java.util.Iterator; | |
| 13 | +import java.util.concurrent.Executors; | |
| 14 | + | |
| 15 | +public class Server implements Runnable { | |
| 16 | + private InetSocketAddress listenAddress; | |
| 17 | + // 메시지는 개행으로 구분한다. | |
| 18 | + private static char CR = (char) 0x0D; | |
| 19 | + private static char LF = (char) 0x0A; | |
| 20 | + // ip와 port 설정 | |
| 21 | + public Server(String address, int port) { | |
| 22 | + listenAddress = new InetSocketAddress(address, port); | |
| 23 | + } | |
| 24 | + // Thread 실행. | |
| 25 | + public void run() { | |
| 26 | + // 셀렉터 설정 | |
| 27 | + try (Selector selector = Selector.open()) { | |
| 28 | + // 채널 설정 | |
| 29 | + try (ServerSocketChannel serverChannel = ServerSocketChannel.open()) { | |
| 30 | + System.out.println("ServerSocketChannel is open"); | |
| 31 | + // non-Blocking 설정 | |
| 32 | + serverChannel.configureBlocking(false); | |
| 33 | + System.out.println("serverChannel.configureBlocking(false) >> "); | |
| 34 | + // 서버 ip, port 설정 | |
| 35 | + serverChannel.socket().bind(listenAddress); | |
| 36 | + System.out.println("serverChannel.socket().bind(listenAddress) >> "); | |
| 37 | + // 채널에 accept 대기 설정 | |
| 38 | + serverChannel.register(selector, SelectionKey.OP_ACCEPT); | |
| 39 | + System.out.println("serverChannel.register(selector, SelectionKey.OP_ACCEPT) >> "); | |
| 40 | + // 셀렉터가 있을 경우. | |
| 41 | +// while (selector.select() > 0) { | |
| 42 | + while (true) { | |
| 43 | + if (selector.select(10000) == 0) { | |
| 44 | + System.out.println("selector.select() == 0"); | |
| 45 | + continue; | |
| 46 | + } | |
| 47 | + System.out.println("selector.select() > 0 >> "); | |
| 48 | + // 셀렉터 키 셋를 가져온다. | |
| 49 | + Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); | |
| 50 | + // 키가 있으면.. | |
| 51 | + while (keys.hasNext()) { | |
| 52 | + SelectionKey key = keys.next(); | |
| 53 | + //키 셋에서 제거. | |
| 54 | + keys.remove(); | |
| 55 | + if (!key.isValid()) { | |
| 56 | + continue; | |
| 57 | + } | |
| 58 | + // 접속일 경우.. | |
| 59 | + if (key.isAcceptable()) { | |
| 60 | + this.accept(selector, key); | |
| 61 | + // 수신일 경우.. | |
| 62 | + } else if (key.isReadable()) { | |
| 63 | + this.receive(selector, key); | |
| 64 | + // 발신일 경우.. | |
| 65 | + } else if (key.isWritable()) { | |
| 66 | + this.send(selector, key); | |
| 67 | + } | |
| 68 | + } | |
| 69 | + } | |
| 70 | + } | |
| 71 | + } catch (IOException e) { | |
| 72 | + e.printStackTrace(); | |
| 73 | + } | |
| 74 | + } | |
| 75 | + // 접속시 호출 함수.. | |
| 76 | + private void accept(Selector selector, SelectionKey key) { | |
| 77 | + try { | |
| 78 | + // 키 채널을 가져온다. | |
| 79 | + ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); | |
| 80 | + // accept을 해서 Socket 채널을 가져온다. | |
| 81 | + SocketChannel channel = serverChannel.accept(); | |
| 82 | + channel.configureBlocking(false); | |
| 83 | + // 소켓 취득 | |
| 84 | + Socket socket = channel.socket(); | |
| 85 | + SocketAddress remoteAddr = socket.getRemoteSocketAddress(); | |
| 86 | + System.out.println("Connected to: " + remoteAddr); | |
| 87 | + // 접속 Socket 단위로 사용되는 Buffer; | |
| 88 | + StringBuffer sb = new StringBuffer(); | |
| 89 | + sb.append("Welcome server!\r\n>"); | |
| 90 | + // Socket 채널을 channel에 송신 등록한다 | |
| 91 | + channel.register(selector, SelectionKey.OP_WRITE, sb); | |
| 92 | + } catch (IOException e) { | |
| 93 | + e.printStackTrace(); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + // 수신시 호출 함수.. | |
| 97 | + private void receive(Selector selector, SelectionKey key) { | |
| 98 | + try { | |
| 99 | + // 키 채널을 가져온다. | |
| 100 | + SocketChannel channel = (SocketChannel) key.channel(); | |
| 101 | + // 연결이 끊겼는지 확인 | |
| 102 | + if ((channel.isConnected() || channel.isOpen()) == false) { | |
| 103 | + System.out.println("java connection reset by client: "); | |
| 104 | + // 키 닫기 | |
| 105 | + key.cancel(); | |
| 106 | + // 소켓 채널 닫기 | |
| 107 | + channel.close(); | |
| 108 | + // 종료 | |
| 109 | + return; | |
| 110 | + } | |
| 111 | + // 채널 Non-blocking 설정 | |
| 112 | + channel.configureBlocking(false); | |
| 113 | + // 소켓 취득 | |
| 114 | + Socket socket = channel.socket(); | |
| 115 | + // Byte 버퍼 생성 | |
| 116 | + ByteBuffer buffer = ByteBuffer.allocate(1024); | |
| 117 | + // ***데이터 수신*** | |
| 118 | + int size = channel.read(buffer); | |
| 119 | + // 수신 크기가 없으면 소켓 접속 종료. | |
| 120 | + if (size == -1) { | |
| 121 | + SocketAddress remoteAddr = socket.getRemoteSocketAddress(); | |
| 122 | + System.out.println("Connection closed by client: " + remoteAddr); | |
| 123 | + // 소켓 채널 닫기 | |
| 124 | + channel.close(); | |
| 125 | + // 소켓 닫기 | |
| 126 | + socket.close(); | |
| 127 | + // 키 닫기 | |
| 128 | + key.attach(null); | |
| 129 | + key.cancel(); | |
| 130 | + return; | |
| 131 | + } | |
| 132 | + // ByteBuffer -> byte[] | |
| 133 | + byte[] data = new byte[size]; | |
| 134 | + System.arraycopy(buffer.array(), 0, data, 0, size); | |
| 135 | + // StringBuffer 취득 | |
| 136 | + StringBuffer sb = (StringBuffer) key.attachment(); | |
| 137 | + // 버퍼에 수신된 데이터 추가 | |
| 138 | + sb.append(new String(data)); | |
| 139 | + // 데이터 끝이 개행 일 경우. | |
| 140 | + if (sb.length() > 2 && sb.charAt(sb.length() - 2) == CR && sb.charAt(sb.length() - 1) == LF) { | |
| 141 | + // 개행 삭제 | |
| 142 | + sb.setLength(sb.length() - 2); | |
| 143 | + // 메시지를 콘솔에 표시한다. | |
| 144 | + String msg = sb.toString(); | |
| 145 | + System.out.println(msg); | |
| 146 | + // exit 경우 접속을 종료한다. | |
| 147 | + if ("exit".equals(msg)) { | |
| 148 | + // 소켓 채널 닫기 | |
| 149 | + channel.close(); | |
| 150 | + // 소켓 닫기 | |
| 151 | + socket.close(); | |
| 152 | + // 키 닫기 | |
| 153 | + key.cancel(); | |
| 154 | + return; | |
| 155 | + } | |
| 156 | + // Echo - 메시지> 의 형태로 재 전송. | |
| 157 | + sb.insert(0, "Echo - "); | |
| 158 | + sb.append("\r\n>"); | |
| 159 | + // Socket 채널을 channel에 송신 등록한다 | |
| 160 | + channel.register(selector, SelectionKey.OP_WRITE, sb); | |
| 161 | + } | |
| 162 | + } catch (IOException e) { | |
| 163 | + e.printStackTrace(); | |
| 164 | + } | |
| 165 | + } | |
| 166 | + // 발신시 호출 함수 | |
| 167 | + private void send(Selector selector, SelectionKey key) { | |
| 168 | + try { | |
| 169 | + // 키 채널을 가져온다. | |
| 170 | + SocketChannel channel = (SocketChannel) key.channel(); | |
| 171 | + // 채널 Non-blocking 설정 | |
| 172 | + channel.configureBlocking(false); | |
| 173 | + // StringBuffer 취득 | |
| 174 | + StringBuffer sb = (StringBuffer) key.attachment(); | |
| 175 | + String data = sb.toString(); | |
| 176 | + // StringBuffer 초기화 | |
| 177 | + sb.setLength(0); | |
| 178 | + // byte 형식으로 변환 | |
| 179 | + ByteBuffer buffer = ByteBuffer.wrap(data.getBytes()); | |
| 180 | + // ***데이터 송신*** | |
| 181 | + channel.write(buffer); | |
| 182 | + // Socket 채널을 channel에 수신 등록한다 | |
| 183 | + channel.register(selector, SelectionKey.OP_READ, sb); | |
| 184 | + } catch (IOException e) { | |
| 185 | + e.printStackTrace(); | |
| 186 | + } | |
| 187 | + } | |
| 188 | + // 시작 함수 | |
| 189 | + public static void main(String[] args) { | |
| 190 | + // 포트는 10000을 Listen한다. | |
| 191 | + Executors.newSingleThreadExecutor().execute(new Server("localhost", 10000)); | |
| 192 | + } | |
| 193 | +} |
+++ src/main/java/com/munjaon/client/server/service/Service.java
... | ... | @@ -0,0 +1,148 @@ |
| 1 | +package com.munjaon.client.server.service; | |
| 2 | + | |
| 3 | +import com.munjaon.client.server.config.ServerConfig; | |
| 4 | +import com.munjaon.client.util.LogUtil; | |
| 5 | +import org.json.simple.JSONObject; | |
| 6 | + | |
| 7 | +import java.text.SimpleDateFormat; | |
| 8 | +import java.time.LocalDateTime; | |
| 9 | +import java.time.format.DateTimeFormatter; | |
| 10 | + | |
| 11 | +public abstract class Service extends Thread { | |
| 12 | + public static final SimpleDateFormat sdf = new SimpleDateFormat("[MM-dd HH:mm:ss]"); | |
| 13 | + public static final String LOG_DATE_FORMAT = "[MM-dd HH:mm:ss]"; | |
| 14 | + | |
| 15 | + private String LOG_FILE; | |
| 16 | + protected LogUtil logger; | |
| 17 | + private Long LAST_PROPERTY_LOAD_TIME = 0L; | |
| 18 | + | |
| 19 | + protected boolean IS_SERVER_RUN; // 서버가 구동중인지 여부 | |
| 20 | + protected boolean IS_READY_YN; // 서비스 구동준비가 완료되었는지 체크 | |
| 21 | + protected boolean IS_RUN_YN; | |
| 22 | + protected boolean IS_STOP_YN; | |
| 23 | + | |
| 24 | + public Service() {} | |
| 25 | + public Service(String serviceName) { | |
| 26 | + super(serviceName); | |
| 27 | + LOG_FILE = System.getProperty("ROOTPATH") + getProp("LOG_FILE"); | |
| 28 | + } | |
| 29 | + | |
| 30 | + protected String getProp(String name) { | |
| 31 | + return getProp(getName(), name); | |
| 32 | + } | |
| 33 | + | |
| 34 | + public static String getProp(String svc, String name) { | |
| 35 | + return PropertyLoader.getProp(svc, name); | |
| 36 | + } | |
| 37 | + | |
| 38 | + protected void checkRun() { | |
| 39 | + this.IS_RUN_YN = getProp("RUN_FLAG") != null && "Y".equals(getProp("RUN_FLAG")); | |
| 40 | + } | |
| 41 | + | |
| 42 | + protected void checkClientRun() { | |
| 43 | + this.IS_SERVER_RUN = getProp("client", "run") != null && "Y".equals(getProp("client", "run")); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public void reloadCheckRun() { | |
| 47 | + if ((System.currentTimeMillis() - this.LAST_PROPERTY_LOAD_TIME) > ServerConfig.INTERVAL_PROPERTY_RELOAD_TIME) { | |
| 48 | + checkRun(); | |
| 49 | + checkClientRun(); | |
| 50 | + this.LAST_PROPERTY_LOAD_TIME = System.currentTimeMillis(); | |
| 51 | + } | |
| 52 | + } | |
| 53 | + | |
| 54 | + public boolean isRun() { | |
| 55 | + return IS_SERVER_RUN && IS_RUN_YN && !IS_STOP_YN; | |
| 56 | + } | |
| 57 | + | |
| 58 | + public boolean isReady() { | |
| 59 | + return IS_READY_YN; | |
| 60 | + } | |
| 61 | + | |
| 62 | + protected void setLogFile(String sLogFile) { | |
| 63 | + if ( logger != null ) { | |
| 64 | + logger.close(); | |
| 65 | + logger = null; | |
| 66 | + } | |
| 67 | + | |
| 68 | + logger = new LogUtil( sLogFile ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + protected void saveSystemLog(Object obj) { | |
| 72 | + saveLog(obj, true); | |
| 73 | + } | |
| 74 | + | |
| 75 | + protected void saveLog(Object obj) { | |
| 76 | + saveLog(obj, false); | |
| 77 | + } | |
| 78 | + | |
| 79 | + protected void saveLog(Object obj, boolean isConsoleOutput) { | |
| 80 | + if(isConsoleOutput) { | |
| 81 | + System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern(LOG_DATE_FORMAT)) + " {{"+ getName() +"}} "+obj); | |
| 82 | + } | |
| 83 | + if (logger != null) { | |
| 84 | + logger.log(obj); | |
| 85 | + } else { | |
| 86 | + if (obj instanceof Throwable) { | |
| 87 | + LogUtil.log(LOG_FILE, obj); | |
| 88 | + } else { | |
| 89 | + LogUtil.log(LOG_FILE, "{{"+ getName() +"}} "+obj); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + } | |
| 93 | + | |
| 94 | + protected void initLogFile() { | |
| 95 | + LOG_FILE = System.getProperty("ROOTPATH") + getProp("LOG_FILE"); | |
| 96 | + System.out.println("LOG_FILE: " + LOG_FILE); | |
| 97 | + setLogFile( LOG_FILE ); | |
| 98 | + saveSystemLog("Service Log Initializing ... ..."); | |
| 99 | + } | |
| 100 | + | |
| 101 | + @Override | |
| 102 | + public void run() { | |
| 103 | + while (true) { | |
| 104 | + try { | |
| 105 | + /* 1. 서비스간의 dependency에 따른 체크 */ | |
| 106 | + checkReady(); | |
| 107 | + /* 2. 로그 초기화 */ | |
| 108 | + initLogFile(); | |
| 109 | + /* 3. Runflag reload */ | |
| 110 | + reloadCheckRun(); | |
| 111 | + | |
| 112 | + if (isRun() && isReady()) { | |
| 113 | + /* 3. 서비스 초기화 */ | |
| 114 | + initResources(); | |
| 115 | + /* 4. 서비스 시작 */ | |
| 116 | + doService(); | |
| 117 | + /* 5. 서비스 자원 해제 */ | |
| 118 | + releaseResources(); | |
| 119 | + saveSystemLog("Service Stopped."); | |
| 120 | + } else { | |
| 121 | + saveSystemLog("Service is Not Running."); | |
| 122 | + } | |
| 123 | + /* 6. 3초간 sleep */ | |
| 124 | + Thread.sleep(3000); | |
| 125 | + } catch (Exception e) { | |
| 126 | + saveSystemLog(e); | |
| 127 | + try { | |
| 128 | + Thread.sleep(3000); | |
| 129 | + } catch (InterruptedException ex) { | |
| 130 | + throw new RuntimeException(ex); | |
| 131 | + } | |
| 132 | + } finally { | |
| 133 | + if(logger != null) { logger.close(); logger = null; } | |
| 134 | + } | |
| 135 | + } | |
| 136 | + } | |
| 137 | + | |
| 138 | + /* 서비스간의 dependency에 따른 체크 */ | |
| 139 | + public abstract void checkReady(); | |
| 140 | + /* 추가적인 프로퍼티, 서비스 자원 초기화 */ | |
| 141 | + public abstract void initResources(); | |
| 142 | + /* 서비스 자원 해제 */ | |
| 143 | + public abstract void releaseResources(); | |
| 144 | + /* 서비스 */ | |
| 145 | + public abstract void doService(); | |
| 146 | + /* 모니터링을 위한 메소드 */ | |
| 147 | + public abstract JSONObject monitorService(); | |
| 148 | +} |
+++ src/main/java/com/munjaon/client/util/ByteUtil.java
... | ... | @@ -0,0 +1,423 @@ |
| 1 | +/* | |
| 2 | + * To change this template, choose Tools | Templates | |
| 3 | + * and open the template in the editor. | |
| 4 | + */ | |
| 5 | + | |
| 6 | +package com.munjaon.client.util; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * bytes 관련 유틸리티 클래스 | |
| 10 | + * @author JDS | |
| 11 | + */ | |
| 12 | +public final class ByteUtil { | |
| 13 | + public static final int BYTE_SIZE = 128; | |
| 14 | + /* 큐에 저장하기전에 바이트를 채울 문자 */ | |
| 15 | + public static final byte SET_DEFAULT_BYTE = (byte) 0x00; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * bytes 배열의 핵사값을 16자리씩 개행하여 반환한다.<BR> | |
| 19 | + * 핵사 로그 출력을 위해 사용한다. | |
| 20 | + * @param src | |
| 21 | + * @param isLine | |
| 22 | + * @return 16자리씩 개행된 핵사값 | |
| 23 | + */ | |
| 24 | + public static String byteToHex(byte[] src, boolean isLine) { | |
| 25 | + String hex = ""; | |
| 26 | + // if(src.length > 16) | |
| 27 | + hex = ""; | |
| 28 | + for (int i = 0, j = 1; i < src.length; i++, j++) { | |
| 29 | + hex += pad0(Integer.toHexString(src[i] & 0xff), 2) + " "; | |
| 30 | + | |
| 31 | + if (isLine && src.length > 16 && (j % 16) == 0) { | |
| 32 | + // hex += " " + new String(codes, j-16, 16); | |
| 33 | + hex += "\r\n"; | |
| 34 | + } | |
| 35 | + } | |
| 36 | + return hex; | |
| 37 | + } | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * bytes 배열의 핵사값을 16자리씩 개행하여 반환한다.<BR> | |
| 41 | + * 핵사 로그 출력을 위해 사용한다. | |
| 42 | + * @param src | |
| 43 | + * @return 16자리씩 개행된 핵사값 | |
| 44 | + */ | |
| 45 | + public static String byteToHex(byte[] src) { | |
| 46 | + String hex = ""; | |
| 47 | + // if(src.length > 16) | |
| 48 | + //hex = "\r\n"; | |
| 49 | + for (int i = 0, j = 1; i < src.length; i++, j++) { | |
| 50 | + hex += pad0(Integer.toHexString(src[i] & 0xff), 2) + " "; | |
| 51 | + | |
| 52 | + if (src.length > 16 && (j % 16) == 0) { | |
| 53 | + // hex += " " + new String(codes, j-16, 16); | |
| 54 | + hex += "\r\n"; | |
| 55 | + } | |
| 56 | + } | |
| 57 | + return hex; | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * bytes 배열의 지정된 위치값에 대해 16자리씩 개행된 핵사값으로 반환한다.<BR> | |
| 62 | + * 핵사 로그 출력을 위해 사용한다. | |
| 63 | + * @param src | |
| 64 | + * @param srcPos | |
| 65 | + * @param length | |
| 66 | + * @return 16자리씩 개행된 핵사값 | |
| 67 | + */ | |
| 68 | + public static String byteToHex(byte[] src, int srcPos, int length) { | |
| 69 | + String hex = ""; | |
| 70 | + //if(src.length > 16) | |
| 71 | + hex = "\r\n"; | |
| 72 | + for (int i = srcPos, j = 1; i < srcPos + length; i++, j++) { | |
| 73 | + hex += pad0(Integer.toHexString(src[i] & 0xff), 2) + " "; | |
| 74 | + if (src.length > 16 && (j % 16) == 0) { | |
| 75 | + hex += "\r\n"; | |
| 76 | + } | |
| 77 | + } | |
| 78 | + return hex; | |
| 79 | + } | |
| 80 | + | |
| 81 | + public static String intToHex(int src) { | |
| 82 | + String hex = "0x"; | |
| 83 | + byte[] bSrc = ByteUtil.intToByte(src); | |
| 84 | + for (int i = 0, j = 1; i < bSrc.length; i++, j++) { | |
| 85 | + hex += pad0(Integer.toHexString(bSrc[i] & 0xff), 2); | |
| 86 | + } | |
| 87 | + return hex; | |
| 88 | + } | |
| 89 | + | |
| 90 | + public static byte[] shortToByte(short s) { | |
| 91 | + byte dest[] = new byte[2]; | |
| 92 | + dest[1] = (byte) (s & 0xff); | |
| 93 | + dest[0] = (byte) (s >>> 8 & 0xff); | |
| 94 | + return dest; | |
| 95 | + } | |
| 96 | + | |
| 97 | + public static byte[] intToByte(int i) { | |
| 98 | + byte dest[] = new byte[4]; | |
| 99 | + dest[3] = (byte) (i & 0xff); | |
| 100 | + dest[2] = (byte) (i >>> 8 & 0xff); | |
| 101 | + dest[1] = (byte) (i >>> 16 & 0xff); | |
| 102 | + dest[0] = (byte) (i >>> 24 & 0xff); | |
| 103 | + return dest; | |
| 104 | + } | |
| 105 | + | |
| 106 | + public static byte[] longToByte(long l) { | |
| 107 | + byte dest[] = new byte[8]; | |
| 108 | + dest[7] = (byte) (int) (l & 255L); | |
| 109 | + dest[6] = (byte) (int) (l >>> 8 & 255L); | |
| 110 | + dest[5] = (byte) (int) (l >>> 16 & 255L); | |
| 111 | + dest[4] = (byte) (int) (l >>> 24 & 255L); | |
| 112 | + dest[3] = (byte) (int) (l >>> 32 & 255L); | |
| 113 | + dest[2] = (byte) (int) (l >>> 40 & 255L); | |
| 114 | + dest[1] = (byte) (int) (l >>> 48 & 255L); | |
| 115 | + dest[0] = (byte) (int) (l >>> 56 & 255L); | |
| 116 | + return dest; | |
| 117 | + } | |
| 118 | + | |
| 119 | + public static byte[] floatToByte(float f) { | |
| 120 | + byte dest[] = new byte[4]; | |
| 121 | + return setFloat(dest, 0, f); | |
| 122 | + } | |
| 123 | + | |
| 124 | + public static byte[] doubleToByte(double d) { | |
| 125 | + byte dest[] = new byte[8]; | |
| 126 | + return setDouble(dest, 0, d); | |
| 127 | + } | |
| 128 | + | |
| 129 | + public static byte getByte(byte src[], int offset) { | |
| 130 | + return src[offset]; | |
| 131 | + } | |
| 132 | + | |
| 133 | + public static byte[] getBytes(String data) { | |
| 134 | + byte[] b = null; | |
| 135 | + | |
| 136 | + b = data.getBytes(); | |
| 137 | + | |
| 138 | + return (b); | |
| 139 | + } | |
| 140 | + | |
| 141 | + public static byte[] getBytes(byte src[], int offset, int length) { | |
| 142 | + byte dest[] = new byte[length]; | |
| 143 | + System.arraycopy(src, offset, dest, 0, length); | |
| 144 | + return dest; | |
| 145 | + } | |
| 146 | + | |
| 147 | + public static short getShort(byte src[], int offset) { | |
| 148 | + return (short) ((src[offset] & 0xff) << 8 | src[offset + 1] & 0xff); | |
| 149 | + } | |
| 150 | + | |
| 151 | + public static int getInt(byte src[], int offset) { | |
| 152 | + return (src[offset] & 0xff) << 24 | (src[offset + 1] & 0xff) << 16 | |
| 153 | + | (src[offset + 2] & 0xff) << 8 | src[offset + 3] & 0xff; | |
| 154 | + } | |
| 155 | + | |
| 156 | + public static long getLong(byte src[], int offset) { | |
| 157 | + return (long) getInt(src, offset) << 32 | |
| 158 | + | (long) getInt(src, offset + 4) & 0xffffffffL; | |
| 159 | + } | |
| 160 | + | |
| 161 | + public static float getfloat(byte src[], int offset) { | |
| 162 | + return Float.intBitsToFloat(getInt(src, offset)); | |
| 163 | + } | |
| 164 | + | |
| 165 | + public static double getdouble(byte src[], int offset) { | |
| 166 | + return Double.longBitsToDouble(getLong(src, offset)); | |
| 167 | + } | |
| 168 | + | |
| 169 | + public static byte[] setByte(byte dest[], int offset, byte b) { | |
| 170 | + dest[offset] = b; | |
| 171 | + return dest; | |
| 172 | + } | |
| 173 | + | |
| 174 | + public static void setBytes(byte[] dest, int offset, String s) { | |
| 175 | + setBytes(dest, offset, s.getBytes()); | |
| 176 | + } | |
| 177 | + | |
| 178 | + public static byte[] setBytes(byte dest[], int offset, byte src[]) { | |
| 179 | + System.arraycopy(src, 0, dest, offset, src.length); | |
| 180 | + return dest; | |
| 181 | + } | |
| 182 | + | |
| 183 | + public static byte[] setBytes(byte dest[], int offset, byte src[], | |
| 184 | + int len) { | |
| 185 | + System.arraycopy(src, 0, dest, offset, len); | |
| 186 | + return dest; | |
| 187 | + } | |
| 188 | + | |
| 189 | + public static byte[] setShort(byte dest[], int offset, short s) { | |
| 190 | + dest[offset] = (byte) (s >>> 8 & 0xff); | |
| 191 | + dest[offset + 1] = (byte) (s & 0xff); | |
| 192 | + return dest; | |
| 193 | + } | |
| 194 | + | |
| 195 | + public static byte[] setInt(byte dest[], int offset, int i) { | |
| 196 | + dest[offset] = (byte) (i >>> 24 & 0xff); | |
| 197 | + dest[offset + 1] = (byte) (i >>> 16 & 0xff); | |
| 198 | + dest[offset + 2] = (byte) (i >>> 8 & 0xff); | |
| 199 | + dest[offset + 3] = (byte) (i & 0xff); | |
| 200 | + return dest; | |
| 201 | + } | |
| 202 | + | |
| 203 | + public static byte[] setLong(byte dest[], int offset, long l) { | |
| 204 | + setInt(dest, offset, (int) (l >>> 32)); | |
| 205 | + setInt(dest, offset + 4, (int) (l & 0xffffffffL)); | |
| 206 | + return dest; | |
| 207 | + } | |
| 208 | + | |
| 209 | + public static byte[] setFloat(byte dest[], int offset, float f) { | |
| 210 | + return setInt(dest, offset, Float.floatToIntBits(f)); | |
| 211 | + } | |
| 212 | + | |
| 213 | + public static byte[] setDouble(byte dest[], int offset, double d) { | |
| 214 | + return setLong(dest, offset, Double.doubleToLongBits(d)); | |
| 215 | + } | |
| 216 | + | |
| 217 | + public static boolean isEquals(byte b[], String s) { | |
| 218 | + if (b == null || s == null) | |
| 219 | + return false; | |
| 220 | + int slen = s.length(); | |
| 221 | + if (b.length != slen) | |
| 222 | + return false; | |
| 223 | + for (int i = slen; i-- > 0;) | |
| 224 | + if (b[i] != s.charAt(i)) | |
| 225 | + return false; | |
| 226 | + | |
| 227 | + return true; | |
| 228 | + } | |
| 229 | + | |
| 230 | + public static boolean isEquals(byte a[], byte b[]) { | |
| 231 | + if (a == null || b == null) | |
| 232 | + return false; | |
| 233 | + if (a.length != b.length) | |
| 234 | + return false; | |
| 235 | + for (int i = a.length; i-- > 0;) | |
| 236 | + if (a[i] != b[i]) | |
| 237 | + return false; | |
| 238 | + | |
| 239 | + return true; | |
| 240 | + } | |
| 241 | + | |
| 242 | + public static int hexToInt(byte[] b) { | |
| 243 | + int sum = 0; | |
| 244 | + int len = 0; | |
| 245 | + int pos = 0; | |
| 246 | + | |
| 247 | + if (b != null) { | |
| 248 | + len = b.length; | |
| 249 | + | |
| 250 | + for (int i = len - 1; i > 0; i--) { | |
| 251 | + sum += sqrt(BYTE_SIZE, i) * (int) b[pos]; | |
| 252 | + pos++; | |
| 253 | + } | |
| 254 | + sum += b[pos]; | |
| 255 | + } | |
| 256 | + | |
| 257 | + return (sum); | |
| 258 | + } | |
| 259 | + | |
| 260 | + public static int hexToInt(byte[] b, int start, int len) { | |
| 261 | + int sum = 0; | |
| 262 | + int pos = start; | |
| 263 | + | |
| 264 | + if (b != null) { | |
| 265 | + for (int i = len - 1; i > 0; i--) { | |
| 266 | + sum += sqrt(BYTE_SIZE, i) * (int) b[pos]; | |
| 267 | + pos++; | |
| 268 | + } | |
| 269 | + sum += b[pos]; | |
| 270 | + } | |
| 271 | + | |
| 272 | + return (sum); | |
| 273 | + } | |
| 274 | + | |
| 275 | + public static byte[] intToHex(int num, int digit) { | |
| 276 | + byte[] b = new byte[digit]; | |
| 277 | + int[] intArr = makeFunction(num, digit, BYTE_SIZE); | |
| 278 | + | |
| 279 | + for (int i = 0; i < digit; i++) { | |
| 280 | + b[i] = (byte) intArr[i]; | |
| 281 | + } | |
| 282 | + | |
| 283 | + return (b); | |
| 284 | + } | |
| 285 | + | |
| 286 | + private static int[] makeFunction(int num, int digit, int jinso) { | |
| 287 | + int[] fun = new int[digit]; | |
| 288 | + int restNum = num; | |
| 289 | + int pos = 0; | |
| 290 | + | |
| 291 | + for (int i = digit - 1; i > 0; i--) { | |
| 292 | + int quotient = restNum / sqrt(jinso, i); | |
| 293 | + fun[pos++] = quotient; | |
| 294 | + restNum -= (quotient * sqrt(jinso, i)); | |
| 295 | + } | |
| 296 | + fun[pos++] = restNum; | |
| 297 | + | |
| 298 | + return (fun); | |
| 299 | + } | |
| 300 | + | |
| 301 | + private static int sqrt(int num, int count) { | |
| 302 | + int sum = 0; | |
| 303 | + | |
| 304 | + sum = (count >= 1) ? num : 0; | |
| 305 | + for (int i = 1; i < count; i++) { | |
| 306 | + sum = sum * num; | |
| 307 | + } | |
| 308 | + | |
| 309 | + return (sum); | |
| 310 | + } | |
| 311 | + | |
| 312 | + public static boolean arrayComp(byte[] src, byte[] obj) { | |
| 313 | + boolean isSame = true; | |
| 314 | + | |
| 315 | + if (src.length != obj.length) { | |
| 316 | + isSame = false; | |
| 317 | + } else { | |
| 318 | + for (int i = 0; i < src.length; i++) { | |
| 319 | + if (src[i] != obj[i]) { | |
| 320 | + isSame = false; | |
| 321 | + break; | |
| 322 | + } | |
| 323 | + } | |
| 324 | + } | |
| 325 | + | |
| 326 | + return (isSame); | |
| 327 | + } | |
| 328 | + | |
| 329 | + public static String pad0(String str, int size) { | |
| 330 | + char[] zeros = new char[size - str.length()]; | |
| 331 | + for (int i = 0; i < zeros.length; i++) | |
| 332 | + zeros[i] = '0'; | |
| 333 | + return new String(zeros) + str; | |
| 334 | + } | |
| 335 | + | |
| 336 | + public static byte[] convertCharArrayToByteArray(char[] ca) { | |
| 337 | + byte[] ba = new byte[ca.length*2]; | |
| 338 | + java.nio.ByteBuffer.wrap(ba).asCharBuffer().put(ca); | |
| 339 | + return ba; | |
| 340 | + } | |
| 341 | + | |
| 342 | + public static char[] convertByteArrayToCharArray(byte[] ba) { | |
| 343 | + char[] ca = new char[ba.length/2]; | |
| 344 | + java.nio.ByteBuffer.wrap(ba).asCharBuffer().get(ca); | |
| 345 | + return ca; | |
| 346 | + } | |
| 347 | + | |
| 348 | + public static byte[] setNull(byte[] b) { | |
| 349 | + for(int i=0; i<b.length; i++) { | |
| 350 | + b[i] = 0x00; | |
| 351 | + } | |
| 352 | + return b; | |
| 353 | + } | |
| 354 | + | |
| 355 | + | |
| 356 | + // Big Endian: network order | |
| 357 | + public static int toInt(byte[] value) { | |
| 358 | + return (int)( | |
| 359 | + (value[3] & 0xff) | | |
| 360 | + (value[2] & 0xff) << 8 | | |
| 361 | + (value[1] & 0xff) << 16 | | |
| 362 | + (value[0] & 0xff) << 24 ); | |
| 363 | + } | |
| 364 | + | |
| 365 | + // Big Endian: network order | |
| 366 | + public static byte[] toBytes(int value) { | |
| 367 | + byte[] ret = new byte[4]; | |
| 368 | + | |
| 369 | + ret[3] = (byte)(value & 0xff); | |
| 370 | + ret[2] = (byte)(value >> 8 & 0xff); | |
| 371 | + ret[1] = (byte)(value >> 16 & 0xff); | |
| 372 | + ret[0] = (byte)(value >> 24 & 0xff); | |
| 373 | + | |
| 374 | + return ret; | |
| 375 | + } | |
| 376 | + | |
| 377 | + // Little Endian | |
| 378 | + public static int toInt2(byte[] value) { | |
| 379 | + return (int)( | |
| 380 | + (value[0] & 0xff) | | |
| 381 | + (value[1] & 0xff) << 8 | | |
| 382 | + (value[2] & 0xff) << 16 | | |
| 383 | + (value[3] & 0xff) << 24 ); | |
| 384 | + } | |
| 385 | + | |
| 386 | + // Little Endian | |
| 387 | + public static byte[] toBytes2(int value) { | |
| 388 | + byte[] ret = new byte[4]; | |
| 389 | + | |
| 390 | + ret[0] = (byte)(value & 0xff); | |
| 391 | + ret[1] = (byte)(value >> 8 & 0xff); | |
| 392 | + ret[2] = (byte)(value >> 16 & 0xff); | |
| 393 | + ret[3] = (byte)(value >> 24 & 0xff); | |
| 394 | + | |
| 395 | + return ret; | |
| 396 | + } | |
| 397 | + | |
| 398 | + public static byte[] setSpaceAll(byte[] data) { | |
| 399 | + return setAll(data, (byte) ' '); | |
| 400 | + } | |
| 401 | + | |
| 402 | + public static byte[] setAll(byte[] data, byte bt) { | |
| 403 | + int len = data.length; | |
| 404 | + | |
| 405 | + for( int i=0; i<len; i++ ) { | |
| 406 | + data[i] = bt; | |
| 407 | + } | |
| 408 | + | |
| 409 | + return data; | |
| 410 | + } | |
| 411 | + | |
| 412 | + public static byte[] replaceAll(byte[] data, byte from, byte to) { | |
| 413 | + int len = data.length; | |
| 414 | + | |
| 415 | + for( int i=0; i<len; i++ ) { | |
| 416 | + if( data[i] == from ) { | |
| 417 | + data[i] = to; | |
| 418 | + } | |
| 419 | + } | |
| 420 | + | |
| 421 | + return data; | |
| 422 | + } | |
| 423 | +} |
+++ src/main/java/com/munjaon/client/util/CommonUtil.java
... | ... | @@ -0,0 +1,359 @@ |
| 1 | +package com.munjaon.client.util; | |
| 2 | + | |
| 3 | +import java.text.ParseException; | |
| 4 | +import java.text.SimpleDateFormat; | |
| 5 | +import java.util.Calendar; | |
| 6 | +import java.util.Date; | |
| 7 | + | |
| 8 | +public final class CommonUtil { | |
| 9 | + public static final String DATE_FORMAT_14 = "yyyyMMddHHmmss"; | |
| 10 | + public static final int TIMEOUT_DIVIDE = 1000 * 60; | |
| 11 | + | |
| 12 | + // NewLine 공백처리하기 | |
| 13 | + public static String cutNewLine(String msg){ | |
| 14 | + String encString = ""; | |
| 15 | + if(msg != null){ | |
| 16 | + encString = msg.replaceAll("\r", ""); | |
| 17 | + encString = encString.replaceAll("\n", ""); | |
| 18 | + } | |
| 19 | + return encString; | |
| 20 | + } | |
| 21 | + // NewLine 인코딩 돌리기 | |
| 22 | + public static String encodeNewLine(String msg){ | |
| 23 | + String encString = ""; | |
| 24 | + if(msg != null){ | |
| 25 | + /*encString = msg.replaceAll("\r", "\n"); | |
| 26 | + encString = encString.replaceAll("\n\n", "\n"); | |
| 27 | + encString = encString.replaceAll("\n", "ⓝⓛ");*/ | |
| 28 | + encString = msg.replaceAll("\r", ""); | |
| 29 | + encString = encString.replaceAll("\n", "ⓝⓛ"); | |
| 30 | + } | |
| 31 | + return encString; | |
| 32 | + } | |
| 33 | + // NewLine 디코딩 돌리기 | |
| 34 | + public static String decodeNewLine(String msg){ | |
| 35 | + String encString = ""; | |
| 36 | + if(msg != null){ | |
| 37 | + encString = msg.replaceAll("ⓝⓛ", "\n"); | |
| 38 | + } | |
| 39 | + return encString; | |
| 40 | + } | |
| 41 | + // Comma 인코딩 돌리기 | |
| 42 | + public static String encodeComma(String msg){ | |
| 43 | + String encString = ""; | |
| 44 | + if(msg != null){ | |
| 45 | + encString = msg.replaceAll("\"", "ⓒⓞⓜⓜⓐ"); | |
| 46 | + } | |
| 47 | + return encString; | |
| 48 | + } | |
| 49 | + // Comma 디코딩 돌리기 | |
| 50 | + public static String decodeComma(String msg){ | |
| 51 | + String decString = ""; | |
| 52 | + if(msg != null){ | |
| 53 | + decString = msg.replaceAll("ⓒⓞⓜⓜⓐ", "\""); | |
| 54 | + } | |
| 55 | + return decString; | |
| 56 | + } | |
| 57 | + // 문자열이 널인지 체크하는 함수 | |
| 58 | + public static boolean checkMsgNull(String msgStr){ | |
| 59 | + boolean isValid = false; | |
| 60 | + String[] msgArray = msgStr.split("ⓜⓢ"); | |
| 61 | + int msgArrayLen = msgArray.length; | |
| 62 | + for(int i=0;i<msgArrayLen;i++){ | |
| 63 | + msgArray[i] = msgArray[i].trim(); | |
| 64 | + if(msgArray[i].length() < 1){ | |
| 65 | + isValid = true; | |
| 66 | + break; | |
| 67 | + } | |
| 68 | + } | |
| 69 | + return isValid; | |
| 70 | + } | |
| 71 | + // 해당 길이만큼 문자열을 자르는 함수 | |
| 72 | + public static String cutString(String str, int limit) | |
| 73 | + { | |
| 74 | + int len = str.length(); | |
| 75 | + int sumLength=0; | |
| 76 | + String cutString = null; | |
| 77 | + byte[] toByte = str.getBytes(); | |
| 78 | + | |
| 79 | + if(limit < 2) | |
| 80 | + return ""; | |
| 81 | + if(toByte.length > limit){ | |
| 82 | + for(int cnt = 0 ; cnt < len ;cnt++){ | |
| 83 | + if ((str.charAt(cnt) & 0xFF00) == 0) // 1 Byte 문자이면 | |
| 84 | + sumLength++; | |
| 85 | + else // 2바이트 문자라면... | |
| 86 | + sumLength = sumLength + 2; | |
| 87 | + if(sumLength > limit){ | |
| 88 | + cutString = str.substring(0, cnt); | |
| 89 | + break; | |
| 90 | + } | |
| 91 | + } | |
| 92 | + }else{ | |
| 93 | + cutString = str; | |
| 94 | + } | |
| 95 | + | |
| 96 | + return cutString; | |
| 97 | + } | |
| 98 | + // 날짜를 구간별로 자르는 함수 | |
| 99 | + // (inDate:날짜 >> (입력형식:2012년 12월 20일일 경우 : 20121220)) | |
| 100 | + public static int substringDate(String inDate, int beginIndex, int lastIndex){ | |
| 101 | + int subDate = -1; | |
| 102 | + inDate = doNumber(inDate); | |
| 103 | + if(inDate.length() == 8){ | |
| 104 | + if((beginIndex < lastIndex) && (lastIndex <= 8)) | |
| 105 | + subDate = Integer.parseInt(inDate.substring(beginIndex, lastIndex)); | |
| 106 | + } | |
| 107 | + return subDate; | |
| 108 | + } | |
| 109 | + // 시간을 구간별로 자르는 함수 | |
| 110 | + public static int substringTime(String inTime, int beginIndex, int lastIndex){ | |
| 111 | + int subDate = -1; | |
| 112 | + inTime = doNumber(inTime); | |
| 113 | + if(inTime.length() == 6){ | |
| 114 | + if((beginIndex < lastIndex) && (lastIndex <= 6)) | |
| 115 | + subDate = Integer.parseInt(inTime.substring(beginIndex, lastIndex)); | |
| 116 | + } | |
| 117 | + return subDate; | |
| 118 | + } | |
| 119 | + public static boolean validDate(String fullDate){ | |
| 120 | + boolean validFlag = true; | |
| 121 | + fullDate = doNumber(fullDate); | |
| 122 | + | |
| 123 | + if(fullDate.length() != 14){ | |
| 124 | + validFlag = false; | |
| 125 | + }else{ | |
| 126 | + if(!(isValidDay(fullDate.substring(0, 8)) && isValidTime(fullDate.substring(8, 14)))) | |
| 127 | + validFlag = false; | |
| 128 | + } | |
| 129 | + return validFlag; | |
| 130 | + } | |
| 131 | + // 유효한 날짜 형식인지 체크하는 함수 | |
| 132 | + public static boolean isValidDay(String inDate){ | |
| 133 | + boolean validFlag = true; | |
| 134 | + int year = substringDate(inDate, 0, 4); | |
| 135 | + // 년도에서 앞에 두자리 체크 | |
| 136 | + if(year < 2013){ | |
| 137 | + validFlag = false; | |
| 138 | + } | |
| 139 | + int month = substringDate(inDate, 4, 6); | |
| 140 | + int day = substringDate(inDate, 6, 8); | |
| 141 | + if (month < 1 || month > 12) { // check month range | |
| 142 | + validFlag = false; | |
| 143 | + } | |
| 144 | + if (day < 1 || day > 31) { | |
| 145 | + validFlag = false; | |
| 146 | + } | |
| 147 | + if ((month==4 || month==6 || month==9 || month==11) && day==31) { | |
| 148 | + validFlag = false; | |
| 149 | + } | |
| 150 | + if (month == 2) { // check for february 29th | |
| 151 | + boolean isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); | |
| 152 | + if (day>29 || (day==29 && !isleap)) { | |
| 153 | + validFlag = false; | |
| 154 | + } | |
| 155 | + } | |
| 156 | + return validFlag; | |
| 157 | + } | |
| 158 | + // 유효한 날짜 형식인지 체크하는 함수 | |
| 159 | + public static boolean isValidTime(String inTime){ | |
| 160 | + boolean validFlag = true; | |
| 161 | + int hour = substringTime(inTime, 0, 2); | |
| 162 | + int minute = substringTime(inTime, 2, 4); | |
| 163 | + int second = substringTime(inTime, 4, 6); | |
| 164 | + if(hour < 0 || hour > 23) | |
| 165 | + validFlag = false; | |
| 166 | + if(minute < 0 || minute > 59) | |
| 167 | + validFlag = false; | |
| 168 | + if(second < 0 || second > 59) | |
| 169 | + validFlag = false; | |
| 170 | + return validFlag; | |
| 171 | + } | |
| 172 | + // 예약일이 하루전인지 체크하는 함수 | |
| 173 | + // 앞의 날짜가 뒤의 날짜보다 이전이면: true / 그렇지않으면 false) | |
| 174 | + public static boolean isBeforeDate(String srcDate, String destDate){ | |
| 175 | + boolean validFlag = false; | |
| 176 | + Calendar srcCal = getCalendar(srcDate); | |
| 177 | + Calendar destCal = getCalendar(destDate); | |
| 178 | + if(srcCal == null || destCal == null){ | |
| 179 | + validFlag = true; | |
| 180 | + }else{ | |
| 181 | + if(srcCal.before(destCal)) | |
| 182 | + validFlag = true; | |
| 183 | + } | |
| 184 | + | |
| 185 | + return validFlag; | |
| 186 | + } | |
| 187 | + | |
| 188 | + public static Calendar getCalendar(int hour){ | |
| 189 | + Calendar thisCal = null; | |
| 190 | + thisCal.add(Calendar.HOUR, hour); | |
| 191 | + return thisCal; | |
| 192 | + } | |
| 193 | + public static Calendar getCalendar(String inDate){ | |
| 194 | + Calendar thisCal = null; | |
| 195 | + inDate = doNumber(inDate); | |
| 196 | + if(inDate.length() == 14 && validDate(inDate)){ | |
| 197 | + String date = inDate.substring(0, 8); | |
| 198 | + String time = inDate.substring(8, 14); | |
| 199 | + int year = substringDate(date, 0, 4); | |
| 200 | + int month = substringDate(date, 4, 6); | |
| 201 | + int day = substringDate(date, 6, 8); | |
| 202 | + int hour = substringTime(time, 0, 2); | |
| 203 | + int minute = substringTime(time, 2, 4); | |
| 204 | + int second = substringTime(time, 4, 6); | |
| 205 | + thisCal = Calendar.getInstance(); | |
| 206 | + thisCal.set(year, (month-1), day, hour, minute, second); | |
| 207 | + } | |
| 208 | + return thisCal; | |
| 209 | + } | |
| 210 | + public static String currentTime(){ | |
| 211 | + Calendar currDate = Calendar.getInstance(); | |
| 212 | + SimpleDateFormat dateForm = new SimpleDateFormat("HHmmss"); | |
| 213 | + return dateForm.format(currDate.getTime()); | |
| 214 | + } | |
| 215 | + public static String getLastDate(String year, String month, String day){ | |
| 216 | + String dateStr = null; | |
| 217 | + String lastDay = null; | |
| 218 | + Calendar currDate = Calendar.getInstance(); | |
| 219 | + currDate.set(Integer.parseInt(year),Integer.parseInt(month)-1,Integer.parseInt(day)); | |
| 220 | + lastDay = Integer.toString(currDate.getActualMaximum(Calendar.DAY_OF_MONTH)); | |
| 221 | + dateStr = year+month+lastDay; | |
| 222 | + return dateStr; | |
| 223 | + } | |
| 224 | + public static String currentDay(){ | |
| 225 | + Calendar currDate = Calendar.getInstance(); | |
| 226 | + SimpleDateFormat dateForm = new SimpleDateFormat("yyyyMMdd"); | |
| 227 | + return dateForm.format(currDate.getTime()); | |
| 228 | + } | |
| 229 | + public static String currentDate(){ | |
| 230 | + Calendar currDate = Calendar.getInstance(); | |
| 231 | + SimpleDateFormat dateForm = new SimpleDateFormat("yyyyMMddHHmmss"); | |
| 232 | + return dateForm.format(currDate.getTime()); | |
| 233 | + } | |
| 234 | + public static String currentLogDate(){ | |
| 235 | + Calendar currDate = Calendar.getInstance(); | |
| 236 | + SimpleDateFormat dateForm = new SimpleDateFormat("[yyyy-MM-dd][HH:mm:ss]"); | |
| 237 | + return dateForm.format(currDate.getTime()); | |
| 238 | + } | |
| 239 | + // "yyyyMMddHHmmss" 의 조합 | |
| 240 | + public static String formatDate(String format){ | |
| 241 | + Calendar currDate = Calendar.getInstance(); | |
| 242 | + SimpleDateFormat dateForm = new SimpleDateFormat(format); | |
| 243 | + return dateForm.format(currDate.getTime()); | |
| 244 | + } | |
| 245 | + public static String getTargetDate(int term){ | |
| 246 | + Calendar currDate = Calendar.getInstance(); | |
| 247 | + currDate.add(Calendar.HOUR, term); | |
| 248 | + SimpleDateFormat dateForm = new SimpleDateFormat("yyyyMMddHHmmss"); | |
| 249 | + return dateForm.format(currDate.getTime()); | |
| 250 | + } | |
| 251 | + public static String getTargetDay(int term){ | |
| 252 | + Calendar currDate = Calendar.getInstance(); | |
| 253 | + currDate.add(Calendar.HOUR, term); | |
| 254 | + SimpleDateFormat dateForm = new SimpleDateFormat("yyyyMMdd"); | |
| 255 | + return dateForm.format(currDate.getTime()); | |
| 256 | + } | |
| 257 | + public static Date parseDate14(String sDay){ | |
| 258 | + Date date = null; | |
| 259 | + if(sDay != null || sDay.length() == 14){ | |
| 260 | + SimpleDateFormat dateForm = new SimpleDateFormat(DATE_FORMAT_14); | |
| 261 | + try { | |
| 262 | + date = dateForm.parse(sDay); | |
| 263 | + } catch (ParseException e) { | |
| 264 | + | |
| 265 | + } | |
| 266 | + } | |
| 267 | + | |
| 268 | + return date; | |
| 269 | + } | |
| 270 | + public static int validTimeOut(String sDay){ | |
| 271 | + int interval = 100000; | |
| 272 | + Date fromDate = parseDate14(sDay); | |
| 273 | + Date toDate = parseDate14(currentDate()); | |
| 274 | + if(fromDate != null && toDate != null){ | |
| 275 | + long duration = toDate.getTime() - fromDate.getTime(); | |
| 276 | + interval = (int)(duration / TIMEOUT_DIVIDE); | |
| 277 | + } | |
| 278 | + | |
| 279 | + return interval; | |
| 280 | + } | |
| 281 | + // 숫자만 반환하는 함수 | |
| 282 | + public static String doNumber(String spell){ | |
| 283 | + String phoneNumber = ""; | |
| 284 | + if(spell == null){ | |
| 285 | + return phoneNumber; | |
| 286 | + } | |
| 287 | + spell = spell.trim(); | |
| 288 | + int spell_Length = spell.length(); | |
| 289 | + if(spell_Length < 1){ | |
| 290 | + return phoneNumber; | |
| 291 | + } | |
| 292 | + for(int i=0;i<spell_Length;i++){ | |
| 293 | + char eachChar = spell.charAt(i); | |
| 294 | + if( 0x30 <= eachChar && eachChar <= 0x39 ){ | |
| 295 | + phoneNumber += eachChar; | |
| 296 | + } | |
| 297 | + } | |
| 298 | + return phoneNumber; | |
| 299 | + } | |
| 300 | + // 소수점 뒤에 해당하는 자리만큼 자르기 | |
| 301 | + public static String cutFloatNumber(String srcNum, int digit){ | |
| 302 | + String headNum = ""; | |
| 303 | + String tailNum = ""; | |
| 304 | + String retNum = ""; | |
| 305 | + | |
| 306 | + if(!(srcNum == null || srcNum.trim().equals(""))){ | |
| 307 | + srcNum = srcNum.trim(); | |
| 308 | + int index = srcNum.indexOf("."); | |
| 309 | + // 소수점 위치가 0보다 큰경우만 처리 | |
| 310 | + if(index > 0){ | |
| 311 | + headNum = srcNum.substring(0, index); | |
| 312 | + tailNum = srcNum.substring((index + 1), srcNum.length()); | |
| 313 | + | |
| 314 | + if(tailNum.length() == 0){ | |
| 315 | + tailNum = "0"; | |
| 316 | + } | |
| 317 | + if(tailNum.length() > digit){ | |
| 318 | + tailNum = tailNum.substring(0, digit); | |
| 319 | + } | |
| 320 | + retNum = headNum + "." + tailNum; | |
| 321 | + } | |
| 322 | + } | |
| 323 | + | |
| 324 | + return retNum; | |
| 325 | + } | |
| 326 | + // 수신번호 체크하기 | |
| 327 | + public static boolean CheckPhone(String src) { | |
| 328 | + if(src == null || src.trim().length() < 10) { | |
| 329 | + return false; | |
| 330 | + } | |
| 331 | + | |
| 332 | + if(!src.startsWith("0")) { | |
| 333 | + return false; | |
| 334 | + } | |
| 335 | + | |
| 336 | + return true; | |
| 337 | + } | |
| 338 | + // 문자열 공백 제거 | |
| 339 | + public static String trim(String obj) { | |
| 340 | + return StringUtil.trim(obj); | |
| 341 | + } | |
| 342 | + /** | |
| 343 | + * @param args | |
| 344 | + */ | |
| 345 | + public static void main(String[] args) { | |
| 346 | + // TODO Auto-generated method stub | |
| 347 | + //System.out.println(QueueUtil.cutFloatNumber("10.2", 2)); | |
| 348 | + //System.out.println(QueueUtil.isValidTime("20131207173555")); | |
| 349 | + | |
| 350 | + /*Calendar currDate = Calendar.getInstance(); | |
| 351 | + currDate.add(Calendar.HOUR, 0); // 1년후의 시간 | |
| 352 | + System.out.println("하루전 시간 : " + currDate.getTime());*/ | |
| 353 | + System.out.println(CommonUtil.isBeforeDate("20131206121212", "20131207121212")); | |
| 354 | + System.out.println("하루전 : " + CommonUtil.getTargetDay(-24)); | |
| 355 | + System.out.println(CommonUtil.currentLogDate()); | |
| 356 | + //Date date = MessageUtil.parseDate14("20141208125958"); | |
| 357 | + System.out.println("validTimeOut() : " + CommonUtil.validTimeOut("20141209154558")); | |
| 358 | + } | |
| 359 | +} |
+++ src/main/java/com/munjaon/client/util/FileUtil.java
... | ... | @@ -0,0 +1,37 @@ |
| 1 | +package com.munjaon.client.util; | |
| 2 | + | |
| 3 | +import java.io.File; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * 파일 관련 유틸리티 클래스 | |
| 7 | + * @author JDS | |
| 8 | + */ | |
| 9 | +public class FileUtil { | |
| 10 | + | |
| 11 | + public static boolean exists(String sFile) { | |
| 12 | + return exists( new File(sFile) ); | |
| 13 | + } | |
| 14 | + | |
| 15 | + public static boolean exists(File file) { | |
| 16 | + return file.exists(); | |
| 17 | + } | |
| 18 | + | |
| 19 | + public static boolean mkdirs(String sPath) { | |
| 20 | + return mkdirs(sPath, false); | |
| 21 | + } | |
| 22 | + | |
| 23 | + public static boolean mkdirs(String sPath, boolean isFilePath) { | |
| 24 | + File file = new File(sPath); | |
| 25 | + | |
| 26 | + if( isFilePath ) { | |
| 27 | + file = file.getParentFile(); | |
| 28 | + } | |
| 29 | + | |
| 30 | + if( file.exists() ) { | |
| 31 | + return true; | |
| 32 | + } | |
| 33 | + | |
| 34 | + return file.mkdirs(); | |
| 35 | + } | |
| 36 | + | |
| 37 | +} |
+++ src/main/java/com/munjaon/client/util/LogUtil.java
... | ... | @@ -0,0 +1,95 @@ |
| 1 | +package com.munjaon.client.util; | |
| 2 | + | |
| 3 | +import java.io.BufferedWriter; | |
| 4 | +import java.io.FileWriter; | |
| 5 | +import java.io.PrintWriter; | |
| 6 | +import java.text.SimpleDateFormat; | |
| 7 | +import java.util.Date; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 로깅 관련 유틸리티 클래스 | |
| 11 | + * @author JDS | |
| 12 | + */ | |
| 13 | +public class LogUtil { | |
| 14 | + | |
| 15 | + private final static String sTimeFormat = "[HH:mm:ss.SSS] "; | |
| 16 | + private final static String sDateFormat = "_yyyyMMdd"; | |
| 17 | + private final static String sFileExt = ".log"; | |
| 18 | + | |
| 19 | + private PrintWriter out; | |
| 20 | + private String sLogFile; | |
| 21 | + private String sDate; | |
| 22 | + | |
| 23 | + public LogUtil(String sLogFile) { | |
| 24 | + this.sLogFile = sLogFile; | |
| 25 | + | |
| 26 | + if ( sLogFile != null ) { | |
| 27 | + FileUtil.mkdirs(sLogFile, true); | |
| 28 | + } | |
| 29 | + } | |
| 30 | + | |
| 31 | + private void open() { | |
| 32 | + close(); | |
| 33 | + | |
| 34 | + if (sLogFile != null) { | |
| 35 | + try { | |
| 36 | + out = new PrintWriter( new BufferedWriter( new FileWriter(sLogFile + sDate + sFileExt, true) ), true ); | |
| 37 | + } catch(Exception e) { | |
| 38 | + out = null; | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 42 | + if( out == null ) { | |
| 43 | + out = new PrintWriter(System.out, true); | |
| 44 | + } | |
| 45 | + } | |
| 46 | + | |
| 47 | + public void close() { | |
| 48 | + if (sLogFile != null && out != null) { | |
| 49 | + try { | |
| 50 | + out.close(); | |
| 51 | + out = null; | |
| 52 | + } catch (Exception e) { | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + public static void log(String sFile, Object oLog) { | |
| 58 | + LogUtil logger = new LogUtil(sFile); | |
| 59 | + logger.log(oLog); | |
| 60 | + logger.close(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + public synchronized void log(Object oLog) { | |
| 64 | + SimpleDateFormat sdf = new SimpleDateFormat(); | |
| 65 | + Date date = new Date(); | |
| 66 | + | |
| 67 | + sdf.applyPattern(sDateFormat); | |
| 68 | + String sDates = sdf.format(date); | |
| 69 | + | |
| 70 | + sdf.applyPattern(sTimeFormat); | |
| 71 | + String sTime = sdf.format(date); | |
| 72 | + | |
| 73 | + try { | |
| 74 | + if (!sDates.equals(this.sDate)) { | |
| 75 | + this.sDate = sDates; | |
| 76 | + open(); | |
| 77 | + } | |
| 78 | + | |
| 79 | + if (oLog instanceof Exception) { | |
| 80 | + out.print( sTime ); | |
| 81 | + ((Exception)oLog).printStackTrace(out); | |
| 82 | + | |
| 83 | + if (sLogFile == null) { | |
| 84 | + ((Exception)oLog).printStackTrace(); | |
| 85 | + } | |
| 86 | + } else { | |
| 87 | + out.println( sTime + oLog ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + out.flush(); | |
| 91 | + } catch ( Exception e ) { | |
| 92 | + close(); | |
| 93 | + } | |
| 94 | + } | |
| 95 | +} |
+++ src/main/java/com/munjaon/client/util/ServiceUtil.java
... | ... | @@ -0,0 +1,32 @@ |
| 1 | +package com.munjaon.client.util; | |
| 2 | + | |
| 3 | +public final class ServiceUtil { | |
| 4 | + /** DB Commit Check Counter */ | |
| 5 | + public static final int COMMIT_COUNT = 30; | |
| 6 | + | |
| 7 | + public static String[] getServiceNames(String[] serviceNames) { | |
| 8 | + if (serviceNames == null) return null; | |
| 9 | + for (int i = 0; i < serviceNames.length; i++) { | |
| 10 | + serviceNames[i] = serviceNames[i].trim(); | |
| 11 | + } | |
| 12 | + | |
| 13 | + return serviceNames; | |
| 14 | + } | |
| 15 | + | |
| 16 | + public static boolean isDuplicate(String[] serviceNames) { | |
| 17 | + if (serviceNames == null) return false; | |
| 18 | + | |
| 19 | + boolean duplicate = false; | |
| 20 | + for (int i = 0; i < serviceNames.length; i++) { | |
| 21 | + for (int j = (i + 1); j < serviceNames.length; j++) { | |
| 22 | + if (serviceNames[i].equals(serviceNames[j])) { | |
| 23 | + duplicate = true; | |
| 24 | + break; | |
| 25 | + } | |
| 26 | + } | |
| 27 | + if (duplicate) break; | |
| 28 | + } | |
| 29 | + | |
| 30 | + return duplicate; | |
| 31 | + } | |
| 32 | +} |
+++ src/main/java/com/munjaon/client/util/StringUtil.java
... | ... | @@ -0,0 +1,167 @@ |
| 1 | +/* | |
| 2 | + * To change this template, choose Tools | Templates | |
| 3 | + * and open the template in the editor. | |
| 4 | + */ | |
| 5 | + | |
| 6 | +package com.munjaon.client.util; | |
| 7 | + | |
| 8 | +import java.text.DecimalFormat; | |
| 9 | +import java.util.ArrayList; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * 문자열 관련 유틸리티 클래스 | |
| 13 | + * @author JDS | |
| 14 | + */ | |
| 15 | +public final class StringUtil { | |
| 16 | + | |
| 17 | + public static final String SystemEncoding = getSystemEncoding(); | |
| 18 | + | |
| 19 | + public static String getSystemEncoding() { | |
| 20 | + return (new java.io.OutputStreamWriter(System.out)).getEncoding(); | |
| 21 | + } | |
| 22 | + | |
| 23 | + public static String trim(byte[] obj) { | |
| 24 | + if( obj == null || obj.length == 0 ) { | |
| 25 | + return ""; | |
| 26 | + } | |
| 27 | + | |
| 28 | + return (new String(obj)).trim(); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public static String trim(Object obj) { | |
| 32 | + return trim(obj, ""); | |
| 33 | + } | |
| 34 | + | |
| 35 | + public static String trim(Object obj, String dflt) { | |
| 36 | + if( obj == null ) { | |
| 37 | + return dflt; | |
| 38 | + } | |
| 39 | + | |
| 40 | + return ((String)obj).trim(); | |
| 41 | + } | |
| 42 | + | |
| 43 | + public static String ltrim(byte[] obj) { | |
| 44 | + return ltrim(new String(obj)); | |
| 45 | + } | |
| 46 | + | |
| 47 | + public static String ltrim(String obj) { | |
| 48 | + return ltrim(obj.toCharArray()); | |
| 49 | + } | |
| 50 | + | |
| 51 | + public static String ltrim(char[] obj) { | |
| 52 | + int len = obj.length; | |
| 53 | + int idx = 0; | |
| 54 | + | |
| 55 | + while( idx < len && obj[idx] <= ' ' ) { | |
| 56 | + idx++; | |
| 57 | + } | |
| 58 | + | |
| 59 | + return new String(obj, idx, len-idx); | |
| 60 | + } | |
| 61 | + | |
| 62 | + public static String rtrim(byte[] obj) { | |
| 63 | + return rtrim(new String(obj)); | |
| 64 | + } | |
| 65 | + | |
| 66 | + public static String rtrim(String obj) { | |
| 67 | + return rtrim(obj.toCharArray()); | |
| 68 | + } | |
| 69 | + | |
| 70 | + public static String rtrim(char[] obj) { | |
| 71 | + int len = obj.length; | |
| 72 | + int idx = len-1; | |
| 73 | + | |
| 74 | + while( idx >= 0 && obj[idx] <= ' ' ) { | |
| 75 | + idx--; | |
| 76 | + } | |
| 77 | + | |
| 78 | + return new String(obj, 0, idx+1); | |
| 79 | + } | |
| 80 | + | |
| 81 | + public static String replaceAll(String src, String from, String to) { | |
| 82 | + StringBuilder sbuf = new StringBuilder(); | |
| 83 | + | |
| 84 | + int len = from.length(); | |
| 85 | + int idx = 0; | |
| 86 | + int stx = 0; | |
| 87 | + | |
| 88 | + while( (idx=src.indexOf(from, stx)) > -1 ) { | |
| 89 | + sbuf.append(src.substring(stx, idx)); | |
| 90 | + sbuf.append(to); | |
| 91 | + stx=idx+len; | |
| 92 | + } | |
| 93 | + | |
| 94 | + sbuf.append(src.substring(stx)); | |
| 95 | + | |
| 96 | + return sbuf.toString(); | |
| 97 | + } | |
| 98 | + | |
| 99 | + public static String[] split(String sSrc, String sDelim) { | |
| 100 | + ArrayList aList = new ArrayList(); | |
| 101 | + | |
| 102 | + String sTmp; | |
| 103 | + int len = sDelim.length(); | |
| 104 | + int idx = 0; | |
| 105 | + int stx = 0; | |
| 106 | + | |
| 107 | + while( (idx=sSrc.indexOf(sDelim, stx)) > -1 ) { | |
| 108 | + sTmp = sSrc.substring(stx, idx); | |
| 109 | + aList.add(sTmp); | |
| 110 | + stx=idx+len; | |
| 111 | + } | |
| 112 | + | |
| 113 | + if( stx <= sSrc.length() ) { | |
| 114 | + aList.add(sSrc.substring(stx)); | |
| 115 | + } | |
| 116 | + | |
| 117 | + String[] sRet = new String[aList.size()]; | |
| 118 | + | |
| 119 | + for( int i=0; i<aList.size(); i++ ) { | |
| 120 | + sRet[i] = (String) aList.get(i); | |
| 121 | + } | |
| 122 | + | |
| 123 | + return sRet; | |
| 124 | + } | |
| 125 | + | |
| 126 | + public static String substring(String obj, int idx, int length) { | |
| 127 | + if( obj.getBytes().length <= idx ) { | |
| 128 | + return ""; | |
| 129 | + } | |
| 130 | + | |
| 131 | + if( obj.getBytes().length <= length ) { | |
| 132 | + return obj; | |
| 133 | + } | |
| 134 | + | |
| 135 | + int totallen=0; | |
| 136 | + int i=idx; | |
| 137 | + for( i=idx; i<obj.length(); i++ ) { | |
| 138 | + totallen += obj.substring(i, i+1).getBytes().length; | |
| 139 | + | |
| 140 | + if( length < totallen ) { | |
| 141 | + break; | |
| 142 | + } | |
| 143 | + } | |
| 144 | + | |
| 145 | + return obj.substring(idx, i); | |
| 146 | + } | |
| 147 | + | |
| 148 | + public static String substring(String obj, int length) { | |
| 149 | + return substring(obj, 0, length); | |
| 150 | + } | |
| 151 | + | |
| 152 | + public static String numFilter(String src) { | |
| 153 | + return src.replaceAll("[^0-9]", ""); | |
| 154 | + } | |
| 155 | + | |
| 156 | + public static String toNumberFormat(Object obj, String fmt) { | |
| 157 | + DecimalFormat df = new DecimalFormat(fmt); | |
| 158 | + return df.format(obj); | |
| 159 | + } | |
| 160 | + | |
| 161 | + public static String pad0(String str, int size) { | |
| 162 | + char[] zeros = new char[size - str.length()]; | |
| 163 | + for (int i = 0; i < zeros.length; i++) | |
| 164 | + zeros[i] = '0'; | |
| 165 | + return new String(zeros) + str; | |
| 166 | + } | |
| 167 | +} |
+++ src/main/java/com/munjaon/client/util/XmlUtil.java
... | ... | @@ -0,0 +1,125 @@ |
| 1 | +package com.munjaon.client.util; | |
| 2 | + | |
| 3 | +import org.jdom2.Document; | |
| 4 | +import org.jdom2.Element; | |
| 5 | +import org.jdom2.JDOMException; | |
| 6 | +import org.jdom2.input.SAXBuilder; | |
| 7 | +import org.jdom2.output.Format; | |
| 8 | +import org.jdom2.output.XMLOutputter; | |
| 9 | + | |
| 10 | +import java.io.File; | |
| 11 | +import java.io.FileOutputStream; | |
| 12 | +import java.io.IOException; | |
| 13 | +import java.io.StringReader; | |
| 14 | +import java.util.List; | |
| 15 | + | |
| 16 | +public class XmlUtil { | |
| 17 | +// private static Document getDOMParsedDocument(final String fileName) { | |
| 18 | +// Document document = null; | |
| 19 | +// try { | |
| 20 | +// | |
| 21 | +// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
| 22 | +// //If want to make namespace aware. | |
| 23 | +// //factory.setNamespaceAware(true); | |
| 24 | +// DocumentBuilder documentBuilder = factory.newDocumentBuilder(); | |
| 25 | +// document = documentBuilder.parse(new File("C:\\Docs\\JDS\\ITN\\MMS01Header.xml")); | |
| 26 | +// | |
| 27 | +// NodeList nodeList = document.getDocumentElement().getChildNodes(); | |
| 28 | +// for (int i = 0; i < nodeList.getLength(); i++) { | |
| 29 | +// Node node = nodeList.item(i); | |
| 30 | +// if (node.getNodeType() == Node.ELEMENT_NODE) { | |
| 31 | +// Element elem = (Element) node; | |
| 32 | +// System.out.println("createDate : " + elem.getAttribute("createDate")); | |
| 33 | +// System.out.println("getTagName : " + elem.getTagName()); | |
| 34 | +// System.out.println("getNodeName : " + elem.getNodeName()); | |
| 35 | +// System.out.println("getTextContent : " + elem.getTextContent()); | |
| 36 | +//// String createDate = elem.getElementsByTagName("createDate").item(0).getChildNodes().item(0).getNodeValue(); | |
| 37 | +//// System.out.println("createDate : " + createDate); | |
| 38 | +//// String PopCounter = elem.getElementsByTagName("PopCounter").item(0).getChildNodes().item(0).getNodeValue(); | |
| 39 | +//// System.out.println("PopCounter : " + PopCounter); | |
| 40 | +//// Double salary = Double.parseDouble(elem.getElementsByTagName("salary").item(0).getChildNodes().item(0).getNodeValue()); | |
| 41 | +// } | |
| 42 | +// } | |
| 43 | +// } | |
| 44 | +// catch (IOException | SAXException | ParserConfigurationException e) { | |
| 45 | +// e.printStackTrace(); | |
| 46 | +// } | |
| 47 | +// return document; | |
| 48 | +// } | |
| 49 | + | |
| 50 | + private static Document getSaxParsedDocument(final String fileName) { | |
| 51 | + Document document = null; | |
| 52 | + | |
| 53 | + String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
| 54 | + + " <catalog>\r\n" | |
| 55 | + + " <book id=\"bk101\">" | |
| 56 | + + " <author>Gambardella, Matthew</author> " | |
| 57 | + + "<title>XML Developer's Guide</title>" | |
| 58 | + + " <genre>Computer</genre>" | |
| 59 | + + " <price>44.95</price> " | |
| 60 | + + "<publish_date>2000-10-01</publish_date> " | |
| 61 | + + "<description>An in-depth look at creating applications with XML.</description> " | |
| 62 | + + "</book>" | |
| 63 | + + " <book id=\"bk102\">" | |
| 64 | + + " <author>Ralls, Kim</author>" | |
| 65 | + + " <title>Midnight Rain</title>" | |
| 66 | + + " <genre>Fantasy</genre>" | |
| 67 | + + " <price>5.95</price>" | |
| 68 | + + " <publish_date>2000-12-16</publish_date>" | |
| 69 | + + " <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>" | |
| 70 | + + " </book> \r\n" | |
| 71 | + + "</catalog>\r\n"; | |
| 72 | + try { | |
| 73 | + | |
| 74 | + SAXBuilder sax = new SAXBuilder(); | |
| 75 | + // String that contains XML | |
| 76 | + Document doc = (Document) sax.build(new File("C:\\Docs\\JDS\\ITN\\MMS01Header.xml")); | |
| 77 | +// org.jdom2.Document doc = sax.build(new StringReader(xml)); | |
| 78 | + | |
| 79 | + Element rootNode = doc.getRootElement(); | |
| 80 | + List<Element> bookElements = rootNode.getChildren(); | |
| 81 | + System.out.println("bookElements: " + bookElements); | |
| 82 | + for(Element bookElement : bookElements){ | |
| 83 | + String name = bookElement.getName(); | |
| 84 | + String value = bookElement.getValue(); | |
| 85 | + System.out.println(name + " : " + value); | |
| 86 | + } | |
| 87 | + | |
| 88 | + } catch (IOException | JDOMException e) { | |
| 89 | + e.printStackTrace(); | |
| 90 | + } | |
| 91 | + return document; | |
| 92 | + } | |
| 93 | + | |
| 94 | + private static void writeSimpleXml() throws JDOMException, IOException { | |
| 95 | + | |
| 96 | + String xml = "<root><child id=\"100\">mkyong</child></root>"; | |
| 97 | + SAXBuilder sb = new SAXBuilder(); | |
| 98 | + Document doc = sb.build(new StringReader(xml)); | |
| 99 | + | |
| 100 | + | |
| 101 | + Document docFile = new Document(); | |
| 102 | + | |
| 103 | + Element rootElement = new Element("ReadQueue"); | |
| 104 | + rootElement.addContent(new Element("createDate").setText("20240527")); | |
| 105 | + rootElement.addContent(new Element("PopCounter").setText(Integer.toString(0))); | |
| 106 | + | |
| 107 | + docFile.setRootElement(rootElement); | |
| 108 | + | |
| 109 | + // default in compact mode | |
| 110 | + // XMLOutputter xmlOutputter = new XMLOutputter(); | |
| 111 | + | |
| 112 | + // pretty print format | |
| 113 | + XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat()); | |
| 114 | + | |
| 115 | + // output to console | |
| 116 | + FileOutputStream fileOutputStream = new FileOutputStream("C:\\Docs\\JDS\\ITN\\file.xml"); | |
| 117 | + xmlOutputter.output(docFile, fileOutputStream); | |
| 118 | + | |
| 119 | + } | |
| 120 | + public static void main(String[] args) throws IOException, JDOMException { | |
| 121 | +// XmlUtil.getDOMParsedDocument("C:\\Docs\\JDS\\ITN\\MMS01Header.xml"); | |
| 122 | + XmlUtil.getSaxParsedDocument("C:\\Docs\\JDS\\ITN\\MMS01Header.xml"); | |
| 123 | + XmlUtil.writeSimpleXml(); | |
| 124 | + } | |
| 125 | +} |
--- src/main/resources/sqlmap/mariadb/mariadb_sql.xml
+++ src/main/resources/sqlmap/mariadb/mariadb_sql.xml
... | ... | @@ -7,18 +7,25 @@ |
| 7 | 7 |
</select> |
| 8 | 8 |
<select id="checkTableForMessage" resultType="int"> |
| 9 | 9 |
/* MariaDBMapper.checkTableForMessage */ |
| 10 |
- SELECT 1 FROM MUNJAON_MSG WHERE 1=1 LIMIT 1 |
|
| 10 |
+-- SELECT 1 FROM MUNJAON_MSG WHERE 1=1 LIMIT 1 |
|
| 11 |
+ SELECT COUNT(*) |
|
| 12 |
+ FROM information_schema.tables |
|
| 13 |
+ WHERE TABLE_NAME = 'MUNJAON_MSG' |
|
| 11 | 14 |
</select> |
| 12 | 15 |
<select id="checkTableForLog" resultType="int"> |
| 13 | 16 |
/* MariaDBMapper.checkTableForLog */ |
| 14 |
- SELECT 1 FROM MUNJAON_MSG_LOG WHERE 1=1 LIMIT 1 |
|
| 17 |
+ SELECT COUNT(*) |
|
| 18 |
+ FROM information_schema.tables |
|
| 19 |
+ WHERE TABLE_NAME = 'MUNJAON_MSG_LOG' |
|
| 20 |
+-- SELECT 1 FROM MUNJAON_MSG_LOG WHERE 1=1 LIMIT 1 |
|
| 15 | 21 |
</select> |
| 16 | 22 |
<select id="selectToDeliver" resultType="com.munjaon.client.model.MunjaonMsg"> |
| 17 | 23 |
/* MariaDBMapper.selectToDeliver */ |
| 18 | 24 |
SELECT |
| 19 | 25 |
MSG_ID |
| 20 | 26 |
, MSG_TYPE |
| 21 |
- , DATE_FORMAT(REQUEST_DATE,'%Y%m%d%H%i%s') REQUEST_DATE |
|
| 27 |
+ , DATE_FORMAT(REQUEST_DATE,'%Y%m%d%H%i%s') RESERVE_DATE |
|
| 28 |
+ , DATE_FORMAT(NOW(),'%Y%m%d%H%i%s') REQUEST_DATE |
|
| 22 | 29 |
, RECV_PHONE |
| 23 | 30 |
, SEND_PHONE |
| 24 | 31 |
, SUBJECT |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?