Search in sources :

Example 11 with HongsExemption

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

the class AutoFilter method getacts.

private Set<String> getacts() {
    if (null != actset) {
        return actset;
    }
    // 总是通过 search 获取动作 class
    // 即使无需 search 也要存在 search 方法才行
    Class cls;
    try {
        cls = ActionRunner.getActions().get(action.substring(1) + "/search").getMclass();
    } catch (NullPointerException ex) {
        throw new HongsExemption(1130, "Auto action '" + action.substring(1) + "/search' is not exists", ex);
    }
    cstset = new HashSet();
    actset = new TreeSet(new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            // 对比两个动作路径层级数
            // 优先匹配层级更深的动作
            int i, c1 = 0, c2 = 0;
            i = 0;
            while ((i = o1.indexOf('/', i)) != -1) {
                i++;
                c1++;
            }
            i = 0;
            while ((i = o2.indexOf('/', i)) != -1) {
                i++;
                c2++;
            }
            i = Integer.compare(c2, c1);
            return i != 0 ? i : 1;
        }
    });
    for (Method mtd : cls.getMethods()) {
        Action ann = mtd.getAnnotation(Action.class);
        if (null != ann) {
            String uri;
            if (!"".equals(ann.value())) {
                uri = "/" + ann.value();
            } else {
                uri = "/" + mtd.getName();
            }
            if (mtd.isAnnotationPresent(CustomReplies.class)) {
                cstset.add(uri);
            }
            actset.add(uri);
        }
    }
    return actset;
}
Also used : Action(io.github.ihongs.action.anno.Action) TreeSet(java.util.TreeSet) HongsExemption(io.github.ihongs.HongsExemption) Method(java.lang.reflect.Method) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Comparator(java.util.Comparator)

Example 12 with HongsExemption

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

the class AutoFilter method getlays.

