new Exception().getStackTrace()用法


StackTraceElement[] stacks = new Exception().getStackTrace();


stacks会打印出当前调用者的方法,会把调用的所有的堆栈信息打印出来,比如:A调用B,B调用C,都会在这里拿到。



import static org.junit.Assert.assertNotNull;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;

import com.yk.platform.framework.newSystem.service.impl.Organization;
import com.yk.platform.framework.system.domain.impl.BaseOrganization;
import com.yk.platform.framework.system.domain.impl.User;
import com.yk.platform.logger.PLogger;
import com.yk.platform.logger.impl.ServerPLoggerImpl;
import com.yk.platform.permission.domain.impl.Dept;
import com.yk.platform.script.formvars.ObjectBean;
import com.yk.platform.script.formvars.ScriptVarsObject;
import com.yk.platform.script.support.MapScriptable;
import com.yk.platform.scriptrunner.SQLOperator;
import com.yk.platform.scriptrunner.ServerWorkflowScriptRunner;
import com.yk.platform.scriptrunnerintf.Context;
import com.yk.platform.ui.client.render.service.AffectedItemAccess;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:TestFrame.xml",
		"classpath*:/commonContext.xml",
		"classpath*:/loggerContext.xml","classpath*:/renderContext.xml",
		"classpath*:/reportContext.xml","classpath*:/ScriptRunnerContext.xml","classpath*:/designerContext.xml",
		"classpath*:/workflowContext.xml","classpath*:/workflowMetadata.xml","classpath*:/invoiceNoContext.xml",
		"classpath:unittestContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
public abstract class TestFrame extends org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests{
	private ScriptEngineManager mgr = null;
	private ScriptEngine engine = null;
	private CompiledScript compiledScript = null;
	private Compilable compiler = null; // 编译
	private PLogger log = new ServerPLoggerImpl();
	private String formId = "";
	private String workflowscript = "";
	public User user = new User();
	public Map<String,String> _user = new HashMap<String,String>();
	public Map<String,String> _dept = new HashMap<String,String>();
	public Map<String,String> _account = new HashMap<String,String>();
	public Map<String,String> _organization = new HashMap<String,String>();
	public Map<String,String> _baseOrganization = new HashMap<String,String>();
	/**
	 * bussinessObject 业务对象
	 */
	private Object bussinessObject = null;
	private String method;

	@Resource
	ServerWorkflowScriptRunner _workflow;
	@Resource
	SQLOperator _sql;
	@Resource
	Context _context;
	@Resource
	DataSource dataSource;
	@Resource
	SessionFactory hib_sessionFactory;
	@Resource
	AffectedItemAccess affectedAccess;
	@Resource
	SQLHelper SQLHelper;
	
	private Session session = null;
	private Connection conn = null;

	/**
	 * 准备脚本运行环境
	 */
	@Before
	public void before() {
		/**********hibernate预处理********
		 * hibernate进一步脱离Spring控制,手动从数据源中取Connection,然后传给SessionFactory创建Session
		 * <property name="useTransactionAwareDataSource" value="true"></property>
		 * */
		
		conn = null;
		conn = DataSourceUtils.getConnection(dataSource);
		/**
		 * 使用dataSource创建连接和Session
		 */
		this.session = hib_sessionFactory.openSession(conn);
		SQLHelper.setSession(this.session);
		SQLHelper.setSqlOperator(_sql);
		/**********************************/
		try{
			//rhino引擎,创建engine
			mgr = new ScriptEngineManager();
			engine = mgr.getEngineByName("rhino");
			
			this.workflowscript = this.getWorkFlowScript();
			this.userInit(user);
			this.workflowInit();
			
			this._context.__addParameter("_workflow",_workflow);
			this._context.__addParameter("_user",new MapScriptable(_user));
			this._context.__addParameter("_dept",new MapScriptable(_dept));
			this._context.__addParameter("_account",new MapScriptable(_account));
			this._context.__addParameter("_organization",new MapScriptable(_organization));
			this._context.__addParameter("_baseorganization",new MapScriptable(_baseOrganization));
			//this._context.__addParameter("_log", new ServerPLoggerScriptRunner(log));
			//_log,_sql,_systemutil,_msg等通过spring注入。
	
			/*this._context.__addParameter("_systemutil", new SystemUtil());
			this._context.__addParameter("_msg",new MessageScriptRunner(log));*/
			
			//this._context._
			Map params=new HashMap();
			params.put("_workflow",_workflow);
			params.put("affectedaccess", affectedAccess);
			params.put("userName", _user.get("user_name"));
			params.put("organizationId", _organization.get("id"));
			params.put("deptId", _dept.get("id"));
			params.put("userId", _user.get("id"));
			params.put("deptCode", _dept.get("dept_code"));
			params.put("orgCode", _organization.get("org_code"));
			this._context.__init(params);
			this._context.__bindToEngine(engine);
			//this._context.__bindToEngine(bindings);
			compiler = (Compilable) engine;
			this.bussinessObject = null;
			
			this.formId = this.getFormId();
			this.init();
			this.scriptRunnerInit();
			
		}catch(Exception e){
			e.printStackTrace();
			DataSourceUtils.releaseConnection(conn, dataSource);
		}
		
	}
	
