All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xworker.debug.DebugAction Maven / Gradle / Ivy

/*******************************************************************************
* Copyright 2007-2013 See AUTHORS file.
 * 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*   http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package xworker.debug;

import java.util.ArrayList;
import java.util.List;

import org.xmeta.Action;
import org.xmeta.ActionContext;
import org.xmeta.Thing;

public class DebugAction {
	
	public static void init(ActionContext actionContext){
		//上下文所在动作上下文
		Thing context = (Thing) actionContext.get("self");
		if(!context.getBoolean("enabled")) return;
		
		context.put("startTime", System.nanoTime());
		
		//要执行的动作上下文
		ActionContext acContext = (ActionContext) actionContext.get("acContext");
		
		//取最后一个Action
		List actions = acContext.getActions();
		Action action = actions.get(actions.size() - 1);
		DebugInfo debugInfo = Debuger.getDebugInfo(Thread.currentThread());
		Debuger debuger = Debuger.getDebuger();
		if(debugInfo.status == DebugInfo.STATUS_DEBUG || debuger.getActionDebugInfo(action.getThing(), "initBreakPoint")){
			debugInfo.startDebug(acContext, DebugInfo.ACTION_STATUS_INIT);
		}		
	}
	
	public static void success(ActionContext actionContext){
		long endTime = System.nanoTime();
		
		//上下文所在动作上下文
		Thing context = (Thing) actionContext.get("self");
		if(!context.getBoolean("enabled")) return;
		
		long time = endTime - (Long) context.get("startTime");
		
		//要执行的动作上下文
		ActionContext acContext = (ActionContext) actionContext.get("acContext");
		
		//取最后一个Action
		List actions = acContext.getActions();
		Action action = actions.get(actions.size() - 1);
		
		Debuger.getDebuger().record(action.getThing().getMetadata().getPath(), time, true);
		
		DebugInfo debugInfo = Debuger.getDebugInfo(Thread.currentThread());	
		Debuger debuger = Debuger.getDebuger();
		if(debugInfo.status == DebugInfo.STATUS_DEBUG || debuger.getActionDebugInfo(action.getThing(), "successBreakPoint")){
			debugInfo.startDebug(acContext, DebugInfo.ACTION_STATUS_SUCCESS);
		}	
	}
	
	public static void exception(ActionContext actionContext){
		long endTime = System.nanoTime();
		
		//上下文所在动作上下文
		Thing context = (Thing) actionContext.get("self");
		if(!context.getBoolean("enabled")) return;
		
		long time = endTime - (Long) context.get("startTime");
		
		//要执行的动作上下文
		ActionContext acContext = (ActionContext) actionContext.get("acContext");
		
		//取最后一个Action
		List actions = acContext.getActions();
		Action action = actions.get(actions.size() - 1);
		
		Debuger.getDebuger().record(action.getThing().getMetadata().getPath(), time, false);
		
		List callers = new ArrayList();
		for(int i=acContext.getScopesSize() - 1; i>=0; i--){
			callers.add(acContext.getScope(i).getCaller());
		}
		
		Debuger.getDebuger().addException(new ExceptionRecord(callers, (Throwable) actionContext.get("exception")));
		
		DebugInfo debugInfo = Debuger.getDebugInfo(Thread.currentThread());	
		Debuger debuger = Debuger.getDebuger();
		if(debugInfo.status == DebugInfo.STATUS_DEBUG || debuger.getActionDebugInfo(action.getThing(), "exceptionBreakPoint")){
			debugInfo.startDebug(acContext, DebugInfo.ACTION_STATUS_EXCEPTION);
		}
	}
	
	public static Object inherit(ActionContext actionContext){
		return null;
	}
}