package CoreFS.SA01;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import CoreFS.SA06.CoreICommunicationService;
import CoreFS.SA06.CoreParamObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SB03.CoreIvokeManage;
import CoreFS.Util.StringUtils;

/**
 * @desc Hession接口的实现类 被spring导出为hession服务 需要在spring服务配置文件中配置
 * @author sunjiawang
 * @create 091128
 * @modify 091128
 */
public class CoreCommunicationService implements CoreICommunicationService {

	public static CoreIvokeManage imanage = null;
	static {
		if (imanage == null) {
			imanage = new CoreIvokeManage();
		}
	}

	public CoreReturnObject MethodHandler(CoreParamObject ctx) {
		CoreReturnObject rtnContext = new CoreReturnObject();
		String id = ctx.v_id;
		Object[] strParms = ctx.v_reqParameters;

		return imanage.callActivity(id, strParms);

	}

	// 平台内部使用
	public CoreReturnObject runActivtity(CoreParamObject ctx) {
		CoreReturnObject rtnContext = new CoreReturnObject();		
		String message = null;
		boolean isover = false;
		String overDate = "2011-12-31";

		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
		String datetime = tempDate.format(new java.util.Date());

		Date od = null;
		try {
			od = tempDate.parse(overDate);
		} catch (ParseException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		/*
		 * 控制
		 */
//		try {
//			if (tempDate.parse(datetime).getTime() > od.getTime()) {
//				message = "null\u60a8\u6240\u4f7f\u7528\u7684\u5f00\u53d1\u7248\u5df2\u5230\u671f\uff0c\u5982\u9700\u7ee7\u7eed\u4f7f\u7528\uff0c\u8bf7\u4e0e\u89c6\u62d3\u516c\u53f8\u8054\u7cfb";
//				rtnContext.setResult(message);
//				rtnContext.setV_errCode(-1);
//				isover = true;
//				CoreException ce = new CoreException(message);
//				throw (ce);
//
//			}
//		} catch (ParseException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		//控制
		if (!isover) {
			String className = ctx.v_serviceName;
			String methodName = ctx.v_method;
			Object[] strParms = ctx.v_reqParameters;
			StringUtils.Sysid=ctx.v_mid;

			System.out.println("className--> " + className + " method-->"
					+ methodName);

			Object instance = null;
			Method method = null;
			String errmsg = null;
			try {
				// 创建反射类的一个实例
				if (isSupport(className)) {
					instance = Class.forName(className).newInstance();
				}
				
				Class[] types = null;
				if (strParms != null) {
					int len = strParms.length;
					// 获取方法参数类型数组
					types = new Class[len];
					for (int i = 0; i < len; i++) {
						if (strParms[i] == null) {
							types[i] = Object.class;
						} else {
							types[i] = strParms[i].getClass();
						}						
					}
				}
				method = instance.getClass().getMethod(methodName, types);

				rtnContext = (CoreReturnObject) method.invoke(instance,
						strParms);
				//添加至统计
				long time = System.currentTimeMillis();
				Date callTime=new Date(time);				
				//StoreFactory.getStore().AddComponents(className+"."+methodName, callTime);
			} catch (CoreException ce) {
				rtnContext = rtnContext.SetErrInfo(ce.getMessage());
			} catch (Exception e) {
				// 处理反射调用方法处理的的异常 modify 20100107
				Throwable t = null;
				if (e instanceof InvocationTargetException) {
					InvocationTargetException targetEx = (InvocationTargetException) e;
					t = targetEx.getTargetException();
					rtnContext = rtnContext.SetErrInfo(t.getMessage());
				} else {
					rtnContext = rtnContext.SetErrInfo(e.getMessage());
				}
			}
		}
		//添加模块统计信息
		
		System.out.println("result-->\n"
				+ (rtnContext.getResult() == null ? "" : "SUCCESS"));
		return rtnContext;
	}

	private boolean isSupport(String classname) {

		try {
			Class.forName(classname).newInstance();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}


	public String runXFire(String classname, String methodname, Object[] params) {
		CoreParamObject ctx = new CoreParamObject();
		ctx.setV_serviceName(classname);
		ctx.setV_method(methodname);
		ctx.setV_reqParameters(params);
		return this.runActivtity(ctx).getResult().toString();
	}

}
