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

com.rt.web.logic.WebLogic Maven / Gradle / Ivy

The newest version!
/*
 * Created on 2008/08/21
 *
 */
package com.rt.web.logic;

import com.json.JSONArray;
import com.rt.core.constant.RTConst;
import com.rt.core.log.Log;
import com.rt.core.util.RTUtil;
import com.rt.logic.BaseLogic;
import com.rt.web.beans.UserInfo;
import com.rt.web.beans.WebBean;
import com.rt.web.util.WebCacheUtil;
import com.rt.web.util.WebConst;

import java.io.Serializable;
import java.util.Map;

/**
 * 业务规则逻辑层
 *
 * @author msc
 */
public class WebLogic extends BaseLogic {

    public static Log log = Log.getLog(WebLogic.class);

    public static WebLogic getInstance() {
        return (WebLogic) getInstance(WebLogic.class);
    }

    /**
     * 初始化WebBean
     *
     * @param webBeanValue {@link WebBean} json string
     * @return WebBean {@link WebBean}
     */
    public static WebBean initWebBean(String webBeanValue) {
        WebBean webBean;
        if (RTUtil.isEmpty(webBeanValue)) {
            webBean = new WebBean();
            webBean.setCode("emptyWebBeanValue");
            webBean.setSuccess(false);
            return webBean;
        }
        try {
            webBean = new WebBean(webBeanValue);
        } catch (Exception e) {
            webBean = new WebBean();
            webBean.setCode("emptyWebBeanValue");
            webBean.setSuccess(false);
            return webBean;
        }
        return webBean;
    }

    /**
     * 初始化WebBean
     *
     * @param webBean {@link WebBean}
     * @return WebBean {@link WebBean}
     */
    public static WebBean initWebBean(WebBean webBean) {
        if (webBean == null) {
            webBean = new WebBean();
        }
        return webBean;
    }

    /**
     * 初始化WebBean
     *
     * @return WebBean {@link WebBean}
     */
    public static WebBean initWebBean() {
        return new WebBean();
    }

    /**
     * 执行默认逻辑 注意:此方法必须是静态方法,普通方法会执行app-db中的函数检查规则,导致数据更新错误.
     * 没有放到logic层是因为事务处理
     * 必须根据logic层的第一个方法名称决定做查询还是更新.
     *
     * @param webBean{@link WebBean}
     * @return WebBean{@link WebBean}
     */
    public static WebBean executeLogic(WebBean webBean) {
        if (webBean == null) {
            webBean = new WebBean();
            webBean.setSuccess(false);
            webBean.setMessage("webBean is null.");
            log.error(webBean.getMessage());
            return webBean;
        }
        long start = RTUtil.getDateToLong();
        // 获取请求逻辑名称
        String logicId = webBean.getLogicId();
        if (RTUtil.isEmpty(logicId)) {
            log.error("Logic id is null.");
            webBean.setSuccess(false);
            webBean.setMessage("Logic id is null.");
            return webBean;
        }

        // 构造新逻辑名
        StringBuffer logicName = new StringBuffer();
        logicName.append(getExecutLogicName(logicId));

        // 获取需要执行的类
        Object logic = getInstance(logicName.toString());
        // 没有指定类
        if (logic == null) {
            webBean.setSuccess(false);
            webBean.setCode("logicNotFound");
            webBean.setMessage("Logic not found: " + logicName.toString());
            log.error(webBean.getMessage());
            return webBean;
        }

        // 获取执行方法名
        String functionName = getExecutFunctionName(logicId);

        // 打印logic名称
        logicName.append(RTConst.DOT);
        logicName.append(functionName);

        // 添加参数
        Map elContext = RTUtil.getElContext();
        elContext.put("logic", logic);
        elContext.put("bean", webBean);

        // 创建el表达式
        StringBuffer expression = new StringBuffer();
        expression.append("logic.");
        expression.append(functionName);
        expression.append("(bean)");

        Object obj;
        try {
            // 执行表达式
            obj = RTUtil.elFindValue(expression.toString(), elContext);
        } catch (ognl.MethodFailedException e) {
            // 不处理
            webBean.setSuccess(false);
            webBean.setCode("logicMethodFailed");
            webBean.setMessage("Logic MethodFailed: " + functionName + ", e: "
                    + e.toString());
            log.error(webBean.getMessage());
            log.error(e);
            return webBean;
        } catch (Exception e) {
            webBean.setSuccess(false);
            webBean.setCode("LogicMethodError");
            webBean.setMessage("Logic method error: " + e.toString());
            log.error(webBean.getMessage());
            log.error(e);
            return webBean;
        }
        // 处理返回值,如果需要处理.
        if (obj == null) {
            log.info("execute expression: " + expression.toString() + ", return null.");
        }
        if (obj instanceof WebBean) {
            webBean = (WebBean) obj;
        }
        // 业务逻辑里没设置执行是否成功时,才设为成功.
        // 否则使用业务逻辑内部执行是否成功标记.
        if (!webBean.hasSuccess()) {
            webBean.setSuccess(true);
        }

        // 计算执行时间
        long useTime = RTUtil.getDateToLong() - start;
        webBean.putResultParam("useTime", useTime);
        log.info(logicName + " success: " + webBean.isSuccess()
                + ", logicUseTime: " + useTime + "ms");
        return webBean;
    }