	@After
	public void After(){
		session.clear();
		DataSourceUtils.releaseConnection(conn, dataSource);
		
	}

	public SQLOperator getSQLOperator() {
		return this._sql;
	}
	/*public InvoiceNo getService(){
		return this.invoiceNoService;
	}*/

	/**
	 * 得到业务对象
	 * 
	 * @return
	 * @throws Exception
	 */
	public Object getBussinessObject() throws Exception {
		
		if (this.bussinessObject != null) {
			return this.bussinessObject;
		}

		//获取领域对象名
		Class clazz = this.getClass();
		ScriptObject objectAnn = (ScriptObject) clazz
				.getAnnotation(ScriptObject.class);
		if (objectAnn == null || objectAnn.equals("")) {
			throw new Exception("未指定ObjectBussiness Name!!");
		}
		String objectName = objectAnn.name();
		String script = "";
		
		script = this.workflowscript;
		if (script==null||script.equals("")){
			// 查询数据库
			try{
				String sql = "select t.udm_scripts from pf_scripts_metadata t where t.udm_scope = 2 and t.udm_name = '"
						+ objectName + "'";
				Map<String, Object> objectParams = new HashMap<String, Object>();
				script = _sql.queryForCLob(sql, objectParams);
				if (script == null||script.equals("")) {
					Assert.fail("未从数据库得到脚本");
					//throw new Exception("未从数据库得到脚本");
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}

		engine.eval(script);
		engine.eval(" var _objme=new " + objectName + "(_context);");
		this.bussinessObject = engine.get("_objme");
		return this.bussinessObject;
	}
	/**
	 * 准备变量
	 */
	private void scriptRunnerInit() {
		
		//this.engine.put("_workflow", _workflow);
	}

	public void putVar(String var, Object object) {
		this.engine.put(var, object);
	}

	/**
	 * 执行脚本中的方法,并返回结果
	 * @param name
	 * @param objs
	 * @return
	 * @throws Exception
	 */
	public Object invoke(String name, Object... objs) throws Exception {
		
		//System.out.println("invoke~~~~~~~~~~~");
		Object bussinessObject = this.getBussinessObject();
		Invocable inv = (Invocable) engine;
		StackTraceElement[] stacks = new Exception().getStackTrace();
		this.method = stacks[1].getMethodName();
		// new Exception().printStackTrace();
		/**
		 * Java转Js
		 */
		Object[] jsparams = new Object[objs.length];
		ObjectBean obean = null;
		for (int i = 0; i < objs.length; i++) {
			obean = new ObjectBean();
			obean.setValue(objs[i]);
			if (objs[i] == null) {
				obean.setClassName(String.class.getName());
			} else {
				obean.setClassName(objs[i].getClass().getName());
			}
			jsparams[i] = ScriptVarsObject.javaToJs(obean);
		}
		/**
		 * 执行结果
		 */
		Object obj = inv.invokeMethod(bussinessObject, name, objs);
		/**
		 * 将执行结果obj:Js转Java
		 */
		if (obj == null)
			return null;
		Object ret = ScriptVarsObject._jsToJava(obj);
		return ret;
	}
	public abstract String getFormId();
	
	//工作流初始化
	private void workflowInit() {
		assertNotNull(this._workflow);
		this._workflow.__setUserName(user.getUserName());
		this._workflow.__setFormId(this.formId);
		this._workflow.__setUserId(user.getId());
	}
	public abstract void init();
	public abstract void userInit(User user);
	public abstract String getWorkFlowScript();

	public Session getSession() {
		return session;
	}

	public SQLHelper getSQLHelper() {
		return SQLHelper;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值