private Set<String> getlays() {
    if (null != layset) {
        return layset;
    }
    File dir = new File(Core.BASE_PATH + layout);
    if (!dir.exists()) {
        throw new HongsExemption(1131, "Auto layout '" + layout.substring(1) + "' is not exists");
    }
    if (!dir.isDirectory()) {
        throw new HongsExemption(1131, "Auto layout '" + layout.substring(1) + "' is not a directory");
    }
    /**
     * 这里不需要管层级的深度
     * 下面是按越深越先加入的
     */
    layset = new LinkedHashSet();
    // 递归获取目录下所有文件
    getlays(layset, dir, "/");
    return layset;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HongsExemption(io.github.ihongs.HongsExemption) File(java.io.File)

Example 13 with HongsExemption

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

the class MoreAction method exec.

private void exec(ActionHelper helper, String cmd, HttpServletRequest req, HttpServletResponse rsp) {
    // 组织参数
    String[] args;
    List opts = Synt.asList(helper.getRequestData().get("args"));
    if (opts == null || opts.isEmpty()) {
        args = new String[1];
    } else {
        args = new String[1 + opts.size()];
        int i = 1;
        for (Object opt : opts) {
            args[i++] = Synt.asString(opt);
        }
    }
    args[0] = cmd;
    try {
        PrintStream out = new PrintStream(rsp.getOutputStream(), true);
        rsp.setContentType("text/plain");
        rsp.setCharacterEncoding("utf-8");
        try {
            CmdletHelper.ENV.set((byte) 1);
            CmdletHelper.ERR.set(out);
            CmdletHelper.OUT.set(out);
            CmdletRunner.exec(args);
        } catch (Error | Exception e) {
            out.print("ERROR: " + e.getMessage());
        } finally {
            CmdletHelper.OUT.remove();
            CmdletHelper.ERR.remove();
            CmdletHelper.ENV.remove();
        }
    } catch (IOException e) {
        throw new HongsExemption(e);
    }
}
Also used : PrintStream(java.io.PrintStream) ArrayList(java.util.ArrayList) List(java.util.List) HongsExemption(io.github.ihongs.HongsExemption) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HongsException(io.github.ihongs.HongsException)

Example 14 with HongsExemption

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

the class CmdletHelper method getOpts.

// ** 参数相关 **/
/**
 * 解析参数
 * 本函数遵循 Perl 的 Getopt::Long 解析规则
 * 同时也遵循 Configure 类似的参数的解析规则
 * @param args
 * @param chks
 * @return
 */
public static Map<String, Object> getOpts(String[] args, String... chks) {
    Map<String, Object[]> chkz = new HashMap();
    Map<String, Object> newOpts = new HashMap();
    List<String> newArgs = new ArrayList();
    Set<String> reqOpts = new LinkedHashSet();
    Set<String> errMsgs = new LinkedHashSet();
    // 命令使用帮助
    String hlp = null;
    // 禁止匿名参数
    boolean vb = true;
    // 禁止未知参数
    boolean ub = true;
    for (String chk : chks) {
        Matcher m = RP.matcher(chk);
        if (!m.find()) {
            if (chk.startsWith("?")) {
                hlp = chk.substring(1);
            } else if (chk.equals("!A") || chk.equals("!Anonymous")) {
                vb = false;
            } else if (chk.equals("!U") || chk.equals("!Undefined")) {
                ub = false;
            } else {
                errMsgs.add(GETERRS[0].replace("%chk", chk));
            }
            continue;
        }
        String name = m.group(1);
        String sign = m.group(2);
        String type = m.group(3);
        if ("=".equals(sign) || "+".equals(sign)) {
            reqOpts.add(name);
        }
        if (type.startsWith("/")) {
            String reg = m.group(4);
            String mod = m.group(5);
            String err = m.group(6);
            Pattern pat;
            if (mod != null) {
                pat = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
            } else {
                pat = Pattern.compile(reg);
            }
            if (err != null) {
                err = err.trim();
            } else {
                err = GETERRS[7];
            }
            reg = "/" + reg + "/" + mod;
            chkz.put(name, new Object[] { sign.charAt(0), 'r', pat, reg, err });
        } else {
            chkz.put(name, new Object[] { sign.charAt(0), type.charAt(0) });
        }
    }
    F: for (int i = 0; i < args.length; i++) {
        String name = args[i];
        if (name.startsWith("--")) {
            name = name.substring(2);
            if (name.length() == 0) {
                continue;
            }
            int q = name.indexOf('=');
            if (q > 0) {
                String arg;
                Object val;
                arg = name.substring(1 + q);
                name = name.substring(0, q);
                if (chkz.containsKey(name)) {
                    Object[] chk = chkz.get(name);
                    char sign = (Character) chk[0];
                    char type = (Character) chk[1];
                    switch(type) {
                        case 'b':
                            if (!BP.matcher(arg).matches()) {
                                errMsgs.add(GETERRS[6].replace("%opt", name));
                                continue;
                            }
                            val = Synt.asBool(arg);
                            break;
                        case 'i':
                            if (!IP.matcher(arg).matches()) {
                                errMsgs.add(GETERRS[4].replace("%opt", name));
                                continue;
                            }
                            val = Long.parseLong(arg);
                            break;
                        case 'f':
                            if (!FP.matcher(arg).matches()) {
                                errMsgs.add(GETERRS[5].replace("%opt", name));
                                continue;
                            }
                            val = Double.parseDouble(arg);
                            break;
                        case 'r':
                            Pattern rp = (Pattern) chk[2];
                            String reg = (String) chk[3];
                            String err = (String) chk[4];
                            if (!rp.matcher(arg).matches()) {
                                errMsgs.add(err.replace("%mat", reg).replace("%opt", name));
                                continue;
                            }
                        default:
                            val = arg;
                    }
                    if ('+' == sign || '*' == sign) {
                        List vals;
                        vals = (List) newOpts.get(name);
                        if (vals == null) {
                            vals = new ArrayList();
                            newOpts.put(name, vals);
                        }
                        vals.add(val);
                    } else {
                        if ('=' == sign && arg.isEmpty()) {
                            errMsgs.add(GETERRS[2].replace("%opt", name));
                        } else if (newOpts.containsKey(name)) {
                            errMsgs.add(GETERRS[3].replace("%opt", name));
                        } else {
                            newOpts.put(name, val);
                        }
                    }
                } else if (ub) {
                    // 未知参数
                    errMsgs.add(GETERRS[8].replace("%opt", name));
                } else {
                    newArgs.add(args[i]);
                }
            } else {
                if (chkz.containsKey(name)) {
                    Object[] chk = chkz.get(name);
                    char sign = (Character) chk[0];
                    char type = (Character) chk[1];
                    Pattern rp = null;
                    String reg = null;
                    String err = null;
                    List vals = null;
                    if ('b' == type) {
                        newOpts.put(name, true);
                        continue;
                    }
                    if ('r' == type) {
                        rp = (Pattern) chk[2];
                        reg = (String) chk[3];
                        err = (String) chk[4];
                    }
                    if ('+' == sign || '*' == sign) {
                        vals = (List) newOpts.get(name);
                        if (vals == null) {
                            vals = new ArrayList();
                            newOpts.put(name, vals);
                        }
                    }
                    W: while (i < args.length - 1) {
                        Object val;
                        String arg = args[i + 1];
                        if (arg.startsWith("--")) {
                            if (vals == null || vals.isEmpty()) {
                                // 缺少取值
                                errMsgs.add(GETERRS[2].replace("%opt", name));
                            }
                            break;
                        } else {
                            i++;
                        }
                        switch(type) {
                            case 'i':
                                if (!IP.matcher(arg).matches()) {
                                    errMsgs.add(GETERRS[4].replace("%opt", name));
                                    continue;
                                }
                                val = Long.parseLong(arg);
                                break;
                            case 'f':
                                if (!FP.matcher(arg).matches()) {
                                    errMsgs.add(GETERRS[5].replace("%opt", name));
                                    continue;
                                }
                                val = Double.parseDouble(arg);
                                break;
                            case 'r':
                                if (!rp.matcher(arg).matches()) {
                                    errMsgs.add(err.replace("%mat", reg).replace("%opt", name));
                                    continue;
                                }
                            default:
                                val = arg;
                        }
                        if ('+' == sign || '*' == sign) {
                            vals.add(val);
                        } else {
                            if ('=' == sign && arg.isEmpty()) {
                                errMsgs.add(GETERRS[2].replace("%opt", name));
                            } else if (newOpts.containsKey(name)) {
                                errMsgs.add(GETERRS[3].replace("%opt", name));
                            } else {
                                newOpts.put(name, val);
                            }
                            break;
                        }
                    }
                } else if (ub) {
                    // 未知参数
                    errMsgs.add(GETERRS[8].replace("%opt", name));
                } else {
                    newArgs.add(args[i]);
                }
            }
        } else if (vb) {
            // 匿名参数
            errMsgs.add(GETERRS[9]);
        } else {
            newArgs.add(args[i]);
        }
    }
    for (String name : reqOpts) {
        if (!newOpts.containsKey(name)) {
            Set<String> err = new LinkedHashSet();
            err.add(GETERRS[1].replace("%opt", name));
            err.addAll(errMsgs);
            errMsgs = err;
        }
    }
    if (!errMsgs.isEmpty()) {
        StringBuilder err = new StringBuilder();
        for (String msg : errMsgs) {
            err.append("\r\n\t").append(msg);
        }
        if (null != hlp) {
            err.append("\r\n\t").append(hlp);
        }
        hlp = err.toString();
        throw new HongsExemption(838, hlp).setLocalizedOptions(hlp);
    } else if (hlp != null && args.length == 0) {
        throw new HongsExemption(839, hlp).setLocalizedOptions(hlp);
    }
    // 把剩余的参数放进去
    newOpts.put("", newArgs.toArray(new String[0]));
    return newOpts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) HongsExemption(io.github.ihongs.HongsExemption) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with HongsExemption

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

the class SearchEntity method getWriter.

@Override
public IndexWriter getWriter() throws HongsException {
    /**
     * 依次检查当前对象和全部空间是否存在 SearchWriter,
     * 需从全局获取时调 SearchWriter.open 计数,
     * 当前实例退出时调 SearchWriter.exit 减掉,
     * 计数归零可被回收.
     */
    final String path = getDbPath();
    final String name = getDbName();
    final boolean[] b = new boolean[] { false };
    if (WRITER != null) {
        b[0] = true;
    } else
        try {
            WRITER = Core.GLOBAL_CORE.get(Writer.class.getName() + ":" + name, new Supplier<Writer>() {

                @Override
                public Writer get() {
                    b[0] = true;
                    return new Writer(path, name);
                }
            });
        } catch (HongsExemption x) {
            throw x.toException();
        }
    // 首次进入无需计数
    if (b[0] == true) {
        return WRITER.conn();
    } else {
        return WRITER.open();
    }
}
Also used : HongsExemption(io.github.ihongs.HongsExemption) IndexWriter(org.apache.lucene.index.IndexWriter)

Aggregations

HongsExemption (io.github.ihongs.HongsExemption)44 HongsException (io.github.ihongs.HongsException)15 IOException (java.io.IOException)12 HashMap (java.util.HashMap)10 Map (java.util.Map)9 LinkedHashMap (java.util.LinkedHashMap)7 HashSet (java.util.HashSet)6 Core (io.github.ihongs.Core)5 ActionHelper (io.github.ihongs.action.ActionHelper)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 ArrayList (java.util.ArrayList)5 File (java.io.File)4 List (java.util.List)4 CoreConfig (io.github.ihongs.CoreConfig)3 FileInputStream (java.io.FileInputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 Matcher (java.util.regex.Matcher)3