Commit cffe57d7 by TongZuu

init project

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.jboss.ide.eclipse.as.core.server.runtime.runtimeTarget/WildFly 10.0 Runtime">
<attributes>
<attribute name="owner.project.facets" value="jst.utility"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="D:/EnterprisePT/Programmer/myHRAppServer/wildfly-10.0.0.tpbs/standalone/deployments/TPBS.ear/hrAppWeb.war/WEB-INF/lib/CSCFrameWork.jar"/>
<classpathentry kind="lib" path="D:/EnterprisePT/Programmer/myHRAppServer/wildfly-10.0.0.tpbs/standalone/deployments/TPBS.ear/hrAppWeb.war/WEB-INF/lib/dynamicjava.jar"/>
<classpathentry kind="lib" path="D:/EnterprisePT/Programmer/myHRAppServer/wildfly-10.0.0.tpbs/standalone/deployments/TPBS.ear/hrAppWeb.war/WEB-INF/lib/jdom.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 combineaccessrules="false" kind="src" path="/csc-websocket-api"/>
<classpathentry kind="output" path=""/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CSCWebsocketBean</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="CSCWebsocketBean">
<wb-resource deploy-path="/" source-path="/"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="WildFly 10.0 Runtime"/>
<fixed facet="java"/>
<fixed facet="jst.utility"/>
<installed facet="java" version="1.6"/>
<installed facet="jst.utility" version="1.0"/>
</faceted-project>
Manifest-Version: 1.0
Class-Path:
/WebSocketAuthen.class
/WebSocketMyAuthen.class
package com.csc.library.authen;
import com.csc.library.properties.CscDevice;
import com.csc.library.utilities.UProfile;
public interface WebSocketAuthen {
public UProfile checkPermission(CscDevice loginProp);
}
package com.csc.library.authen;
import com.csc.library.properties.CscDevice;
import com.csc.library.utilities.UProfile;
public class WebSocketMyAuthen implements WebSocketAuthen{
@Override
public UProfile checkPermission(CscDevice loginProp) {
// TODO Auto-generated method stub
return new UProfile();
}
}
/Command001.class
/Command002.class
/Command003.class
/CommandBase.class
/CommandLists.class
/CommandProcess.class
/CommandRecord.class
/CscCommand.class
package com.csc.library.command;
import java.util.Iterator;
import java.util.Map;
import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.response.ResponseData;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialRecord;
/* read record */
public class Command001 extends CommandRecord<CommonModeParameter>{
public void execute(CommonModeParameter param) throws Exception {
try{
this.sendMessage("Start Command Common Read Record");
DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName());
rec.setGroupfield(param.getGroupField());
CommonParameterUtil.setKeyRecord(rec, param);
if(rec.search()==1){
this.sendMessage("Command001 Search Found Data");
this.setRecord(rec);
}else{
this.sendMessage("Command001 Search Not Found Data");
}
}catch(Exception e){
e.printStackTrace();
this.sendMessage("Exception : "+e.getMessage());
}
}
}
package com.csc.library.command;
import java.util.Iterator;
import com.csc.library.command.util.CommonParameterUtil;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.MyLog;
/* save record */
public class Command002 extends CommandRecord<CommonModeParameter>{
@Override
public void execute(CommonModeParameter param) throws Exception {
try{
DbRecord rec = new InitialRecord(this.getUProfile()).getDbRecord(param.getTableName());
CommonParameterUtil.setKeyRecord(rec, param);
CommonParameterUtil.setJsonData(rec, param);
int saveStatus = rec.save();
this.sendMessage("SaveRecord status : "+saveStatus);
this.setRecord(rec);
}catch(Exception e){
MyLog.error(this ,this.getUProfile(), e);
this.sendMessage(e.getMessage());
}
}
}
package com.csc.library.command;
public class Command003 {
}
package com.csc.library.command;
import com.csc.library.factory.ResponseFactory;
import com.csc.library.properties.CommandState;
import com.csc.library.properties.CscSession;
import com.csc.library.request.CMDMode;
import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseStatus;
import com.csc.library.utilities.UProfile;
public abstract class CommandBase<T> implements CscCommand<T>{
protected CMDMode commandMode = null;
private CscSession session = null;
private RequestWebsocket request = null;
protected CommandState statusCode = CommandState.Created;
public final void setSession(CscSession session){
this.session = session;
}
public final CscSession getSession(){
return this.session;
}
public final void setRequest(RequestWebsocket req){
this.request = req;
}
public final RequestWebsocket getRequest(){
return this.request;
}
public UProfile getUProfile(){
return this.session.getUProfile();
}
public CommandState getStatusCode(){
return this.statusCode;
}
public final void sendMessage(String message){
ResponseFactory.getInstance().sendResponseWebsocket(
this.getSession(),
this.getRequest(),
ResponseStatus.SendMessage,
message);
}
}
package com.csc.library.command;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import com.csc.library.response.ResponseData;
import com.csc.library.session.DbRecord;
import com.csc.library.utilities.MyLog;
public abstract class CommandLists<T> extends CommandBase<T>{
private List<Map<String,String>> result = new ArrayList<Map<String,String>>();
@Override
public final void setResult(ResponseData res) {
res.setData(this.result);
}
protected final void putNewRecord(DbRecord rec){
try{
Map<String,String> map = new HashMap<String,String>();
Vector v = rec.getArrayFieldList();
for(int i = 0; i < v.size(); i++){
String field = (String) v.get(i);
map.put(field, rec.getString(field));
}
result.add(map);
}catch(Exception e){
MyLog.error(this , this.getUProfile() , e);
}
}
protected final void putNewRecord(Map<String,String> map){
result.add(map);
}
}
package com.csc.library.command;
import com.csc.library.response.ResponseData;
public abstract class CommandProcess<T> extends CommandBase<T>{
@Override
public final void setResult(ResponseData res) {
}
}
package com.csc.library.command;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import com.csc.library.response.ResponseData;
import com.csc.library.session.DbRecord;
import com.csc.library.utilities.MyLog;
public abstract class CommandRecord<T> extends CommandBase<T>{
private Map<String,String> result = new HashMap<String,String>();
@Override
public final void setResult(ResponseData res) {
res.setData(this.result);
}
protected final void setString(String fieldName , String data){
result.put(fieldName, data);
}
protected final void setRecord(DbRecord rec){
try{
Vector v = rec.getArrayFieldList();
for(int i = 0; i < v.size(); i++){
String field = (String) v.get(i);
this.setString(field, rec.getString(field));
}
}catch(Exception e){
MyLog.error(this , this.getUProfile() , e);
}
}
}
package com.csc.library.command;
import com.csc.library.properties.CommandState;
import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseData;
public interface CscCommand<T> {
public CommandState getStatusCode();
public void setRequest(RequestWebsocket req);
public void execute(T param) throws Exception;
public void setResult(ResponseData res);
}
package com.csc.library.command.util;
import java.util.Iterator;
import java.util.Map;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.session.DbRecord;
public class CommonParameterUtil {
public static void setKeyRecord(DbRecord rec , CommonModeParameter param) throws Exception {
Map<String,String> m = param.getKey();
Iterator it = m.keySet().iterator();
while(it.hasNext()){
String key = it.next().toString();
rec.set(key, m.get(key));
}
}
public static void setJsonData(DbRecord rec , CommonModeParameter param) throws Exception {
Map<String,String> mapJson = param.getJson();
Iterator it = mapJson.keySet().iterator();
while(it.hasNext()){
String key = it.next().toString();
if(!rec.getAllKey().contains(key)){
rec.set(key, mapJson.get(key));
}
}
}
}
/CMDModeCtrlCommand$CMDModeCtrl.class
/CMDModeCtrlCommand$CommonCtrl.class
/CMDModeCtrlCommand$ProcessCtrl.class
/CMDModeCtrlCommand$SpecialCtrl.class
/CMDModeCtrlCommand.class
/StateController.class
/WebSocketCommandController.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());
}
}
}
package com.csc.library.controller;
public class StateController {
private static StateController instance;
private StateController(){}
public StateController getInstance(){
if(instance == null){
instance = new StateController();
}
return instance;
}
}
package com.csc.library.controller;
import com.csc.library.command.CscCommand;
import com.csc.library.factory.CommandFactory;
import com.csc.library.factory.PropertiesFactory;
import com.csc.library.factory.RequestFactory;
import com.csc.library.factory.ResponseFactory;
import com.csc.library.factory.ThreadsPoolsFactory;
import com.csc.library.properties.CscSession;
import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseStatus;
import com.csc.library.task.CommandTask;
import com.csc.library.task.TaskCommand;
import com.csc.library.utilities.MyLog;
public class WebSocketCommandController {
private static WebSocketCommandController instance;
//private Map<CscSession , Map<String , RequestWebsocket>> currentCommand = new HashMap<CscSession , Map<String , RequestWebsocket>>();
private ResponseFactory responseFact;
private PropertiesFactory propFact;
private CommandFactory commandFact;
private ThreadsPoolsFactory threadsFact;
private RequestFactory reqFact;
private WebSocketCommandController(){
}
public static WebSocketCommandController getInstance(){
try{
if(instance == null){
instance = new WebSocketCommandController();
instance.responseFact = ResponseFactory.getInstance();
instance.reqFact = RequestFactory.getInstance();
instance.commandFact = CommandFactory.getInstance();
instance.threadsFact = ThreadsPoolsFactory.getInstance();
}
}catch(Exception e){
e.printStackTrace();
}
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){
responseFact.sendResponseWebsocket(session, req,
ResponseStatus.Receive,
ResponseStatus.Receive.getDescription());
final CscCommand command = commandFact.getCommand(session , req);
CommandTask task = new TaskCommand(session , req , command);
threadsFact.execute(task);
}
}
/CommandFactory.class
/PropertiesFactory.class
/RequestFactory.class
/ResponseDataFactory.class
/ResponseFactory.class
/SessionFactory.class
/StateCommandFactory.class
/ThreadsPoolsFactory.class
package com.csc.library.factory;
import java.util.ArrayList;
import java.util.List;
import com.csc.library.command.CommandBase;
import com.csc.library.command.CscCommand;
import com.csc.library.properties.CscSession;
import com.csc.library.request.CMDMode;
import com.csc.library.request.RequestWebsocket;
public class CommandFactory {
private static CommandFactory instance;
private List<CscCommand> listCommand = new ArrayList<CscCommand>();
private CMDMode mode = CMDMode.common;
private CommandFactory(){
/*load command*/
}
public static CommandFactory getInstance(){
if(instance == null){
instance = new CommandFactory();
}
return instance;
}
public CscCommand getCommand(CscSession session ,RequestWebsocket req){
CommandBase command = null;
if(mode.common == req.getCommandMode()){
try{
Class c = Class.forName("com.csc.library.command."+req.getCommandCode());
command = (CommandBase)c.newInstance();
command.setRequest(req);
command.setSession(session);
}catch(Exception e){
e.printStackTrace();
}
}
return command;
}
}
package com.csc.library.factory;
import com.csc.library.gson.SerializeAndDeserializeBase;
import com.csc.library.properties.CscDevice;
import com.csc.library.properties.DeviceProperties;
import com.csc.library.request.RequestPropertiesImpl;
import com.csc.library.request.RequestWebsocket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class PropertiesFactory {
private static PropertiesFactory instance;
private Gson gson = null;
private RequestFactory reqFact;
private PropertiesFactory(){
if(gson == null){
GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapter(RequestWebsocket.class, new SerializeAndDeserializeBase(RequestPropertiesImpl.class));
gson = gb.serializeNulls().create();
}
}
public static PropertiesFactory getInstance(){
if( instance == null ){
instance = new PropertiesFactory();
}
return instance;
}
public RequestWebsocket createRequestWebSocket(String json){
return gson.fromJson(json, RequestPropertiesImpl.class);
}
public CscDevice createDevice(String json){
return gson.fromJson(json, DeviceProperties.class);
}
public String toJson(Object obj){
return gson.toJson(obj);
}
}
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);
}
}
package com.csc.library.factory;
import java.util.List;
import java.util.Map;
import com.csc.library.command.CommandLists;
import com.csc.library.command.CommandRecord;
import com.csc.library.command.CscCommand;
import com.csc.library.response.ResDataImpl;
import com.csc.library.response.ResponseData;
public class ResponseDataFactory {
private static ResponseDataFactory instance;
private ResponseDataFactory(){}
public static ResponseDataFactory getInstance(){
if(instance == null){
instance = new ResponseDataFactory();
}
return instance;
}
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();
command.setResult(resData);
return resData;
}
}
package com.csc.library.factory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.csc.library.properties.CscSession;
import com.csc.library.properties.ResponsePools;
import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseData;
import com.csc.library.response.ResponsePropertiesImpl;
import com.csc.library.response.ResponseStatus;
import com.csc.library.response.ResponseWebsocket;
import com.csc.library.utilities.MyLog;
public class ResponseFactory {
private static ResponseFactory instance;
//private Map<CscSession,List<ResponseWebsocket>> response = new HashMap<CscSession ,List<ResponseWebsocket>>();
private ResponsePools<ResponsePropertiesImpl> response;
private PropertiesFactory propInstance;
private ResponseFactory(){
}
public static ResponseFactory getInstance(){
if(instance == null){
instance = new ResponseFactory();
instance.propInstance = PropertiesFactory.getInstance();
instance.response = ResponsePools.getInstance();
}
return instance;
}
public void sendResponseWebsocket(CscSession session , RequestWebsocket req ,ResponseStatus state, String message){
ResponsePropertiesImpl res = this.getResponseWebsocket(session);
res.setRequestWebsocket(req);
res.setMessage(message);
res.setResponseStatus(state);
this.sendResponse(session, res);
}
public void sendResponseWebsocket(CscSession session , RequestWebsocket req ,ResponseData resData, String message){
ResponsePropertiesImpl res = this.getResponseWebsocket(session);
res.setRequestWebsocket(req);
res.setMessage(message);
res.setResponseStatus(ResponseStatus.Complete);
res.setResponseData(resData);
this.sendResponse(session, res);
}
protected ResponsePropertiesImpl getResponseWebsocket(CscSession session){
ResponsePropertiesImpl res = null;
try{
res = this.response.pop();
if(res == null){
res = new ResponsePropertiesImpl();
}
}catch(Exception e){
MyLog.error(this , session.getUProfile() , e);
}
return res;
}
protected void sendResponse(CscSession session , ResponseWebsocket res){
session.sendMessage(propInstance.toJson(res));
res.clear();
this.response.push((ResponsePropertiesImpl)res);
}
}
package com.csc.library.factory;
import javax.websocket.Session;
import com.csc.library.properties.CscDevice;
import com.csc.library.properties.CscSession;
import com.csc.library.properties.SessionHandle;
import com.csc.library.utilities.UProfile;
public class SessionFactory {
private static SessionFactory instance;
private SessionFactory(){
}
public static SessionFactory getInstance(){
if(instance == null){
instance = new SessionFactory();
}
return instance;
}
public CscSession createSession(UProfile upf,Session session , CscDevice device){
return new SessionHandle(upf , session , device);
}
}
package com.csc.library.factory;
import com.csc.library.properties.CscState;
import com.csc.library.properties.MyStateCommand;
public class StateCommandFactory {
private static StateCommandFactory instance;
private StateCommandFactory(){}
public static StateCommandFactory getInstance(){
if(instance == null){
instance = new StateCommandFactory();
}
return instance;
}
public CscState createCommandState(){
return new MyStateCommand();
}
}
package com.csc.library.factory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadsPoolsFactory {
private static ThreadsPoolsFactory instance = null;
private ExecutorService executor = Executors.newFixedThreadPool(5);
private ThreadsPoolsFactory(){
}
public static ThreadsPoolsFactory getInstance(){
if(instance == null){
instance = new ThreadsPoolsFactory();
}
return instance;
}
public void execute(Runnable r){
executor.execute(r);
}
}
package com.csc.library.fix;
public interface StaticVariable {
public String UPROFILE = "uprofile";
public String DEVICE = "device";
}
/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);
}
}
/CommandMode.class
/CommandState.class
/CscDevice.class
/CscPath.class
/CscSession.class
/CscState.class
/DeviceProperties.class
/MyStateCommand.class
/Path.class
/ResponsePools.class
/SessionHandle.class
package com.csc.library.properties;
public enum CommandMode {
common(1),spacial(2),process(3);
int mode = 0;
private CommandMode(int mode){
this.mode = mode;
}
public int getMode(){
return this.mode;
}
}
package com.csc.library.properties;
public enum CommandState {
Created(0 , "created") ,
ReadyAndWaiting(1 , "Ready And Waiting") ,
Running(2 , "Running") ,
Terminated(3 , "Terminated") ,
Finish(4 , "Finish");
int state = 0;
String description = "";
private CommandState(int state , String desc){
state = state;
description = desc;
}
public int getState(){
return this.state;
}
public String getDescription(){
return this.description;
}
}
package com.csc.library.properties;
public interface CscDevice {
public String getUserName();
public String getPassWord();
public String getDeviceID();
}
package com.csc.library.properties;
public interface CscPath {
public String getServerName();
public String getGatewayName();
}
package com.csc.library.properties;
import com.csc.library.utilities.UProfile;
public interface CscSession {
public CscDevice getDevice();
public UProfile getUProfile();
public void sendMessage(String message);
}
package com.csc.library.properties;
public interface CscState {
public CommandState getState();
public String getMessage();
public void setState(CommandState state);
public void setMessage(String message);
}
package com.csc.library.properties;
public class DeviceProperties implements CscDevice{
private String username = "";
private String password = "";
private String deviceId = "";
private String phoneNumber = "";
public String getUserName(){
return this.username;
}
public String getPassWord(){
return this.password;
}
public String getDeviceID(){
return this.deviceId;
}
public String getPhoneNumber(){
return this.phoneNumber;
}
}
package com.csc.library.properties;
public class MyStateCommand implements CscState{
private CommandState state = CommandState.Created;
private String message = "";
public void setState(CommandState state){
this.state = state;
}
public CommandState getState(){
return this.state;
}
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return this.message;
}
}
package com.csc.library.properties;
public class Path implements CscPath{
private String serverName;
private String gateWayName;
public String getServerName(){
return this.serverName;
}
public String getGatewayName(){
return this.gateWayName;
}
}
package com.csc.library.properties;
import java.util.LinkedList;
import com.csc.library.response.ResponseWebsocket;
public class ResponsePools<T> {
private static ResponsePools instance;
private LinkedList<T> list = new LinkedList<T>();
private ResponsePools(){}
public static ResponsePools getInstance(){
if(instance==null){
instance = new ResponsePools();
}
return instance;
}
public synchronized void push(T res) {
synchronized (list) {
list.addLast(res);
}
}
public synchronized T pop() throws Exception {
synchronized (list) {
if (list.size() == 0) {
return null;
}
return list.removeFirst();
}
}
}
package com.csc.library.properties;
import java.io.IOException;
import javax.websocket.Session;
import com.csc.library.response.ResponseWebsocket;
import com.csc.library.utilities.UProfile;
public class SessionHandle implements CscSession{
private CscDevice device = null;
private Session session = null;
private UProfile uprofile = null;
public SessionHandle(UProfile upf , Session session , CscDevice device) {
this.uprofile = upf;
this.session = session;
this.device = device;
}
@Override
public CscDevice getDevice() {
// TODO Auto-generated method stub
return device;
}
@Override
public UProfile getUProfile() {
// TODO Auto-generated method stub
return uprofile;
}
public void sendMessage(String message){
if(!this.isAlive()){
return;
}
try {
this.session.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isAlive(){
return this.session.isOpen();
}
}
/CommandTask.class
/TaskCommand.class
package com.csc.library.task;
import com.csc.library.command.CscCommand;
import com.csc.library.properties.CscSession;
import com.csc.library.request.RequestWebsocket;
public interface CommandTask extends Runnable{
public RequestWebsocket getRequest();
public CscSession getSession();
public CscCommand getCommand();
}
package com.csc.library.task;
import com.csc.library.command.CscCommand;
import com.csc.library.controller.CMDModeCtrlCommand;
import com.csc.library.factory.ResponseDataFactory;
import com.csc.library.factory.ResponseFactory;
import com.csc.library.properties.CscSession;
import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseStatus;
import com.csc.library.utilities.MyLog;
public class TaskCommand implements CommandTask{
private CscSession session;
private RequestWebsocket request;
private CscCommand command;
public TaskCommand(){
}
public TaskCommand(CscSession session, RequestWebsocket req , CscCommand command) {
this.session = session;
this.request = req;
this.command = command;
}
public RequestWebsocket getRequest(){
return this.request;
}
public CscSession getSession(){
return this.session;
}
public CscCommand getCommand(){
return this.command;
}
public void run() {
try {
CMDModeCtrlCommand.getInstance()
.execute(this.getCommand(), this.getRequest());
ResponseFactory.getInstance().sendResponseWebsocket(
this.getSession(),
this.getRequest(),
ResponseDataFactory.getInstance().createResponseData(this.getCommand()),
"");
} catch (Exception e) {
MyLog.error(this , this.getSession().getUProfile(), e);
ResponseFactory.getInstance().sendResponseWebsocket(
this.getSession(),
this.getRequest(),
ResponseStatus.Error,
e.getMessage());
}
}
}
/ServerWebSocket.class
/WebSocketController.class
/WebSocketTest.class
package com.csc.library.websocket;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import com.csc.library.authen.WebSocketAuthen;
import com.csc.library.authen.WebSocketMyAuthen;
import com.csc.library.factory.PropertiesFactory;
import com.csc.library.factory.SessionFactory;
import com.csc.library.fix.StaticVariable;
import com.csc.library.properties.CscDevice;
import com.csc.library.properties.CscSession;
import com.csc.library.properties.DeviceProperties;
import com.csc.library.utilities.UProfile;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class ServerWebSocket {
private static ServerWebSocket instance = null;
private String serverName = "";
private String urlGateWay = "";
private Class classHandle = null;
private Session sessionGateWay = null;
private Map<String , CscSession> sessions = new HashMap<String , CscSession>();
private WebSocketAuthen authen = new WebSocketMyAuthen();
private ServerWebSocket(){
//load config xml websocket server
//load serverName,urlGateWay,classHandle
}
public static ServerWebSocket getInstance(){
if(instance == null){
instance = new ServerWebSocket();
try {
if(instance.classHandle != null && !instance.urlGateWay.equals("")){
instance.sessionGateWay = ContainerProvider.getWebSocketContainer()
.connectToServer(instance.classHandle , new URI(instance.urlGateWay));
}
} catch (DeploymentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
return instance;
}
public String getServerName(){
return this.serverName;
}
public boolean registerSession(Session session , String jsonProp){
try{
/* check user login */
Map<String,Object> prop = session.getUserProperties();
CscDevice deviceProp = PropertiesFactory.getInstance().createDevice(jsonProp);
UProfile upf = authen.checkPermission(deviceProp);
if(upf != null){
prop.put(StaticVariable.UPROFILE , upf);
prop.put(StaticVariable.DEVICE , deviceProp);
CscSession ses = SessionFactory.getInstance().createSession(upf, session, deviceProp);
this.addSession(deviceProp, ses);
return true;
}else{
return false;
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
public Map<String, CscSession> getSessions(){
return Collections.unmodifiableMap(sessions);
}
public void addSession(CscDevice prop , CscSession session){
sessions.put(prop.getUserName(), session);
}
public CscSession getCscSession(Session session){
Map<String,Object> prop = session.getUserProperties();
if( session != null && prop.containsKey(StaticVariable.DEVICE)
&& prop.containsKey(StaticVariable.UPROFILE)){
CscDevice device = (CscDevice)prop.get(StaticVariable.DEVICE);
if(sessions.containsKey(device.getUserName())){
return sessions.get(device.getUserName());
}
}
return null;
}
}
package com.csc.library.websocket;
import java.io.IOException;
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;
@ServerEndpoint(value = "/CSCWS/{login_properties}")
public class WebSocketController {
private ServerWebSocket serverSocket = null;
private WebSocketCommandController commandControl = WebSocketCommandController.getInstance();
//private static final Set<Session> sessions = new HashSet<Session>();
@OnOpen
public void open(Session session, EndpointConfig conf,@PathParam("login_properties") String prop) throws IOException {
serverSocket = ServerWebSocket.getInstance();
if(serverSocket.registerSession(session, prop)){
//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);
}*/
commandControl.executeCommand(serverSocket.getCscSession(session), message);
}
}
package com.csc.library.websocket;
import java.io.IOException;
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.websocket.config.CscWsServerEndpointConfig;
@ServerEndpoint(
value = "/TESTWS",
configurator = CscWsServerEndpointConfig.class
)
public class WebSocketTest {
private ServerWebSocket serverSocket = null;
private WebSocketCommandController commandControl = WebSocketCommandController.getInstance();
//private static final Set<Session> sessions = new HashSet<Session>();
@OnOpen
public void open(Session session, EndpointConfig conf) throws IOException {
serverSocket = ServerWebSocket.getInstance();
//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);
}*/
commandControl.executeCommand(serverSocket.getCscSession(session), message);
}
}
/CscWsServerEndpointConfig.class
package com.csc.library.websocket.config;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
public class CscWsServerEndpointConfig extends ServerEndpointConfig.Configurator{
@Override
public boolean checkOrigin(String originHeaderValue) {
String x = "";
return super.checkOrigin(originHeaderValue);
}
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
String xx = "";
super.modifyHandshake(sec, request, response);
}
}
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