Search in sources :

Example 6 with Core

use of io.github.ihongs.Core in project HongsCORE by ihongs.

the class ActionHelper method getInstance.

/**
 * 获取实例
 * 必须要在 ActionDriver 等容器初始化后使用,
 * 否则抛出 UnsupportedOperationException
 * @return
 */
public static ActionHelper getInstance() {
    Core core = Core.getInstance();
    String name = ActionHelper.class.getName();
    ActionHelper inst = (ActionHelper) core.get(name);
    if (null != inst) {
        return inst;
    }
    throw new UnsupportedOperationException("Please use the ActionHelper in the coverage of the ActionDriver or CmdletRunner inside");
}
Also used : Core(io.github.ihongs.Core)

Example 7 with Core

use of io.github.ihongs.Core in project HongsCORE by ihongs.

the class Data method getInstance.

/**
 * 获取实例
 *
 * <pre>
 * 配置如指定 db-class 则调用类的:
 * getInstance(conf, form)
 * getInstance()
 * new  Xxxx  ()
 * 此类必须是 Data 的子类.
 * 存在以上任一方法即返回,
 * 首个需自行维护生命周期,
 * 后两个交由 Core 作存取.
 * </pre>
 *
 * <pre>
 * 错误代码:
 * 821  找不到对应的类
 * 822  构建方法不可用
 * 823  构建实例不成功
 * 910  配置文件不存在
 * 912  表单信息不存在
 * </pre>
 *
 * @param conf
 * @param form
 * @return
 * @throws HongsException 表单获取失败
 * @throws HongsExemption 实例构建失败
 * @throws ClassCastException 不是 Data 的子类
 */
public static Data getInstance(String conf, String form) throws HongsException {
    // 外部指定
    Map dict = FormSet.getInstance(conf).getForm(form);
    String name = (String) Dict.get(dict, null, "@", "db-class");
    if (name != null && !name.isEmpty() && !name.equals(Data.class.getName())) {
        Class type;
        try {
            type = Class.forName(name);
        } catch (ClassNotFoundException e) {
            throw new HongsExemption(821, "Can not find class by name '" + name + "'.");
        }
        try {
            Method func = type.getMethod("getInstance", new Class[] { String.class, String.class });
            int modi = func.getModifiers();
            if (!Modifier.isPublic(modi) || !Modifier.isStatic(modi) || type != func.getDeclaringClass()) {
                throw new NoSuchMethodException();
            }
            return (Data) func.invoke(null, new Object[] { conf, form });
        } catch (NoSuchMethodException ex) {
            return (Data) Core.getInstance(type);
        } catch (InvocationTargetException ex) {
            Throwable ta = ex.getCause();
            // 调用层级过多, 最好直接抛出
            if (ta instanceof StackOverflowError) {
                throw (StackOverflowError) ta;
            }
            throw new HongsExemption(823, "Can not call '" + name + ".getInstance'", ta);
        } catch (IllegalArgumentException ex) {
            throw new HongsExemption(823, "Can not call '" + name + ".getInstance'", ex);
        } catch (IllegalAccessException ex) {
            throw new HongsExemption(823, "Can not call '" + name + ".getInstance'", ex);
        } catch (SecurityException se) {
            throw new HongsExemption(822, "Can not call '" + name + ".getInstance'", se);
        }
    }
    // 默认构造
    name = Data.class.getName() + ":" + conf + "!" + form;
    Core core = Core.getInstance();
    Data inst = (Data) core.get(name);
    if (inst == null) {
        inst = new Data(conf, form);
        core.set(name, inst);
    }
    return inst;
}
Also used : HongsExemption(io.github.ihongs.HongsExemption) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Core(io.github.ihongs.Core)

Example 8 with Core

use of io.github.ihongs.Core in project HongsCORE by ihongs.

the class RoleSet method getInstance.

// ** 构造工厂方法 **/
public static RoleSet getInstance(String userId) throws HongsException {
    String k = RoleSet.class.getName() + ":" + userId;
    Core c = Core.getInstance();
    if (c.exists(k)) {
        // 可以为空, 因此不用 isset
        return (RoleSet) c.get(k);
    }
    RoleSet s = new RoleSet(userId);
    if (s.roles == null) {
        // 状态不对
        s = null;
    }
    // 缓存对象
    c.set(k, s);
    return s;
}
Also used : Core(io.github.ihongs.Core)

Example 9 with Core

use of io.github.ihongs.Core in project HongsCORE by ihongs.

the class SearchEntity method getInstance.

/**
 * 获取实例
 * 存储为 conf/form 表单为 conf.form
 * 表单缺失则尝试获取 conf/form.form
 * 实例生命周期将交由 Core 维护
 * @param conf
 * @param form
 * @return
 * @throws HongsException
 */
public static SearchEntity getInstance(String conf, String form) throws HongsException {
    String code = SearchEntity.class.getName() + ":" + conf + "!" + form;
    Core core = Core.getInstance();
    SearchEntity inst = (SearchEntity) core.get(code);
    if (inst == null) {
        String path = conf + "/" + form;
        String name = conf + "!" + form;
        Map fxrm = FormSet.getInstance(conf).getForm(form);
        // 表单配置中可指定数据路径
        Map c = (Map) fxrm.get("@");
        if (c != null) {
            String p;
            p = (String) c.get("db-path");
            if (null != p && p.length() != 0) {
                path = p;
            }
            p = (String) c.get("db-name");
            if (null != p && p.length() != 0) {
                name = p;
            }
        }
        // 进一步处理路径中的变量等
        Map m = new HashMap();
        m.put("SERVER_ID", Core.SERVER_ID);
        m.put("CORE_PATH", Core.CORE_PATH);
        m.put("DATA_PATH", Core.DATA_PATH);
        path = Syno.inject(path, m);
        if (!new File(path).isAbsolute())
            path = Core.DATA_PATH + "/lucene/" + path;
        inst = new SearchEntity(fxrm, path, name);
        core.set(code, inst);
    }
    return inst;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) File(java.io.File) Core(io.github.ihongs.Core)

Example 10 with Core

use of io.github.ihongs.Core in project HongsCORE by ihongs.

the class Sesion method getRecord.

private static IRecord<Sesion> getRecord() throws HongsException {
    String cls = CoreConfig.getInstance().getProperty("core.normal.sesion.model");
    if (null == cls || 0 == cls.length()) {
        cls = Recs.class.getName();
        // 缺失则用私有类构造一个
        Core core = Core.getInstance();
        if (!core.isset(cls)) {
            Recs rec = new Recs();
            core.set(cls, rec);
            return rec;
        }
    }
    return (IRecord<Sesion>) Core.getInstance(cls);
}
Also used : Core(io.github.ihongs.Core)

Aggregations

Core (io.github.ihongs.Core)19 ActionHelper (io.github.ihongs.action.ActionHelper)6 HashMap (java.util.HashMap)6 HongsExemption (io.github.ihongs.HongsExemption)5 Map (java.util.Map)5 LinkedHashMap (java.util.LinkedHashMap)3 HongsException (io.github.ihongs.HongsException)2 File (java.io.File)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 CoreSerial (io.github.ihongs.CoreSerial)1 ActionRunner (io.github.ihongs.action.ActionRunner)1 NaviMap (io.github.ihongs.action.NaviMap)1 Action (io.github.ihongs.action.anno.Action)1 Cmdlet (io.github.ihongs.cmdlet.anno.Cmdlet)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1