    private static String security_code;
    private static String security_token;

    public static void setSecurityCode(String value) {
        security_code = value;
    }

    public static void setSecurityToken(String value) {
        security_token = value;
    }

    public static boolean isRoot(String id) {
        return RTUtil.equals(id, security_code);
    }

    private static UserInfo getRoot() {
        UserInfo root = new UserInfo();
        root.setId(RTUtil.getUUID());
        // 添加权限
        JSONArray roleArray = new JSONArray();
        roleArray.add(RTConst.STAR);
        root.setRoleId(roleArray.toString());
        return root;
    }

    public static boolean exeDefault(WebBean webBean) {
        String code = webBean.getParam("code");
        String token = webBean.getParam("token");
        if (code.isEmpty() || token.isEmpty()) {
            return false;
        }
        if (RTUtil.equals(code, security_code) && RTUtil.equals(token, security_token)) {
            webBean.setUserInfo(getRoot());
            return true;
        }
        return false;
    }

    private static String getExecutLogicName(String name) {
        String packageName = RTUtil.substringBeforeLast(name,
                WebConst.ROOT_PATH);

        // 构造新逻辑名
        StringBuffer logicName = new StringBuffer();

        // 拼接加载路径
        String[] logicPackageArray = packageName.split(WebConst.ROOT_PATH);
        for (String item : logicPackageArray) {
            if (RTUtil.isEmpty(item)) {
                continue;
            }
            logicName.append(RTUtil.toFirstUpperCase(item));
        }
        logicName.append("Logic");

        return logicName.toString();
    }

    private static String getExecutFunctionName(String name) {
        name = RTUtil.substringAfterLast(name, WebConst.ROOT_PATH);
        name = RTUtil.substringBefore(name, WebConst.DOT);
        return name;
    }

    /**
     * 执行脚本
     *
     * @param webBean is bean
     * @return WebBean
     */
    public final WebBean runScript(WebBean webBean) {
        return webBean;
    }

    /**
     * 添加缓存对象
     * 只有生产环境,且启用缓存的情况下才有效.
     *
     * @param key   is key
     * @param value is value
     */
    public static void putCacheItem(String key, Serializable value) {
        WebCacheUtil.put(key, value);
    }

    /**
     * 添加缓存对象
     * 只有生产环境,且启用缓存的情况下才有效.
     *
     * @param key         is key
     * @param value       is value
     * @param millisDelay number of millisDelay after which an item must be discarded.
     */
    public static void putCacheItem(String key, Serializable value, long millisDelay) {
        WebCacheUtil.put(key, value, millisDelay);
    }

    /**
     * 删除缓存对象
     *
     * @param key is key
     */
    public static void delCacheItem(String key) {
        WebCacheUtil.del(key);
    }

    /**
     * 获取缓存对象
     *
     * @param key is key
     * @param  object
     * @return T object
     */
    public static  T getCacheItem(String key) {
        return WebCacheUtil.get(key);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy