Search in sources :

Example 1 with Cmdlet

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

the class CoreRoster method addServ.

private static void addServ(Map<String, Mathod> acts, Map<String, Method> cmds, String... pkgs) {
    for (String pkgn : pkgs) {
        pkgn = pkgn.trim();
        if (pkgn.length() == 0) {
            continue;
        }
        Set<String> clss = getClss(pkgn);
        for (String clsn : clss) {
            Class clso = getClso(clsn);
            // 从注解提取动作名
            Action acto = (Action) clso.getAnnotation(Action.class);
            if (acto != null) {
                addActs(acts, acto, clsn, clso);
                continue;
            }
            Cmdlet cmdo = (Cmdlet) clso.getAnnotation(Cmdlet.class);
            if (cmdo != null) {
                addCmds(cmds, cmdo, clsn, clso);
            }
        }
    }
}
Also used : Action(app.hongs.action.anno.Action) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 2 with Cmdlet

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

the class ServerCmdlet method start.

@Cmdlet("start")
public static void start(String[] args) throws HongsException {
    int port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;
    String conf = Core.CORE_PATH + File.separator + "web.xml";
    if (!(new File(conf)).exists()) {
        conf = Core.CONF_PATH + File.separator + "web.xml";
    }
    String serd = Core.DATA_PATH + File.separator + "server";
    File ppid = new File(serd + File.separator + port + ".pid");
    File ppcd = ppid.getParentFile();
    // 检查进程
    if (ppid.exists() == true) {
        CmdletHelper.println("Process already exists!");
        return;
    }
    if (ppcd.exists() == false) {
        ppcd.mkdirs();
    }
    try {
        String pid = ManagementFactory.getRuntimeMXBean().getName().split("@", 2)[0];
        FileWriter dip = new FileWriter(ppid, true);
        dip.write(pid);
        dip.close();
    } catch (IOException e) {
        throw new HongsException.Common(e);
    }
    // 构建应用
    Server server;
    WebAppContext webapp;
    server = new Server(port);
    webapp = new WebAppContext();
    webapp.setDescriptor(conf);
    webapp.setContextPath(Core.BASE_HREF);
    webapp.setResourceBase(Core.BASE_PATH);
    webapp.setParentLoaderPriority(true);
    server.setHandler(webapp);
    // 外部配置
    CoreConfig c = CoreConfig.getInstance("defines");
    for (Map.Entry t : c.entrySet()) {
        String k = (String) t.getKey();
        String v = (String) t.getValue();
        if (k.startsWith("jetty.attr.")) {
            webapp.setAttribute(k.substring(11), v);
        } else if (k.startsWith("jetty.para.")) {
            webapp.setInitParameter(k.substring(11), v);
        }
    }
    /**
     * 初始设置
     * 光能外部配置参数还不够方便
     * 可能需要替换 JSP 解析器或 Session 容器
     * 可以设置 jetty.init 来注入 Initer 对象
     */
    String xs = c.getProperty("jetty.init");
    if (null != xs) {
        String[] xa = xs.split(";");
        for (String xn : xa) {
            xn = xn.trim();
            if ("".equals(xn)) {
                continue;
            }
            try {
                ((Initer) Class.forName(xn).newInstance()).init(webapp);
            } catch (ClassNotFoundException ex) {
                throw new HongsError.Common(ex);
            } catch (InstantiationException ex) {
                throw new HongsError.Common(ex);
            } catch (IllegalAccessException ex) {
                throw new HongsError.Common(ex);
            }
        }
    }
    // 停止机制
    Runtime.getRuntime().addShutdownHook(new Stoper(server, ppid));
    try {
        server.start();
        server.join();
    } catch (Exception e) {
        throw new HongsException.Common(e);
    } catch (Error e) {
        throw new HongsError.Common(e);
    }
}
Also used : HongsError(app.hongs.HongsError) Server(org.eclipse.jetty.server.Server) CoreConfig(app.hongs.CoreConfig) FileWriter(java.io.FileWriter) HongsError(app.hongs.HongsError) IOException(java.io.IOException) IOException(java.io.IOException) HongsException(app.hongs.HongsException) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) HongsException(app.hongs.HongsException) File(java.io.File) Map(java.util.Map) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 3 with Cmdlet

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

the class SystemCmdlet method exec.

/**
 * 维护命令
 * @param args
 * @throws HongsException
 */
@Cmdlet("__main__")
public static void exec(String[] args) throws HongsException {
    Map<String, Object> opts;
    opts = CmdletHelper.getOpts(args, "date:s");
    args = (String[]) opts.remove("");
    if (0 == args.length) {
        System.err.println("Serve name required!");
        return;
    }
    String fn = args[0];
    File fu = new File(fn);
    Date dt = new Date();
    // 日期参数
    if (opts.containsKey("date")) {
        dt = getTin((String) opts.get("date"), dt);
    }
    // 相对且不存在则看作内部目录
    if (!fu.isAbsolute() && !fu.exists()) {
        fn = Core.CORE_PATH + File.separator + "bin" + File.separator + fn;
        fu = new File(fn);
    }
    // 获取目录下全部待执行的文件
    List<File> fxs = new ArrayList();
    if (!fu.isFile()) {
        File[] fus = fu.listFiles();
        for (File fo : fus) {
            if (!fo.isFile() || fo.getName().startsWith(".")) {
                continue;
            }
            fxs.add(fo);
        }
        Collections.sort(fxs, new Sorter());
    } else {
        fxs.add(fu);
    }
    Logger lgr = new Logger();
    String act = Core.ACTION_NAME.get();
    long now = Core.ACTION_TIME.get();
    // 逐个执行
    for (File fo : fxs) {
        Matcher met = filNamPatt.matcher(fo.getName());
        if (!met.matches()) {
            continue;
        }
        String ent = met.group(2).trim();
        String ext = met.group(3).trim();
        try {
            if ("cmd.xml".equals(ext)) {
                SystemCmdlet.runCmd(dt, fo, lgr);
            } else if (/**/
            "sql".equals(ext)) {
                runSql(dt, fo, ent);
            }
        } catch (Exception ex) {
            lgr.error(ex);
        } catch (Error ex) {
            lgr.error(ex);
        } finally {
            // 放回名称和开始时间
            // 避免时间或日志模糊
            Core.ACTION_NAME.set(act);
            Core.ACTION_TIME.set(now);
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) CoreLogger(app.hongs.CoreLogger) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HongsException(app.hongs.HongsException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) File(java.io.File) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 4 with Cmdlet

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

the class Common method showActions.

@Cmdlet("show-actions")
public static void showActions(String[] args) {
    Map<String, String> a = new TreeMap(new PropComparator());
    int i = 0, j;
    for (Map.Entry<String, Mathod> et : ActionRunner.getActions().entrySet()) {
        String k = et.getKey();
        Mathod v = et.getValue();
        a.put(k, v.toString());
        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 : TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Mathod(app.hongs.CoreRoster.Mathod) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 5 with Cmdlet

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

the class SearchCmdlet method search.

@Cmdlet("search")
public void search(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "name=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"));
    ActionHelper ah = Core.getInstance(ActionHelper.class);
    LuceneRecord so = LuceneRecord.getInstance(conf, name);
    Map req = ah.getRequestData();
    req.putAll(opts);
    Map rsp = so.search(req);
    CmdletHelper.preview(rsp);
}
Also used : ActionHelper(app.hongs.action.ActionHelper) LuceneRecord(app.hongs.dh.lucene.LuceneRecord) Map(java.util.Map) 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