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

com.alogic.cache.loader.ScriptSingle Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.alogic.cache.loader;

import com.alogic.cache.CacheObject;
import com.alogic.load.Loader;
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.*;
import org.w3c.dom.Element;

import java.util.HashMap;

/**
 * 通过脚本实现单个对象加载
 * @since 1.6.12.17
 */
public class ScriptSingle extends Loader.Abstract{
    /**
     * 加载事件脚本
     */
    protected Logiclet onLoad = null;

    protected String cacheObjectId = "$cache-object";
    protected String cacheFoundId = "$cache-found";

    @Override
    public void configure(Properties p){
        super.configure(p);
        cacheObjectId = PropertiesConstants.getString(p,"cacheObjectId",cacheObjectId,true);
        cacheFoundId = PropertiesConstants.getString(p,"cacheFoundId",cacheFoundId);
    }

    @Override
    public void configure(Element e, Properties p) {
        Properties props = new XmlElementProperties(e,p);

        Element onLoadElem = XmlTools.getFirstElementByPath(e, "on-load");
        if (onLoadElem != null){
            onLoad = Script.create(onLoadElem, props);
        }

        configure(props);
    }

    @Override
    public CacheObject load(String id, boolean cacheAllowed) {
        CacheObject found = null;
        if (onLoad != null){
            LogicletContext logicletContext = new LogicletContext(Settings.get());
            try {
                found = new CacheObject.Simple(id);
                found.hSet(CacheObject.DEFAULT_GROUP,"id",id,true);
                logicletContext.setObject(cacheObjectId,found);
                XsObject doc = new JsonObject("root",new HashMap());
                onLoad.execute(doc,doc, logicletContext, null);

                if (!PropertiesConstants.getBoolean(logicletContext,cacheFoundId,true)){
                    found = null;
                }
            }finally{
                logicletContext.removeObject(cacheObjectId);
            }
        }
        return found;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy