Search in sources :

Example 6 with Cmdlet

use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.

the class Common method showCmdlets.

@Cmdlet("show-cmdlets")
public static void showCmdlets(String[] args) {
    Map<String, String> a = new TreeMap(new PathComparator('.'));
    int i = 0, j;
    for (Map.Entry<String, Method> et : CmdletRunner.getCmdlets().entrySet()) {
        String k = et.getKey();
        Method v = et.getValue();
        a.put(k, v.getDeclaringClass().getName() + "." + v.getName());
        j = k.length();
        if (i < j && j < 39) {
            i = j;
        }
    }
    PrintStream out = CmdletHelper.OUT.get();
    for (Map.Entry<String, String> n : a.entrySet()) {
        StringBuilder s = new StringBuilder();
        s.append(n.getKey());
        for (j = n.getKey().length(); j < i; j++) {
            s.append(" ");
        }
        s.append("\t");
        s.append(n.getValue());
        out.println(s);
    }
}
Also used : PrintStream(java.io.PrintStream) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Cmdlet(io.github.ihongs.cmdlet.anno.Cmdlet)

Example 7 with Cmdlet

use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.

the class LuceneCmdlet method search.

@Cmdlet("search")
public static void search(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "form=s", "id*s", "wd*s", "rb*s", "ob*s", "pn:i", "gn:i", "rn:i" });
    String conf = Synt.asString(opts.remove("conf"));
    String name = Synt.asString(opts.remove("name"));
    LuceneRecord so = LuceneRecord.getInstance(conf, name);
    Map rsp = so.search(opts);
    CmdletHelper.preview(rsp);
}
Also used : Map(java.util.Map) Cmdlet(io.github.ihongs.cmdlet.anno.Cmdlet)

Example 8 with Cmdlet

use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.

the class LuceneCmdlet method update.

@Cmdlet("update")
public static void update(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "form=s", "id*s", "!A" });
    args = (String[]) opts.get("");
    String conf = Synt.asString(opts.remove("conf"));
    String name = Synt.asString(opts.remove("name"));
    List<String> ds = Synt.asList(opts.remove("id"));
    LuceneRecord so = LuceneRecord.getInstance(conf, name);
    Map rd = data(args[0]);
    try {
        so.begin();
        for (String id : ds) {
            so.put(id, rd);
        }
        so.commit();
    } catch (HongsException ex) {
        so.revert();
        throw ex;
    } finally {
        so.close();
    }
}
Also used : HongsException(io.github.ihongs.HongsException) Map(java.util.Map) Cmdlet(io.github.ihongs.cmdlet.anno.Cmdlet)

Example 9 with Cmdlet

use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.

the class CoreRoster method addCmds.

private static void addCmds(Map<String, Method> acts, Cmdlet anno, String clsn, Class clso) {
    String actn = anno.value();
    if (actn == null || actn.length() == 0) {
        actn = clsn;
    }
    Method[] mtds = clso.getMethods();
    for (Method mtdo : mtds) {
        String mtdn = mtdo.getName();
        // 从注解提取动作名
        Cmdlet annx = (Cmdlet) mtdo.getAnnotation(Cmdlet.class);
        if (annx == null) {
            continue;
        }
        String actx = annx.value();
        if (actx == null || actx.length() == 0) {
            actx = mtdn;
        }
        // 检查方法是否合法
        Class[] prms = mtdo.getParameterTypes();
        if (prms == null || prms.length != 1 || !String[].class.isAssignableFrom(prms[0])) {
            throw new HongsExemption(832, "Can not find cmdlet method '" + clsn + "." + mtdn + "(String[])'.");
        }
        if ("__main__".equals(actx)) {
            acts.put(actn, /*__main__*/
            mtdo);
        } else {
            acts.put(actn + "." + actx, mtdo);
        }
    }
}
Also used : Cmdlet(io.github.ihongs.cmdlet.anno.Cmdlet) Method(java.lang.reflect.Method)

Example 10 with Cmdlet

use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.

the class LuceneCmdlet method delete.

@Cmdlet("delete")
public static void delete(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "form=s", "id*s" });
    String conf = Synt.asString(opts.remove("conf"));
    String name = Synt.asString(opts.remove("name"));
    List<String> ds = Synt.asList(opts.remove("id"));
    LuceneRecord so = LuceneRecord.getInstance(conf, name);
    try {
        so.begin();
        for (String id : ds) {
            so.del(id);
        }
        so.commit();
    } catch (HongsException ex) {
        so.revert();
        throw ex;
    } finally {
        so.close();
    }
}
Also used : HongsException(io.github.ihongs.HongsException) Map(java.util.Map) Cmdlet(io.github.ihongs.cmdlet.anno.Cmdlet)

Aggregations

Cmdlet (io.github.ihongs.cmdlet.anno.Cmdlet)18 Map (java.util.Map)12 HongsException (io.github.ihongs.HongsException)8 PrintStream (java.io.PrintStream)8 HashMap (java.util.HashMap)7 IOException (java.io.IOException)4 TreeMap (java.util.TreeMap)4 PrintWriter (java.io.PrintWriter)3 DB (io.github.ihongs.db.DB)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Method (java.lang.reflect.Method)2 HttpURLConnection (java.net.HttpURLConnection)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Core (io.github.ihongs.Core)1 CoreConfig (io.github.ihongs.CoreConfig)1 Mathod (io.github.ihongs.CoreRoster.Mathod)1