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

com.anysoft.stream.ScriptHandler Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.anysoft.stream;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.Script;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.json.JsonObject;
import com.anysoft.util.DataProviderProperties;
import com.anysoft.util.IOTools;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.Settings;
import com.anysoft.util.XmlTools;
import com.anysoft.util.resource.ResourceFactory;

/**
 * 基于XScript的处理器
 * 
 * @author yyduan
 *
 * @param 
 * 
 * @since 1.6.6.13
 * 
 * @version 1.6.8.14 [20170509 duanyy] 
* - 增加xscript的中间文档模型,以便支持多种报文协议
* */ public class ScriptHandler extends AbstractHandler { /** * 脚本 */ protected Logiclet stmt = null; @Override protected void onConfigure(Element e, Properties p) { Element script = XmlTools.getFirstElementByPath(e, "script"); if (script != null){ stmt = new Script("script",null); stmt.configure(script, p); }else{ String src = PropertiesConstants.getString(p,"xrc",""); if (StringUtils.isNotEmpty(src)){ Document doc = loadDocument(src); if (doc != null){ stmt = new Script("script",null); stmt.configure(doc.getDocumentElement(), p); } }else{ LOG.error("Can not find script to run."); } } } @Override protected void onHandle(data _data, long timestamp) { if (stmt == null){ LOG.error("The script is null"); return ; } try { // 向队列报告任务已经开始 Map root = new HashMap(); XsObject doc = new JsonObject("root",root); DataProviderProperties p = new DataProviderProperties(_data); LogicletContext ctx = new LogicletContext(p); // 执行任务 stmt.execute(doc, doc, ctx, null); } catch (Exception t) { LOG.error("Failed to execute script", t); } } @Override protected void onFlush(long timestamp) { // nothing to do } private Document loadDocument(String src) { ResourceFactory rm = Settings.getResourceFactory(); InputStream in = null; try { in = rm.load(src,null, null); return XmlTools.loadFromInputStream(in); } catch (Exception ex){ LOG.error("Error occurs when load xml file,source=" + src, ex); }finally { IOTools.closeStream(in); } return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy