All Downloads are FREE. Search and download functionalities are using the official Maven repository.
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.plugins.DocCache Maven / Gradle / Ivy
package com.alogic.xscript.plugins;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Element;
import com.alogic.load.Loadable;
import com.alogic.load.Store.HashStore;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.doc.json.JsonObject;
import com.anysoft.util.BaseException;
import com.anysoft.util.JsonTools;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.XmlTools;
/**
* 文档缓存
*
* @author yyduan
* @since 1.6.12.3 [20181013 duanyy]
*
* @version 1.6.12.6 [20181105 duanyy]
* - 增加refresh参数
*/
public class DocCache extends Segment {
protected String $tag = "data";
protected String $id = "";
protected DocStore store = null;
protected String $refresh = "false";
public DocCache(String tag, Logiclet p) {
super(tag, p);
}
@Override
public void configure(Properties p){
super.configure(p);
$id = PropertiesConstants.getRaw(p,"id","");
$tag = PropertiesConstants.getRaw(p,"tag", $tag);
$refresh = PropertiesConstants.getRaw(p,"refresh", $refresh);
store = new DocStore();
store.configure(p);
}
@SuppressWarnings("unchecked")
@Override
protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
if (current instanceof JsonObject){
String tag = PropertiesConstants.transform(ctx, $tag, "");
String id = PropertiesConstants.transform(ctx, $id, "");
if (StringUtils.isNotEmpty(tag) && StringUtils.isNotEmpty(id)){
boolean refresh = PropertiesConstants.transform(ctx,$refresh,false);
CachedDoc doc = refresh ? null:store.load(id, true);
if (doc == null){
synchronized(this){
doc = refresh ? null:store.load(id, true);
if (doc == null){
doc = new CachedDoc(id,new HashMap());
JsonObject newCurrent = new JsonObject(tag,(Map)doc.getData());
super.onExecute(root, newCurrent, ctx, watcher);
store.save(id, doc, true);
}
}
}
Map jsonCurrent = (Map) current.getContent();
jsonCurrent.put(tag, doc.getData());
}
}else{
throw new BaseException("core.e1000",
String.format("Tag %s does not support protocol %s",
getXmlTag(),root.getClass().getName()));
}
}
/**
* Store
* @author yyduan
*
*/
public static class DocStore extends HashStore{
@Override
public CachedDoc newObject(String id) {
return new CachedDoc(id,null);
}
}
/**
* 文档
* @author yyduan
*
*/
public static class CachedDoc extends Loadable.Abstract{
protected String id;
protected Object data;
public CachedDoc(String id,Object data){
this.id = id;
this.data = data;
}
public Object getData(){
return this.data;
}
public void setData(Object data){
this.data = data;
}
@Override
public String getId() {
return id;
}
@Override
public void report(Element xml) {
if (xml != null){
XmlTools.setString(xml,"module",getClass().getName());
}
}
@Override
public void report(Map json) {
if (json != null){
JsonTools.setString(json,"module",getClass().getName());
}
}
}
}