Search in sources :

Example 6 with Cmdlet

use of app.hongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.

the class SearchCmdlet method update.

@Cmdlet("update")
public void update(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "name=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"));
    ActionHelper ah = Core.getInstance(ActionHelper.class);
    LuceneRecord so = LuceneRecord.getInstance(conf, name);
    Map rd = ah.getRequestData();
    if (!rd.isEmpty()) {
        // 有数据则校验数据
        VerifyHelper vh = new VerifyHelper();
        vh.addRulesByForm(conf, name);
        rd = vh.verify(rd);
        try {
            so.begin();
            for (String id : ds) {
                so.set(id, rd);
            }
            so.commit();
        } catch (HongsException ex) {
            so.revert();
            throw ex;
        } finally {
            so.close();
        }
    } else {
        // 不给内容即为删除
        try {
            so.begin();
            for (String id : ds) {
                so.del(id);
            }
            so.commit();
        } catch (HongsException ex) {
            so.revert();
            throw ex;
        } finally {
            so.close();
        }
    }
}
Also used : VerifyHelper(app.hongs.action.VerifyHelper) HongsException(app.hongs.HongsException) ActionHelper(app.hongs.action.ActionHelper) LuceneRecord(app.hongs.dh.lucene.LuceneRecord) Map(java.util.Map) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 7 with Cmdlet

use of app.hongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.

the class Cmdrun method exec.

@Cmdlet("__main__")
public static void exec(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, "from=s", "to=s", "sync:b", "del-tables:b", "del-fields", "!U", "!A");
    String fr = (String) opts.get("from");
    String to = (String) opts.get("to");
    boolean sc = (boolean) opts.get("sync");
    boolean dt = (boolean) opts.get("del-tables");
    boolean df = (boolean) opts.get("del-fields");
    Pattern patt = Pattern.compile("\\.\\w+$");
    Matcher frMt = patt.matcher(fr);
    Matcher toMt = patt.matcher(to);
    if (frMt.find() && toMt.find()) {
        DB frDb = DB.getInstance(fr.substring(0, frMt.start()));
        Table frTb = frDb.getTable(fr.substring(frMt.start() + 1));
        DB toDb = DB.getInstance(to.substring(0, toMt.start()));
        Table toTb = toDb.getTable(to.substring(toMt.start() + 1));
        if (sc) {
            new TableDeff(frTb).syncSlaver(toTb, df);
        } else {
            new TableDeff(frTb).deffSlaver(toTb, df);
        }
    } else {
        DB frDb = DB.getInstance(fr);
        DB toDb = DB.getInstance(fr);
        if (sc) {
            new DBDeff(frDb).syncSlaver(toDb, null, null, dt, df);
        } else {
            new DBDeff(frDb).deffSlaver(toDb, null, null, dt, df);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Table(app.hongs.db.Table) Matcher(java.util.regex.Matcher) Map(java.util.Map) DB(app.hongs.db.DB) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 8 with Cmdlet

use of app.hongs.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 PropComparator());
    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 < 31) {
            i = j;
        }
    }
    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());
        System.out.println(s);
    }
}
Also used : Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 9 with Cmdlet

use of app.hongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.

the class Common method execAction.

@Cmdlet("exec-action")
public static void execAction(String[] args) throws HongsException {
    Map<String, Object> opts;
    opts = CmdletHelper.getOpts(args, "request:s", "context:s", "session:s", "cookies:s");
    args = (String[]) opts.get("");
    if (args.length == 0) {
        System.err.println("Action name required!\r\nUsage: ACTION_NAME --request QUERY_STRING --cookies QUERY_STRING --session QUERY_STRING --context QUERY_STRING");
        return;
    }
    ActionHelper helper = Core.getInstance(ActionHelper.class);
    helper.setRequestData(data((String) opts.get("request")));
    helper.setContextData(data((String) opts.get("context")));
    helper.setSessionData(data((String) opts.get("session")));
    helper.setCookiesData(data((String) opts.get("cookies")));
    ActionRunner runner = new ActionRunner(helper, args[0]);
    runner.doAction();
    CmdletHelper.preview(helper.getResponseData());
}
Also used : ActionRunner(app.hongs.action.ActionRunner) ActionHelper(app.hongs.action.ActionHelper) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 10 with Cmdlet

use of app.hongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.

the class Common method callAction.

@Cmdlet("call-action")
public static void callAction(String[] args) throws HongsException {
    Map<String, Object> opts;
    opts = CmdletHelper.getOpts(args, "request:s", "cookies:s");
    args = (String[]) opts.get("");
    String act = args[0];
    String req = text((String) opts.get("request"));
    Map<String, String> cok = data((String) opts.get("cookies"));
    String hst = System.getProperty("server.host", "localhost");
    String pot = System.getProperty("server.port", "8080");
    String url = "http://" + hst + ":" + pot + Core.BASE_HREF + "/" + act + Cnst.ACT_EXT;
    try {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setConnectTimeout(0);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Accept", "application/json,text/html,*/*;q=0.8");
        conn.setRequestProperty("X-Requested-With", "HongsCORE/0.4");
        // 放入 cookie
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> et : cok.entrySet()) {
            sb.append(URLEncoder.encode(et.getKey(), "UTF-8"));
            sb.append("=");
            sb.append(URLEncoder.encode(et.getValue(), "UTF-8"));
            sb.append(";");
        }
        conn.setRequestProperty("Cookie", sb.toString());
        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(req);
        out.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.print(line);
        }
    } catch (UnsupportedEncodingException ex) {
        throw new HongsException.Common(ex);
    } catch (MalformedURLException ex) {
        throw new HongsException.Common(ex);
    } catch (IOException ex) {
        throw new HongsException.Common(ex);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) HongsException(app.hongs.HongsException) BufferedReader(java.io.BufferedReader) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) PrintWriter(java.io.PrintWriter) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Aggregations

Cmdlet (app.hongs.cmdlet.anno.Cmdlet)11 Map (java.util.Map)7 HongsException (app.hongs.HongsException)4 ActionHelper (app.hongs.action.ActionHelper)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 TreeMap (java.util.TreeMap)3 LuceneRecord (app.hongs.dh.lucene.LuceneRecord)2 File (java.io.File)2 Method (java.lang.reflect.Method)2 Matcher (java.util.regex.Matcher)2 CoreConfig (app.hongs.CoreConfig)1 CoreLogger (app.hongs.CoreLogger)1 Mathod (app.hongs.CoreRoster.Mathod)1 HongsError (app.hongs.HongsError)1 ActionRunner (app.hongs.action.ActionRunner)1 VerifyHelper (app.hongs.action.VerifyHelper)1 Action (app.hongs.action.anno.Action)1 DB (app.hongs.db.DB)1 Table (app.hongs.db.Table)1