package com.huawei.sdtrp.command;
import java.util.logging.Logger;
/**
* <pre>
* 命令解析器,默认实现
* 方法一:
* 使用ClassLorder来加载(包名+CMD+"Command")命令类路径
* 具有包名限制
* 方法二:
* 在实际Action中,采用Spring注入包名称属性,再使用命令解析器实例化,具体的命令对象(可行,但是设计不太合理[不是Action要做的事情])
* 方法三:
* 有没有另外的方式,来构造命令对象???(只知道命令字符串)
* </pre>
* @author cWX194449
*
*/
public class CommandParserImpl implements CommandParser {
private static Logger log = Logger.getLogger(CommandParserImpl.class.getCanonicalName());
private String packageNameSpace = "com.huawei.sdtrp.command";//命令实现类包路径
@Override
public Command parse(String command) {
//这种方法,包名定死了
// String packageNameSpace = "com.huawei.sdtrp.command" ;
String suffix = "Command";
String fullCommandName = packageNameSpace.concat(".").concat(command).concat(suffix);
try {
// return (Command)Class.forName(fullCommandName).newInstance();
return (Command)this.getClass().getClassLoader().loadClass(fullCommandName).newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return null;
/*
String suffix = "Command";
ActionContext ac = ActionContext.getContext();
ac.
WebApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(config.getServletContext());
return (Command)ctx.getBean(command.concat(suffix));
return null;
*/
}
}
//Action
package com.huawei.sdtrp.action;
import com.huawei.sdtrp.command.Command;
import com.huawei.sdtrp.command.CommandParser;
import com.huawei.sdtrp.model.ExecutedResult;
import com.huawei.sdtrp.model.Terminal;
import com.opensymphony.xwork2.ActionSupport;
/**
* 基本Action
* @author cWX194449
*
*/
public abstract class BaseAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Terminal terminal ;//表单属性搜集
public Terminal getTerminal() {
return terminal;
}
public void setTerminal(Terminal terminal) {
this.terminal = terminal;
}
private CommandParser commandParser ;//注入
public CommandParser getCommandParser() {
return commandParser;
}
public void setCommandParser(CommandParser commandParser) {
this.commandParser = commandParser;
}
private ExecutedResult executedResult ;
public ExecutedResult getExecutedResult() {
return executedResult;
}
public void setExecutedResult(ExecutedResult executedResult) {
this.executedResult = executedResult;
}
@Override
public String execute() throws Exception {
// Object object = this.commandParser.parse(this.terminal.getTerminalData().getCmd()).execute();
// Object object = this.generateCommand(terminal).execute();
// return null == object ?"NULL":object.toString();
if(this.validateParameters()){
this.executedResult = this.generateCommand(terminal).execute();
}
if(null == this.executedResult){//异常,构造命令执行结果
// String msg = this.terminal.getTerminalData().getCmd().concat("命令请求异常");
// throw new Exception(msg);
//TODO 异常命令执行结果,构造
this.executedResult = new ExecutedResult();
}
/*以流的形式,响应终端*/
this.responseTerminal();
// return this.executedResult.getResultStatus();
return null;
}
/**
* 抽象工厂CommandParser产生Command对象
* @param terminal
* @return
*/
public Command generateCommand(Terminal terminal){
// return this.commandParser.parse(this.terminal.getTerminalData().getCmd());
// Command command = this.commandParser.parse(this.terminal.getTerminalData().getCmd());
Command command = this.commandParser.parse(this.terminal.getCmd());
command.init(terminal);
return command ;
};
/**
* 请求参数,检验(default:true相当于不验证)
* 若false,需要重新赋值给命令执行结果
* (过滤非法请求)
* @return
*/
public boolean validateParameters(){
return true;
}
/**
* <pre>
* 以流的形式,写回到终端
* 根据每个每个命令,返回的内容不一样,需要自已定义实现化
* </pre>
*/
public abstract void responseTerminal();
}
//Action
package com.huawei.sdtrp.action;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.huawei.sdtrp.model.ExecutedResult;
import com.opensymphony.xwork2.ActionContext;
/**
* 注册动作
* 1、请求数据校验
* 2、授权检查
* @author cWX194449
*
*/
public class RegisterationAction extends BaseAction {
private static final long serialVersionUID = -4767244117752570601L;
private String encoding = "UTF-8";
@Override
public boolean validateParameters() {
// TODO Auto-generated method stub
return true;
}
@Override
public void responseTerminal() {
ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
Map session = context.getSession();
ExecutedResult executedResult = this.getExecutedResult();
try {
OutputStream out = response.getOutputStream();
byte[] cmdBytes = "Cmd=".concat(executedResult.getCmd()).getBytes(encoding);
out.write(cmdBytes);//命令回写
out.write("&".getBytes(encoding));//分隔符
byte[] codeBytes = "Code=".concat(executedResult.getCode()).getBytes(encoding);
out.write(codeBytes);//状态回写
out.write("&".getBytes(encoding));//分隔符
byte[] descriptionBytes = "Description=".concat(executedResult.getDescription()).getBytes(encoding);
out.write(descriptionBytes);
// out.write("&".getBytes(encoding));//分隔符
out.flush();
// out.close();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
分享到:
相关推荐
非常简单的类,留给自己看非常简单的类,留给自己看非常简单的类,留给自己看
参数中-vmargs的意思是设置JVM...非堆就是JVM留给自己用的,所以方法区、JVM内部处理或优化所需的内存(如JIT编译后的代码缓存)、每个类结构(如运行时常数池、字段和方法数据)以及方法和构造方法的代码都在非堆内存中。
现代人越来越忙了,如何管理自己的时间是一个十分重的问题,看看这由“时间管理教皇”德国作家洛塔尔•赛韦特写的文章吧,你你一定收获不小!
ssh基本简单整合 基础代码 留给自己用的
linux虚拟机的搭档,留给自己用的,找着方便
根据当下实际情况使用升级版本的jar,目前框架未搭建成功。但我想留下一些记录
主要留给自己用的一个小教程组合,实用性很高,各位网友就不要下了吧
yodao字典,非常牛的,留给自己用,她具有翻译整篇文章的功能,看英文资料很方便
一款常用的android app框架,里面实现了侧边栏,下拉刷新,以及类似QQ底部tab切换的效果,使用这个框架可以搭建自己的app,只需要修改布局文件,里面有很多没实现的地方,提交上来一是留给自己将来完善,二是留给...
baidu地图离线api2.0版的,和官方的版本一模一样。 本来做的是留给自己用的,想着还是拿出来分享,帮助有需要人。 不知道做这样的文档是不是“违规”会不必被“和谐”。
留给自己以后开发备用,已做简单的封装
留给自己看的备件
MapReduce过程 visio图 留给自己看的
kernel-devel玩linux会用得到的包,留给自己和用得到的人
最新版的jQuery 1.6 ,下载下来留给自己,也留给需要使用的人,有很多的新特性呢~~~
呵呵,最新版的Ext 4.0,官网下载,留给自己,也留给大家!
单反相机使用攻略,这个主要是留给自己以后查看的,写的真是太好了
自己在开发工程中整理的那些关于liferay portlet开发的一些东西,留给自己做笔记用
自己留给自己当做备忘录,用于下载最新的PropEdit的方法。
opencv工程应用使用小工具,只是留给自己做备份,不建议下载!!!!!