`

留给自己用的

    博客分类:
  • java
 
阅读更多
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();
		}
	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics