Commit 73f5043b by TongZuu

update

parent cffe57d7
...@@ -18,5 +18,7 @@ ...@@ -18,5 +18,7 @@
<classpathentry kind="lib" path="D:/EnterprisePT/jar/mail.jar"/> <classpathentry kind="lib" path="D:/EnterprisePT/jar/mail.jar"/>
<classpathentry kind="lib" path="D:/EnterprisePT/Programmer/myHRAppServer/wildfly-10.0.0.tpbs/standalone/deployments/TPBS.ear/hrAppWeb.war/WEB-INF/lib/gson.jar"/> <classpathentry kind="lib" path="D:/EnterprisePT/Programmer/myHRAppServer/wildfly-10.0.0.tpbs/standalone/deployments/TPBS.ear/hrAppWeb.war/WEB-INF/lib/gson.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/csc-websocket-api"/> <classpathentry combineaccessrules="false" kind="src" path="/csc-websocket-api"/>
<classpathentry kind="lib" path="D:/EnterprisePT/jar/commons/commons-lang-2.3.jar" sourcepath="D:/EnterprisePT/jar/commons/src/commons-lang-2.3-sources.jar"/>
<classpathentry kind="lib" path="D:/EnterprisePT/Programmer/myHRAppServer/wildfly-10.0.0.tpbs/standalone/deployments/TPBS.ear/hrAppWeb.war/WEB-INF/lib/CscServlet.jar"/>
<classpathentry kind="output" path=""/> <classpathentry kind="output" path=""/>
</classpath> </classpath>
...@@ -6,3 +6,6 @@ ...@@ -6,3 +6,6 @@
/CommandProcess.class /CommandProcess.class
/CommandRecord.class /CommandRecord.class
/CscCommand.class /CscCommand.class
/CommandNoResult.class
/Command004.class
/CommandFileResult.class
package com.csc.library.command;
public class Command003 {
}
package com.csc.library.command; package com.csc.library.command;
import java.util.Collections;
import java.util.Map;
import com.csc.library.factory.ResponseFactory; import com.csc.library.factory.ResponseFactory;
import com.csc.library.properties.CommandState;
import com.csc.library.properties.CscSession; import com.csc.library.properties.CscSession;
import com.csc.library.request.CMDMode; import com.csc.library.request.CMDMode;
import com.csc.library.request.CommandState;
import com.csc.library.request.RequestWebsocket; import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseStatus; import com.csc.library.response.ResponseStatus;
import com.csc.library.utilities.UProfile; import com.csc.library.utilities.UProfile;
...@@ -12,7 +15,8 @@ public abstract class CommandBase<T> implements CscCommand<T>{ ...@@ -12,7 +15,8 @@ public abstract class CommandBase<T> implements CscCommand<T>{
protected CMDMode commandMode = null; protected CMDMode commandMode = null;
private CscSession session = null; private CscSession session = null;
private RequestWebsocket request = null; private RequestWebsocket request = null;
protected CommandState statusCode = CommandState.Created; private Map<String,String> paramConfig = null;
private CommandState commandState = CommandState.noAction;
public final void setSession(CscSession session){ public final void setSession(CscSession session){
this.session = session; this.session = session;
...@@ -30,19 +34,40 @@ public abstract class CommandBase<T> implements CscCommand<T>{ ...@@ -30,19 +34,40 @@ public abstract class CommandBase<T> implements CscCommand<T>{
return this.request; return this.request;
} }
public UProfile getUProfile(){ public final UProfile getUProfile(){
return this.session.getUProfile(); return this.session.getUProfile();
} }
public CommandState getStatusCode(){ public final void setParamConfig(Map<String, String> param){
return this.statusCode; this.paramConfig = param;
}
public Map<String, String> getParamConfig(){
return this.paramConfig;
}
public final void setCommandState(CommandState state){
this.commandState = state;
}
public CommandState getCommandState(){
return this.commandState;
} }
public final void sendMessage(String message){ public final void sendMessage(String message){
ResponseFactory.getInstance().sendResponseWebsocket( ResponseFactory.getInstance().sendResponseWebsocket(
this.getSession(), this.getSession(),
this.getRequest(), this.getRequest(),
this.commandState,
ResponseStatus.SendMessage, ResponseStatus.SendMessage,
message); message);
} }
public final void sendMessage(String message , CommandState stateComm ){
this.setCommandState(stateComm);
ResponseFactory.getInstance().sendResponseWebsocket(
this.getSession(),
this.getRequest(),
this.commandState,
ResponseStatus.SendMessage ,
message);
}
} }
package com.csc.library.command;
import com.csc.library.properties.JsonFile;
import com.csc.library.response.ResponseData;
public abstract class CommandFileResult<T> extends CommandBase<T>{
private JsonFile file = null;
@Override
final public void setResult(ResponseData res) {
res.setFile(this.file);
}
final protected void setFile(JsonFile file){
this.file = file;
}
}
...@@ -2,7 +2,7 @@ package com.csc.library.command; ...@@ -2,7 +2,7 @@ package com.csc.library.command;
import com.csc.library.response.ResponseData; import com.csc.library.response.ResponseData;
public abstract class CommandProcess<T> extends CommandBase<T>{ public abstract class CommandNoResult<T> extends CommandBase<T>{
@Override @Override
public final void setResult(ResponseData res) { public final void setResult(ResponseData res) {
} }
......
package com.csc.library.command; package com.csc.library.command;
import com.csc.library.properties.CommandState; import com.csc.library.properties.CommandStateTemp;
import com.csc.library.request.CommandState;
import com.csc.library.request.RequestWebsocket; import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseData; import com.csc.library.response.ResponseData;
public interface CscCommand<T> { public interface CscCommand<T> {
public CommandState getStatusCode();
public void setRequest(RequestWebsocket req); public void setRequest(RequestWebsocket req);
public void execute(T param) throws Exception; public void execute(T param) throws Exception;
public void setResult(ResponseData res); public void setResult(ResponseData res);
public CommandState getCommandState();
} }
/Command001.class
/Command002.class
/Command003.class
/Command004.class
/Command005.class
/Command006.class
/Command007.class
/Command008.class
package com.csc.library.command; package com.csc.library.command.common;
import java.util.Iterator;
import java.util.Map;
import com.csc.library.command.CommandRecord;
import com.csc.library.command.util.CommonParameterUtil; import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.request.CommandState;
import com.csc.library.request.CommonModeParameter; import com.csc.library.request.CommonModeParameter;
import com.csc.library.response.ResponseData; import com.csc.library.session.DbInquiry;
import com.csc.library.session.DbRecord; import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialRecord; import com.csc.library.session.InitialRecord;
...@@ -21,10 +20,10 @@ public class Command001 extends CommandRecord<CommonModeParameter>{ ...@@ -21,10 +20,10 @@ public class Command001 extends CommandRecord<CommonModeParameter>{
CommonParameterUtil.setKeyRecord(rec, param); CommonParameterUtil.setKeyRecord(rec, param);
if(rec.search()==1){ if(rec.search()==1){
this.sendMessage("Command001 Search Found Data"); this.sendMessage("Command001 : Search Record Success" , CommandState.success);
this.setRecord(rec); this.setRecord(rec);
}else{ }else{
this.sendMessage("Command001 Search Not Found Data"); this.sendMessage("Command001 : Search Record Fail : "+CommandState.fail);
} }
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
......
package com.csc.library.command; package com.csc.library.command.common;
import java.util.Iterator;
import com.csc.library.command.CommandRecord;
import com.csc.library.command.util.CommonParameterUtil; import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.request.CommandState;
import com.csc.library.request.CommonModeParameter; import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord; import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialRecord; import com.csc.library.session.InitialRecord;
...@@ -15,13 +15,26 @@ public class Command002 extends CommandRecord<CommonModeParameter>{ ...@@ -15,13 +15,26 @@ public class Command002 extends CommandRecord<CommonModeParameter>{
public void execute(CommonModeParameter param) throws Exception { public void execute(CommonModeParameter param) throws Exception {
try{ try{
DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName()); DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName());
if(param.getGroupField().equals("")){
CommonParameterUtil.setKeyRecord(rec, param); }else{
CommonParameterUtil.setJsonData(rec, param); rec.setGroupfield(param.getGroupField());
}
CommonParameterUtil.setKeyRecord(rec, param);
if(rec.search() == 0){
CommonParameterUtil.setJsonRecord(rec, param);
CommonParameterUtil.setKeyRecord(rec, param);
int saveStatus = rec.save(); int saveStatus = rec.save();
this.sendMessage("SaveRecord status : "+saveStatus); if(saveStatus == 1){
this.sendMessage("Command002 : Save Record Success" , CommandState.success);
this.setRecord(rec); this.setRecord(rec);
}else{
this.sendMessage("Command002 : Save Record Fail" , CommandState.fail);
}
}else{
this.sendMessage("Command002 : Save Record Fail because Data Record is found data in database." , CommandState.fail);
}
}catch(Exception e){ }catch(Exception e){
MyLog.error(this ,this.getUProfile(), e); MyLog.error(this ,this.getUProfile(), e);
this.sendMessage(e.getMessage()); this.sendMessage(e.getMessage());
......
package com.csc.library.command.common;
import com.csc.library.command.CommandNoResult;
import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.request.CommandState;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.MyLog;
/* Delete Record */
public class Command003 extends CommandNoResult<CommonModeParameter>{
@Override
public void execute(CommonModeParameter param) throws Exception {
try{
DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName());
CommonParameterUtil.setKeyRecord(rec, param);
if(rec.search() == 1){
int delStatus = rec.delete();
if(delStatus == 4){
this.sendMessage("Command003 : Delete Record Success" , CommandState.success);
}else{
this.sendMessage("Command003 : Delete Record Fail" , CommandState.fail);
}
}else{
this.sendMessage("Command002 : Save Record Fail because Data Record not found data in database." , CommandState.fail);
}
}catch(Exception e){
MyLog.error(this ,this.getUProfile() , e);
this.sendMessage(e.getMessage());
}
}
}
package com.csc.library.command.common;
import com.csc.library.command.CommandNoResult;
import com.csc.library.command.CommandRecord;
import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.request.CommandState;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.MyLog;
/* Update Record */
public class Command004 extends CommandNoResult<CommonModeParameter>{
@Override
public void execute(CommonModeParameter param) throws Exception {
try{
DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName());
CommonParameterUtil.setKeyRecord(rec, param);
if(rec.search()== 1){
CommonParameterUtil.setJsonRecord(rec, param);
CommonParameterUtil.setKeyRecord(rec, param);
int updateStatus = rec.save();
if(updateStatus == 2){
this.sendMessage("Command004 : Update Record Success" , CommandState.success);
}else{
this.sendMessage("Command004 : Update Record Fail" , CommandState.fail);
}
}else{
this.sendMessage("Command004 : Update Record Fail because Data Record not found data in database." , CommandState.fail);
}
}catch(Exception e){
MyLog.error(this , this.getUProfile() , e);
}
}
}
package com.csc.library.command.common;
import java.util.Vector;
import com.csc.library.command.CommandRecord;
import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.database.FieldManager;
import com.csc.library.request.CommandState;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord;
import com.csc.library.session.DbTable;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.MyLog;
/* Read Json Record */
public class Command005 extends CommandRecord<CommonModeParameter>{
@Override
public void execute(CommonModeParameter param) throws Exception {
try{
DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName());
rec.setColumn(param.getJsonField());
CommonParameterUtil.setKeyRecord(rec, param);
FieldManager fm = rec.getField(param.getJsonField());
String jsontable = (String)fm.getProperty().get("jsonrecord");
DbRecord recJson = new InitialRecord(this.getUProfile()).getDbRecord(jsontable);
String filter = CommonParameterUtil.getFilterKeyRecord(recJson, param);
rec.setJsonTable(param.getJsonField() , filter);
DbTable dt = rec.getJsonTable(param.getJsonField());
if(dt.recCount()>0){
dt.first();
for(;!dt.eof();dt.next()){
this.setRecord(dt.getCurrentRecordManager());
this.sendMessage("Command005 : Search Json Data is Found.", CommandState.success);
}
}else{
this.sendMessage("Command005 : Search Json Data is not Found.", CommandState.fail);
}
}catch(Exception e){
MyLog.error(this , this.getUProfile() ,e);
}
}
}
package com.csc.library.command.common;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import com.csc.library.command.CommandRecord;
import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.database.FieldManager;
import com.csc.library.request.CommandState;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord;
import com.csc.library.session.DbTable;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.MyLog;
/* save json record */
public class Command006 extends CommandRecord<CommonModeParameter> {
@Override
public void execute(CommonModeParameter param) throws Exception {
try{
DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName());
rec.setColumn(param.getJsonField());
CommonParameterUtil.setKeyRecord(rec, param);
FieldManager fm = rec.getField(param.getJsonField());
String jsontable = (String)fm.getProperty().get("jsonrecord");
DbRecord recJson = new InitialRecord(this.getUProfile()).getDbRecord(jsontable);
String filter = CommonParameterUtil.getFilterKeyRecord(recJson, param);
rec.setJsonTable(param.getJsonField() , filter);
DbTable dt = rec.getJsonTable(param.getJsonField());
if(dt.recCount()==0){
DbRecord db = dt.getNewRecord();
CommonParameterUtil.setJsonRecord(db, param);
CommonParameterUtil.setKeyRecord(rec, param);
db.setJsonParentField(fm);
db.setParentJsonField(rec);
db.setPreJsonFieldKey();
int saveStatus = db.save();
if(saveStatus==1){
this.setRecord(db);
this.sendMessage("Command006 : Save Json Record Success.", CommandState.success);
}else{
this.sendMessage("Command006 : Save Json Record Fail.", CommandState.fail);
}
}else{
this.sendMessage("Command006 : Search Json Record is not Found. cannot save Json Record", CommandState.fail);
}
}catch(Exception e){
MyLog.error(this ,this.getUProfile() , e);
}
}
}
package com.csc.library.command.common;
import java.util.Vector;
import com.csc.library.command.CommandNoResult;
import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.database.FieldManager;
import com.csc.library.request.CommandState;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord;
import com.csc.library.session.DbTable;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.MyLog;
/* Delete Json Record */
public class Command007 extends CommandNoResult<CommonModeParameter>{
@Override
public void execute(CommonModeParameter param) throws Exception {
try{
DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName());
rec.setColumn(param.getJsonField());
CommonParameterUtil.setKeyRecord(rec, param);
FieldManager fm = rec.getField(param.getJsonField());
String jsontable = (String)fm.getProperty().get("jsonrecord");
DbRecord recJson = new InitialRecord(this.getUProfile()).getDbRecord(jsontable);
Vector allKey = recJson.getAllKey();
StringBuilder filter = new StringBuilder("1=1");
for(int i = 0; i < allKey.size(); i++){
String key = allKey.get(i).toString();
if(param.getJsonKey().containsKey(key)){
filter.append(" and ").append(key).append("='").append(param.getJsonKey().get(key)).append("'");
}
}
rec.setJsonTable(param.getJsonField() , filter.toString());
DbTable dt = rec.getJsonTable(param.getJsonField());
if(dt.recCount() == 1){
int deleteStatus = dt.getCurrentRecordManager().delete();
if(deleteStatus == 4){
this.sendMessage("Command007 : Delete Json Record Success.", CommandState.success);
}else{
this.sendMessage("Command007 : Delete Json Record Fail.", CommandState.fail);
}
}else{
this.sendMessage("Command007 : Search Json Record is not Found. cannot Delete Json Record", CommandState.fail);
}
}catch(Exception e){
MyLog.error(this ,this.getUProfile() , e);
}
}
}
package com.csc.library.command.common;
import com.csc.library.command.CommandNoResult;
import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.database.FieldManager;
import com.csc.library.request.CommandState;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord;
import com.csc.library.session.DbTable;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.MyLog;
/* Update Json Record */
public class Command008 extends CommandNoResult<CommonModeParameter>{
@Override
public void execute(CommonModeParameter param) throws Exception {
try{
DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName());
rec.setColumn(param.getJsonField());
CommonParameterUtil.setKeyRecord(rec, param);
FieldManager fm = rec.getField(param.getJsonField());
String jsontable = (String)fm.getProperty().get("jsonrecord");
DbRecord recJson = new InitialRecord(this.getUProfile()).getDbRecord(jsontable);
String filter = CommonParameterUtil.getFilterKeyRecord(recJson, param);
rec.setJsonTable(param.getJsonField() , filter);
DbTable dt = rec.getJsonTable(param.getJsonField());
if(dt.recCount()==0){
DbRecord db = dt.getNewRecord();
CommonParameterUtil.setJsonRecord(db, param);
CommonParameterUtil.setKeyRecord(rec, param);
db.setJsonParentField(fm);
db.setParentJsonField(rec);
db.setPreJsonFieldKey();
int saveStatus = db.save();
if(saveStatus==1){
this.sendMessage("Command006 : Save Json Record Success.", CommandState.success);
}else{
this.sendMessage("Command006 : Save Json Record Fail.", CommandState.fail);
}
}else{
this.sendMessage("Command006 : Search Json Record is not Found. cannot save Json Record", CommandState.fail);
}
}catch(Exception e){
MyLog.error(this ,this.getUProfile() , e);
}
}
}
/CommandUpload.class
/UploadFtp.class
/DownloadFtp.class
package com.csc.library.command.ftp;
import java.io.File;
import com.csc.library.command.CommandFileResult;
import com.csc.library.factory.FileFtpFactory;
import com.csc.library.properties.JsonFile;
import com.csc.library.properties.JsonFileImpl;
import com.csc.library.request.CommandState;
public class DownloadFtp extends CommandFileResult<JsonFile>{
@Override
public void execute(JsonFile param) throws Exception {
File file = new File(param.getDirectory()+"/"+param.getFileName());
if(file.exists()){
this.setFile(FileFtpFactory.getInstance().createJsonFile(file));
this.sendMessage("DownloadFtp : Success" , CommandState.success);
}else{
this.sendMessage("DownloadFtp : Fail because file not found." , CommandState.fail);
}
}
}
package com.csc.library.command.ftp;
import java.io.File;
import com.csc.library.command.CommandNoResult;
import com.csc.library.properties.JsonFile;
import com.csc.library.properties.JsonFileImpl;
public class UploadFtp extends CommandNoResult<JsonFile>{
@Override
public void execute(JsonFile jsonFile) throws Exception {
if(jsonFile != null){
jsonFile.writeFile(jsonFile.getDirectory()+"/"+jsonFile.getFileName());
}
}
}
package com.csc.library.command.special;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.csc.library.command.CommandBase;
import com.csc.library.command.CommandLists;
import com.csc.library.database.DataSet;
import com.csc.library.fix.StaticVariable;
import com.csc.library.request.CommandState;
import com.csc.library.response.ResponseData;
import com.csc.library.session.InitialEnvironment;
import com.csc.library.utilities.MyLog;
public class CommandSpecialBase extends CommandLists<List<String>>{
public void execute(List<String> param) throws Exception {
try{
String sql = this.getSQLConfig();
for(int i = 0; i < param.size(); i++){
String regex = "{"+i+"}";
sql = StringUtils.replace(sql , regex , "'"+param.get(i)+"'");
}
DataSet ds = new DataSet();
ds.setUPofile(this.getUProfile());
ds.setDbName(this.getUProfile().getDbName());
ds.setDbType(new InitialEnvironment(this.getUProfile().getDbName()).getValue("datasource-dbtype"));
ds.setSql(sql);
ds.initConnection(this.getUProfile());
while(ds.next()){
Map<String,String> record = new HashMap<String,String>();
for(int i = 0; i < ds.getFieldList().size(); i++){
String field = (String)ds.getFieldList().get(i);
record.put(field, ds.getString(field));
}
this.putNewRecord(record);
this.setCommandState(CommandState.success);
}
}catch(Exception e){
MyLog.error(this ,this.getUProfile() , e);
}
}
public final String getSQLConfig(){
return this.getParamConfig().get(StaticVariable.CommandSpecialAttribute.SQL);
}
}
/CommonParameterUtil.class /CommonParameterUtil.class
/ReadXMLCommand.class
package com.csc.library.command.util; package com.csc.library.command.util;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Vector;
import com.csc.library.request.CommonModeParameter; import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord; import com.csc.library.session.DbRecord;
import com.csc.library.utilities.MyLog;
public class CommonParameterUtil { public class CommonParameterUtil {
public static void setKeyRecord(DbRecord rec , CommonModeParameter param) throws Exception { public static void setKeyRecord(DbRecord rec , CommonModeParameter param) throws Exception {
Map<String,String> m = param.getKey(); Map<String,String> m = param.getKey();
Iterator it = m.keySet().iterator(); CommonParameterUtil.setKey(rec, m);
}
public static void setJsonKeyRecord(DbRecord rec , CommonModeParameter param) throws Exception {
Map<String,String> m = param.getJsonKey();
CommonParameterUtil.setKey(rec, m);
}
private static void setKey(DbRecord rec , Map<String,String> keyMap) throws Exception {
Iterator it = keyMap.keySet().iterator();
while(it.hasNext()){ while(it.hasNext()){
String key = it.next().toString(); String key = it.next().toString();
rec.set(key, m.get(key)); if(rec.getAllKey().contains(key.toLowerCase())){
rec.set(key, keyMap.get(key));
}
} }
} }
public static void setJsonData(DbRecord rec , CommonModeParameter param) throws Exception { public static void setJsonRecord(DbRecord rec , CommonModeParameter param) throws Exception {
Map<String,String> mapJson = param.getJson(); Map<String,String> mapJson = param.getJson();
Iterator it = mapJson.keySet().iterator(); Iterator it = mapJson.keySet().iterator();
while(it.hasNext()){ while(it.hasNext()){
String key = it.next().toString(); String key = it.next().toString();
if(!rec.getAllKey().contains(key)){
rec.set(key, mapJson.get(key)); rec.set(key, mapJson.get(key));
} }
} }
public static String getFilterKeyRecord(DbRecord rec , CommonModeParameter param){
StringBuilder filter = new StringBuilder("1=1");
try{
Vector allKey = rec.getAllKey();
for(int i = 0; i < allKey.size(); i++){
String key = allKey.get(i).toString();
if(param.getJson().containsKey(key)){
filter.append(" and ").append(key).append("='").append(param.getJson().get(key)).append("'");
}
}
}catch(Exception e){
MyLog.error(rec.getUProfile(),e);
}
return filter.toString();
} }
} }
...@@ -5,3 +5,5 @@ ...@@ -5,3 +5,5 @@
/CMDModeCtrlCommand.class /CMDModeCtrlCommand.class
/StateController.class /StateController.class
/WebSocketCommandController.class /WebSocketCommandController.class
/CMDModeCtrlCommand$JsonFileCtrl.class
/CMDModeCtrlCommand$UploadCtrl.class
package com.csc.library.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.csc.library.command.CscCommand;
import com.csc.library.request.CMDMode;
import com.csc.library.request.RequestParameter;
import com.csc.library.request.RequestWebsocket;
public class CMDModeCtrlCommand {
private static CMDModeCtrlCommand instance;
private List<CMDModeCtrl> list = new ArrayList<CMDModeCtrl>();
//private Map<CMDMode , CMDModeCtrl> mode = new HashMap<CMDMode ,CMDModeCtrl>();
private CMDModeCtrlCommand() throws Exception{
list.set(CMDMode.common.getModeCode(), new CommonCtrl());
list.set(CMDMode.special.getModeCode(), new SpecialCtrl());
list.set(CMDMode.process.getModeCode(), new ProcessCtrl());
/*mode.put(CMDMode.common , new CommonCtrl());
mode.put(CMDMode.special , new SpecialCtrl());
mode.put(CMDMode.process , new ProcessCtrl());*/
if(CMDMode.values().length != list.size()){
throw new Exception("!!! CMDModeCtrlCommand :: Error CMDMode not match Controller !!!");
}
}
public static CMDModeCtrlCommand getInstance() throws Exception{
if(instance == null){
instance = new CMDModeCtrlCommand();
}
return instance;
}
public void execute(CscCommand command , RequestWebsocket req) throws Exception {
//mode.get(req.getCommandMode()).execute(command, req.getRequestParameter());
list.get(req.getCommandMode().getModeCode()).execute(command, req.getRequestParameter());
}
interface CMDModeCtrl{
public void execute(CscCommand command ,RequestParameter reqParam) throws Exception ;
}
class CommonCtrl implements CMDModeCtrl{
@Override
public void execute(CscCommand command , RequestParameter reqParam) throws Exception {
command.execute(reqParam.getCommonParameter());
}
}
class SpecialCtrl implements CMDModeCtrl{
@Override
public void execute(CscCommand command , RequestParameter reqParam) throws Exception {
command.execute(reqParam.getSpecialParameter());
}
}
class ProcessCtrl implements CMDModeCtrl{
@Override
public void execute(CscCommand command , RequestParameter reqParam) throws Exception {
command.execute(reqParam.getProcessParameter());
}
}
}
...@@ -3,15 +3,14 @@ package com.csc.library.controller; ...@@ -3,15 +3,14 @@ package com.csc.library.controller;
import com.csc.library.command.CscCommand; import com.csc.library.command.CscCommand;
import com.csc.library.factory.CommandFactory; import com.csc.library.factory.CommandFactory;
import com.csc.library.factory.PropertiesFactory; import com.csc.library.factory.PropertiesFactory;
import com.csc.library.factory.RequestFactory;
import com.csc.library.factory.ResponseFactory; import com.csc.library.factory.ResponseFactory;
import com.csc.library.factory.ThreadsPoolsFactory; import com.csc.library.factory.ThreadsPoolsFactory;
import com.csc.library.properties.CscSession; import com.csc.library.properties.CscSession;
import com.csc.library.request.CommandState;
import com.csc.library.request.RequestWebsocket; import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseStatus; import com.csc.library.response.ResponseStatus;
import com.csc.library.task.CommandTask; import com.csc.library.task.CommandTask;
import com.csc.library.task.TaskCommand; import com.csc.library.task.TaskCommand;
import com.csc.library.utilities.MyLog;
public class WebSocketCommandController { public class WebSocketCommandController {
private static WebSocketCommandController instance; private static WebSocketCommandController instance;
...@@ -20,7 +19,7 @@ public class WebSocketCommandController { ...@@ -20,7 +19,7 @@ public class WebSocketCommandController {
private PropertiesFactory propFact; private PropertiesFactory propFact;
private CommandFactory commandFact; private CommandFactory commandFact;
private ThreadsPoolsFactory threadsFact; private ThreadsPoolsFactory threadsFact;
private RequestFactory reqFact;
private WebSocketCommandController(){ private WebSocketCommandController(){
} }
...@@ -30,7 +29,6 @@ public class WebSocketCommandController { ...@@ -30,7 +29,6 @@ public class WebSocketCommandController {
if(instance == null){ if(instance == null){
instance = new WebSocketCommandController(); instance = new WebSocketCommandController();
instance.responseFact = ResponseFactory.getInstance(); instance.responseFact = ResponseFactory.getInstance();
instance.reqFact = RequestFactory.getInstance();
instance.commandFact = CommandFactory.getInstance(); instance.commandFact = CommandFactory.getInstance();
instance.threadsFact = ThreadsPoolsFactory.getInstance(); instance.threadsFact = ThreadsPoolsFactory.getInstance();
} }
...@@ -40,22 +38,15 @@ public class WebSocketCommandController { ...@@ -40,22 +38,15 @@ public class WebSocketCommandController {
return instance; return instance;
} }
public void executeCommand(CscSession session, String json){
try{
RequestWebsocket req = reqFact.createRequestWebSocket(json);
executeCommand(session, req);
}catch(Exception e){
MyLog.error(this , session.getUProfile() , e);
}
}
public void executeCommand(final CscSession session, final RequestWebsocket req){ public void executeCommand(final CscSession session, final RequestWebsocket req){
responseFact.sendResponseWebsocket(session, req, responseFact.sendResponseWebsocket(session, req,
CommandState.noAction,
ResponseStatus.Receive, ResponseStatus.Receive,
ResponseStatus.Receive.getDescription()); ResponseStatus.Receive.getDescription());
final CscCommand command = commandFact.getCommand(session , req); final CscCommand command = commandFact.getCommand(session , req);
CommandTask task = new TaskCommand(session , req , command); CommandTask task = new TaskCommand(session , req , command);
threadsFact.execute(task); threadsFact.execute(task);
} }
} }
...@@ -6,3 +6,9 @@ ...@@ -6,3 +6,9 @@
/SessionFactory.class /SessionFactory.class
/StateCommandFactory.class /StateCommandFactory.class
/ThreadsPoolsFactory.class /ThreadsPoolsFactory.class
/CommandFactory$CMDBuilder.class
/CommandFactory$CMDModeBuilder.class
/CommandFactory$CMDCommonBuilder.class
/CommandFactory$CMDSpecialBuilder.class
/CMDModeFactory.class
/ThreadsPoolsFactory$1.class
package com.csc.library.factory;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.csc.library.modecontroller.AbsModeManagement;
import com.csc.library.modecontroller.CMDModeManagement;
import com.csc.library.modecontroller.ManageCommonMode;
import com.csc.library.modecontroller.ManageProcessMode;
import com.csc.library.modecontroller.ManageSpecialMode;
import com.csc.library.modecontroller.ManageFtpMode;
import com.csc.library.request.CMDMode;
import com.csc.library.session.InitialEnvironment;
public class CMDModeFactory {
private static CMDModeFactory instance;
private Map<CMDMode , AbsModeManagement> mapMode = new HashMap<CMDMode,AbsModeManagement>();
private CMDModeFactory(){
init();
}
protected synchronized void init(){
if(instance == null){
mapMode.put(CMDMode.common, new ManageCommonMode());
mapMode.put(CMDMode.special, new ManageSpecialMode());
mapMode.put(CMDMode.process, new ManageProcessMode());
mapMode.put(CMDMode.ftp, new ManageFtpMode());
if(CMDMode.values().length == mapMode.size()){
mapMode = Collections.unmodifiableMap(mapMode);
String path = new InitialEnvironment().getValue("COMMAND_CONFIG-dir");
for(AbsModeManagement manager : mapMode.values()){
manager.load(path);
}
}
}
}
public static CMDModeFactory getInstance(){
if(instance == null){
instance = new CMDModeFactory();
}
return instance;
}
public CMDModeManagement.ModeCtrl getModeCtrl(CMDMode mode){
return this.mapMode.get(mode);
}
public CMDModeManagement.ModeBuilder getModeBuilder(CMDMode mode){
return this.mapMode.get(mode);
}
}
package com.csc.library.factory; package com.csc.library.factory;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List; import java.util.Map;
import java.util.Map.Entry;
import com.csc.library.command.CommandBase; import com.csc.library.command.CommandBase;
import com.csc.library.command.CscCommand; import com.csc.library.command.CscCommand;
import com.csc.library.modecontroller.CMDModeManagement;
import com.csc.library.properties.CscSession; import com.csc.library.properties.CscSession;
import com.csc.library.request.CMDMode; import com.csc.library.request.CMDMode;
import com.csc.library.request.RequestWebsocket; import com.csc.library.request.RequestWebsocket;
import com.csc.library.session.InitialEnvironment;
public class CommandFactory { public class CommandFactory {
private static CommandFactory instance; private static CommandFactory instance;
private List<CscCommand> listCommand = new ArrayList<CscCommand>();
private CMDMode mode = CMDMode.common;
private CommandFactory(){ private CommandFactory(){
/*load command*/
} }
public static CommandFactory getInstance(){ public static CommandFactory getInstance(){
...@@ -27,16 +27,17 @@ public class CommandFactory { ...@@ -27,16 +27,17 @@ public class CommandFactory {
public CscCommand getCommand(CscSession session ,RequestWebsocket req){ public CscCommand getCommand(CscSession session ,RequestWebsocket req){
CommandBase command = null; CommandBase command = null;
if(mode.common == req.getCommandMode()){
try{ try{
Class c = Class.forName("com.csc.library.command."+req.getCommandCode()); CMDModeManagement.ModeBuilder builder = CMDModeFactory.getInstance().getModeBuilder(req.getCommandMode());
String className = builder.getClassName(req.getCommandCode());
Class c = Class.forName("com.csc.library.command."+className);
command = (CommandBase)c.newInstance(); command = (CommandBase)c.newInstance();
command.setRequest(req); command.setRequest(req);
command.setSession(session); command.setSession(session);
command.setParamConfig(builder.getParamConfig(req.getCommandCode()));
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
} }
}
return command; return command;
} }
} }
...@@ -12,12 +12,9 @@ public class PropertiesFactory { ...@@ -12,12 +12,9 @@ public class PropertiesFactory {
private static PropertiesFactory instance; private static PropertiesFactory instance;
private Gson gson = null; private Gson gson = null;
private RequestFactory reqFact;
private PropertiesFactory(){ private PropertiesFactory(){
if(gson == null){ if(gson == null){
GsonBuilder gb = new GsonBuilder(); GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapter(RequestWebsocket.class, new SerializeAndDeserializeBase(RequestPropertiesImpl.class));
gson = gb.serializeNulls().create(); gson = gb.serializeNulls().create();
} }
} }
...@@ -29,10 +26,6 @@ public class PropertiesFactory { ...@@ -29,10 +26,6 @@ public class PropertiesFactory {
return instance; return instance;
} }
public RequestWebsocket createRequestWebSocket(String json){
return gson.fromJson(json, RequestPropertiesImpl.class);
}
public CscDevice createDevice(String json){ public CscDevice createDevice(String json){
return gson.fromJson(json, DeviceProperties.class); return gson.fromJson(json, DeviceProperties.class);
} }
......
package com.csc.library.factory;
import com.csc.library.gson.SerializeAndDeserializeBase;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.request.CommonParameterImpl;
import com.csc.library.request.ReqParameterImpl;
import com.csc.library.request.RequestParameter;
import com.csc.library.request.RequestPropertiesImpl;
import com.csc.library.request.RequestWebsocket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class RequestFactory {
private static RequestFactory instance;
private Gson gson = null;
private RequestFactory() {
GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapter(RequestWebsocket.class, new SerializeAndDeserializeBase(RequestPropertiesImpl.class));
gb.registerTypeAdapter(RequestParameter.class, new SerializeAndDeserializeBase(ReqParameterImpl.class));
gb.registerTypeAdapter(CommonModeParameter.class, new SerializeAndDeserializeBase(CommonParameterImpl.class));
gson = gb.serializeNulls().create();
}
public static RequestFactory getInstance(){
if(instance==null){
instance = new RequestFactory();
}
return instance;
}
public RequestWebsocket createRequestWebSocket(String json){
return gson.fromJson(json, RequestPropertiesImpl.class);
}
}
...@@ -22,17 +22,6 @@ public class ResponseDataFactory { ...@@ -22,17 +22,6 @@ public class ResponseDataFactory {
} }
public ResponseData createResponseData(CscCommand command){ public ResponseData createResponseData(CscCommand command){
/*ResDataImpl resData = null;
Object result = command.getResult();
if(result != null){
if(command instanceof CommandLists){
resData = new ResDataImpl();
resData.setDataList((List<Map<String, String>>) result);
}else if(command instanceof CommandRecord){
resData = new ResDataImpl();
resData.setDataRecord((Map<String, String>) result);
}
}*/
ResDataImpl resData = new ResDataImpl(); ResDataImpl resData = new ResDataImpl();
command.setResult(resData); command.setResult(resData);
return resData; return resData;
......
...@@ -8,6 +8,7 @@ import java.util.Map; ...@@ -8,6 +8,7 @@ import java.util.Map;
import com.csc.library.properties.CscSession; import com.csc.library.properties.CscSession;
import com.csc.library.properties.ResponsePools; import com.csc.library.properties.ResponsePools;
import com.csc.library.request.CommandState;
import com.csc.library.request.RequestWebsocket; import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseData; import com.csc.library.response.ResponseData;
import com.csc.library.response.ResponsePropertiesImpl; import com.csc.library.response.ResponsePropertiesImpl;
...@@ -33,20 +34,22 @@ public class ResponseFactory { ...@@ -33,20 +34,22 @@ public class ResponseFactory {
return instance; return instance;
} }
public void sendResponseWebsocket(CscSession session , RequestWebsocket req ,ResponseStatus state, String message){ public void sendResponseWebsocket(CscSession session , RequestWebsocket req ,CommandState stateComm,ResponseStatus stateRes, String message){
ResponsePropertiesImpl res = this.getResponseWebsocket(session); ResponsePropertiesImpl res = this.getResponseWebsocket(session);
res.setRequestWebsocket(req); res.setRequestWebsocket(req);
res.setMessage(message); res.setMessage(message);
res.setResponseStatus(state); res.setResponseStatus(stateRes);
res.setStateCommand(stateComm);
this.sendResponse(session, res); this.sendResponse(session, res);
} }
public void sendResponseWebsocket(CscSession session , RequestWebsocket req ,ResponseData resData, String message){ public void sendResponseWebsocket(CscSession session , RequestWebsocket req ,CommandState stateComm,ResponseData resData, String message){
ResponsePropertiesImpl res = this.getResponseWebsocket(session); ResponsePropertiesImpl res = this.getResponseWebsocket(session);
res.setRequestWebsocket(req); res.setRequestWebsocket(req);
res.setMessage(message); res.setMessage(message);
res.setResponseStatus(ResponseStatus.Complete); res.setResponseStatus(ResponseStatus.Finish);
res.setResponseData(resData); res.setResponseData(resData);
res.setStateCommand(stateComm);
this.sendResponse(session, res); this.sendResponse(session, res);
} }
......
...@@ -16,7 +16,14 @@ public class ThreadsPoolsFactory { ...@@ -16,7 +16,14 @@ public class ThreadsPoolsFactory {
return instance; return instance;
} }
public void execute(Runnable r){ public void execute(final Runnable r){
executor.execute(r); executor.execute(new Runnable() {
@Override
public void run() {
/* call begin thread */
r.run();
/* call ending thread */
}
});
} }
} }
/StaticVariable.class /StaticVariable.class
/StaticVariable$CommandSpecialAttribute.class
...@@ -3,4 +3,10 @@ package com.csc.library.fix; ...@@ -3,4 +3,10 @@ package com.csc.library.fix;
public interface StaticVariable { public interface StaticVariable {
public String UPROFILE = "uprofile"; public String UPROFILE = "uprofile";
public String DEVICE = "device"; public String DEVICE = "device";
interface CommandSpecialAttribute{
public String DESCRIPTION = "description";
public String SQL = "sql";
public String CLASSNAME = "classname";
}
} }
/SerializeAndDeserializeBase.class
package com.csc.library.gson;
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class SerializeAndDeserializeBase<T> implements JsonDeserializer<T>,JsonSerializer<T>{
private Class myClass = null;
public SerializeAndDeserializeBase(Class clss) {
this.myClass = clss;
}
public Class getMyClass(){
return this.myClass;
}
public JsonElement serialize(Object src, Type typeOfSrc,
JsonSerializationContext context) {
return context.serialize(src, this.myClass);
}
public T deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return context.deserialize(json, this.myClass);
}
}
/CMDModeCtrl.class
/CMDModeControl.class
/CMDModeControl$CMDModeCtrl.class
/CMDModeControl$CMDModeBuilder.class
/CMDModeManagement$CMDModeBuilder.class
/CMDModeManagement$CMDModeCtrl.class
/CMDModeManagement.class
/CMDModeManagement$ModeBuilder.class
/CMDModeManagement$ModeCtrl.class
/ManageCommonMode.class
/AbsModeManagement.class
/ManageSpecialMode.class
/ManageProcessMode.class
/ManageUploadMode.class
/ManageFtpMode.class
package com.csc.library.modecontroller;
public abstract class AbsModeManagement implements CMDModeManagement.ModeCtrl , CMDModeManagement.ModeBuilder{
}
package com.csc.library.modecontroller;
import java.util.Map;
import com.csc.library.command.CscCommand;
import com.csc.library.request.RequestParameter;
public interface CMDModeManagement{
public interface ModeCtrl{
public void execute(CscCommand command ,RequestParameter reqParam) throws Exception;
}
public interface ModeBuilder{
public void load(String path);
public String getClassName(String code);
public Map<String, String> getParamConfig(String code);
}
}
package com.csc.library.modecontroller;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Element;
import com.csc.library.command.CscCommand;
import com.csc.library.request.CMDMode;
import com.csc.library.request.RequestParameter;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.ReadXml;
public class ManageCommonMode extends AbsModeManagement{
private Map<String,String> map = new HashMap<String,String>();
protected CMDMode mode = CMDMode.common;
@Override
public void load(String path) {
try{
File fileDir = new File(path + mode.toString());
if(fileDir.isDirectory()){
File[] listFile = fileDir.listFiles();
for(File f : listFile){
ReadXml rm = new ReadXml( f.getPath() , false);
Element eRoot = rm.getRootElement();
List childs = eRoot.getChildren();
for( int i = 0 ; i < childs.size() ; i++ ){
Element child = (Element)childs.get(i);
this.map.put(mode.toString()+"-"+child.getAttributeValue("name") , mode.toString()+"."+child.getAttributeValue("classname"));
}
}
}
}catch(Exception ex){
MyLog.error(this , ex);
}
}
@Override
public String getClassName(String code) {
return this.map.get(mode.toString()+"-"+code);
}
@Override
public Map<String, String> getParamConfig(String code) {
return null;
}
@Override
public void execute(CscCommand command, RequestParameter reqParam) throws Exception {
command.execute(reqParam.getCommonParameter());
}
}
package com.csc.library.modecontroller;
import java.util.Map;
import com.csc.library.command.CscCommand;
import com.csc.library.request.CMDMode;
import com.csc.library.request.RequestParameter;
public class ManageFtpMode extends ManageCommonMode{
public ManageFtpMode() {
this.mode = CMDMode.ftp;
}
@Override
public void execute(CscCommand command, RequestParameter reqParam) throws Exception {
command.execute(reqParam.getJsonFile());
}
@Override
public Map<String, String> getParamConfig(String code) {
return null;
}
}
package com.csc.library.modecontroller;
import java.util.Map;
import com.csc.library.command.CscCommand;
import com.csc.library.request.RequestParameter;
public class ManageProcessMode extends AbsModeManagement{
private String _DEFAULT_CLASS = "";
@Override
public void execute(CscCommand command, RequestParameter reqParam) throws Exception {
command.execute(reqParam.getProcessParameter());
}
@Override
public void load(String path) {
// TODO Auto-generated method stub
}
@Override
public String getClassName(String code) {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, String> getParamConfig(String code) {
// TODO Auto-generated method stub
return null;
}
}
package com.csc.library.modecontroller;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Element;
import com.csc.library.command.CscCommand;
import com.csc.library.fix.StaticVariable;
import com.csc.library.request.CMDMode;
import com.csc.library.request.RequestParameter;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.ReadXml;
public class ManageSpecialMode extends AbsModeManagement{
private Map<String,Map<String,String>> map = new HashMap<String,Map<String,String>>();
private final CMDMode mode = CMDMode.special;
@Override
public void execute(CscCommand command, RequestParameter reqParam) throws Exception {
command.execute(reqParam.getSpecialParameter());
}
@Override
public void load(String path) {
try{
File fileDir = new File(path + mode.toString());
if(fileDir.isDirectory()){
File[] listFile = fileDir.listFiles();
for(File f : listFile){
ReadXml rm = new ReadXml( f.getPath() , false);
Element eRoot = rm.getRootElement();
Map<String,String> hm = new HashMap<String,String>();
hm.put(StaticVariable.CommandSpecialAttribute.CLASSNAME, mode.toString()+".CommandSpecialBase");
List childs = eRoot.getChildren();
for( int i = 0 ; i < childs.size() ; i++ ){
Element child = (Element)childs.get(i);
hm.put(child.getName().toLowerCase() , child.getText());
}
this.map.put(eRoot.getName() , Collections.unmodifiableMap(hm));
}
}
}catch(Exception ex){
MyLog.error(this , ex);
}
}
@Override
public String getClassName(String code) {
if(this.map.containsKey(code)){
return this.map.get(code).get(StaticVariable.CommandSpecialAttribute.CLASSNAME);
}else{
return "";
}
}
@Override
public Map<String, String> getParamConfig(String code) {
return this.map.get(code);
}
}
...@@ -9,3 +9,5 @@ ...@@ -9,3 +9,5 @@
/Path.class /Path.class
/ResponsePools.class /ResponsePools.class
/SessionHandle.class /SessionHandle.class
/HandleMessageData.class
/CommandStateTemp.class
package com.csc.library.properties; package com.csc.library.properties;
public enum CommandState { public enum CommandStateTemp {
Created(0 , "created") , Created(0 , "created") ,
ReadyAndWaiting(1 , "Ready And Waiting") , ReadyAndWaiting(1 , "Ready And Waiting") ,
Running(2 , "Running") , Running(2 , "Running") ,
...@@ -9,7 +9,7 @@ public enum CommandState { ...@@ -9,7 +9,7 @@ public enum CommandState {
int state = 0; int state = 0;
String description = ""; String description = "";
private CommandState(int state , String desc){ private CommandStateTemp(int state , String desc){
state = state; state = state;
description = desc; description = desc;
} }
......
package com.csc.library.properties; package com.csc.library.properties;
public interface CscState { public interface CscState {
public CommandState getState(); public CommandStateTemp getState();
public String getMessage(); public String getMessage();
public void setState(CommandState state); public void setState(CommandStateTemp state);
public void setMessage(String message); public void setMessage(String message);
} }
...@@ -9,16 +9,28 @@ public class DeviceProperties implements CscDevice{ ...@@ -9,16 +9,28 @@ public class DeviceProperties implements CscDevice{
public String getUserName(){ public String getUserName(){
return this.username; return this.username;
} }
public void setUserName(String userName){
this.username = userName;
}
public String getPassWord(){ public String getPassWord(){
return this.password; return this.password;
} }
public void setPassWord(String passWord){
this.password = passWord;
}
public String getDeviceID(){ public String getDeviceID(){
return this.deviceId; return this.deviceId;
} }
public void setDeviceID(String deviceId){
this.deviceId = deviceId;
}
public String getPhoneNumber(){ public String getPhoneNumber(){
return this.phoneNumber; return this.phoneNumber;
} }
public void setPhoneNumber(String phoneNumber){
this.phoneNumber = phoneNumber;
}
} }
package com.csc.library.properties;
import com.csc.library.controller.WebSocketCommandController;
import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseWebsocket;
public class HandleMessageData implements HandlePackage<CscSession>{
@Override
public boolean isExistsRequest(CscSession session, String reqId) {
return false;
}
@Override
public void onRequest(CscSession session, RequestWebsocket req) {
WebSocketCommandController.getInstance().executeCommand(session, req);
}
@Override
public void onResponse(CscSession session, ResponseWebsocket res) {
}
@Override
public boolean validateSendRequest(CscSession session, RequestWebsocket req) {
return true;
}
@Override
public boolean validateSendResponse(CscSession session, ResponseWebsocket req) {
return true;
}
@Override
public void send(CscSession session, String jsonSting) {
}
}
package com.csc.library.properties; package com.csc.library.properties;
public class MyStateCommand implements CscState{ public class MyStateCommand implements CscState{
private CommandState state = CommandState.Created; private CommandStateTemp state = CommandStateTemp.Created;
private String message = ""; private String message = "";
public void setState(CommandState state){ public void setState(CommandStateTemp state){
this.state = state; this.state = state;
} }
public CommandState getState(){ public CommandStateTemp getState(){
return this.state; return this.state;
} }
public void setMessage(String message){ public void setMessage(String message){
......
package com.csc.library.servlet.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CheckRequestWebSocket extends AbstractFilter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws ServletException, IOException {
String x = "";
chain.doFilter(request, response);
String y = "";
}
}
package com.csc.library.task; package com.csc.library.task;
import com.csc.library.command.CscCommand; import com.csc.library.command.CscCommand;
import com.csc.library.controller.CMDModeCtrlCommand; import com.csc.library.factory.CMDModeFactory;
import com.csc.library.factory.ResponseDataFactory; import com.csc.library.factory.ResponseDataFactory;
import com.csc.library.factory.ResponseFactory; import com.csc.library.factory.ResponseFactory;
import com.csc.library.properties.CscSession; import com.csc.library.properties.CscSession;
import com.csc.library.request.CommandState;
import com.csc.library.request.RequestWebsocket; import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseStatus; import com.csc.library.response.ResponseStatus;
import com.csc.library.utilities.MyLog; import com.csc.library.utilities.MyLog;
...@@ -37,12 +38,13 @@ public class TaskCommand implements CommandTask{ ...@@ -37,12 +38,13 @@ public class TaskCommand implements CommandTask{
public void run() { public void run() {
try { try {
CMDModeCtrlCommand.getInstance() CMDModeFactory.getInstance().getModeCtrl(this.getRequest().getCommandMode())
.execute(this.getCommand(), this.getRequest()); .execute(this.getCommand(), this.getRequest().getRequestParameter());
ResponseFactory.getInstance().sendResponseWebsocket( ResponseFactory.getInstance().sendResponseWebsocket(
this.getSession(), this.getSession(),
this.getRequest(), this.getRequest(),
this.getCommand().getCommandState(),
ResponseDataFactory.getInstance().createResponseData(this.getCommand()), ResponseDataFactory.getInstance().createResponseData(this.getCommand()),
""); "");
} catch (Exception e) { } catch (Exception e) {
...@@ -50,6 +52,7 @@ public class TaskCommand implements CommandTask{ ...@@ -50,6 +52,7 @@ public class TaskCommand implements CommandTask{
ResponseFactory.getInstance().sendResponseWebsocket( ResponseFactory.getInstance().sendResponseWebsocket(
this.getSession(), this.getSession(),
this.getRequest(), this.getRequest(),
CommandState.fail,
ResponseStatus.Error, ResponseStatus.Error,
e.getMessage()); e.getMessage());
} }
......
/ServerWebSocket.class /ServerWebSocket.class
/WebSocketController.class /WebSocketController.class
/WebSocketTest.class /WebSocketTest.class
/WebSocketTest2.class
package com.csc.library.websocket; package com.csc.library.websocket;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -15,16 +16,27 @@ import javax.websocket.server.PathParam; ...@@ -15,16 +16,27 @@ import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint; import javax.websocket.server.ServerEndpoint;
import com.csc.library.controller.WebSocketCommandController; import com.csc.library.controller.WebSocketCommandController;
import com.csc.library.factory.PackageDataFactory;
import com.csc.library.factory.PropertiesFactory;
import com.csc.library.properties.DeviceProperties;
import com.csc.library.properties.HandleMessageData;
import com.csc.library.properties.HandlePackage;
@ServerEndpoint(value = "/CSCWS/{login_properties}") @ServerEndpoint(value = "/CSCWS")
public class WebSocketController { public class WebSocketController {
private ServerWebSocket serverSocket = null; private ServerWebSocket serverSocket = null;
private WebSocketCommandController commandControl = WebSocketCommandController.getInstance(); private WebSocketCommandController commandControl = WebSocketCommandController.getInstance();
private HandlePackage handle = new HandleMessageData();
//private static final Set<Session> sessions = new HashSet<Session>(); //private static final Set<Session> sessions = new HashSet<Session>();
@OnOpen @OnOpen
public void open(Session session, EndpointConfig conf,@PathParam("login_properties") String prop) throws IOException { public void open(Session session, EndpointConfig conf ) throws IOException {
serverSocket = ServerWebSocket.getInstance(); serverSocket = ServerWebSocket.getInstance();
if(serverSocket.registerSession(session, prop)){ DeviceProperties device = new DeviceProperties();
device.setDeviceID("XXXXX");
device.setPassWord("YYYY");
device.setUserName("XYZ");
if(serverSocket.registerSession(session, PropertiesFactory.getInstance().toJson(device))){
//serverSocket. //serverSocket.
} }
//sessions.add(session); //sessions.add(session);
...@@ -44,6 +56,18 @@ public class WebSocketController { ...@@ -44,6 +56,18 @@ public class WebSocketController {
/*for(Session ses : sessions){ /*for(Session ses : sessions){
ses.getBasicRemote().sendText(message); ses.getBasicRemote().sendText(message);
}*/ }*/
commandControl.executeCommand(serverSocket.getCscSession(session), message); System.out.println("========> OnMessage String : "+message);
PackageDataFactory.getInstance().handlePackageData(serverSocket.getCscSession(session), handle , message);
//commandControl.executeCommand(serverSocket.getCscSession(session), message);
}
@OnMessage
public void handleMessage(Session session , ByteBuffer buffer) throws IOException{
/*for(Session ses : sessions){
ses.getBasicRemote().sendText(message);
}*/
System.out.println("========> OnMessage ByteBuffer : "+buffer);
//PackageDataFactory.getInstance().handlePackageData(serverSocket.getCscSession(session), handle , message);
//commandControl.executeCommand(serverSocket.getCscSession(session), message);
} }
} }
...@@ -15,6 +15,7 @@ import javax.websocket.server.PathParam; ...@@ -15,6 +15,7 @@ import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint; import javax.websocket.server.ServerEndpoint;
import com.csc.library.controller.WebSocketCommandController; import com.csc.library.controller.WebSocketCommandController;
import com.csc.library.utilities.MyLog;
import com.csc.library.websocket.config.CscWsServerEndpointConfig; import com.csc.library.websocket.config.CscWsServerEndpointConfig;
@ServerEndpoint( @ServerEndpoint(
...@@ -24,16 +25,24 @@ import com.csc.library.websocket.config.CscWsServerEndpointConfig; ...@@ -24,16 +25,24 @@ import com.csc.library.websocket.config.CscWsServerEndpointConfig;
public class WebSocketTest { public class WebSocketTest {
private ServerWebSocket serverSocket = null; private ServerWebSocket serverSocket = null;
private WebSocketCommandController commandControl = WebSocketCommandController.getInstance(); private WebSocketCommandController commandControl = WebSocketCommandController.getInstance();
//private static final Set<Session> sessions = new HashSet<Session>(); private static final Set<Session> sessions = new HashSet<Session>();
@OnOpen @OnOpen
public void open(Session session, EndpointConfig conf) throws IOException { public void open(Session session, EndpointConfig conf) throws IOException {
serverSocket = ServerWebSocket.getInstance(); serverSocket = ServerWebSocket.getInstance();
//sessions.add(session); sessions.add(session);
for(Session ses : this.sessions){
if(!ses.isOpen()){
sessions.remove(ses);
}
}
System.out.println("Open Connections Size : "+sessions.size());
} }
@OnClose @OnClose
public void close(Session session , CloseReason closeReason) { public void close(Session session , CloseReason closeReason) {
sessions.remove(session);
System.out.println("Close Connections Size : "+sessions.size());
} }
@OnError @OnError
...@@ -42,9 +51,11 @@ public class WebSocketTest { ...@@ -42,9 +51,11 @@ public class WebSocketTest {
@OnMessage @OnMessage
public void handleMessage(Session session , String message) throws IOException{ public void handleMessage(Session session , String message) throws IOException{
/*for(Session ses : sessions){ session.getAsyncRemote().sendText(message);
ses.getBasicRemote().sendText(message); /*for(Session ses : this.sessions){
if(session != ses){
ses.getAsyncRemote().sendText(System.currentTimeMillis()+" : "+message);
}
}*/ }*/
commandControl.executeCommand(serverSocket.getCscSession(session), message);
} }
} }
package com.csc.library.websocket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.CloseReason;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import com.csc.library.controller.WebSocketCommandController;
import com.csc.library.factory.PackageDataFactory;
import com.csc.library.properties.HandleMessageData;
import com.csc.library.properties.HandlePackage;
@ServerEndpoint(value = "/TESTWS2")
public class WebSocketTest2 {
private ServerWebSocket serverSocket = null;
private WebSocketCommandController commandControl = WebSocketCommandController.getInstance();
private HandlePackage handle = new HandleMessageData();
//private static final Set<Session> sessions = new HashSet<Session>();
@OnOpen
public void open(Session session, EndpointConfig conf) throws IOException {
serverSocket = ServerWebSocket.getInstance();
if(serverSocket.registerSession(session, "{}")){
//serverSocket.
}
//sessions.add(session);
}
@OnClose
public void close(Session session , CloseReason closeReason) {
}
@OnError
public void onError(Throwable error) {
}
@OnMessage
public void handleMessage(Session session , String message) throws IOException{
/*for(Session ses : sessions){
ses.getBasicRemote().sendText(message);
}*/
System.out.println("========> OnMessage String : "+message);
PackageDataFactory.getInstance().handlePackageData(serverSocket.getCscSession(session), handle , message);
//commandControl.executeCommand(serverSocket.getCscSession(session), message);
}
@OnMessage
public void handleMessage(Session session , ByteBuffer buffer) throws IOException{
/*for(Session ses : sessions){
ses.getBasicRemote().sendText(message);
}*/
System.out.println("========> OnMessage ByteBuffer : "+buffer);
//PackageDataFactory.getInstance().handlePackageData(serverSocket.getCscSession(session), handle , message);
//commandControl.executeCommand(serverSocket.getCscSession(session), message);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment