Search in sources :

Example 11 with Core

use of app.hongs.Core in project HongsCORE by ihongs.

the class ConfAction method service.

/**
 * 服务方法
 * 判断配置和消息有没有生成, 如果没有则生成; 消息按客户语言存放
 * @param req
 * @param rsp
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    Core core = ActionDriver.getActualCore(req);
    ActionHelper helper = core.get(ActionHelper.class);
    String name = req.getPathInfo();
    if (name == null || name.length() == 0) {
        helper.error400("Path info required");
        return;
    }
    int p = name.lastIndexOf('.');
    if (p < 0) {
        helper.error400("File type required");
        return;
    }
    String type = name.substring(1 + p);
    name = name.substring(1, p);
    if (!"js".equals(type) && !"json".equals(type)) {
        helper.error400("Wrong file type: " + type);
        return;
    }
    /**
     * 如果指定配置的数据并没有改变
     * 则直接返回 304 Not modified
     */
    String m;
    m = helper.getRequest().getHeader("If-Modified-Since");
    if (m != null && m.equals(ConfAction.MTIMES.get(name))) {
        helper.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }
    /**
     * 如果没有配置
     * 则调用工厂方法构造 JS 代码
     */
    String s;
    if (!ConfAction.CACHES.containsKey(name)) {
        try {
            s = this.makeConf(name);
        } catch (HongsError ex) {
            helper.error500(ex.getMessage());
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        m = sdf.format(new Date());
        ConfAction.CACHES.put(name, s);
        ConfAction.MTIMES.put(name, m);
    } else {
        s = ConfAction.CACHES.get(name);
        m = ConfAction.MTIMES.get(name);
    }
    // 标明修改时间
    helper.getResponse().setHeader("Last-Modified", m);
    // 输出配置信息
    if ("json".equals(type)) {
        helper.print(s, "application/json");
    } else {
        String c = req.getParameter("callback");
        if (c != null && c.length() != 0) {
            if (!c.matches("^[a-zA-Z_\\$][a-zA-Z0-9_]*$")) {
                helper.error400("Illegal callback function name!");
                return;
            }
            helper.print("function " + c + "() { return " + s + "; }", "text/javascript");
        } else {
            helper.print("if(!self.HsCONF)self.HsCONF={};Object.assign(self.HsCONF," + s + ");", "text/javascript");
        }
    }
}
Also used : HongsError(app.hongs.HongsError) ActionHelper(app.hongs.action.ActionHelper) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Core(app.hongs.Core)

Example 12 with Core

use of app.hongs.Core in project HongsCORE by ihongs.

the class LangAction method service.

/**
 * 服务方法
 * 判断配置和消息有没有生成, 如果没有则生成; 消息按客户语言存放
 * @param req
 * @param rsp
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException, ServletException {
    Core core = ActionDriver.getActualCore(req);
    ActionHelper helper = core.get(ActionHelper.class);
    String name = req.getPathInfo();
    if (name == null || name.length() == 0) {
        helper.error400("Path info required");
        return;
    }
    int p = name.lastIndexOf('.');
    if (p < 0) {
        helper.error400("File type required");
        return;
    }
    String type = name.substring(1 + p);
    name = name.substring(1, p);
    if (!"js".equals(type) && !"json".equals(type)) {
        helper.error400("Wrong file type: " + type);
        return;
    }
    /**
     * 如果指定语言的数据并没有改变
     * 则直接返回 304 Not modified
     */
    String m;
    m = helper.getRequest().getHeader("If-Modified-Since");
    if (m != null && m.equals(LangAction.MTIMES.get(name))) {
        helper.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }
    /**
     * 如果没有语言
     * 则调用工厂方法构造 JS 代码
     */
    String s;
    if (!LangAction.CACHES.containsKey(name)) {
        try {
            s = this.makeLang(name);
        } catch (HongsError ex) {
            helper.error500(ex.getMessage());
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        m = sdf.format(new Date());
        LangAction.CACHES.put(name, s);
        LangAction.MTIMES.put(name, m);
    } else {
        s = LangAction.CACHES.get(name);
        m = LangAction.MTIMES.get(name);
    }
    // 标明修改时间
    helper.getResponse().setHeader("Last-Modified", m);
    // 输出语言信息
    if ("json".equals(type)) {
        helper.print(s, "application/json");
    } else {
        String c = req.getParameter("callback");
        if (c != null && c.length() != 0) {
            if (!c.matches("^[a-zA-Z_\\$][a-zA-Z0-9_]*$")) {
                helper.error400("Illegal callback function name!");
                return;
            }
            helper.print("function " + c + "() { return " + s + "; }", "text/javascript");
        } else {
            helper.print("if(!self.HsLANG)self.HsLANG={};Object.assign(self.HsLANG," + s + ");", "text/javascript");
        }
    }
}
Also used : HongsError(app.hongs.HongsError) ActionHelper(app.hongs.action.ActionHelper) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Core(app.hongs.Core)

Example 13 with Core

use of app.hongs.Core in project HongsCORE by ihongs.

the class CmdletRunner method main.

public static void main(String[] args) throws IOException, HongsException {
    args = init(args);
    Core core = Core.getInstance();
    String act = Core.ACTION_NAME.get();
    if (null == act || act.length() == 0) {
        System.err.println("ERROR: Cmdlet name can not be empty.");
        System.exit(2);
        return;
    }
    // 获取方法
    Method method = getCmdlets().get(act);
    if (null == method) {
        System.err.println("ERROR: Cmdlet " + act + " is not exists.");
        System.exit(2);
        return;
    }
    // 执行方法
    try {
        if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
            CmdletHelper.println("Starting...");
        }
        method.invoke(null, new Object[] { args });
        if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
            CmdletHelper.println("Finished!!!");
        }
    } catch (IllegalAccessException ex) {
        CoreLogger.error("Illegal access for method '" + method.getClass().getName() + "." + method.getName() + "(String[]).");
        System.exit(3);
    } catch (IllegalArgumentException ex) {
        CoreLogger.error("Illegal params for method '" + method.getClass().getName() + "." + method.getName() + "(String[]).");
        System.exit(3);
    } catch (InvocationTargetException ex) {
        Throwable ta = ex.getCause();
        if (0 < Core.DEBUG) {
            CoreLogger.error(ta);
            return;
        }
        /**
         * 构建错误消息
         */
        String error = ta.getLocalizedMessage();
        if (!(ta instanceof HongsException) && !(ta instanceof HongsExpedient) && !(ta instanceof HongsError)) {
            CoreLocale lang = Core.getInstance(CoreLocale.class);
            if (error == null || error.length() == 0) {
                error = lang.translate("core.error.unkwn", ta.getClass().getName());
            } else {
                error = lang.translate("core.error.label", ta.getClass().getName()) + ": " + error;
            }
        }
        CoreLogger.error(error);
        System.exit(4);
    } finally {
        try {
            core.close();
        } catch (Throwable er) {
            CoreLogger.error(er);
            System.exit(5);
        }
        /**
         * 输出总的运行时间
         * 并清除参数及核心
         */
        if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
            CmdletHelper.println("Total exec time: " + (Tool.humanTime(System.currentTimeMillis() - Core.STARTS_TIME)));
        }
    }
}
Also used : CoreLocale(app.hongs.CoreLocale) HongsError(app.hongs.HongsError) HongsException(app.hongs.HongsException) HongsExpedient(app.hongs.HongsExpedient) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Core(app.hongs.Core)

Example 14 with Core

use of app.hongs.Core in project HongsCORE by ihongs.

the class DB method getInstance.

// ** 构造工厂 **/
/**
 * 获取指定数据库对象
 * <b>注意:</b>
 * <p>
 * 会根据当前运行环境自动设置 OBJECT_MODE,TRNSCT_MODE;
 * 数据库配置中有指定 dbClass, 请务必添加 getInstance:
 * </p>
 * <pre>
 *    public static XxDB getInstance()
 *       throws HongsException
 *    {
 *       return new XxDB();
 *    }
 * </pre>
 * @param name
 * @return 指定DB对象
 * @throws app.hongs.HongsException
 */
public static DB getInstance(String name) throws HongsException {
    DB db;
    do {
        String key = DB.class.getName() + ":" + name;
        Core core = Core.THREAD_CORE.get();
        if (core.containsKey(key)) {
            db = (DB) core.get(key);
            break;
        }
        Core gore = Core.GLOBAL_CORE;
        if (gore.containsKey(key)) {
            db = (DB) gore.get(key);
            break;
        }
        /**
         * 如果存在dbClass描述则调用对应类来获取实例
         */
        DBConfig cf = new DBConfig(name);
        if (cf.dbClass != null && cf.dbClass.length() != 0) {
            db = (DB) Core.getInstance(cf.dbClass);
        } else {
            db = new DB(cf);
        }
        /**
         * 如有设置dbName的单次加载则将其放入静态映射
         */
        CoreConfig conf = Core.getInstance(CoreConfig.class);
        if (conf.getProperty("core.load.db." + name + ".once", false)) {
            gore.put(key, db);
        } else {
            core.put(key, db);
        }
        db.OBJECT_MODE = conf.getProperty("core.in.object.mode", false);
        db.TRNSCT_MODE = conf.getProperty("core.in.trnsct.mode", false);
    } while (false);
    db.OBJECT_MODE = Synt.declare(Core.getInstance().got(Cnst.OBJECT_MODE), db.OBJECT_MODE);
    db.TRNSCT_MODE = Synt.declare(Core.getInstance().got(Cnst.TRNSCT_MODE), db.TRNSCT_MODE);
    return db;
}
Also used : CoreConfig(app.hongs.CoreConfig) Core(app.hongs.Core)

Example 15 with Core

use of app.hongs.Core in project HongsCORE by ihongs.

the class ActionHelper method newInstance.

/**
 * 新建实例
 * 用于使用 ActionRunner 时快速构建请求对象,
 * 可用以上 setXxxxxData 在构建之后设置参数.
 * @return
 */
public static ActionHelper newInstance() {
    Core core = Core.getInstance();
    String inst = ActionHelper.class.getName();
    if (core.containsKey(inst)) {
        return ((ActionHelper) core.got(inst)).clone();
    } else {
        return new ActionHelper(null, null, null, null);
    }
}
Also used : Core(app.hongs.Core)

Aggregations

Core (app.hongs.Core)17 HongsError (app.hongs.HongsError)5 HongsExpedient (app.hongs.HongsExpedient)4 ActionHelper (app.hongs.action.ActionHelper)4 HongsException (app.hongs.HongsException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CoreConfig (app.hongs.CoreConfig)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 ServletException (javax.servlet.ServletException)2 CoreLocale (app.hongs.CoreLocale)1 ActionRunner (app.hongs.action.ActionRunner)1 NaviMap (app.hongs.action.NaviMap)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1