Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.alogic.xscript.AbstractLogiclet Maven / Gradle / Ivy
package com.alogic.xscript;
import java.lang.reflect.Constructor;
import java.util.Hashtable;
import java.util.Map;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.json.JsonObject;
import com.alogic.xscript.doc.xml.XmlObject;
import com.alogic.xscript.log.LogInfo;
import com.alogic.xscript.plugins.Array;
import com.alogic.xscript.plugins.ArrayItem;
import com.alogic.xscript.plugins.Asynchronized;
import com.alogic.xscript.plugins.Check;
import com.alogic.xscript.plugins.CheckAndSetDefault;
import com.alogic.xscript.plugins.Constants;
import com.alogic.xscript.plugins.Decrypt;
import com.alogic.xscript.plugins.Encrypt;
import com.alogic.xscript.plugins.Except;
import com.alogic.xscript.plugins.ForEach;
import com.alogic.xscript.plugins.Formula;
import com.alogic.xscript.plugins.FromEnv;
import com.alogic.xscript.plugins.FromSettings;
import com.alogic.xscript.plugins.Get;
import com.alogic.xscript.plugins.GetAsJson;
import com.alogic.xscript.plugins.Include;
import com.alogic.xscript.plugins.Location;
import com.alogic.xscript.plugins.Log;
import com.alogic.xscript.plugins.Lowercase;
import com.alogic.xscript.plugins.Match;
import com.alogic.xscript.plugins.Message;
import com.alogic.xscript.plugins.Obj;
import com.alogic.xscript.plugins.Now;
import com.alogic.xscript.plugins.Remove;
import com.alogic.xscript.plugins.Repeat;
import com.alogic.xscript.plugins.Scope;
import com.alogic.xscript.plugins.Segment;
import com.alogic.xscript.plugins.Select;
import com.alogic.xscript.plugins.Set;
import com.alogic.xscript.plugins.SetAsJson;
import com.alogic.xscript.plugins.Sleep;
import com.alogic.xscript.plugins.Substr;
import com.alogic.xscript.plugins.Switch;
import com.alogic.xscript.plugins.Template;
import com.alogic.xscript.plugins.Throw;
import com.alogic.xscript.plugins.Trim;
import com.alogic.xscript.plugins.UUid;
import com.alogic.xscript.plugins.Uppercase;
import com.alogic.xscript.plugins.Using;
import com.alogic.xscript.plugins.Hash;
import com.alogic.metrics.Fragment;
import com.alogic.metrics.stream.MetricsCollector;
import com.alogic.metrics.stream.MetricsHandlerFactory;
import com.alogic.tracer.Tool;
import com.alogic.tracer.TraceContext;
import com.anysoft.stream.Handler;
import com.anysoft.util.BaseException;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.XmlElementProperties;
/**
* 虚基类
*
* @author duanyy
* @version 1.6.5.13 [20160612 duanyy]
* - 增加Encrypt,Decrypt,Check,CheckAndSetDefault等插件
*
* @version 1.6.6.1 [20160823 duanyy]
* - 增加getAsJson和setAsJson插件
*
* @version 1.6.6.13 [20170109 duanyy]
* - 采用新的指标接口
*
* @version 1.6.7.3 [20170118 duanyy]
* - 对tlog的开启开关进行了统一
*
* @version 1.6.7.9 [20170201 duanyy]
* - 采用SLF4j日志框架输出日志
*
* @version 1.6.8.4 [20170329 duanyy]
* - 增加match插件
*
* @version 1.6.8.5 [20170331 duanyy]
* - 增加rem插件
*
* @version 1.6.8.14 [20170509 duanyy]
* - 增加xscript的中间文档模型,以便支持多种报文协议
*
*/
public abstract class AbstractLogiclet implements Logiclet,MetricsCollector{
/**
* a logger of log4j
*/
public static final Logger logger = LoggerFactory.getLogger(Logiclet.class);
/**
* 父节点
*/
private Logiclet parent = null;
/**
* xml tag
*/
private String xmlTag = null;
/**
* 当前活动
*/
private String activity;
private boolean traceEnable = false;
/**
* 静态注册表(全局有效)
*/
protected static Hashtable> staticModules =
new Hashtable>();
/**
* 注册表(实例有效)
*/
private Hashtable> modules =
new Hashtable>();
public static final String STMT_FINALLY = "finally";
public static final String STMT_EXCEPTION = "except";
public static final String STMT_ASYNC = "async";
public static final String STMT_USING = "using";
public static final String STMT_SEGMENT = "segment";
public static final String STMT_THROW = "throw";
public static final String STMT_INCLUDE = "include";
public static final String STMT_TEMPLATE = "template";
public static final String STMT_HELLO = "hello";
public static final String STMT_MSG = "msg";
public static final String STMT_LOCATION = "location";
public static final String STMT_SCOPE = "scope";
public static final String STMT_GET = "get";
public static final String STMT_SET = "set";
public static final String STMT_SELECT = "select";
public static final String STMT_ENCRYPT = "encrypt";
public static final String STMT_DECRYPT = "decrypt";
public static final String STMT_CHECK = "check";
public static final String STMT_CHECK_AND_SET = "checkAndSet";
public static final String STMT_REPEAT = "repeat";
public static final String STMT_CASE = "case";
public static final String STMT_SWITCH = "switch";
public static final String STMT_DEFAULT = "default";
public static final String STMT_FORMULA = "formula";
public static final String STMT_NOW = "now";
public static final String STMT_UUID = "uuid";
public static final String STMT_CONSTANTS = "constants";
public static final String STMT_LOGGER = "logger";
public static final String STMT_LOG = "log";
public static final String STMT_SLEEP = "sleep";
public static final String STMT_FOREACH = "foreach";
public static final String STMT_OBJ = "obj";
public static final String STMT_ARRAY = "array";
public static final String STMT_ARRAYITEM = "array-item";
public static final String STMT_TRIM = "trim";
public static final String STMT_UPPERCASE = "uppercase";
public static final String STMT_LOWERCASE = "lowercase";
public static final String STMT_SUBSTR = "substr";
public static final String STMT_SETTING = "setting";
public static final String STMT_ENV = "env";
public static final String STMT_GETASJSON = "getAsJson";
public static final String STMT_SETASJSON = "setAsJson";
public static final String STMT_HASH = "hash";
public static final String STMT_MATCH = "match";
public static final String STMT_REM = "rem";
protected static Handler metricsHandler = null;
static{
staticModules.put(STMT_INCLUDE, Include.class);
staticModules.put(STMT_SEGMENT, Segment.class);
staticModules.put(STMT_USING, Using.class);
staticModules.put(STMT_ASYNC, Asynchronized.class);
staticModules.put(STMT_THROW, Throw.class);
staticModules.put(STMT_EXCEPTION, Except.class);
staticModules.put(STMT_FINALLY, Except.class);
staticModules.put(STMT_TEMPLATE, Template.class);
staticModules.put(STMT_MSG, Message.class);
staticModules.put(STMT_LOCATION, Location.class);
staticModules.put(STMT_SCOPE, Scope.class);
staticModules.put(STMT_GET, Get.class);
staticModules.put(STMT_SET, Set.class);
staticModules.put(STMT_SELECT, Select.class);
staticModules.put(STMT_ENCRYPT,Encrypt.class);
staticModules.put(STMT_DECRYPT,Decrypt.class);
staticModules.put(STMT_CHECK,Check.class);
staticModules.put(STMT_CHECK_AND_SET,CheckAndSetDefault.class);
staticModules.put(STMT_REPEAT,Repeat.class);
staticModules.put(STMT_SWITCH, Switch.class);
staticModules.put(STMT_FORMULA, Formula.class);
staticModules.put(STMT_NOW,Now.class);
staticModules.put(STMT_UUID,UUid.class);
staticModules.put(STMT_CONSTANTS, Constants.class);
staticModules.put(STMT_LOGGER, com.alogic.xscript.plugins.Logger.class);
staticModules.put(STMT_LOG, Log.class);
staticModules.put(STMT_SLEEP, Sleep.class);
staticModules.put(STMT_FOREACH, ForEach.class);
staticModules.put(STMT_OBJ, Obj.class);
staticModules.put(STMT_ARRAY, Array.class);
staticModules.put(STMT_ARRAYITEM, ArrayItem.class);
staticModules.put(STMT_TRIM, Trim.class);
staticModules.put(STMT_UPPERCASE,Uppercase.class);
staticModules.put(STMT_LOWERCASE,Lowercase.class);
staticModules.put(STMT_SUBSTR,Substr.class);
staticModules.put(STMT_SETTING, FromSettings.class);
staticModules.put(STMT_ENV,FromEnv.class);
staticModules.put(STMT_GETASJSON, GetAsJson.class);
staticModules.put(STMT_SETASJSON, SetAsJson.class);
staticModules.put(STMT_HASH, Hash.class);
staticModules.put(STMT_MATCH, Match.class);
staticModules.put(STMT_REM,Remove.class);
metricsHandler = MetricsHandlerFactory.getClientInstance();
}
public AbstractLogiclet(String tag,Logiclet p){
xmlTag = tag;
parent = p;
}
public boolean traceEnable(){
return traceEnable;
}
@Override
public void configure(Properties p) {
activity = PropertiesConstants.getString(p,"activity",xmlTag);
traceEnable = PropertiesConstants.getBoolean(p, "tracer.xscript.enable", traceEnable);
}
@Override
public void configure(Element e, Properties p) {
Properties props = new XmlElementProperties(e,p);
configure(props);
}
@Override
public void log(LogInfo logInfo){
if (parent != null){
parent.log(logInfo);
}
}
public void log(String message,String level,int progress){
log(new LogInfo(activity,message,level,progress));
}
public void log(String message,String level){
log(message,level,-2);
}
public void log(String message,int progress){
log(message,"info",progress);
}
public void log(String message){
log(message,-2);
}
@Override
public void report(Element xml) {
if (xml != null){
xml.setAttribute("module", getClass().getName());
}
}
@Override
public void report(Map json) {
if (json != null){
json.put("module", getClass().getName());
}
}
public void execute(XsObject root,XsObject current,LogicletContext ctx,ExecuteWatcher watcher){
long start = System.currentTimeMillis();
boolean error = false;
String msg = "OK";
TraceContext tc = null;
if (traceEnable()){
tc = Tool.start();
}
try {
onExecute(root,current,ctx,watcher);
}catch (Exception ex){
error = true;
msg = ex.getMessage();
throw ex;
}finally{
if (watcher != null){
watcher.executed(this, ctx, error,start, System.currentTimeMillis() - start);
}
if (traceEnable()){
Tool.end(tc, "LOGICLET", getXmlTag(), error?"FAILED":"OK", msg);
}
}
}
@SuppressWarnings("unchecked")
protected void onExecute(XsObject root,XsObject current,final LogicletContext ctx,final ExecuteWatcher watcher){
if (root instanceof JsonObject){
onExecute((Map)root.getContent(),(Map)current.getContent(),ctx,watcher);
}else{
if (root instanceof XmlObject){
onExecute((Element)root.getContent(),(Element)current.getContent(),ctx,watcher);
}else{
throw new BaseException("core.not_supported",String.format("Tag %s does not support protocol %s",this.xmlTag,root.getClass().getName()));
}
}
}
protected void onExecute(final Map root, final Map current,final LogicletContext ctx,final ExecuteWatcher watcher){
throw new BaseException("core.not_supported",String.format("Tag %s does not support protocol %s",this.xmlTag,root.getClass().getName()));
}
protected void onExecute(final Element root, final Element current,final LogicletContext ctx,final ExecuteWatcher watcher){
throw new BaseException("core.not_supported",String.format("Tag %s does not support protocol %s",this.xmlTag,root.getClass().getName()));
}
public String getArgument(String id,String dftValue,LogicletContext ctx){
return ctx.GetValue(id, dftValue);
}
public long getArgument(String id,long dftValue,LogicletContext ctx){
String value = getArgument(id,String.valueOf(dftValue),ctx);
try {
return Long.parseLong(value);
}catch (NumberFormatException ex){
return dftValue;
}
}
public int getArgument(String id,int dftValue,LogicletContext ctx){
String value = getArgument(id,String.valueOf(dftValue),ctx);
try {
return Integer.parseInt(value);
}catch (NumberFormatException ex){
return dftValue;
}
}
public boolean getArgument(String id,boolean dftValue,LogicletContext ctx){
String value = getArgument(id,Boolean.toString(dftValue),ctx);
try {
return BooleanUtils.toBoolean(value);
}catch (NumberFormatException ex){
return dftValue;
}
}
public String getArgument(String id,LogicletContext ctx){
String value = ctx.GetValue(id, "");
if (StringUtils.isEmpty(value)){
throw new BaseException("client.args_not_found","Can not find parameter:" + id);
}
return value;
}
@Override
public Logiclet parent() {
return parent;
}
@Override
public boolean isExecutable() {
return true;
}
@Override
public String getXmlTag() {
return xmlTag;
}
@Override
public Logiclet createLogiclet(String xmlTag, Logiclet parent) {
Logiclet found = null;
//首先到staticModules中查找
Class extends Logiclet> clazz = staticModules.get(xmlTag);
if (clazz != null){
found = createLogiclet(clazz,xmlTag,parent);
}
if (found == null){
found = onCreateLogiclet(xmlTag,parent);
}
if (found == null){
Logiclet p = parent();
if (p != null){
found = p.createLogiclet(xmlTag, parent);
}
}
return found;
}
protected Logiclet onCreateLogiclet(String xml, Logiclet p) {
Class extends Logiclet> clazz = modules.get(xml);
return clazz == null ? null : createLogiclet(clazz,xml,p);
}
protected Logiclet createLogiclet(Class extends Logiclet> clazz,String xmlTag,Logiclet parent){
try {
Constructor extends Logiclet> constructor = clazz.getConstructor(String.class,Logiclet.class);
return constructor.newInstance(new Object[]{xmlTag,parent});
} catch (Exception e) {
logger.error("Can not create segment instance:" + clazz.getName(),e);
return null;
}
}
@Override
public void registerModule(String xmltag, Class extends Logiclet> clazz) {
modules.put(xmltag, clazz);
}
@Override
public void registerExceptionHandler(String id, Logiclet exceptionHandler) {
logger.warn("Exception handler is not supported,Ignored.");
}
@Override
public void registerLogger(Handler logHandler) {
logger.warn("Log handler is not supported,Ignored.");
}
@Override
public void metricsIncr(Fragment fragment){
if (metricsHandler != null){
metricsHandler.handle(fragment,System.currentTimeMillis());
}
}
}