package com.eactive.eai.agent.command; 

import java.lang.reflect.Constructor;

import com.eactive.eai.common.util.Logger;

/** 
* 1. 기능 : 공통 command
* 2. 처리 개요 : 
* 3. 주의사항 
*
* @author  
* @version  v 1.0.0
* @see 관련 기능을 참조
* @since   
*  
*/
public class CommonCommand extends Command { 

	private static final long serialVersionUID = 1L;
	
	
	public CommonCommand(String name,Object args) {
		this.name = name;
		this.args = args;
	}
	
	public CommonCommand(String name) {
		this.name = name;
	}
	
    public Object execute() throws CommandException
    {
        if (name.indexOf(".") >=0 ){
			try {
				if(this.name != null) this.name = this.name.trim();
				@SuppressWarnings("rawtypes")				
				Class cl = Class.forName(this.name);
				@SuppressWarnings({ "rawtypes", "unchecked" })
				Constructor constructor = cl.getConstructor();
		        Object obj = constructor.newInstance();
				Command c = (Command)obj;
				c.setArgs(args);
				return c.execute();
				
			} catch (Exception e) {
				Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
				logger.error("CommonCommand Exception "+e.getMessage(),e);
	            throw new CommandException(e.getMessage());
			}
        }else{
            String rspErrorCode = "RECEAIMCM001";
            String msg = makeException(rspErrorCode, null);
            throw new CommandException(msg);
        }
    }
} 
